1
|
<?php
|
2
|
/**
|
3
|
* @file
|
4
|
* Administrative forms for variable realms.
|
5
|
*/
|
6
|
|
7
|
/**
|
8
|
* Select variables for realm.
|
9
|
*/
|
10
|
function variable_realm_select_variables_form($form, &$form_state, $realm_name) {
|
11
|
$controller = variable_realm_controller($realm_name);
|
12
|
$current = $controller->getEnabledVariables();
|
13
|
$optional = $controller->getAvailableVariables();
|
14
|
// The final list will be the sum of both lists. We may have unknown variables
|
15
|
// we want to preserve.
|
16
|
$list = array_unique(array_merge($optional, $current));
|
17
|
$form['realm_name'] = array('#type' => 'value', '#value' => $realm_name);
|
18
|
$form['message']['select']['#markup'] = '<h3>' . t('Select variables to be set for this realm.') . '</h3>';
|
19
|
if ($current) {
|
20
|
$form['message']['current']['#markup'] = '<p>' . t('Currently selected variables are: <em>!variables</em>', array('!variables' => variable_list_name($current))) . '</p>';
|
21
|
}
|
22
|
$form['variables'] = array(
|
23
|
'#type' => 'vertical_tabs',
|
24
|
'#tree' => TRUE,
|
25
|
);
|
26
|
$variable_groups = variable_group_variables($list);
|
27
|
foreach ($variable_groups as $group => $group_list) {
|
28
|
$group_info = variable_get_group($group);
|
29
|
$group_current = array_intersect($group_list, $current);
|
30
|
$form['variables'][$group] = array(
|
31
|
'#type' => 'fieldset',
|
32
|
'#title' => $group_info['title'],
|
33
|
'#theme' => 'variable_table_select',
|
34
|
'#collapsible' => TRUE, '#collapsed' => TRUE,
|
35
|
);
|
36
|
foreach ($group_list as $name) {
|
37
|
// Variable names may clash with form element names, so we need to replace '[' and ']'
|
38
|
$safename = str_replace(array('[', ']'), array('<', '>'), $name);
|
39
|
$form['variables'][$group][$safename] = array(
|
40
|
'#type' => 'checkbox',
|
41
|
'#default_value' => in_array($name, $group_current),
|
42
|
'#variable_name' => $name,
|
43
|
'#parents' => array('variables', $safename),
|
44
|
);
|
45
|
}
|
46
|
}
|
47
|
$form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save configuration'));
|
48
|
return $form;
|
49
|
}
|
50
|
|
51
|
/**
|
52
|
* Select variables for realm.
|
53
|
*/
|
54
|
function variable_realm_select_variables_form_submit($form, &$form_state) {
|
55
|
// Get realm name and current list of variables.
|
56
|
$realm_name = $form_state['values']['realm_name'];
|
57
|
$controller = variable_realm_controller($realm_name);
|
58
|
$old_variables = $controller->getEnabledVariables();
|
59
|
// Get main variable names
|
60
|
$variables = $form_state['values']['variables'];
|
61
|
unset($variables['variables__active_tab']);
|
62
|
$variables = array_keys(array_filter($variables));
|
63
|
// Translate variable names
|
64
|
foreach ($variables as $index => $name) {
|
65
|
$variables[$index] = str_replace(array('<', '>'), array('[', ']'), $name);
|
66
|
}
|
67
|
// Hook for modules to alter this variable list.
|
68
|
drupal_alter('variable_realm_variable_list', $variables, $realm_name);
|
69
|
// And save the list to a variable.
|
70
|
$controller->setRealmVariable('list', $variables);
|
71
|
// Spawn multiple variables and translate into actual variables
|
72
|
$new_list = variable_children($variables);
|
73
|
// Delete variables from realm that are not in the new list.
|
74
|
$old_list = variable_children($old_variables);
|
75
|
foreach (array_diff($old_list, $new_list) as $name) {
|
76
|
$controller->deleteVariable($name);
|
77
|
drupal_set_message(t('Deleted existing values of %name from realm variables.', array('%name' => $name)));
|
78
|
}
|
79
|
}
|
80
|
|
81
|
/**
|
82
|
* Edit variables for realm.
|
83
|
*/
|
84
|
function variable_realm_edit_variables_form($form, &$form_state, $realm_name, $realm_key) {
|
85
|
$controller = variable_realm_controller($realm_name);
|
86
|
$form['realm_name'] = array('#type' => 'value', '#value' => $realm_name);
|
87
|
$form['realm_key'] = array('#type' => 'value', '#value' => $realm_key);
|
88
|
$options['realm'] = variable_realm($realm_name, $realm_key);
|
89
|
|
90
|
if ($variable_list = $controller->getEnabledVariables()) {
|
91
|
$form = variable_base_form($form, $form_state, $variable_list, $options);
|
92
|
|
93
|
// Group variables by variable group for vertical tabls
|
94
|
$group_list = array();
|
95
|
foreach ($variable_list as $variable_name) {
|
96
|
$variable_info = variable_get_info($variable_name, $options);
|
97
|
$group = $variable_info['group'];
|
98
|
$group_list[$group][] = $variable_name;
|
99
|
}
|
100
|
$form['variables'] = array(
|
101
|
'#type' => 'vertical_tabs',
|
102
|
'#tree' => TRUE,
|
103
|
);
|
104
|
foreach ($group_list as $group => $group_variables) {
|
105
|
$group_info = variable_get_group($group);
|
106
|
$form['variables'][$group] = array(
|
107
|
'#type' => 'fieldset',
|
108
|
'#title' => $group_info['title'],
|
109
|
'#collapsible' => TRUE, '#collapsed' => TRUE,
|
110
|
);
|
111
|
// Set form parents for this variable / group.
|
112
|
$options['form parents'] = array('variables', $group);
|
113
|
$form['variables'][$group] += variable_edit_subform($group_variables, $options);
|
114
|
}
|
115
|
$form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save configuration'));
|
116
|
}
|
117
|
else {
|
118
|
$form['message']['#markup'] = '<h3>' . t('No variables have been selected as %realm_name specific.', array('%realm_name' => $controller->getTitle())) . '</h3>';
|
119
|
}
|
120
|
if (!empty($realm_info['select path'])) {
|
121
|
$form['select']['#markup'] = t('To select variables to be configured, see <a href="!select-url">%realm_name variable settings</a>.', array(
|
122
|
'%realm_name' => $realm_info['title'],
|
123
|
'!select-url' => url($realm_info['select path'])
|
124
|
));
|
125
|
}
|
126
|
return $form;
|
127
|
}
|
128
|
|
129
|
/**
|
130
|
* Edit variables for realm.
|
131
|
*/
|
132
|
function variable_realm_edit_variables_form_submit($form, &$form_state) {
|
133
|
$realm_name = $form_state['values']['realm_name'];
|
134
|
$realm_key = $form_state['values']['realm_key'];
|
135
|
foreach ($form_state['values']['variables'] as $group => $group_variables) {
|
136
|
if (is_array($group_variables)) {
|
137
|
foreach ($group_variables as $name => $value) {
|
138
|
$current = variable_realm_get($realm_name, $realm_key, $name);
|
139
|
if ($current !== $value) {
|
140
|
variable_realm_set($realm_name, $realm_key, $name, $value);
|
141
|
}
|
142
|
}
|
143
|
}
|
144
|
}
|
145
|
// Redirect later depending on query string parameters.
|
146
|
_variable_realm_form_submit_redirect($form, $form_state);
|
147
|
}
|
148
|
|
149
|
/**
|
150
|
* Key selector for realm forms.
|
151
|
*/
|
152
|
function variable_realm_form_key_selector($realm_name, $current_key) {
|
153
|
$element_name = VARIABLE_REALM_FORM_SWITCHER . $realm_name;
|
154
|
$query_name = 'variable_realm_' . $realm_name . '_key';
|
155
|
$controller = variable_realm_controller($realm_name);
|
156
|
$keys = $controller->getAllKeys();
|
157
|
// Don't show selector if there aren't any keys to select.
|
158
|
if (empty($keys)) {
|
159
|
return array();
|
160
|
}
|
161
|
$form[$element_name] = array(
|
162
|
'#type' => 'fieldset',
|
163
|
'#title' => t('There are %name variables in this form', array('%name' => $controller->getVariableName())),
|
164
|
'#weight' => -100,
|
165
|
'#description' => t('Check you are editing the variables for the right %realm value or select the desired %realm.', array('%realm' => $controller->getTitle())),
|
166
|
);
|
167
|
// Replace only this element on current query string, there may be others.
|
168
|
$current_query = $_GET;
|
169
|
unset($current_query['q']);
|
170
|
foreach ($keys as $realm_key => $key_name) {
|
171
|
$query[VARIABLE_REALM_QUERY_STRING . $realm_name] = $realm_key;
|
172
|
$link = l($key_name, $_GET['q'], array('query' => $query + $current_query));
|
173
|
$items[] = $current_key == $realm_key ? '<strong>' . $link . '</strong>' : $link;
|
174
|
}
|
175
|
$form[$element_name]['select_key'] = array(
|
176
|
'#type' => 'item',
|
177
|
'#markup' => implode(' | ', $items),
|
178
|
);
|
179
|
return $form;
|
180
|
}
|
181
|
|
182
|
/**
|
183
|
* Get current realm key from query string or from current realm value.
|
184
|
*/
|
185
|
function variable_realm_form_key_current($realm_name) {
|
186
|
$realm_controller = variable_realm_controller($realm_name);
|
187
|
if ($key = variable_realm_params($realm_name)) {
|
188
|
return $key;
|
189
|
}
|
190
|
elseif ($key = $realm_controller->getKey()) {
|
191
|
return $key;
|
192
|
}
|
193
|
elseif ($key = $realm_controller->getRequestKey()) {
|
194
|
return $key;
|
195
|
}
|
196
|
else {
|
197
|
return $realm_controller->getDefaultKey();
|
198
|
}
|
199
|
}
|
200
|
|
201
|
/**
|
202
|
* Alter settings form and return list of found variables.
|
203
|
*/
|
204
|
function _variable_realm_variable_settings_form_alter(&$form, $realm_name, $variables) {
|
205
|
$result = array();
|
206
|
foreach (element_children($form) as $field) {
|
207
|
if (count(element_children($form[$field])) && empty($form[$field]['#tree'])) {
|
208
|
// Rewrite fieldsets recursively.
|
209
|
$result += _variable_realm_variable_settings_form_alter($form[$field], $realm_name, $variables);
|
210
|
}
|
211
|
elseif (in_array($field, $variables)) {
|
212
|
if (isset($form[$field]['#variable_realm'])) {
|
213
|
// Oh-oh, variable already taken by another realm.
|
214
|
_variable_realm_variable_settings_form_conflict($field);
|
215
|
$form[$field]['#disabled'] = TRUE;
|
216
|
}
|
217
|
else {
|
218
|
// Mark variable as already taken by a realm
|
219
|
$form[$field]['#variable_realm'] = $realm_name;
|
220
|
}
|
221
|
_variable_realm_variable_settings_form_mark($realm_name, $form[$field]);
|
222
|
// Addd field => name to result
|
223
|
$result[$field] = !empty($form[$field]['#title']) ? $form[$field]['#title'] : $field;
|
224
|
}
|
225
|
}
|
226
|
return $result;
|
227
|
}
|
228
|
|
229
|
/**
|
230
|
* Mark variable as belonging to a realm.
|
231
|
*/
|
232
|
function _variable_realm_variable_settings_form_mark($realm_name, &$element) {
|
233
|
$realm_info = variable_realm_info($realm_name);
|
234
|
// Add form field class (i18n-variable) and description text.
|
235
|
if (!empty($realm_info['variable class'])) {
|
236
|
$element['#attributes']['class'][] = $realm_info['variable class'];
|
237
|
}
|
238
|
$element['#description'] = !empty($element['#description']) ? $element['#description'] : '';
|
239
|
$element['#description'] .= ' <strong>' . t('This is a @name variable.', array('@name' => $realm_info['variable name'])) . '</strong> ';
|
240
|
}
|
241
|
|
242
|
/**
|
243
|
* Warning about variable conflict.
|
244
|
*/
|
245
|
function _variable_realm_variable_settings_form_conflict($variable) {
|
246
|
static $warnings;
|
247
|
if (!isset($warnings[$variable])) {
|
248
|
$warnings[$variable] = TRUE;
|
249
|
drupal_set_message(t('There are conflicting realm variables in the form. The variable %name is enabled for more than one realm. Review your realm settings', array('%name' => variable_name($variable))), 'warning');
|
250
|
}
|
251
|
}
|
252
|
|
253
|
/**
|
254
|
* Save realm variables and remove them from form.
|
255
|
*/
|
256
|
function variable_realm_variable_settings_form_submit($form, &$form_state) {
|
257
|
$op = isset($form_state['values']['op']) ? $form_state['values']['op'] : '';
|
258
|
foreach ($form['#realm_keys'] as $realm_name => $realm_key) {
|
259
|
$realm_controller = variable_realm_controller($realm_name);
|
260
|
//$language = i18n_language($form_state['values']['i18n_variable_language']);
|
261
|
//unset($form_state['values']['i18n_variable_language']);
|
262
|
$variables = array_keys($form['#realm_variables'][$realm_name]);
|
263
|
foreach ($variables as $variable_name) {
|
264
|
if (isset($form_state['values'][$variable_name])) {
|
265
|
if ($op == t('Reset to defaults')) {
|
266
|
variable_realm_del($realm_name, $realm_key, $variable_name);
|
267
|
}
|
268
|
else {
|
269
|
$value = $form_state['values'][$variable_name];
|
270
|
if (is_array($value) && isset($form_state['values']['array_filter'])) {
|
271
|
$value = array_keys(array_filter($value));
|
272
|
}
|
273
|
variable_realm_set($realm_name, $realm_key, $variable_name, $value);
|
274
|
}
|
275
|
// If current is not default realm key, we don't set any global variable (without realm)
|
276
|
if ($realm_key != $realm_controller->getDefaultKey()) {
|
277
|
unset($form_state['values'][$variable_name]);
|
278
|
}
|
279
|
}
|
280
|
}
|
281
|
}
|
282
|
// Redirect later depending on query string parameters.
|
283
|
_variable_realm_form_submit_redirect($form, $form_state);
|
284
|
// The form will go now through system_settings_form_submit()
|
285
|
}
|
286
|
|
287
|
/**
|
288
|
* Process system_theme_settings form submissions.
|
289
|
*
|
290
|
* @see system_theme_settings_submit()
|
291
|
*/
|
292
|
function variable_realm_variable_theme_form_submit($form, &$form_state) {
|
293
|
// Regular theme submission without variable set.
|
294
|
$values = $form_state['values'];
|
295
|
|
296
|
// If the user uploaded a new logo or favicon, save it to a permanent location
|
297
|
// and use it in place of the default theme-provided file.
|
298
|
if ($file = $values['logo_upload']) {
|
299
|
unset($values['logo_upload']);
|
300
|
$filename = file_unmanaged_copy($file->uri);
|
301
|
$values['default_logo'] = 0;
|
302
|
$values['logo_path'] = $filename;
|
303
|
$values['toggle_logo'] = 1;
|
304
|
}
|
305
|
if ($file = $values['favicon_upload']) {
|
306
|
unset($values['favicon_upload']);
|
307
|
$filename = file_unmanaged_copy($file->uri);
|
308
|
$values['default_favicon'] = 0;
|
309
|
$values['favicon_path'] = $filename;
|
310
|
$values['toggle_favicon'] = 1;
|
311
|
}
|
312
|
|
313
|
// If the user entered a path relative to the system files directory for
|
314
|
// a logo or favicon, store a public:// URI so the theme system can handle it.
|
315
|
if (!empty($values['logo_path'])) {
|
316
|
$values['logo_path'] = _system_theme_settings_validate_path($values['logo_path']);
|
317
|
}
|
318
|
if (!empty($values['favicon_path'])) {
|
319
|
$values['favicon_path'] = _system_theme_settings_validate_path($values['favicon_path']);
|
320
|
}
|
321
|
|
322
|
if (empty($values['default_favicon']) && !empty($values['favicon_path'])) {
|
323
|
$values['favicon_mimetype'] = file_get_mimetype($values['favicon_path']);
|
324
|
}
|
325
|
$key = $values['var'];
|
326
|
|
327
|
// Exclude unnecessary elements before saving.
|
328
|
unset($values['var'], $values['submit'], $values['reset'], $values['form_id'], $values['op'], $values['form_build_id'], $values['form_token']);
|
329
|
|
330
|
// Set realm variable.
|
331
|
$variable_name = $key;
|
332
|
$realm_name = $form['#realm_theme'];
|
333
|
$realm_key = $form['#realm_keys'][$realm_name];
|
334
|
variable_realm_set($realm_name, $realm_key, $variable_name, $values);
|
335
|
|
336
|
// If current is default realm key, set global variable too.
|
337
|
$realm_controller = variable_realm_controller($realm_name);
|
338
|
if ($realm_key == $realm_controller->getDefaultKey()) {
|
339
|
variable_set($variable_name, $values);
|
340
|
}
|
341
|
// Confirmation, clear cache, taken from system_theme_settings_submit()
|
342
|
drupal_set_message(t('The configuration options have been saved.'));
|
343
|
cache_clear_all();
|
344
|
// Redirect later depending on query string parameters.
|
345
|
_variable_realm_form_submit_redirect($form, $form_state);
|
346
|
}
|
347
|
|
348
|
/**
|
349
|
* Get variable list for settings forms handling multiple realms.
|
350
|
*
|
351
|
* Variables available for more than one realm, will be kept only in the list
|
352
|
* for the realm with the higher weight.
|
353
|
*/
|
354
|
function _variable_realm_variable_settings_form_list() {
|
355
|
$list = array();
|
356
|
foreach (variable_realm_list_all() as $realm_name => $controller) {
|
357
|
if ($controller->getInfo('form settings') && ($realm_list = $controller->getEnabledVariables())) {
|
358
|
// Remove from previous realms with lower weight.
|
359
|
foreach ($list as $name => $variables) {
|
360
|
$list[$name] = array_diff($variables, $realm_list);
|
361
|
}
|
362
|
$list[$realm_name] = $realm_list;
|
363
|
}
|
364
|
}
|
365
|
return $list;
|
366
|
}
|
367
|
|
368
|
/**
|
369
|
* Redirect to current page after form submission, using query string parameters.
|
370
|
*/
|
371
|
function _variable_realm_form_submit_redirect($form, &$form_state) {
|
372
|
if ($query_params = variable_realm_params()) {
|
373
|
foreach ($query_params as $realm => $value) {
|
374
|
$query[VARIABLE_REALM_QUERY_STRING . $realm] = $value;
|
375
|
}
|
376
|
// Parameters to be passed to drupal_goto().
|
377
|
$form_state['redirect'] = array(current_path(), array('query' => $query));
|
378
|
}
|
379
|
}
|
380
|
|
381
|
/**
|
382
|
* Add realm switcher to the form.
|
383
|
*/
|
384
|
function _variable_realm_variable_settings_form_switcher(&$form) {
|
385
|
// Add switchers for current realms and current key.
|
386
|
// Add realm values and subform realm / key selector.
|
387
|
foreach (array_keys($form['#realm_variables']) as $realm_name) {
|
388
|
$current_key = variable_realm_form_key_current($realm_name);
|
389
|
$info = variable_realm_info($realm_name);
|
390
|
if (!empty($info['form switcher'])) {
|
391
|
$form += variable_realm_form_key_selector($realm_name, $current_key);
|
392
|
}
|
393
|
$form['#realm_keys'][$realm_name] = $current_key;
|
394
|
}
|
395
|
// Make sure realm switchers are added for all parent realms of current ones.
|
396
|
foreach (variable_realm_list_all() as $realm_name => $realm_controller) {
|
397
|
if (($parent_realms = $realm_controller->getParentRealms()) && !empty($form['#realm_variables'][$realm_name]) && empty($form[VARIABLE_REALM_FORM_SWITCHER . $realm_name])) {
|
398
|
// Check we have selectors for the other realms.
|
399
|
foreach ($parent_realms as $realm) {
|
400
|
$info = variable_realm_info($realm);
|
401
|
if (!empty($info['form switcher']) && empty($form[VARIABLE_REALM_FORM_SWITCHER . $realm])) {
|
402
|
$current_key = variable_realm_form_key_current($realm);
|
403
|
$form += variable_realm_form_key_selector($realm, $current_key);
|
404
|
}
|
405
|
}
|
406
|
}
|
407
|
}
|
408
|
}
|