Projet

Général

Profil

Paste
Télécharger (7,68 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / webform / components / textarea.inc @ ebcc4118

1
<?php
2

    
3
/**
4
 * @file
5
 * Webform module textarea component.
6
 */
7

    
8
/**
9
 * Implements _webform_defaults_component().
10
 */
11
function _webform_defaults_textarea() {
12
  return array(
13
    'name' => '',
14
    'form_key' => NULL,
15
    'pid' => 0,
16
    'weight' => 0,
17
    'value' => '',
18
    'required' => 0,
19
    'extra' => array(
20
      'cols' => '',
21
      'rows' => '',
22
      'title_display' => 0,
23
      'resizable' => 1,
24
      'disabled' => 0,
25
      'description' => '',
26
      'description_above' => FALSE,
27
      'placeholder' => '',
28
      'attributes' => array(),
29
      'private' => FALSE,
30
      'analysis' => FALSE,
31
    ),
32
  );
33
}
34

    
35

    
36
/**
37
 * Implements _webform_theme_component().
38
 */
39
function _webform_theme_textarea() {
40
  return array(
41
    'webform_display_textarea' => array(
42
      'render element' => 'element',
43
      'file' => 'components/textarea.inc',
44
    ),
45
  );
46
}
47

    
48
/**
49
 * Implements _webform_edit_component().
50
 */
51
function _webform_edit_textarea($component) {
52
  $form = array();
53
  $form['value'] = array(
54
    '#type' => 'textarea',
55
    '#title' => t('Default value'),
56
    '#default_value' => $component['value'],
57
    '#description' => t('The default value of the field.') . ' ' . theme('webform_token_help'),
58
    '#cols' => 60,
59
    '#rows' => 5,
60
    '#weight' => 0,
61
  );
62
  $form['display']['cols'] = array(
63
    '#type' => 'textfield',
64
    '#title' => t('Width'),
65
    '#default_value' => $component['extra']['cols'],
66
    '#description' => t('Width of the textarea in columns. This property might not have a visual impact depending on the CSS of your site.') . ' ' . t('Leaving blank will use the default size.'),
67
    '#size' => 5,
68
    '#maxlength' => 10,
69
    '#parents' => array('extra', 'cols'),
70
  );
71
  $form['display']['rows'] = array(
72
    '#type' => 'textfield',
73
    '#title' => t('Height'),
74
    '#default_value' => $component['extra']['rows'],
75
    '#description' => t('Height of the textarea in rows.') . ' ' . t('Leaving blank will use the default size.'),
76
    '#size' => 5,
77
    '#maxlength' => 10,
78
    '#parents' => array('extra', 'rows'),
79
  );
80
  $form['display']['resizable'] = array(
81
    '#type' => 'checkbox',
82
    '#title' => t('Resizable'),
83
    '#description' => t('Make this field resizable by the user.'),
84
    '#weight' => 2,
85
    '#default_value' => $component['extra']['resizable'],
86
    '#parents' => array('extra', 'resizable'),
87
  );
88
  $form['display']['placeholder'] = array(
89
    '#type' => 'textfield',
90
    '#title' => t('Placeholder'),
91
    '#default_value' => $component['extra']['placeholder'],
92
    '#description' => t('The placeholder will be shown in the field until the user starts entering a value.'),
93
    '#parents' => array('extra', 'placeholder'),
94
  );
95
  $form['display']['disabled'] = array(
96
    '#type' => 'checkbox',
97
    '#title' => t('Disabled'),
98
    '#return_value' => 1,
99
    '#description' => t('Make this field non-editable. Useful for setting an unchangeable default value.'),
100
    '#weight' => 11,
101
    '#default_value' => $component['extra']['disabled'],
102
    '#parents' => array('extra', 'disabled'),
103
  );
104
  return $form;
105
}
106

    
107
/**
108
 * Implements _webform_render_component().
109
 */
110
function _webform_render_textarea($component, $value = NULL, $filter = TRUE, $submission = NULL) {
111
  $node = isset($component['nid']) ? node_load($component['nid']) : NULL;
112

    
113
  $element = array(
114
    '#type' => 'textarea',
115
    '#title' => $filter ? webform_filter_xss($component['name']) : $component['name'],
116
    '#title_display' => $component['extra']['title_display'] ? $component['extra']['title_display'] : 'before',
117
    '#default_value' => $filter ? webform_replace_tokens($component['value'], $node) : $component['value'],
118
    '#required' => $component['required'],
119
    '#weight' => $component['weight'],
120
    '#description' => $filter ? webform_filter_descriptions($component['extra']['description'], $node) : $component['extra']['description'],
121
    '#rows' => !empty($component['extra']['rows']) ? $component['extra']['rows'] : 5,
122
    '#cols' => !empty($component['extra']['cols']) ? $component['extra']['cols'] : 60,
123
    '#attributes' => $component['extra']['attributes'],
124
    '#resizable' => (bool) $component['extra']['resizable'], // MUST be FALSE to disable.
125
    '#theme_wrappers' => array('webform_element'),
126
    '#translatable' => array('title', 'description'),
127
  );
128

    
129
  if ($component['required']) {
130
    $element['#attributes']['required'] = 'required';
131
  }
132

    
133
  if ($component['extra']['placeholder']) {
134
    $element['#attributes']['placeholder'] = $component['extra']['placeholder'];
135
  }
136

    
137
  if ($component['extra']['disabled']) {
138
    if ($filter) {
139
      $element['#attributes']['readonly'] = 'readonly';
140
    }
141
    else {
142
      $element['#disabled'] = TRUE;
143
    }
144
  }
145

    
146
  if (isset($value[0])) {
147
    $element['#default_value'] = $value[0];
148
  }
149

    
150
  return $element;
151
}
152

    
153
/**
154
 * Implements _webform_display_component().
155
 */
156
function _webform_display_textarea($component, $value, $format = 'html', $submission = array()) {
157
  return array(
158
    '#title' => $component['name'],
159
    '#title_display' => $component['extra']['title_display'] ? $component['extra']['title_display'] : 'before',
160
    '#weight' => $component['weight'],
161
    '#theme' => 'webform_display_textarea',
162
    '#theme_wrappers' => $format == 'html' ? array('webform_element') : array('webform_element_text'),
163
    '#format' => $format,
164
    '#value' => isset($value[0]) ? $value[0] : '',
165
    '#translatable' => array('title'),
166
  );
167
}
168

    
169
/**
170
 * Format the output of data for this component.
171
 */
172
function theme_webform_display_textarea($variables) {
173
  $element = $variables['element'];
174
  $output = $element['#format'] == 'html' ? nl2br(check_plain($element['#value'])) : $element['#value'];
175
  if (drupal_strlen($output) > 80) {
176
    $output = ($element['#format'] == 'html') ? '<div class="webform-long-answer">' . $output . '</div>' : $output;
177
  }
178
  return $output !== '' ? $output : ' ';
179
}
180

    
181
/**
182
 * Implements _webform_analysis_component().
183
 */
184
function _webform_analysis_textarea($component, $sids = array(), $single = FALSE, $join = NULL) {
185
  $query = db_select('webform_submitted_data', 'wsd', array('fetch' => PDO::FETCH_ASSOC))
186
    ->fields('wsd', array('no', 'data'))
187
    ->condition('wsd.nid', $component['nid'])
188
    ->condition('wsd.cid', $component['cid']);
189

    
190
  if (count($sids)) {
191
    $query->condition('wsd.sid', $sids, 'IN');
192
  }
193

    
194
  if ($join) {
195
    $query->innerJoin($join, 'ws2_', 'wsd.sid = ws2_.sid');
196
  }
197

    
198
  $nonblanks = 0;
199
  $submissions = 0;
200
  $wordcount = 0;
201

    
202
  $result = $query->execute();
203
  foreach ($result as $data) {
204
    if (drupal_strlen(trim($data['data'])) > 0) {
205
      $nonblanks++;
206
      $wordcount += str_word_count(trim($data['data']));
207
    }
208
    $submissions++;
209
  }
210

    
211
  $rows[0] = array(t('Left Blank'), ($submissions - $nonblanks));
212
  $rows[1] = array(t('User entered value'), $nonblanks);
213

    
214
  $other[] = array(t('Average submission length in words (ex blanks)'), ($nonblanks != 0 ? number_format($wordcount/$nonblanks, 2) : '0'));
215

    
216
  return array(
217
    'table_rows' => $rows,
218
    'other_data' => $other,
219
  );
220
}
221

    
222
/**
223
 * Implements _webform_table_component().
224
 */
225
function _webform_table_textarea($component, $value) {
226
  return empty($value[0]) ? '' : check_plain($value[0]);
227
}
228

    
229
/**
230
 * Implements _webform_action_set_component().
231
 */
232
function _webform_action_set_textarea($component, &$element, &$form_state, $value) {
233
  $element['#value'] = $value;
234
  form_set_value($element, $value, $form_state);
235
}
236

    
237
/**
238
 * Implements _webform_csv_headers_component().
239
 */
240
function _webform_csv_headers_textarea($component, $export_options) {
241
  $header = array();
242
  $header[0] = '';
243
  $header[1] = '';
244
  $header[2] = $export_options['header_keys'] ? $component['form_key'] : $component['name'];
245
  return $header;
246
}
247

    
248
/**
249
 * Implements _webform_csv_data_component().
250
 */
251
function _webform_csv_data_textarea($component, $export_options, $value) {
252
  return empty($value[0]) ? '' : $value[0];
253
}