textarea.inc 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <?php
  2. /**
  3. * @file
  4. * Webform module textarea component.
  5. */
  6. /**
  7. * Implements _webform_defaults_component().
  8. */
  9. function _webform_defaults_textarea() {
  10. return array(
  11. 'name' => '',
  12. 'form_key' => NULL,
  13. 'pid' => 0,
  14. 'weight' => 0,
  15. 'value' => '',
  16. 'required' => 0,
  17. 'extra' => array(
  18. 'cols' => '',
  19. 'rows' => '',
  20. 'title_display' => 0,
  21. 'resizable' => 1,
  22. 'disabled' => 0,
  23. 'description' => '',
  24. 'description_above' => FALSE,
  25. 'placeholder' => '',
  26. 'attributes' => array(),
  27. 'private' => FALSE,
  28. 'analysis' => FALSE,
  29. ),
  30. );
  31. }
  32. /**
  33. * Implements _webform_theme_component().
  34. */
  35. function _webform_theme_textarea() {
  36. return array(
  37. 'webform_display_textarea' => array(
  38. 'render element' => 'element',
  39. 'file' => 'components/textarea.inc',
  40. ),
  41. );
  42. }
  43. /**
  44. * Implements _webform_edit_component().
  45. */
  46. function _webform_edit_textarea($component) {
  47. $form = array();
  48. $form['value'] = array(
  49. '#type' => 'textarea',
  50. '#title' => t('Default value'),
  51. '#default_value' => $component['value'],
  52. '#description' => t('The default value of the field.') . ' ' . theme('webform_token_help'),
  53. '#cols' => 60,
  54. '#rows' => 5,
  55. '#weight' => 0,
  56. );
  57. $form['display']['cols'] = array(
  58. '#type' => 'textfield',
  59. '#title' => t('Width'),
  60. '#default_value' => $component['extra']['cols'],
  61. '#description' => t('Width of the textarea in columns. This property might not have a visual impact depending on the CSS of your site.') . ' ' . t('Leaving blank will use the default size.'),
  62. '#size' => 5,
  63. '#maxlength' => 10,
  64. '#parents' => array('extra', 'cols'),
  65. );
  66. $form['display']['rows'] = array(
  67. '#type' => 'textfield',
  68. '#title' => t('Height'),
  69. '#default_value' => $component['extra']['rows'],
  70. '#description' => t('Height of the textarea in rows.') . ' ' . t('Leaving blank will use the default size.'),
  71. '#size' => 5,
  72. '#maxlength' => 10,
  73. '#parents' => array('extra', 'rows'),
  74. );
  75. $form['display']['resizable'] = array(
  76. '#type' => 'checkbox',
  77. '#title' => t('Resizable'),
  78. '#description' => t('Make this field resizable by the user.'),
  79. '#weight' => 2,
  80. '#default_value' => $component['extra']['resizable'],
  81. '#parents' => array('extra', 'resizable'),
  82. );
  83. $form['display']['placeholder'] = array(
  84. '#type' => 'textfield',
  85. '#title' => t('Placeholder'),
  86. '#default_value' => $component['extra']['placeholder'],
  87. '#description' => t('The placeholder will be shown in the field until the user starts entering a value.'),
  88. '#parents' => array('extra', 'placeholder'),
  89. );
  90. $form['display']['disabled'] = array(
  91. '#type' => 'checkbox',
  92. '#title' => t('Disabled'),
  93. '#return_value' => 1,
  94. '#description' => t('Make this field non-editable. Useful for displaying default value. Changeable via JavaScript or developer tools.'),
  95. '#weight' => 11,
  96. '#default_value' => $component['extra']['disabled'],
  97. '#parents' => array('extra', 'disabled'),
  98. );
  99. return $form;
  100. }
  101. /**
  102. * Implements _webform_render_component().
  103. */
  104. function _webform_render_textarea($component, $value = NULL, $filter = TRUE, $submission = NULL) {
  105. $node = isset($component['nid']) ? node_load($component['nid']) : NULL;
  106. $element = array(
  107. '#type' => 'textarea',
  108. '#title' => $filter ? webform_filter_xss($component['name']) : $component['name'],
  109. '#title_display' => $component['extra']['title_display'] ? $component['extra']['title_display'] : 'before',
  110. '#default_value' => $filter ? webform_replace_tokens($component['value'], $node) : $component['value'],
  111. '#required' => $component['required'],
  112. '#weight' => $component['weight'],
  113. '#description' => $filter ? webform_filter_descriptions($component['extra']['description'], $node) : $component['extra']['description'],
  114. '#rows' => !empty($component['extra']['rows']) ? $component['extra']['rows'] : 5,
  115. '#cols' => !empty($component['extra']['cols']) ? $component['extra']['cols'] : 60,
  116. '#attributes' => $component['extra']['attributes'],
  117. // MUST be FALSE to disable.
  118. '#resizable' => (bool) $component['extra']['resizable'],
  119. '#theme_wrappers' => array('webform_element'),
  120. '#translatable' => array('title', 'description', 'placeholder'),
  121. );
  122. if ($component['required']) {
  123. $element['#attributes']['required'] = 'required';
  124. }
  125. if ($component['extra']['placeholder']) {
  126. $element['#attributes']['placeholder'] = $component['extra']['placeholder'];
  127. }
  128. if ($component['extra']['disabled']) {
  129. if ($filter) {
  130. $element['#attributes']['readonly'] = 'readonly';
  131. }
  132. else {
  133. $element['#disabled'] = TRUE;
  134. }
  135. }
  136. if (isset($value[0])) {
  137. $element['#default_value'] = $value[0];
  138. }
  139. return $element;
  140. }
  141. /**
  142. * Implements _webform_display_component().
  143. */
  144. function _webform_display_textarea($component, $value, $format = 'html', $submission = array()) {
  145. return array(
  146. '#title' => $component['name'],
  147. '#title_display' => $component['extra']['title_display'] ? $component['extra']['title_display'] : 'before',
  148. '#weight' => $component['weight'],
  149. '#theme' => 'webform_display_textarea',
  150. '#theme_wrappers' => $format == 'html' ? array('webform_element') : array('webform_element_text'),
  151. '#format' => $format,
  152. '#value' => isset($value[0]) ? $value[0] : '',
  153. '#translatable' => array('title', 'placeholder'),
  154. );
  155. }
  156. /**
  157. * Format the output of data for this component.
  158. */
  159. function theme_webform_display_textarea($variables) {
  160. $element = $variables['element'];
  161. $output = $element['#format'] == 'html' ? nl2br(check_plain($element['#value'])) : $element['#value'];
  162. if (drupal_strlen($output) > 80) {
  163. $output = ($element['#format'] == 'html') ? '<div class="webform-long-answer">' . $output . '</div>' : $output;
  164. }
  165. return $output !== '' ? $output : ' ';
  166. }
  167. /**
  168. * Implements _webform_analysis_component().
  169. */
  170. function _webform_analysis_textarea($component, $sids = array(), $single = FALSE, $join = NULL) {
  171. $query = db_select('webform_submitted_data', 'wsd', array('fetch' => PDO::FETCH_ASSOC))
  172. ->fields('wsd', array('no', 'data'))
  173. ->condition('wsd.nid', $component['nid'])
  174. ->condition('wsd.cid', $component['cid']);
  175. if (count($sids)) {
  176. $query->condition('wsd.sid', $sids, 'IN');
  177. }
  178. if ($join) {
  179. $query->innerJoin($join, 'ws2_', 'wsd.sid = ws2_.sid');
  180. }
  181. $nonblanks = 0;
  182. $submissions = 0;
  183. $wordcount = 0;
  184. $result = $query->execute();
  185. foreach ($result as $data) {
  186. if (drupal_strlen(trim($data['data'])) > 0) {
  187. $nonblanks++;
  188. $wordcount += str_word_count(trim($data['data']));
  189. }
  190. $submissions++;
  191. }
  192. $rows[0] = array(t('Left Blank'), ($submissions - $nonblanks));
  193. $rows[1] = array(t('User entered value'), $nonblanks);
  194. $other[] = array(
  195. t('Average submission length in words (ex blanks)'),
  196. $nonblanks != 0 ? number_format($wordcount / $nonblanks, 2) : '0',
  197. );
  198. return array(
  199. 'table_rows' => $rows,
  200. 'other_data' => $other,
  201. );
  202. }
  203. /**
  204. * Implements _webform_table_component().
  205. */
  206. function _webform_table_textarea($component, $value) {
  207. return empty($value[0]) ? '' : check_plain($value[0]);
  208. }
  209. /**
  210. * Implements _webform_action_set_component().
  211. */
  212. function _webform_action_set_textarea($component, &$element, &$form_state, $value) {
  213. $element['#value'] = $value;
  214. form_set_value($element, $value, $form_state);
  215. }
  216. /**
  217. * Implements _webform_csv_headers_component().
  218. */
  219. function _webform_csv_headers_textarea($component, $export_options) {
  220. $header = array();
  221. $header[0] = '';
  222. $header[1] = '';
  223. $header[2] = $export_options['header_keys'] ? $component['form_key'] : $component['name'];
  224. return $header;
  225. }
  226. /**
  227. * Implements _webform_csv_data_component().
  228. */
  229. function _webform_csv_data_textarea($component, $export_options, $value) {
  230. return empty($value[0]) ? '' : $value[0];
  231. }