Projet

Général

Profil

Paste
Télécharger (5,36 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / webform / components / hidden.inc @ 01f36513

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
      'analysis' => FALSE,
22
    ),
23
  );
24
}
25

    
26
/**
27
 * Implements _webform_theme_component().
28
 */
29
function _webform_theme_hidden() {
30
  return array(
31
    'webform_display_hidden' => array(
32
      'render element' => 'element',
33
      'file' => 'components/hidden.inc',
34
    ),
35
  );
36
}
37

    
38
/**
39
 * Implements _webform_edit_component().
40
 */
41
function _webform_edit_hidden($component) {
42
  $form = array();
43
  $form['value'] = array(
44
    '#type' => 'textarea',
45
    '#title' => t('Default value'),
46
    '#default_value' => $component['value'],
47
    '#description' => t('The default value of the field.') . ' ' . theme('webform_token_help'),
48
    '#cols' => 60,
49
    '#rows' => 5,
50
    '#weight' => 0,
51
  );
52

    
53
  $form['display']['hidden_type'] = array(
54
    '#type' => 'radios',
55
    '#options' => array(
56
      'value' => t('Secure value (allows use of all tokens)'),
57
      'hidden' => t('Hidden element (less secure, changeable via JavaScript)'),
58
    ),
59
    '#title' => t('Hidden type'),
60
    '#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.'),
61
    '#default_value' => $component['extra']['hidden_type'],
62
    '#parents' => array('extra', 'hidden_type'),
63
  );
64

    
65
  return $form;
66
}
67

    
68
/**
69
 * Implements _webform_render_component().
70
 */
71
function _webform_render_hidden($component, $value = NULL, $filter = TRUE, $submission = NULL) {
72
  $node = isset($component['nid']) ? node_load($component['nid']) : NULL;
73

    
74
  $default_value = $filter ? webform_replace_tokens($component['value'], $node) : $component['value'];
75
  if (isset($value[0])) {
76
    $default_value = $value[0];
77
  }
78

    
79
  $element = array(
80
    '#title' => $filter ? NULL : $component['name'],
81
    '#weight' => $component['weight'],
82
    '#translatable' => array('title'),
83
  );
84

    
85
  if ($component['extra']['hidden_type'] == 'value') {
86
    $element['#type'] = 'value';
87
    $element['#value'] = $default_value;
88
  }
89
  else {
90
    $element['#type'] = 'hidden';
91
    $element['#default_value'] = $default_value;
92

    
93
    // Same-page conditionals depend on the wrapper around elements for getting
94
    // values. Wrap, but hide, the wrapper around hidden elements.
95
    $element['#theme_wrappers'] = array('webform_element');
96
    $element['#wrapper_attributes']['class'] = array();
97
    $element['#wrapper_attributes']['style'] = array('display: none');
98
  }
99

    
100
  return $element;
101
}
102

    
103
/**
104
 * Implements _webform_display_component().
105
 */
106
function _webform_display_hidden($component, $value, $format = 'html', $submission = array()) {
107
  $element = array(
108
    '#title' => $component['name'],
109
    '#markup' => isset($value[0]) ? $value[0] : NULL,
110
    '#weight' => $component['weight'],
111
    '#format' => $format,
112
    '#theme' => 'webform_display_hidden',
113
    '#theme_wrappers' => $format == 'text' ? array('webform_element_text') : array('webform_element'),
114
    '#translatable' => array('title'),
115
  );
116

    
117
  return $element;
118
}
119

    
120
/**
121
 * Theme callback.
122
 */
123
function theme_webform_display_hidden($variables) {
124
  $element = $variables['element'];
125

    
126
  return $element['#format'] == 'html' ? check_plain($element['#markup']) : $element['#markup'];
127
}
128

    
129
/**
130
 * Implements _webform_analysis_component().
131
 */
132
function _webform_analysis_hidden($component, $sids = array(), $single = FALSE, $join = NULL) {
133
  $query = db_select('webform_submitted_data', 'wsd', array('fetch' => PDO::FETCH_ASSOC))
134
    ->fields('wsd', array('no', 'data'))
135
    ->condition('wsd.nid', $component['nid'])
136
    ->condition('wsd.cid', $component['cid']);
137

    
138
  if (count($sids)) {
139
    $query->condition('wsd.sid', $sids, 'IN');
140
  }
141

    
142
  if ($join) {
143
    $query->innerJoin($join, 'ws2_', 'wsd.sid = ws2_.sid');
144
  }
145

    
146
  $nonblanks = 0;
147
  $submissions = 0;
148
  $wordcount = 0;
149

    
150
  $result = $query->execute();
151
  foreach ($result as $data) {
152
    if (strlen(trim($data['data'])) > 0) {
153
      $nonblanks++;
154
      $wordcount += str_word_count(trim($data['data']));
155
    }
156
    $submissions++;
157
  }
158

    
159
  $rows[0] = array(t('Empty'), ($submissions - $nonblanks));
160
  $rows[1] = array(t('Non-empty'), $nonblanks);
161
  $other[0] = array(t('Average submission length in words (ex blanks)'), ($nonblanks != 0 ? number_format($wordcount / $nonblanks, 2) : '0'));
162

    
163
  return array(
164
    'table_rows' => $rows,
165
    'other_data' => $other,
166
  );
167
}
168

    
169
/**
170
 * Implements _webform_csv_data_component().
171
 */
172
function _webform_table_hidden($component, $value) {
173
  return check_plain(empty($value[0]) ? '' : $value[0]);
174
}
175

    
176
/**
177
 * Implements _webform_action_set_component().
178
 */
179
function _webform_action_set_hidden($component, &$element, &$form_state, $value) {
180
  $element['#value'] = $value;
181
  form_set_value($element, $value, $form_state);
182
}
183

    
184
/**
185
 * Implements _webform_csv_headers_component().
186
 */
187
function _webform_csv_headers_hidden($component, $export_options) {
188
  $header = array();
189
  $header[0] = '';
190
  $header[1] = '';
191
  $header[2] = $export_options['header_keys'] ? $component['form_key'] : $component['name'];
192
  return $header;
193
}
194

    
195
/**
196
 * Implements _webform_csv_data_component().
197
 */
198
function _webform_csv_data_hidden($component, $export_options, $value) {
199
  return isset($value[0]) ? $value[0] : '';
200
}