Projet

Général

Profil

Paste
Télécharger (8,3 ko) Statistiques
| Branche: | Révision:

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

1 85ad3d82 Assos Assos
<?php
2
/**
3
 * @file
4
 * Variable API module. Extended API.
5
 */
6
7
/**
8
 * Build generic variable information
9
 */
10
function variable_build_info($type, $options) {
11
  variable_module_include();
12
  switch ($type) {
13
    case 'variable':
14
      return variable_build_list_info($options);
15
    case 'group':
16
    case 'type':
17
      $info = variable_invoke_all('variable_' . $type . '_info');
18
      drupal_alter('variable_' . $type . '_info', $info);
19
      return $info;
20
  }
21
}
22
23
/**
24
 * Build variable information
25
 */
26
function variable_build_list_info($options) {
27
  $variables = array();
28
  foreach (module_implements('variable_info') as $module) {
29
    $result = call_user_func($module . '_variable_info', $options);
30
    if (isset($result) && is_array($result)) {
31
      foreach ($result as $name => $variable) {
32
        // Support name => title declarations
33
        $variable = is_array($variable) ? $variable : array('title' => $variable);
34
        $variable += array('name' => $name, 'module' => $module);
35
        // Check variable name for multiple values
36
        $multiple = NULL;
37
        if (preg_match('/\[(\w+)\]/', $name, $matches)) {
38
          $multiple = $matches[1];
39
          $variable += array('type' => 'multiple');
40
        }
41
        $variable += array('group' => 'default', 'type' => 'default');
42
        $variables[$name] = $variable + variable_get_type($variable['type']);
43
        // Add this at the end so it doesn't override type properties
44
        if (!empty($multiple)) {
45
          $variables[$name] += array('multiple' => $multiple);
46
        }
47
      }
48
    }
49
  }
50
  // Last chance for modules to alter variable info.
51
  drupal_alter('variable_info', $variables, $options);
52
  return $variables;
53
}
54
55
/**
56
 * Build multiple variables
57
 */
58
function variable_build_multiple($variable, $options) {
59
  // Invoke variable callbacks
60
  if (!empty($variable['multiple callback'])) {
61
    $variable['multiple'] = variable_callback($variable['multiple callback'], $variable, $options);
62
  }
63
  if (isset($variable['multiple'])) {
64
    if (!is_array($variable['multiple'])) {
65
      $variable['multiple'] = variable_option_list($variable['multiple'], $variable, $options);
66
    }
67
    $variable += array('children' => array(), 'repeat' => array());
68
    // Build children variables with the name => value array
69
    foreach ($variable['multiple'] as $key => $title) {
70
      $name = preg_replace('/\[\w+\]/', $key, $variable['name']);
71
      // Be careful to respect previously set properties, add but do not override.
72
      $child = isset($variable['children'][$name]) ? $variable['children'][$name] : array();
73
      $child += $variable['repeat'];
74
      $child += array(
75
        'name' => $name, 'index' => $key, 'title' => $title,
76
        'type' => 'default', 'parent' => $variable['name'], 'module' => $variable['module'],
77
      );
78
      // Set default value from parent
79
      if (isset($variable['default']) && is_array($variable['default']) && isset($variable['default'][$key])) {
80
        $child += array('default' => $variable['default'][$key]);
81
      }
82
      $child += variable_get_type($child['type']);
83
      $variable['children'][$name] = $child;
84
    }
85
  }
86
  return $variable;
87
}
88
89
/**
90
 * Build variable with options
91
 */
92
function variable_build_options($variable, $options) {
93
  $variable = variable_build_variable($variable, $options);
94
  if (isset($variable['options callback'])) {
95
    $variable['options'] = variable_callback($variable['options callback'], $variable, $options);
96
  }
97
  if (!empty($variable['options']) && !is_array($variable['options'])) {
98
    $variable['options'] = variable_option_list($variable['options'], $variable, $options);
99
  }
100
  return $variable;
101
}
102
103
/**
104
 * Build single variable
105
 *
106
 * Some variables may spawn into multiple ones
107
 */
108
function variable_build_variable($variable, $options = array()) {
109
  if (empty($variable['built'])) {
110
    variable_include($variable);
111
    // Mark as built so we don't build it again
112
    $variable['built'] = TRUE;
113
    $options = _variable_options($options);
114
    $variable = _variable_variable($variable, $options);
115
    // If the variable has a build callback, go for it
116
    if (isset($variable['build callback'])) {
117
      $variable = variable_callback($variable['build callback'], $variable, $options);
118
    }
119
  }
120
  return $variable;
121
}
122
123
/**
124
 * Invoke variable callback
125
 *
126
 * @param $callback
127
 *   Function name to invoke or array with module and funcion in this order
128
 * @param $variable
129
 *   Array of variable information.
130
 * @param $options
131
 *   Options to pass to the callback
132
 * @param $module
133
 *   Optional module to include its '*.variable.inc' file if the function not found
134
 */
135
function variable_callback($callback, $variable, $options = array()) {
136
  if (is_array($callback)) {
137
    list($module, $function) = $callback;
138
  }
139
  else {
140
    $function = $callback;
141
  }
142
  if (!function_exists($function)) {
143
    if (isset($module)) {
144
      variable_module_include($module);
145
    }
146
    else {
147
      variable_include($variable);
148
    }
149
  }
150
  return call_user_func($function, $variable, $options);
151
}
152
153
/**
154
 * List variables for a group
155
 */
156
function variable_list_group($group) {
157
  $list = array();
158
  foreach (variable_get_info() as $name => $variable) {
159
    if ($variable['group'] == $group) {
160
      $list[$name] = $variable;
161
    }
162
  }
163
  return $list;
164
}
165
166
/**
167
 * List variables for a module
168
 */
169
function variable_list_module($module) {
170
  $list = array();
171
  foreach (variable_get_info() as $name => $variable) {
172
    if ($variable['module'] == $module) {
173
      $list[$name] = $variable;
174
    }
175
  }
176
  return $list;
177
}
178
179
/**
180
 * Fetch options for variable
181
 */
182
function variable_option_list($type, $variable, $options) {
183
  $cache = &drupal_static(__FUNCTION__);
184
  if (isset($cache[$type])) {
185
    return $cache[$type];
186
  }
187
  elseif ($info = variable_get_type($type)) {
188
    if (isset($info['options callback'])) {
189
      $info['options'] = variable_callback(array($info['module'], $info['options callback']), $variable, $options);
190
    }
191
    if (!empty($info['cache'])) {
192
      $cache[$type] = $info['options'];
193
    }
194
    return $info['options'];
195
  }
196
  else {
197
    return array();
198
  }
199
}
200
201
/**
202
 * General function to include variable definitions for all modules
203
 */
204
function variable_module_include($modules = NULL) {
205
  static $core_modules = array('locale', 'forum', 'menu', 'node', 'system', 'taxonomy', 'translation', 'user');
206
  static $included = array();
207
  $modules = $modules ? (is_array($modules) ? $modules : array($modules)) : $core_modules;
208
  foreach ($modules as $module) {
209
    if (!isset($included[$module])) {
210
      if (module_exists($module)) {
211
        if (in_array($module, $core_modules)) {
212
          $included[$module] = module_load_include('variable.inc', 'variable', 'includes/' . $module);
213
        }
214
        else {
215
          $included[$module] = module_load_include('variable.inc', $module);
216
        }
217
      }
218
    }
219
  }
220
}
221
222
/**
223
 * Disable variables for module
224
 *
225
 * Store module variables so we can delete them if uninstalled
226
 */
227
function variable_module_disable($module) {
228
}
229
230
/**
231
 * Disable variables for module
232
 *
233
 * Store module variables so we can delete them if uninstalled
234
 */
235
function variable_module_enable($module) {
236
  if ($variables = variable_list_module($module)) {
237
    $list = variable_get('variable_module_list', array());
238
    $list[$module] = variable_children($variables);
239
    variable_set('variable_module_list', $list);
240
  }
241
}
242
243
/**
244
 * Uninstall variables for module
245
 *
246
 * This will be called from variable_modules_uninstalled(), no need to implement it directly.
247
 */
248
function variable_module_uninstall($module) {
249
  $list = variable_get('variable_module_list', array());
250
  if (isset($list[$module])) {
251
    // This is a plain list of variables so we can use raw delete.
252
    array_map('variable_delete', $list[$module]);
253
    unset($list[$module]);
254
    variable_set('variable_module_list', $list);
255
  }
256
}
257
258
/**
259
 * Get value for multiple variable
260
 */
261
function variable_multiple_get_value($variable, $options = array()) {
262
  $variable = variable_build($variable, $options);
263
  $values = array();
264
  foreach ($variable['children'] as $child) {
265
    $values[$child['index']] = variable_get_value($child, $options);
266
  }
267
  return $values;
268
}
269
270
/**
271
 * Get defaults for multiple variable
272
 */
273
function variable_multiple_get_default($variable, $options = array()) {
274
  $variable = variable_build($variable, $options);
275
  $values = array();
276
  foreach ($variable['children'] as $child) {
277
    $values[$child['index']] = variable_get_default($child, $options);
278
  }
279
  return $values;
280
}