hidden.inc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. /**
  3. * @file
  4. * Webform module hidden component.
  5. */
  6. /**
  7. * Implements _webform_defaults_component().
  8. */
  9. function _webform_defaults_hidden() {
  10. return array(
  11. 'name' => '',
  12. 'form_key' => NULL,
  13. 'pid' => 0,
  14. 'weight' => 0,
  15. 'value' => '',
  16. 'extra' => array(
  17. 'private' => FALSE,
  18. 'hidden_type' => 'value',
  19. 'analysis' => FALSE,
  20. ),
  21. );
  22. }
  23. /**
  24. * Implements _webform_theme_component().
  25. */
  26. function _webform_theme_hidden() {
  27. return array(
  28. 'webform_display_hidden' => array(
  29. 'render element' => 'element',
  30. 'file' => 'components/hidden.inc',
  31. ),
  32. );
  33. }
  34. /**
  35. * Implements _webform_edit_component().
  36. */
  37. function _webform_edit_hidden($component) {
  38. $form = array();
  39. $form['value'] = array(
  40. '#type' => 'textarea',
  41. '#title' => t('Default value'),
  42. '#default_value' => $component['value'],
  43. '#description' => t('The default value of the field.') . ' ' . theme('webform_token_help'),
  44. '#cols' => 60,
  45. '#rows' => 5,
  46. '#weight' => 0,
  47. );
  48. $form['display']['hidden_type'] = array(
  49. '#type' => 'radios',
  50. '#options' => array(
  51. 'value' => t('Secure value (allows use of all tokens)'),
  52. 'hidden' => t('Hidden element (less secure, changeable via JavaScript)'),
  53. ),
  54. '#title' => t('Hidden type'),
  55. '#description' => t('Both types of hidden fields are not shown to end-users. Using a <em>Secure value</em> allows the use of <em>all tokens</em>, even for anonymous users.'),
  56. '#default_value' => $component['extra']['hidden_type'],
  57. '#parents' => array('extra', 'hidden_type'),
  58. );
  59. return $form;
  60. }
  61. /**
  62. * Implements _webform_render_component().
  63. */
  64. function _webform_render_hidden($component, $value = NULL, $filter = TRUE, $submission = NULL) {
  65. $node = isset($component['nid']) ? node_load($component['nid']) : NULL;
  66. $default_value = $filter ? webform_replace_tokens($component['value'], $node) : $component['value'];
  67. if (isset($value[0])) {
  68. $default_value = $value[0];
  69. }
  70. $element = array(
  71. '#title' => $filter ? NULL : $component['name'],
  72. '#weight' => $component['weight'],
  73. '#translatable' => array('title'),
  74. );
  75. if ($component['extra']['hidden_type'] == 'value') {
  76. $element['#type'] = 'value';
  77. $element['#value'] = $default_value;
  78. }
  79. else {
  80. $element['#type'] = 'hidden';
  81. $element['#default_value'] = $default_value;
  82. // Same-page conditionals depend on the wrapper around elements for getting
  83. // values. Wrap, but hide, the wrapper around hidden elements.
  84. $element['#theme_wrappers'] = array('webform_element');
  85. $element['#wrapper_attributes']['class'] = array();
  86. $element['#wrapper_attributes']['style'] = array('display: none');
  87. }
  88. return $element;
  89. }
  90. /**
  91. * Implements _webform_display_component().
  92. */
  93. function _webform_display_hidden($component, $value, $format = 'html', $submission = array()) {
  94. $element = array(
  95. '#title' => $component['name'],
  96. '#markup' => isset($value[0]) ? $value[0] : NULL,
  97. '#weight' => $component['weight'],
  98. '#format' => $format,
  99. '#theme' => 'webform_display_hidden',
  100. '#theme_wrappers' => $format == 'text' ? array('webform_element_text') : array('webform_element'),
  101. '#translatable' => array('title'),
  102. );
  103. return $element;
  104. }
  105. /**
  106. * Theme callback.
  107. */
  108. function theme_webform_display_hidden($variables) {
  109. $element = $variables['element'];
  110. return $element['#format'] == 'html' ? check_plain($element['#markup']) : $element['#markup'];
  111. }
  112. /**
  113. * Implements _webform_analysis_component().
  114. */
  115. function _webform_analysis_hidden($component, $sids = array(), $single = FALSE, $join = NULL) {
  116. $query = db_select('webform_submitted_data', 'wsd', array('fetch' => PDO::FETCH_ASSOC))
  117. ->fields('wsd', array('no', 'data'))
  118. ->condition('wsd.nid', $component['nid'])
  119. ->condition('wsd.cid', $component['cid']);
  120. if (count($sids)) {
  121. $query->condition('wsd.sid', $sids, 'IN');
  122. }
  123. if ($join) {
  124. $query->innerJoin($join, 'ws2_', 'wsd.sid = ws2_.sid');
  125. }
  126. $nonblanks = 0;
  127. $submissions = 0;
  128. $wordcount = 0;
  129. $result = $query->execute();
  130. foreach ($result as $data) {
  131. if (strlen(trim($data['data'])) > 0) {
  132. $nonblanks++;
  133. $wordcount += str_word_count(trim($data['data']));
  134. }
  135. $submissions++;
  136. }
  137. $rows[0] = array(t('Empty'), ($submissions - $nonblanks));
  138. $rows[1] = array(t('Non-empty'), $nonblanks);
  139. $other[0] = array(
  140. t('Average submission length in words (ex blanks)'),
  141. $nonblanks != 0 ? number_format($wordcount / $nonblanks, 2) : '0',
  142. );
  143. return array(
  144. 'table_rows' => $rows,
  145. 'other_data' => $other,
  146. );
  147. }
  148. /**
  149. * Implements _webform_csv_data_component().
  150. */
  151. function _webform_table_hidden($component, $value) {
  152. return check_plain(empty($value[0]) ? '' : $value[0]);
  153. }
  154. /**
  155. * Implements _webform_action_set_component().
  156. */
  157. function _webform_action_set_hidden($component, &$element, &$form_state, $value) {
  158. $element['#value'] = $value;
  159. form_set_value($element, $value, $form_state);
  160. }
  161. /**
  162. * Implements _webform_csv_headers_component().
  163. */
  164. function _webform_csv_headers_hidden($component, $export_options) {
  165. $header = array();
  166. $header[0] = '';
  167. $header[1] = '';
  168. $header[2] = $export_options['header_keys'] ? $component['form_key'] : $component['name'];
  169. return $header;
  170. }
  171. /**
  172. * Implements _webform_csv_data_component().
  173. */
  174. function _webform_csv_data_hidden($component, $export_options, $value) {
  175. return isset($value[0]) ? $value[0] : '';
  176. }