hidden.inc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. ),
  20. );
  21. }
  22. /**
  23. * Implements _webform_theme_component().
  24. */
  25. function _webform_theme_hidden() {
  26. return array(
  27. 'webform_display_hidden' => array(
  28. 'render element' => 'element',
  29. 'file' => 'components/hidden.inc',
  30. ),
  31. );
  32. }
  33. /**
  34. * Implements _webform_edit_component().
  35. */
  36. function _webform_edit_hidden($component) {
  37. $form = array();
  38. $form['value'] = array(
  39. '#type' => 'textarea',
  40. '#title' => t('Default value'),
  41. '#default_value' => $component['value'],
  42. '#description' => t('The default value of the field.') . theme('webform_token_help'),
  43. '#cols' => 60,
  44. '#rows' => 5,
  45. '#weight' => 0,
  46. );
  47. $form['display']['hidden_type'] = array(
  48. '#type' => 'radios',
  49. '#options' => array(
  50. 'value' => t('Secure value (allows use of all tokens)'),
  51. 'hidden' => t('Hidden element (less secure, changeable via JavaScript)'),
  52. ),
  53. '#title' => t('Hidden type'),
  54. '#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.'),
  55. '#default_value' => $component['extra']['hidden_type'],
  56. '#parents' => array('extra', 'hidden_type'),
  57. );
  58. return $form;
  59. }
  60. /**
  61. * Implements _webform_render_component().
  62. */
  63. function _webform_render_hidden($component, $value = NULL, $filter = TRUE) {
  64. $node = isset($component['nid']) ? node_load($component['nid']) : NULL;
  65. // Set filtering options for "value" types, which are not displayed to the
  66. // end user so they do not need to be sanitized.
  67. $strict = $component['extra']['hidden_type'] != 'value';
  68. $allow_anonymous = $component['extra']['hidden_type'] == 'value';
  69. $default_value = $filter ? _webform_filter_values($component['value'], $node, NULL, NULL, $strict, $allow_anonymous) : $component['value'];
  70. if (isset($value[0])) {
  71. $default_value = $value[0];
  72. }
  73. $element = array(
  74. '#title' => $filter ? _webform_filter_xss($component['name']) : $component['name'],
  75. '#weight' => $component['weight'],
  76. '#translatable' => array('title'),
  77. );
  78. if ($component['extra']['hidden_type'] == 'value') {
  79. $element['#type'] = 'value';
  80. $element['#value'] = $default_value;
  81. }
  82. else {
  83. $element['#type'] = 'hidden';
  84. $element['#default_value'] = $default_value;
  85. }
  86. return $element;
  87. }
  88. /**
  89. * Implements _webform_display_component().
  90. */
  91. function _webform_display_hidden($component, $value, $format = 'html') {
  92. $element = array(
  93. '#title' => $component['name'],
  94. '#markup' => isset($value[0]) ? $value[0] : NULL,
  95. '#weight' => $component['weight'],
  96. '#format' => $format,
  97. '#theme' => 'webform_display_hidden',
  98. '#theme_wrappers' => $format == 'text' ? array('webform_element_text') : array('webform_element'),
  99. '#translatable' => array('title'),
  100. );
  101. // TODO: This check is unusual. It shows hidden fields in e-mails but not
  102. // when viewing in the browser unless you're an administrator. This should be
  103. // a more logical check. See these related issues:
  104. // http://drupal.org/node/313639
  105. // http://drupal.org/node/781786
  106. if ($format == 'html') {
  107. $element['#access'] = user_access('edit all webform submissions') || user_access('access all webform results');
  108. }
  109. return $element;
  110. }
  111. function theme_webform_display_hidden($variables) {
  112. $element = $variables['element'];
  113. return $element['#format'] == 'html' ? check_plain($element['#markup']) : $element['#markup'];
  114. }
  115. /**
  116. * Implements _webform_analysis_component().
  117. */
  118. function _webform_analysis_hidden($component, $sids = array()) {
  119. $query = db_select('webform_submitted_data', 'wsd', array('fetch' => PDO::FETCH_ASSOC))
  120. ->fields('wsd', array('no', 'data'))
  121. ->condition('nid', $component['nid'])
  122. ->condition('cid', $component['cid']);
  123. if (count($sids)) {
  124. $query->condition('sid', $sids, 'IN');
  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. $rows[2] = array( t('Average submission length in words (ex blanks)'),
  140. ($nonblanks !=0 ? number_format($wordcount/$nonblanks, 2) : '0'));
  141. return $rows;
  142. }
  143. /**
  144. * Implements _webform_csv_data_component().
  145. */
  146. function _webform_table_hidden($component, $value) {
  147. return check_plain(empty($value[0]) ? '' : $value[0]);
  148. }
  149. /**
  150. * Implements _webform_csv_data_component().
  151. */
  152. function _webform_csv_headers_hidden($component, $export_options) {
  153. $header = array();
  154. $header[0] = '';
  155. $header[1] = '';
  156. $header[2] = $component['name'];
  157. return $header;
  158. }
  159. /**
  160. * Implements _webform_csv_data_component().
  161. */
  162. function _webform_csv_data_hidden($component, $export_options, $value) {
  163. return isset($value[0]) ? $value[0] : '';
  164. }