comparison sites/all/modules/webform/components/hidden.inc @ 0:ff03f76ab3fe

initial version
author danieleb <danielebarchiesi@me.com>
date Wed, 21 Aug 2013 18:51:11 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:ff03f76ab3fe
1 <?php
2
3 /**
4 * @file
5 * Webform module hidden component.
6 */
7
8 /**
9 * Implements _webform_defaults_component().
10 */
11 function _webform_defaults_hidden() {
12 return array(
13 'name' => '',
14 'form_key' => NULL,
15 'pid' => 0,
16 'weight' => 0,
17 'value' => '',
18 'extra' => array(
19 'private' => FALSE,
20 'hidden_type' => 'value',
21 ),
22 );
23 }
24
25 /**
26 * Implements _webform_theme_component().
27 */
28 function _webform_theme_hidden() {
29 return array(
30 'webform_display_hidden' => array(
31 'render element' => 'element',
32 'file' => 'components/hidden.inc',
33 ),
34 );
35 }
36
37 /**
38 * Implements _webform_edit_component().
39 */
40 function _webform_edit_hidden($component) {
41 $form = array();
42 $form['value'] = array(
43 '#type' => 'textarea',
44 '#title' => t('Default value'),
45 '#default_value' => $component['value'],
46 '#description' => t('The default value of the field.') . theme('webform_token_help'),
47 '#cols' => 60,
48 '#rows' => 5,
49 '#weight' => 0,
50 );
51
52 $form['display']['hidden_type'] = array(
53 '#type' => 'radios',
54 '#options' => array(
55 'value' => t('Secure value (allows use of all tokens)'),
56 'hidden' => t('Hidden element (less secure, changeable via JavaScript)'),
57 ),
58 '#title' => t('Hidden type'),
59 '#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.'),
60 '#default_value' => $component['extra']['hidden_type'],
61 '#parents' => array('extra', 'hidden_type'),
62 );
63
64 return $form;
65 }
66
67 /**
68 * Implements _webform_render_component().
69 */
70 function _webform_render_hidden($component, $value = NULL, $filter = TRUE) {
71 $node = isset($component['nid']) ? node_load($component['nid']) : NULL;
72
73 // Set filtering options for "value" types, which are not displayed to the
74 // end user so they do not need to be sanitized.
75 $strict = $component['extra']['hidden_type'] != 'value';
76 $allow_anonymous = $component['extra']['hidden_type'] == 'value';
77 $default_value = $filter ? _webform_filter_values($component['value'], $node, NULL, NULL, $strict, $allow_anonymous) : $component['value'];
78 if (isset($value[0])) {
79 $default_value = $value[0];
80 }
81
82 $element = array(
83 '#title' => $filter ? _webform_filter_xss($component['name']) : $component['name'],
84 '#weight' => $component['weight'],
85 '#translatable' => array('title'),
86 );
87
88 if ($component['extra']['hidden_type'] == 'value') {
89 $element['#type'] = 'value';
90 $element['#value'] = $default_value;
91 }
92 else {
93 $element['#type'] = 'hidden';
94 $element['#default_value'] = $default_value;
95 }
96
97 return $element;
98 }
99
100 /**
101 * Implements _webform_display_component().
102 */
103 function _webform_display_hidden($component, $value, $format = 'html') {
104 $element = array(
105 '#title' => $component['name'],
106 '#markup' => isset($value[0]) ? $value[0] : NULL,
107 '#weight' => $component['weight'],
108 '#format' => $format,
109 '#theme' => 'webform_display_hidden',
110 '#theme_wrappers' => $format == 'text' ? array('webform_element_text') : array('webform_element'),
111 '#translatable' => array('title'),
112 );
113
114 return $element;
115 }
116
117 function theme_webform_display_hidden($variables) {
118 $element = $variables['element'];
119
120 return $element['#format'] == 'html' ? check_plain($element['#markup']) : $element['#markup'];
121 }
122
123 /**
124 * Implements _webform_analysis_component().
125 */
126 function _webform_analysis_hidden($component, $sids = array()) {
127 $query = db_select('webform_submitted_data', 'wsd', array('fetch' => PDO::FETCH_ASSOC))
128 ->fields('wsd', array('no', 'data'))
129 ->condition('nid', $component['nid'])
130 ->condition('cid', $component['cid']);
131
132 if (count($sids)) {
133 $query->condition('sid', $sids, 'IN');
134 }
135
136 $nonblanks = 0;
137 $submissions = 0;
138 $wordcount = 0;
139
140 $result = $query->execute();
141 foreach ($result as $data) {
142 if (strlen(trim($data['data'])) > 0) {
143 $nonblanks++;
144 $wordcount += str_word_count(trim($data['data']));
145 }
146 $submissions++;
147 }
148
149 $rows[0] = array( t('Empty'), ($submissions - $nonblanks));
150 $rows[1] = array( t('Non-empty'), $nonblanks);
151 $rows[2] = array( t('Average submission length in words (ex blanks)'),
152 ($nonblanks !=0 ? number_format($wordcount/$nonblanks, 2) : '0'));
153 return $rows;
154 }
155
156 /**
157 * Implements _webform_csv_data_component().
158 */
159 function _webform_table_hidden($component, $value) {
160 return check_plain(empty($value[0]) ? '' : $value[0]);
161 }
162
163 /**
164 * Implements _webform_csv_data_component().
165 */
166 function _webform_csv_headers_hidden($component, $export_options) {
167 $header = array();
168 $header[0] = '';
169 $header[1] = '';
170 $header[2] = $component['name'];
171 return $header;
172 }
173
174 /**
175 * Implements _webform_csv_data_component().
176 */
177 function _webform_csv_data_hidden($component, $export_options, $value) {
178 return isset($value[0]) ? $value[0] : '';
179 }