Projet

Général

Profil

Paste
Télécharger (11,2 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / variable / variable.variable.inc @ ba09eb79

1 85ad3d82 Assos Assos
<?php
2
/**
3
 * @file
4
 * Variable module hook implementations
5
 */
6
7
/**
8
 * Implements hook_variable_group_info().
9
 */
10
function variable_variable_group_info() {
11
  // Group for variable that have no group
12
  $groups['default'] = array(
13
    'title' => t('Other'),
14
    'description' => t("Variables that don't belong to any other group."),
15
  );
16
  $groups['debug'] = array(
17
    'title' => t('Debug'),
18
    'description' => t('Debug and development options.'),
19
  );
20
  $groups['variable'] = array(
21
    'title' => t('Variable'),
22
    'description' => t('Variables that contain metadata about the variable system.'),
23
  );
24
  return $groups;
25
}
26
27
/**
28
 * Implements hook_variable_type_info().
29
 */
30
function variable_variable_type_info() {
31
  // Array of values
32
  $type['array'] = array(
33
    'title' => t('Array'),
34
    'element' => array('#type' => 'fieldset', '#tree' => TRUE),
35
    // Properties for each array item
36
    'repeat' => array(
37
      'element' => array('#type' => 'textfield'),
38
    ),
39
    'format callback' => 'variable_format_array',
40
    'element callback' => 'variable_form_element_array',
41
    'default' => array(),
42
  );
43
  // Array whose keys are named properties.
44
  $type['properties'] = array(
45
    'title' => t('Properties'),
46
    'format callback' => 'variable_format_properties',
47
    'type' => 'array',
48
  );
49
  // TRUE / FALSE value, checkbox
50
  $type['boolean'] = array(
51
    'title' => t('Boolean'),
52
    'element' => array('#type' => 'checkbox'),
53
    'format callback' => 'variable_format_boolean',
54
  );
55
  // Default type for variables with no other type
56
  $type['default'] = array(
57
    'title' => t('Default'),
58
    'element' => array('#type' => 'textfield'),
59
    'access' => 'administer site configuration',
60
  );
61
  // Enable/Disable
62
  $type['enable'] = array(
63
    'title' => t('Enable'),
64
    'options' => array(t('Disabled'), t('Enabled')),
65
    'default' => 0,
66
    'element' => array('#type' => 'radios'),
67
    'format callback' => 'variable_format_selection',
68
  );
69
  // Multiple variable that will spawn into multiple elements
70
  $type['multiple'] = array(
71
    'title' => t('Multiple'),
72
    'element' => array('#type' => 'fieldset'),
73
    'build callback' => 'variable_build_multiple',
74
    'format callback' => 'variable_format_multiple',
75
    'element callback' => 'variable_form_element_multiple',
76
    'value callback' => 'variable_multiple_get_value',
77
    'default callback' => 'variable_multiple_get_default',
78
  );
79
  $type['mail_address'] = array(
80
    'title' => t('E-mail address'),
81
    'element' => array('#type' => 'textfield'),
82
    'token' => TRUE,
83
  );
84
  $type['mail_text'] = array(
85
    'title' => t('Mail text'),
86
    'multiple' => array('subject' => t('Subject'), 'body' => t('Body')),
87
    'build callback' => 'variable_build_mail_text',
88
    'localize' => TRUE,
89
    'type' => 'multiple',
90
  );
91
  $type['number'] = array(
92
    'title' => t('Number'),
93
    'element' => array('#type' => 'textfield', '#size' => 15, '#maxlength' => 10),
94
    'token' => TRUE,
95
    'validate callback' => 'variable_validate_number',
96
    'format callback' => 'variable_format_number',
97
  );
98
  // Select multiple options from multiple choices
99
  $type['options'] = array(
100
    'title' => t('Options'),
101
    'options' => TRUE,
102
    'element' => array('#type' => 'checkboxes'),
103
    'element callback' => 'variable_form_element_options',
104
    'format callback' => 'variable_format_options',
105
  );
106
  // Select single option from multiple choices
107
  $type['select'] = array(
108
    'title' => t('Select'),
109
    'options' => TRUE,
110
    // This will become radios or drop-down depending on the number of options
111
    'element callback' => 'variable_form_element_options',
112
    'format callback' => 'variable_format_selection',
113
  );
114
  // Select number from array of values. Options array that can be list of numbers will be converted to a value => value
115
  $type['select_number'] = array(
116
    'title' => t('Select'),
117
    'options' => TRUE,
118
    'element callback' => 'variable_form_element_options',
119
    'options callback' => 'variable_options_select_number',
120
  );
121
  $type['string'] = array(
122
    'title' => t('String'),
123
    'element' => array('#type' => 'textfield'),
124
    'localize' => TRUE,
125
    'format callback' => 'variable_format_string',
126
    'token' => TRUE,
127
    // This type may have an 'allowed tags' attribute.
128
    // If empty it will be formatted as plain text
129
    'allowed tags' => array(),
130
  );
131
  $type['text'] = array(
132
    'title' => t('Text'),
133
    'element' => array('#type' => 'textarea'),
134
    'localize' => TRUE,
135
    'format callback' => 'variable_format_text',
136
    'token' => TRUE,
137
    // This type may have an 'allowed tags' attribute.
138
    // If empty it will be formatted with filter_xss_admin.
139
    'allowed tags' => array(),
140
  );
141
  // Default type for variables with no other type
142
  $type['unknown'] = array(
143
    'title' => t('Unknown'),
144
    'access' => 'administer site configuration',
145
    'format' => 'variable_format_unknown',
146
    'element callback' => 'variable_form_element_unknown',
147
    'element' => array('#type' => 'item'),
148
  );
149
  $type['url'] = array(
150
    'title' => t('URL'),
151
    'element' => array('#type' => 'textfield', '#size' => 80, '#maxlength' => 255),
152
    'token' => TRUE,
153
  );
154
  $type['mail_part'] = array(
155
    'title' => t('Mail parts'),
156
    'options' => array('subject' => t('Subject'), 'body' => t('Body')),
157
  );
158
  $type['text_format'] = array(
159
    'title' => t('Formatted text'),
160
    'element' => array('#type' => 'text_format'),
161
    'element callback' => 'variable_form_element_text_format',
162
    'format callback' => 'variable_format_text_format',
163
    'default callback' => 'variable_text_format_default',
164
    'localize' => TRUE,
165
  );
166
  return $type;
167
}
168
169
/**
170
 * Build multiple mail variable
171
 */
172
function variable_build_mail_text($variable, $options = array()) {
173
  $name = str_replace('[mail_part]', '', $variable['name']);
174
  // For mail text, children have different types
175
  $variable['children'][$name . 'subject']['type'] = 'string';
176
  $variable['children'][$name . 'body']['type'] = 'text';
177
  $variable = variable_build_multiple($variable, $options);
178
  return $variable;
179
}
180
181
/**
182
 * Format select variable
183
 */
184
function variable_format_selection($variable, $options = array()) {
185
  $variable = variable_build_options($variable, $options);
186
  if (isset($variable['value'])) {
187
    return isset($variable['options'][$variable['value']]) ? $variable['options'][$variable['value']] : '<' . t('Invalid option') . '>';
188
  }
189
  else {
190
    return variable_format_empty($variable);
191
  }
192
}
193
194
/**
195
 * Format options variable. Value is an array of options.
196
 */
197
function variable_format_options($variable, $options = array()) {
198
  $variable = variable_build_options($variable, $options);
199
  $names = array();
200
  if (isset($variable['value']) && $variable['value']) {
201
    if (is_array($variable['value'])) {
202
      foreach ($variable['value'] as $index => $value) {
203
         $names[$index] = isset($variable['options'][$value]) ? $variable['options'][$value] : '<' . t('Invalid option') . '>';
204
      }
205
      return implode(', ', $names);
206
    }
207
    else {
208
      return '<' . t('Invalid value') . '>';
209
    }
210
  }
211
  else {
212
    return variable_format_empty($variable);
213
  }
214
}
215
216
/**
217
 * Format array variable, handling nested arrays
218
 */
219
function variable_format_array($variable = NULL, $options = array()) {
220
  if (empty($variable['value'])) {
221
    return variable_format_empty($variable);
222
  }
223
  else {
224
    $list = array();
225
    foreach ($variable['value'] as $index => $item) {
226
      if (is_array($item) || is_object($item)) {
227
        $list[$index] = variable_format_array(array('value' => (array)$item), $options);
228
      }
229
      else {
230
        $list[$index] = check_plain((string)$item);
231
      }
232
    }
233
    return theme('item_list', array('items' => $list));
234
  }
235
}
236
/**
237
 * Format array variable with known keys, handling nested arrays
238
 */
239
function variable_format_properties($variable = NULL, $options = array()) {
240
  if (empty($variable['value'])) {
241
    return variable_format_empty($variable);
242
  }
243
  else {
244
    $rows = array();
245
    foreach ($variable['value'] as $name => $item) {
246
      $title = check_plain((string)$name);
247
      if (is_array($item) || is_object($item)) {
248
        $value = variable_format_array(array('value' => (array)$item), $options);
249
      }
250
      else {
251
        $value = check_plain((string)$item);
252
      }
253
      $rows[] = array('<em>' . $title . '</em>', $value);
254
    }
255
    return theme('table', array('rows' => $rows));
256
  }
257
}
258
259
/**
260
 * Format boolean variable
261
 */
262
function variable_format_boolean($variable, $options = array()) {
263
  if (isset($variable['value'])) {
264
    return $variable['value'] ? t('True') : t('False');
265
  }
266
  else {
267
    return t('Undefined');
268
  }
269
}
270
271
/**
272
 * Format variable empty value
273
 */
274
function variable_format_empty($variable) {
275
  return isset($variable['empty']) ? $variable['empty'] : t('Empty');
276
}
277
278
/**
279
 * Format variable as number.
280
 */
281
function variable_format_number($variable, $options = array()) {
282
  if (is_numeric($variable['value'])) {
283
    return (string)$variable['value'];
284
  }
285
  elseif (empty($variable['value'])) {
286
    return '';
287
  }
288
  else {
289
    return check_plain($variable['value']);
290
  }
291
}
292
293
/**
294
 * Format variable as string. Either check plain for filter_xss.
295
 */
296
function variable_format_string($variable, $options = array()) {
297
  if (empty($variable['value'])) {
298
    return '';
299
  }
300
  elseif (!empty($variable['allowed tags'])) {
301
    return filter_xss($variable['value'], $variable['allowed tags']);
302
  }
303
  else {
304
    return check_plain($variable['value']);
305
  }
306
}
307
308
/**
309
 * Format text variable
310
 */
311
function variable_format_text($variable, $options = array()) {
312
  if (empty($variable['value'])) {
313
    return '';
314
  }
315
  elseif (!empty($variable['allowed tags'])) {
316
    return filter_xss($variable['value'], $variable['allowed tags']);
317
  }
318
  else {
319
    return filter_xss_admin($variable['value']);
320
  }
321
}
322
323
/**
324
 * Options callback for numeric select
325
 */
326
function variable_options_select_number($variable, $options = array()) {
327
  return drupal_map_assoc($variable['options']);
328
}
329
330
/**
331
 * Default callback for text_format.
332
 */
333
function variable_text_format_default($variable, $options = array()) {
334
  $out = array(
335
    'value' => '',
336
    'format' => filter_default_format(),
337
  );
338
  if (!empty($variable['default'])) {
339
    if (is_string($variable['default'])) {
340
      $out['value'] = $variable['default'];
341
    }
342
    elseif (is_array($variable['default'])) {
343
      if (isset($variable['default']['value'])) {
344
        $out['value'] = $variable['default']['value'];
345
      }
346
      if (isset($variable['default']['format'])) {
347
        $out['format'] = $variable['default']['format'];
348
      }
349
    }
350
  }
351
  return $out;
352
}
353
354
/**
355
 * Format text_format.
356
 */
357
function variable_format_text_format($variable, $options = array()) {
358
  return check_markup($variable['value']['value'], $variable['value']['format']);
359
}
360
361
/**
362
 * Format multiple variable.
363
 */
364
function variable_format_multiple($variable, $options = array()) {
365
  $rows = array();
366
  foreach ($variable['children'] as $name => $child) {
367
    $rows[$name] = array(
368
      array('data' => check_plain($child['title']), 'header' => TRUE),
369
      variable_format_value($child)
370
    );
371
  }
372
  return theme('table', array('rows' => $rows));
373
}
374
375
/**
376
 * Validate numeric variable.
377
 */
378
function variable_validate_number($variable) {
379 6331c987 Assos Assos
  if (empty($variable['required']) && empty($variable['value'])) {
380
    return;
381
  }
382
  elseif (!is_numeric($variable['value'])) {
383 85ad3d82 Assos Assos
    return t('The value is not a number.');
384
  }
385
}