Projet

Général

Profil

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

root / drupal7 / sites / all / modules / variable / variable_realm / variable_realm.form.inc @ 6331c987

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
    // variable_base_form() adds its own submit handler overriding the default,
93
    // so we need to add it as a explicit submit callback here.
94
    $form['#submit'][] = 'variable_realm_edit_variables_form_submit';
95

    
96
    // Group variables by variable group for vertical tabls
97
    $group_list = array();
98
    foreach ($variable_list as $variable_name) {
99
      $variable_info = variable_get_info($variable_name, $options);
100
      $group = $variable_info['group'];
101
      $group_list[$group][] = $variable_name;
102
    }
103
    $form['variables'] = array(
104
      '#type' => 'vertical_tabs',
105
      '#tree' => TRUE,
106
    );
107
    foreach ($group_list as $group => $group_variables) {
108
      $group_info = variable_get_group($group);
109
      $form['variables'][$group] = array(
110
        '#type' => 'fieldset',
111
        '#title' => $group_info['title'],
112
        '#collapsible' => TRUE, '#collapsed' => TRUE,
113
      );
114
      // Set form parents for this variable / group.
115
      $options['form parents'] = array('variables', $group);
116
      $form['variables'][$group] += variable_edit_subform($group_variables, $options);
117
    }
118
    $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save configuration'));
119
  }
120
  else {
121
    $form['message']['#markup'] = '<h3>' . t('No variables have been selected as %realm_name specific.', array('%realm_name' => $controller->getTitle())) . '</h3>';
122
  }
123
  if (!empty($realm_info['select path'])) {
124
    $form['select']['#markup'] = t('To select variables to be configured, see <a href="!select-url">%realm_name variable settings</a>.', array(
125
      '%realm_name' => $realm_info['title'],
126
      '!select-url' => url($realm_info['select path'])
127
    ));
128
  }
129
  return $form;
130
}
131

    
132
/**
133
 * Edit variables for realm.
134
 */
135
function variable_realm_edit_variables_form_submit($form, &$form_state) {
136
  $realm_name = $form_state['values']['realm_name'];
137
  $realm_key = $form_state['values']['realm_key'];
138
  foreach ($form_state['values']['variables'] as $group => $group_variables) {
139
    if (is_array($group_variables)) {
140
      foreach ($group_variables as $name => $value) {
141
        $current = variable_realm_get($realm_name, $realm_key, $name);
142
        if ($current !== $value) {
143
          variable_realm_set($realm_name, $realm_key, $name, $value);
144
        }
145
      }
146
    }
147
  }
148
  // Redirect later depending on query string parameters.
149
  _variable_realm_form_submit_redirect($form, $form_state);
150
}
151

    
152
/**
153
 * Key selector for realm forms.
154
 */
155
function variable_realm_form_key_selector($realm_name, $current_key) {
156
  $element_name = VARIABLE_REALM_FORM_SWITCHER . $realm_name;
157
  $query_name = 'variable_realm_' . $realm_name . '_key';
158
  $controller = variable_realm_controller($realm_name);
159
  $keys = $controller->getAllKeys();
160
  // Don't show selector if there aren't any keys to select.
161
  if (empty($keys)) {
162
    return array();
163
  }
164
  $form[$element_name] = array(
165
    '#type' => 'fieldset',
166
    '#title' => t('There are %name variables in this form', array('%name' => $controller->getVariableName())),
167
    '#weight' => -100,
168
    '#description' => t('Check you are editing the variables for the right %realm value or select the desired %realm.', array('%realm' => $controller->getTitle())),
169
  );
170
  // Replace only this element on current query string, there may be others.
171
  $current_query = $_GET;
172
  unset($current_query['q']);
173
  foreach ($keys as $realm_key => $key_name) {
174
    $query[VARIABLE_REALM_QUERY_STRING . $realm_name] = $realm_key;
175
    $link =  l($key_name, $_GET['q'], array('query' => $query + $current_query));
176
    $items[] = $current_key == $realm_key ? '<strong>' . $link . '</strong>' : $link;
177
  }
178
  $form[$element_name]['select_key'] = array(
179
    '#type' => 'item',
180
    '#markup' => implode(' | ', $items),
181
  );
182
  return $form;
183
}
184

    
185
/**
186
 * Get current realm key from query string or from current realm value.
187
 */
188
function variable_realm_form_key_current($realm_name) {
189
  $realm_controller = variable_realm_controller($realm_name);
190
  if ($key = variable_realm_params($realm_name)) {
191
    return $key;
192
  }
193
  elseif ($key = $realm_controller->getKey()) {
194
    return $key;
195
  }
196
  elseif ($key = $realm_controller->getRequestKey()) {
197
    return $key;
198
  }
199
  else {
200
    return $realm_controller->getDefaultKey();
201
  }
202
}
203

    
204
/**
205
 * Alter settings form and return list of found variables.
206
 */
207
function _variable_realm_variable_settings_form_alter(&$form, $realm_name, $variables) {
208
  $result = array();
209
  foreach (element_children($form) as $field) {
210
    if (count(element_children($form[$field])) && empty($form[$field]['#tree'])) {
211
      // Rewrite fieldsets recursively.
212
      $result += _variable_realm_variable_settings_form_alter($form[$field], $realm_name, $variables);
213
    }
214
    elseif (in_array($field, $variables)) {
215
      if (isset($form[$field]['#variable_realm'])) {
216
        // Oh-oh, variable already taken by another realm.
217
        _variable_realm_variable_settings_form_conflict($field);
218
        $form[$field]['#disabled'] = TRUE;
219
      }
220
      else {
221
        // Mark variable as already taken by a realm
222
        $form[$field]['#variable_realm'] = $realm_name;
223
      }
224
      _variable_realm_variable_settings_form_mark($realm_name, $form[$field]);
225
      // Addd field => name to result
226
      $result[$field] = !empty($form[$field]['#title']) ? $form[$field]['#title'] : $field;
227
    }
228
  }
229
  return $result;
230
}
231

    
232
/**
233
 * Mark variable as belonging to a realm.
234
 */
235
function _variable_realm_variable_settings_form_mark($realm_name, &$element) {
236
  $realm_info = variable_realm_info($realm_name);
237
  // Add form field class (i18n-variable) and description text.
238
  if (!empty($realm_info['variable class'])) {
239
    $element['#attributes']['class'][] = $realm_info['variable class'];
240
  }
241
  $element['#description'] = !empty($element['#description']) ? $element['#description'] : '';
242
  $element['#description'] .= ' <strong>' . t('This is a @name variable.', array('@name' => $realm_info['variable name'])) . '</strong> ';
243
}
244

    
245
/**
246
 * Warning about variable conflict.
247
 */
248
function _variable_realm_variable_settings_form_conflict($variable) {
249
  static $warnings;
250
  if (!isset($warnings[$variable])) {
251
    $warnings[$variable] = TRUE;
252
    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');
253
  }
254
}
255

    
256
/**
257
 * Save realm variables and remove them from form.
258
 */
259
function variable_realm_variable_settings_form_submit($form, &$form_state) {
260
  $op = isset($form_state['values']['op']) ? $form_state['values']['op'] : '';
261
  foreach ($form['#realm_keys'] as $realm_name => $realm_key) {
262
    $realm_controller = variable_realm_controller($realm_name);
263
    //$language = i18n_language($form_state['values']['i18n_variable_language']);
264
    //unset($form_state['values']['i18n_variable_language']);
265
    $variables = array_keys($form['#realm_variables'][$realm_name]);
266
    foreach ($variables as $variable_name) {
267
      if (isset($form_state['values'][$variable_name])) {
268
        if ($op == t('Reset to defaults')) {
269
          variable_realm_del($realm_name, $realm_key, $variable_name);
270
        }
271
        else {
272
          $value = $form_state['values'][$variable_name];
273
          if (is_array($value) && isset($form_state['values']['array_filter'])) {
274
            $value = array_keys(array_filter($value));
275
          }
276
          variable_realm_set($realm_name, $realm_key, $variable_name, $value);
277
        }
278
        // If current is not default realm key, we don't set any global variable (without realm)
279
        if ($realm_key != $realm_controller->getDefaultKey()) {
280
          unset($form_state['values'][$variable_name]);
281
        }
282
      }
283
    }
284
  }
285
  // Redirect later depending on query string parameters.
286
  _variable_realm_form_submit_redirect($form, $form_state);
287
  // The form will go now through system_settings_form_submit()
288
}
289

    
290
/**
291
 * Process system_theme_settings form submissions.
292
 *
293
 * @see system_theme_settings_submit()
294
 */
295
function variable_realm_variable_theme_form_submit($form, &$form_state) {
296
  // Regular theme submission without variable set.
297
  $values = $form_state['values'];
298

    
299
  // If the user uploaded a new logo or favicon, save it to a permanent location
300
  // and use it in place of the default theme-provided file.
301
  if ($file = $values['logo_upload']) {
302
    unset($values['logo_upload']);
303
    $filename = file_unmanaged_copy($file->uri);
304
    $values['default_logo'] = 0;
305
    $values['logo_path'] = $filename;
306
    $values['toggle_logo'] = 1;
307
  }
308
  if ($file = $values['favicon_upload']) {
309
    unset($values['favicon_upload']);
310
    $filename = file_unmanaged_copy($file->uri);
311
    $values['default_favicon'] = 0;
312
    $values['favicon_path'] = $filename;
313
    $values['toggle_favicon'] = 1;
314
  }
315

    
316
  // If the user entered a path relative to the system files directory for
317
  // a logo or favicon, store a public:// URI so the theme system can handle it.
318
  if (!empty($values['logo_path'])) {
319
    $values['logo_path'] = _system_theme_settings_validate_path($values['logo_path']);
320
  }
321
  if (!empty($values['favicon_path'])) {
322
    $values['favicon_path'] = _system_theme_settings_validate_path($values['favicon_path']);
323
  }
324

    
325
  if (empty($values['default_favicon']) && !empty($values['favicon_path'])) {
326
    $values['favicon_mimetype'] = file_get_mimetype($values['favicon_path']);
327
  }
328
  $key = $values['var'];
329

    
330
  // Exclude unnecessary elements before saving.
331
  unset($values['var'], $values['submit'], $values['reset'], $values['form_id'], $values['op'], $values['form_build_id'], $values['form_token']);
332

    
333
  // Set realm variable.
334
  $variable_name = $key;
335
  $realm_name = $form['#realm_theme'];
336
  $realm_key = $form['#realm_keys'][$realm_name];
337
  variable_realm_set($realm_name, $realm_key, $variable_name, $values);
338

    
339
  // If current is default realm key, set global variable too.
340
  $realm_controller = variable_realm_controller($realm_name);
341
  if ($realm_key == $realm_controller->getDefaultKey()) {
342
    variable_set($variable_name, $values);
343
  }
344
  // Confirmation, clear cache, taken from system_theme_settings_submit()
345
  drupal_set_message(t('The configuration options have been saved.'));
346
  cache_clear_all();
347
  // Redirect later depending on query string parameters.
348
  _variable_realm_form_submit_redirect($form, $form_state);
349
}
350

    
351
/**
352
 * Get variable list for settings forms handling multiple realms.
353
 *
354
 * Variables available for more than one realm, will be kept only in the list
355
 * for the realm with the higher weight.
356
 */
357
function _variable_realm_variable_settings_form_list() {
358
  $list = array();
359
  foreach (variable_realm_list_all() as $realm_name => $controller) {
360
    if ($controller->getInfo('form settings') && ($realm_list = $controller->getEnabledVariables())) {
361
      // Remove from previous realms with lower weight.
362
      foreach ($list as $name => $variables) {
363
        $list[$name] = array_diff($variables, $realm_list);
364
      }
365
      $list[$realm_name] = $realm_list;
366
    }
367
  }
368
  return $list;
369
}
370

    
371
/**
372
 * Redirect to current page after form submission, using query string parameters.
373
 */
374
function _variable_realm_form_submit_redirect($form, &$form_state) {
375
  if ($query_params = variable_realm_params()) {
376
    foreach ($query_params as $realm => $value) {
377
      $query[VARIABLE_REALM_QUERY_STRING . $realm] = $value;
378
    }
379
    // Parameters to be passed to drupal_goto().
380
    $form_state['redirect'] = array(current_path(), array('query' => $query));
381
  }
382
}
383

    
384
/**
385
 * Add realm switcher to the form.
386
 */
387
function _variable_realm_variable_settings_form_switcher(&$form) {
388
  // Add switchers for current realms and current key.
389
  // Add realm values and subform realm / key selector.
390
  foreach (array_keys($form['#realm_variables']) as $realm_name) {
391
    $current_key = variable_realm_form_key_current($realm_name);
392
    $info = variable_realm_info($realm_name);
393
    if (!empty($info['form switcher'])) {
394
      $form += variable_realm_form_key_selector($realm_name, $current_key);
395
    }
396
    $form['#realm_keys'][$realm_name] = $current_key;
397
  }
398
  // Make sure realm switchers are added for all parent realms of current ones.
399
  foreach (variable_realm_list_all() as $realm_name => $realm_controller) {
400
    if (($parent_realms = $realm_controller->getParentRealms()) && !empty($form['#realm_variables'][$realm_name]) && empty($form[VARIABLE_REALM_FORM_SWITCHER . $realm_name])) {
401
      // Check we have selectors for the other realms.
402
      foreach ($parent_realms as $realm) {
403
        $info = variable_realm_info($realm);
404
        if (!empty($info['form switcher']) && empty($form[VARIABLE_REALM_FORM_SWITCHER . $realm])) {
405
          $current_key = variable_realm_form_key_current($realm);
406
          $form += variable_realm_form_key_selector($realm, $current_key);
407
        }
408
      }
409
    }
410
  }
411
}