textarea.inc 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. 'mandatory' => 0,
  17. 'extra' => array(
  18. 'cols' => '',
  19. 'rows' => '',
  20. 'title_display' => 0,
  21. 'resizable' => 1,
  22. 'disabled' => 0,
  23. 'description' => '',
  24. 'attributes' => array(),
  25. 'private' => FALSE,
  26. ),
  27. );
  28. }
  29. /**
  30. * Implements _webform_theme_component().
  31. */
  32. function _webform_theme_textarea() {
  33. return array(
  34. 'webform_display_textarea' => array(
  35. 'render element' => 'element',
  36. 'file' => 'components/textarea.inc',
  37. ),
  38. );
  39. }
  40. /**
  41. * Implements _webform_edit_component().
  42. */
  43. function _webform_edit_textarea($component) {
  44. $form = array();
  45. $form['value'] = array(
  46. '#type' => 'textarea',
  47. '#title' => t('Default value'),
  48. '#default_value' => $component['value'],
  49. '#description' => t('The default value of the field.') . theme('webform_token_help'),
  50. '#cols' => 60,
  51. '#rows' => 5,
  52. '#weight' => 0,
  53. );
  54. $form['display']['cols'] = array(
  55. '#type' => 'textfield',
  56. '#title' => t('Width'),
  57. '#default_value' => $component['extra']['cols'],
  58. '#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.'),
  59. '#size' => 5,
  60. '#maxlength' => 10,
  61. '#parents' => array('extra', 'cols'),
  62. );
  63. $form['display']['rows'] = array(
  64. '#type' => 'textfield',
  65. '#title' => t('Height'),
  66. '#default_value' => $component['extra']['rows'],
  67. '#description' => t('Height of the textarea in rows.') . ' ' . t('Leaving blank will use the default size.'),
  68. '#size' => 5,
  69. '#maxlength' => 10,
  70. '#parents' => array('extra', 'rows'),
  71. );
  72. $form['display']['resizable'] = array(
  73. '#type' => 'checkbox',
  74. '#title' => t('Resizable'),
  75. '#description' => t('Make this field resizable by the user.'),
  76. '#weight' => 2,
  77. '#default_value' => $component['extra']['resizable'],
  78. '#parents' => array('extra', 'resizable'),
  79. );
  80. $form['display']['disabled'] = array(
  81. '#type' => 'checkbox',
  82. '#title' => t('Disabled'),
  83. '#return_value' => 1,
  84. '#description' => t('Make this field non-editable. Useful for setting an unchangeable default value.'),
  85. '#weight' => 11,
  86. '#default_value' => $component['extra']['disabled'],
  87. '#parents' => array('extra', 'disabled'),
  88. );
  89. return $form;
  90. }
  91. /**
  92. * Implements _webform_render_component().
  93. */
  94. function _webform_render_textarea($component, $value = NULL, $filter = TRUE) {
  95. $node = isset($component['nid']) ? node_load($component['nid']) : NULL;
  96. $element = array(
  97. '#type' => 'textarea',
  98. '#title' => $filter ? _webform_filter_xss($component['name']) : $component['name'],
  99. '#title_display' => $component['extra']['title_display'] ? $component['extra']['title_display'] : 'before',
  100. '#default_value' => $filter ? _webform_filter_values($component['value'], $node) : $component['value'],
  101. '#required' => $component['mandatory'],
  102. '#weight' => $component['weight'],
  103. '#description' => $filter ? _webform_filter_descriptions($component['extra']['description'], $node) : $component['extra']['description'],
  104. '#rows' => !empty($component['extra']['rows']) ? $component['extra']['rows'] : 5,
  105. '#cols' => !empty($component['extra']['cols']) ? $component['extra']['cols'] : 60,
  106. '#attributes' => $component['extra']['attributes'],
  107. '#resizable' => (bool) $component['extra']['resizable'], // MUST be FALSE to disable.
  108. '#theme_wrappers' => array('webform_element'),
  109. '#translatable' => array('title', 'description'),
  110. );
  111. if ($component['extra']['disabled']) {
  112. if ($filter) {
  113. $element['#attributes']['readonly'] = 'readonly';
  114. }
  115. else {
  116. $element['#disabled'] = TRUE;
  117. }
  118. }
  119. if (isset($value)) {
  120. $element['#default_value'] = $value[0];
  121. }
  122. return $element;
  123. }
  124. /**
  125. * Implements _webform_display_component().
  126. */
  127. function _webform_display_textarea($component, $value, $format = 'html') {
  128. return array(
  129. '#title' => $component['name'],
  130. '#weight' => $component['weight'],
  131. '#theme' => 'webform_display_textarea',
  132. '#theme_wrappers' => $format == 'html' ? array('webform_element') : array('webform_element_text'),
  133. '#format' => $format,
  134. '#value' => isset($value[0]) ? $value[0] : '',
  135. '#translatable' => array('title'),
  136. );
  137. }
  138. /**
  139. * Format the output of data for this component.
  140. */
  141. function theme_webform_display_textarea($variables) {
  142. $element = $variables['element'];
  143. $output = $element['#format'] == 'html' ? nl2br(check_plain($element['#value'])) : $element['#value'];
  144. if (drupal_strlen($output) > 80) {
  145. $output = ($element['#format'] == 'html') ? '<div class="webform-long-answer">' . $output . '</div>' : $output;
  146. }
  147. return $output !== '' ? $output : ' ';
  148. }
  149. /**
  150. * Implements _webform_analysis_component().
  151. */
  152. function _webform_analysis_textarea($component, $sids = array()) {
  153. $query = db_select('webform_submitted_data', 'wsd', array('fetch' => PDO::FETCH_ASSOC))
  154. ->fields('wsd', array('no', 'data'))
  155. ->condition('nid', $component['nid'])
  156. ->condition('cid', $component['cid']);
  157. if (count($sids)) {
  158. $query->condition('sid', $sids, 'IN');
  159. }
  160. $nonblanks = 0;
  161. $submissions = 0;
  162. $wordcount = 0;
  163. $result = $query->execute();
  164. foreach ($result as $data) {
  165. if (drupal_strlen(trim($data['data'])) > 0) {
  166. $nonblanks++;
  167. $wordcount += str_word_count(trim($data['data']));
  168. }
  169. $submissions++;
  170. }
  171. $rows[0] = array(t('Left Blank'), ($submissions - $nonblanks));
  172. $rows[1] = array(t('User entered value'), $nonblanks);
  173. $rows[2] = array(t('Average submission length in words (ex blanks)'), ($nonblanks != 0 ? number_format($wordcount/$nonblanks, 2) : '0'));
  174. return $rows;
  175. }
  176. /**
  177. * Implements _webform_table_component().
  178. */
  179. function _webform_table_textarea($component, $value) {
  180. return empty($value[0]) ? '' : check_plain($value[0]);
  181. }
  182. /**
  183. * Implements _webform_csv_headers_component().
  184. */
  185. function _webform_csv_headers_textarea($component, $export_options) {
  186. $header = array();
  187. $header[0] = '';
  188. $header[1] = '';
  189. $header[2] = $component['name'];
  190. return $header;
  191. }
  192. /**
  193. * Implements _webform_csv_data_component().
  194. */
  195. function _webform_csv_data_textarea($component, $export_options, $value) {
  196. return empty($value[0]) ? '' : $value[0];
  197. }