textfield.inc 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <?php
  2. /**
  3. * @file
  4. * Webform module textfield component.
  5. */
  6. /**
  7. * Implements _webform_defaults_component().
  8. */
  9. function _webform_defaults_textfield() {
  10. return array(
  11. 'name' => '',
  12. 'form_key' => NULL,
  13. 'pid' => 0,
  14. 'weight' => 0,
  15. 'value' => '',
  16. 'mandatory' => 0,
  17. 'extra' => array(
  18. 'width' => '',
  19. 'maxlength' => '',
  20. 'field_prefix' => '',
  21. 'field_suffix' => '',
  22. 'disabled' => 0,
  23. 'unique' => 0,
  24. 'title_display' => 0,
  25. 'description' => '',
  26. 'attributes' => array(),
  27. 'private' => FALSE,
  28. ),
  29. );
  30. }
  31. /**
  32. * Implements _webform_theme_component().
  33. */
  34. function _webform_theme_textfield() {
  35. return array(
  36. 'webform_display_textfield' => array(
  37. 'render element' => 'element',
  38. 'file' => 'components/textfield.inc',
  39. ),
  40. );
  41. }
  42. /**
  43. * Implements _webform_edit_component().
  44. */
  45. function _webform_edit_textfield($component) {
  46. $form = array();
  47. $form['value'] = array(
  48. '#type' => 'textfield',
  49. '#title' => t('Default value'),
  50. '#default_value' => $component['value'],
  51. '#description' => t('The default value of the field.') . theme('webform_token_help'),
  52. '#size' => 60,
  53. '#maxlength' => 1024,
  54. '#weight' => 0,
  55. );
  56. $form['display']['width'] = array(
  57. '#type' => 'textfield',
  58. '#title' => t('Width'),
  59. '#default_value' => $component['extra']['width'],
  60. '#description' => t('Width of the textfield.') . ' ' . t('Leaving blank will use the default size.'),
  61. '#size' => 5,
  62. '#maxlength' => 10,
  63. '#weight' => 0,
  64. '#parents' => array('extra', 'width'),
  65. );
  66. $form['display']['field_prefix'] = array(
  67. '#type' => 'textfield',
  68. '#title' => t('Label placed to the left of the textfield'),
  69. '#default_value' => $component['extra']['field_prefix'],
  70. '#description' => t('Examples: $, #, -.'),
  71. '#size' => 20,
  72. '#maxlength' => 127,
  73. '#weight' => 1.1,
  74. '#parents' => array('extra', 'field_prefix'),
  75. );
  76. $form['display']['field_suffix'] = array(
  77. '#type' => 'textfield',
  78. '#title' => t('Label placed to the right of the textfield'),
  79. '#default_value' => $component['extra']['field_suffix'],
  80. '#description' => t('Examples: lb, kg, %.'),
  81. '#size' => 20,
  82. '#maxlength' => 127,
  83. '#weight' => 1.2,
  84. '#parents' => array('extra', 'field_suffix'),
  85. );
  86. $form['display']['disabled'] = array(
  87. '#type' => 'checkbox',
  88. '#title' => t('Disabled'),
  89. '#return_value' => 1,
  90. '#description' => t('Make this field non-editable. Useful for setting an unchangeable default value.'),
  91. '#weight' => 11,
  92. '#default_value' => $component['extra']['disabled'],
  93. '#parents' => array('extra', 'disabled'),
  94. );
  95. $form['validation']['unique'] = array(
  96. '#type' => 'checkbox',
  97. '#title' => t('Unique'),
  98. '#return_value' => 1,
  99. '#description' => t('Check that all entered values for this field are unique. The same value is not allowed to be used twice.'),
  100. '#weight' => 1,
  101. '#default_value' => $component['extra']['unique'],
  102. '#parents' => array('extra', 'unique'),
  103. );
  104. $form['validation']['maxlength'] = array(
  105. '#type' => 'textfield',
  106. '#title' => t('Maxlength'),
  107. '#default_value' => $component['extra']['maxlength'],
  108. '#description' => t('Maximum length of the textfield value.'),
  109. '#size' => 5,
  110. '#maxlength' => 10,
  111. '#weight' => 2,
  112. '#parents' => array('extra', 'maxlength'),
  113. );
  114. return $form;
  115. }
  116. /**
  117. * Implements _webform_render_component().
  118. */
  119. function _webform_render_textfield($component, $value = NULL, $filter = TRUE) {
  120. $node = isset($component['nid']) ? node_load($component['nid']) : NULL;
  121. $element = array(
  122. '#type' => 'textfield',
  123. '#title' => $filter ? _webform_filter_xss($component['name']) : $component['name'],
  124. '#title_display' => $component['extra']['title_display'] ? $component['extra']['title_display'] : 'before',
  125. '#default_value' => $filter ? _webform_filter_values($component['value'], $node, NULL, NULL, FALSE) : $component['value'],
  126. '#required' => $component['mandatory'],
  127. '#weight' => $component['weight'],
  128. '#field_prefix' => empty($component['extra']['field_prefix']) ? NULL : ($filter ? _webform_filter_xss($component['extra']['field_prefix']) : $component['extra']['field_prefix']),
  129. '#field_suffix' => empty($component['extra']['field_suffix']) ? NULL : ($filter ? _webform_filter_xss($component['extra']['field_suffix']) : $component['extra']['field_suffix']),
  130. '#description' => $filter ? _webform_filter_descriptions($component['extra']['description'], $node) : $component['extra']['description'],
  131. '#attributes' => $component['extra']['attributes'],
  132. '#theme_wrappers' => array('webform_element'),
  133. '#translatable' => array('title', 'description', 'field_prefix', 'field_suffix'),
  134. );
  135. if ($component['extra']['disabled']) {
  136. if ($filter) {
  137. $element['#attributes']['readonly'] = 'readonly';
  138. }
  139. else {
  140. $element['#disabled'] = TRUE;
  141. }
  142. }
  143. // Enforce uniqueness.
  144. if ($component['extra']['unique']) {
  145. $element['#element_validate'][] = 'webform_validate_unique';
  146. }
  147. // Change the 'width' option to the correct 'size' option.
  148. if ($component['extra']['width'] > 0) {
  149. $element['#size'] = $component['extra']['width'];
  150. }
  151. if ($component['extra']['maxlength'] > 0) {
  152. $element['#maxlength'] = $component['extra']['maxlength'];
  153. }
  154. if (isset($value)) {
  155. $element['#default_value'] = $value[0];
  156. }
  157. return $element;
  158. }
  159. /**
  160. * Implements _webform_display_component().
  161. */
  162. function _webform_display_textfield($component, $value, $format = 'html') {
  163. return array(
  164. '#title' => $component['name'],
  165. '#weight' => $component['weight'],
  166. '#theme' => 'webform_display_textfield',
  167. '#theme_wrappers' => $format == 'html' ? array('webform_element') : array('webform_element_text'),
  168. '#field_prefix' => $component['extra']['field_prefix'],
  169. '#field_suffix' => $component['extra']['field_suffix'],
  170. '#format' => $format,
  171. '#value' => isset($value[0]) ? $value[0] : '',
  172. '#translatable' => array('title', 'field_prefix', 'field_suffix'),
  173. );
  174. }
  175. /**
  176. * Format the output of data for this component.
  177. */
  178. function theme_webform_display_textfield($variables) {
  179. $element = $variables['element'];
  180. $prefix = $element['#format'] == 'html' ? '' : $element['#field_prefix'];
  181. $suffix = $element['#format'] == 'html' ? '' : $element['#field_suffix'];
  182. $value = $element['#format'] == 'html' ? check_plain($element['#value']) : $element['#value'];
  183. return $value !== '' ? ($prefix . $value . $suffix) : ' ';
  184. }
  185. /**
  186. * Implements _webform_analysis_component().
  187. */
  188. function _webform_analysis_textfield($component, $sids = array()) {
  189. $query = db_select('webform_submitted_data', 'wsd', array('fetch' => PDO::FETCH_ASSOC))
  190. ->fields('wsd', array('data'))
  191. ->condition('nid', $component['nid'])
  192. ->condition('cid', $component['cid']);
  193. if (count($sids)) {
  194. $query->condition('sid', $sids, 'IN');
  195. }
  196. $nonblanks = 0;
  197. $submissions = 0;
  198. $wordcount = 0;
  199. $result = $query->execute();
  200. foreach ($result as $data) {
  201. if (drupal_strlen(trim($data['data'])) > 0) {
  202. $nonblanks++;
  203. $wordcount += str_word_count(trim($data['data']));
  204. }
  205. $submissions++;
  206. }
  207. $rows[0] = array(t('Left Blank'), ($submissions - $nonblanks));
  208. $rows[1] = array(t('User entered value'), $nonblanks);
  209. $rows[2] = array(t('Average submission length in words (ex blanks)'), ($nonblanks != 0 ? number_format($wordcount/$nonblanks, 2) : '0'));
  210. return $rows;
  211. }
  212. /**
  213. * Implements _webform_table_component().
  214. */
  215. function _webform_table_textfield($component, $value) {
  216. return check_plain(empty($value[0]) ? '' : $value[0]);
  217. }
  218. /**
  219. * Implements _webform_csv_headers_component().
  220. */
  221. function _webform_csv_headers_textfield($component, $export_options) {
  222. $header = array();
  223. $header[0] = '';
  224. $header[1] = '';
  225. $header[2] = $component['name'];
  226. return $header;
  227. }
  228. /**
  229. * Implements _webform_csv_data_component().
  230. */
  231. function _webform_csv_data_textfield($component, $export_options, $value) {
  232. return !isset($value[0]) ? '' : $value[0];
  233. }