Projet

Général

Profil

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

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

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
      'placeholder' => '',
27
      'attributes' => array(),
28
      'private' => FALSE,
29
      'analysis' => FALSE,
30
    ),
31
  );
32
}
33

    
34

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

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

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

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

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

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

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

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

    
149
  return $element;
150
}
151

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

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

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

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

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

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

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

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

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

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

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

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

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

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