textfield.inc 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. 'required' => 0,
  17. 'extra' => array(
  18. 'width' => '',
  19. 'maxlength' => '',
  20. 'minlength' => '',
  21. 'field_prefix' => '',
  22. 'field_suffix' => '',
  23. 'disabled' => 0,
  24. 'unique' => 0,
  25. 'title_display' => 0,
  26. 'description' => '',
  27. 'description_above' => FALSE,
  28. 'placeholder' => '',
  29. 'attributes' => array(),
  30. 'private' => FALSE,
  31. 'analysis' => FALSE,
  32. ),
  33. );
  34. }
  35. /**
  36. * Implements _webform_theme_component().
  37. */
  38. function _webform_theme_textfield() {
  39. return array(
  40. 'webform_display_textfield' => array(
  41. 'render element' => 'element',
  42. 'file' => 'components/textfield.inc',
  43. ),
  44. );
  45. }
  46. /**
  47. * Implements _webform_edit_component().
  48. */
  49. function _webform_edit_textfield($component) {
  50. $form = array();
  51. $form['value'] = array(
  52. '#type' => 'textfield',
  53. '#title' => t('Default value'),
  54. '#default_value' => $component['value'],
  55. '#description' => t('The default value of the field.') . ' ' . theme('webform_token_help'),
  56. '#size' => 60,
  57. '#maxlength' => 1024,
  58. '#weight' => 0,
  59. );
  60. $form['display']['width'] = array(
  61. '#type' => 'textfield',
  62. '#title' => t('Width'),
  63. '#default_value' => $component['extra']['width'],
  64. '#description' => t('Width of the textfield.') . ' ' . t('Leaving blank will use the default size.'),
  65. '#size' => 5,
  66. '#maxlength' => 10,
  67. '#weight' => 0,
  68. '#parents' => array('extra', 'width'),
  69. );
  70. $form['display']['placeholder'] = array(
  71. '#type' => 'textfield',
  72. '#title' => t('Placeholder'),
  73. '#default_value' => $component['extra']['placeholder'],
  74. '#description' => t('The placeholder will be shown in the field until the user starts entering a value.'),
  75. '#weight' => 1,
  76. '#parents' => array('extra', 'placeholder'),
  77. );
  78. $form['display']['field_prefix'] = array(
  79. '#type' => 'textfield',
  80. '#title' => t('Prefix text placed to the left of the textfield'),
  81. '#default_value' => $component['extra']['field_prefix'],
  82. '#description' => t('Examples: $, #, -.'),
  83. '#size' => 20,
  84. '#maxlength' => 127,
  85. '#weight' => 2.1,
  86. '#parents' => array('extra', 'field_prefix'),
  87. );
  88. $form['display']['field_suffix'] = array(
  89. '#type' => 'textfield',
  90. '#title' => t('Postfix text placed to the right of the textfield'),
  91. '#default_value' => $component['extra']['field_suffix'],
  92. '#description' => t('Examples: lb, kg, %.'),
  93. '#size' => 20,
  94. '#maxlength' => 127,
  95. '#weight' => 2.2,
  96. '#parents' => array('extra', 'field_suffix'),
  97. );
  98. $form['display']['disabled'] = array(
  99. '#type' => 'checkbox',
  100. '#title' => t('Disabled'),
  101. '#return_value' => 1,
  102. '#description' => t('Make this field non-editable. Useful for displaying default value. Changeable via JavaScript or developer tools.'),
  103. '#weight' => 11,
  104. '#default_value' => $component['extra']['disabled'],
  105. '#parents' => array('extra', 'disabled'),
  106. );
  107. $form['validation']['unique'] = array(
  108. '#type' => 'checkbox',
  109. '#title' => t('Unique'),
  110. '#return_value' => 1,
  111. '#description' => t('Check that all entered values for this field are unique. The same value is not allowed to be used twice.'),
  112. '#weight' => 1,
  113. '#default_value' => $component['extra']['unique'],
  114. '#parents' => array('extra', 'unique'),
  115. );
  116. $form['validation']['maxlength'] = array(
  117. '#type' => 'textfield',
  118. '#title' => t('Maxlength'),
  119. '#default_value' => $component['extra']['maxlength'],
  120. '#description' => t('Maximum length of the textfield value.'),
  121. '#size' => 5,
  122. '#maxlength' => 10,
  123. '#weight' => 2,
  124. '#parents' => array('extra', 'maxlength'),
  125. );
  126. $form['validation']['minlength'] = array(
  127. '#type' => 'textfield',
  128. '#title' => t('Minlength'),
  129. '#default_value' => $component['extra']['minlength'],
  130. '#description' => t('Minimum length of the textfield value. The component may still be empty unless it is set as Required.'),
  131. '#size' => 5,
  132. '#maxlength' => 10,
  133. '#weight' => 3,
  134. '#parents' => array('extra', 'minlength'),
  135. );
  136. return $form;
  137. }
  138. /**
  139. * Implements _webform_render_component().
  140. */
  141. function _webform_render_textfield($component, $value = NULL, $filter = TRUE, $submission = NULL) {
  142. $node = isset($component['nid']) ? node_load($component['nid']) : NULL;
  143. $element = array(
  144. '#type' => 'textfield',
  145. '#title' => $filter ? webform_filter_xss($component['name']) : $component['name'],
  146. '#title_display' => $component['extra']['title_display'] ? $component['extra']['title_display'] : 'before',
  147. '#default_value' => $filter ? webform_replace_tokens($component['value'], $node) : $component['value'],
  148. '#required' => $component['required'],
  149. '#weight' => $component['weight'],
  150. '#field_prefix' => empty($component['extra']['field_prefix']) ? NULL : ($filter ? webform_filter_xss($component['extra']['field_prefix']) : $component['extra']['field_prefix']),
  151. '#field_suffix' => empty($component['extra']['field_suffix']) ? NULL : ($filter ? webform_filter_xss($component['extra']['field_suffix']) : $component['extra']['field_suffix']),
  152. '#description' => $filter ? webform_filter_descriptions($component['extra']['description'], $node) : $component['extra']['description'],
  153. '#attributes' => $component['extra']['attributes'],
  154. '#theme_wrappers' => array('webform_element'),
  155. '#translatable' => array(
  156. 'title',
  157. 'description',
  158. 'field_prefix',
  159. 'field_suffix',
  160. 'placeholder',
  161. ),
  162. );
  163. if ($component['required']) {
  164. $element['#attributes']['required'] = 'required';
  165. }
  166. if ($component['extra']['placeholder']) {
  167. $element['#attributes']['placeholder'] = $component['extra']['placeholder'];
  168. }
  169. if ($component['extra']['disabled']) {
  170. if ($filter) {
  171. $element['#attributes']['readonly'] = 'readonly';
  172. }
  173. else {
  174. $element['#disabled'] = TRUE;
  175. }
  176. }
  177. // Enforce uniqueness.
  178. if ($component['extra']['unique']) {
  179. $element['#element_validate'][] = 'webform_validate_unique';
  180. }
  181. // Change the 'width' option to the correct 'size' option.
  182. if ($component['extra']['width'] > 0) {
  183. $element['#size'] = $component['extra']['width'];
  184. }
  185. if ($component['extra']['maxlength'] > 0) {
  186. $element['#maxlength'] = $component['extra']['maxlength'];
  187. }
  188. if ($component['extra']['minlength'] > 0) {
  189. $element['#minlength'] = $component['extra']['minlength'];
  190. }
  191. if (isset($value[0])) {
  192. $element['#default_value'] = $value[0];
  193. }
  194. return $element;
  195. }
  196. /**
  197. * Implements _webform_display_component().
  198. */
  199. function _webform_display_textfield($component, $value, $format = 'html', $submission = array()) {
  200. return array(
  201. '#title' => $component['name'],
  202. '#title_display' => $component['extra']['title_display'] ? $component['extra']['title_display'] : 'before',
  203. '#weight' => $component['weight'],
  204. '#theme' => 'webform_display_textfield',
  205. '#theme_wrappers' => $format == 'html' ? array('webform_element') : array('webform_element_text'),
  206. '#field_prefix' => $component['extra']['field_prefix'],
  207. '#field_suffix' => $component['extra']['field_suffix'],
  208. '#format' => $format,
  209. '#value' => isset($value[0]) ? $value[0] : '',
  210. '#translatable' => array('title', 'field_prefix', 'field_suffix', 'placeholder'),
  211. );
  212. }
  213. /**
  214. * Format the output of data for this component.
  215. */
  216. function theme_webform_display_textfield($variables) {
  217. $element = $variables['element'];
  218. $prefix = $element['#format'] == 'html' ? '' : $element['#field_prefix'];
  219. $suffix = $element['#format'] == 'html' ? '' : $element['#field_suffix'];
  220. $value = $element['#format'] == 'html' ? check_plain($element['#value']) : $element['#value'];
  221. return $value !== '' ? ($prefix . $value . $suffix) : ' ';
  222. }
  223. /**
  224. * Implements _webform_analysis_component().
  225. */
  226. function _webform_analysis_textfield($component, $sids = array(), $single = FALSE, $join = NULL) {
  227. $query = db_select('webform_submitted_data', 'wsd', array('fetch' => PDO::FETCH_ASSOC))
  228. ->fields('wsd', array('data'))
  229. ->condition('wsd.nid', $component['nid'])
  230. ->condition('wsd.cid', $component['cid']);
  231. if (count($sids)) {
  232. $query->condition('wsd.sid', $sids, 'IN');
  233. }
  234. if ($join) {
  235. $query->innerJoin($join, 'ws2_', 'wsd.sid = ws2_.sid');
  236. }
  237. $nonblanks = 0;
  238. $submissions = 0;
  239. $wordcount = 0;
  240. $result = $query->execute();
  241. foreach ($result as $data) {
  242. if (drupal_strlen(trim($data['data'])) > 0) {
  243. $nonblanks++;
  244. $wordcount += str_word_count(trim($data['data']));
  245. }
  246. $submissions++;
  247. }
  248. $rows[0] = array(t('Left Blank'), ($submissions - $nonblanks));
  249. $rows[1] = array(t('User entered value'), $nonblanks);
  250. $other[] = array(
  251. t('Average submission length in words (ex blanks)'),
  252. $nonblanks != 0 ? number_format($wordcount / $nonblanks, 2) : '0',
  253. );
  254. return array(
  255. 'table_rows' => $rows,
  256. 'other_data' => $other,
  257. );
  258. }
  259. /**
  260. * Implements _webform_table_component().
  261. */
  262. function _webform_table_textfield($component, $value) {
  263. return check_plain(empty($value[0]) ? '' : $value[0]);
  264. }
  265. /**
  266. * Implements _webform_action_set_component().
  267. */
  268. function _webform_action_set_textfield($component, &$element, &$form_state, $value) {
  269. $element['#value'] = $value;
  270. form_set_value($element, $value, $form_state);
  271. }
  272. /**
  273. * Implements _webform_csv_headers_component().
  274. */
  275. function _webform_csv_headers_textfield($component, $export_options) {
  276. $header = array();
  277. $header[0] = '';
  278. $header[1] = '';
  279. $header[2] = $export_options['header_keys'] ? $component['form_key'] : $component['name'];
  280. return $header;
  281. }
  282. /**
  283. * Implements _webform_csv_data_component().
  284. */
  285. function _webform_csv_data_textfield($component, $export_options, $value) {
  286. return !isset($value[0]) ? '' : $value[0];
  287. }