hidden.inc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. return $element;
  102. }
  103. function theme_webform_display_hidden($variables) {
  104. $element = $variables['element'];
  105. return $element['#format'] == 'html' ? check_plain($element['#markup']) : $element['#markup'];
  106. }
  107. /**
  108. * Implements _webform_analysis_component().
  109. */
  110. function _webform_analysis_hidden($component, $sids = array()) {
  111. $query = db_select('webform_submitted_data', 'wsd', array('fetch' => PDO::FETCH_ASSOC))
  112. ->fields('wsd', array('no', 'data'))
  113. ->condition('nid', $component['nid'])
  114. ->condition('cid', $component['cid']);
  115. if (count($sids)) {
  116. $query->condition('sid', $sids, 'IN');
  117. }
  118. $nonblanks = 0;
  119. $submissions = 0;
  120. $wordcount = 0;
  121. $result = $query->execute();
  122. foreach ($result as $data) {
  123. if (strlen(trim($data['data'])) > 0) {
  124. $nonblanks++;
  125. $wordcount += str_word_count(trim($data['data']));
  126. }
  127. $submissions++;
  128. }
  129. $rows[0] = array( t('Empty'), ($submissions - $nonblanks));
  130. $rows[1] = array( t('Non-empty'), $nonblanks);
  131. $rows[2] = array( t('Average submission length in words (ex blanks)'),
  132. ($nonblanks !=0 ? number_format($wordcount/$nonblanks, 2) : '0'));
  133. return $rows;
  134. }
  135. /**
  136. * Implements _webform_csv_data_component().
  137. */
  138. function _webform_table_hidden($component, $value) {
  139. return check_plain(empty($value[0]) ? '' : $value[0]);
  140. }
  141. /**
  142. * Implements _webform_csv_data_component().
  143. */
  144. function _webform_csv_headers_hidden($component, $export_options) {
  145. $header = array();
  146. $header[0] = '';
  147. $header[1] = '';
  148. $header[2] = $component['name'];
  149. return $header;
  150. }
  151. /**
  152. * Implements _webform_csv_data_component().
  153. */
  154. function _webform_csv_data_hidden($component, $export_options, $value) {
  155. return isset($value[0]) ? $value[0] : '';
  156. }