Projet

Général

Profil

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

root / drupal7 / sites / all / modules / rules / rules_admin / rules_admin.inc @ 76e2e7c3

1
<?php
2

    
3
/**
4
 * @file Rules Admin UI
5
 *   Implements rule management and configuration screens.
6
 */
7

    
8
/**
9
 * Reaction rules overview.
10
 */
11
function rules_admin_reaction_overview($form, &$form_state, $base_path) {
12
  RulesPluginUI::formDefaults($form, $form_state);
13

    
14
  $conditions = array('plugin' => 'reaction rule', 'active' => TRUE);
15
  $collapsed = TRUE;
16
  if (empty($_GET['tag'])) {
17
    $tag = 0;
18
  }
19
  else {
20
    $tag = $_GET['tag'];
21
    $conditions['tags'] = array($tag);
22
    $collapsed = FALSE;
23
  }
24
  if (empty($_GET['event'])) {
25
    $event = 0;
26
  }
27
  else {
28
    $event = $_GET['event'];
29
    // Filter using a wildcard suffix so configured event names with suffixes
30
    // are found also.
31
    $conditions['event'] = $event . '%';
32
    $collapsed = FALSE;
33
  }
34
  $form['help'] = array(
35
    '#type' => 'markup',
36
    '#markup' => t('Reaction rules, listed below, react on selected events on the site. Each reaction rule may fire any number of <em>actions</em>, and may have any number of <em>conditions</em> that must be met for the actions to be executed. You can also set up <a href="@url1">components</a> – stand-alone sets of Rules configuration that can be used in Rules and other parts of your site. See <a href="@url2">the online documentation</a> for an introduction on how to use Rules.',
37
      array('@url1' => url('admin/config/workflow/rules/components'),
38
            '@url2' => rules_external_help('rules'))),
39
  );
40

    
41
  $form['filter'] = array(
42
    '#type' => 'fieldset',
43
    '#title' => t('Filter'),
44
    '#collapsible' => TRUE,
45
  );
46
  $form['filter']['#id'] = 'rules-filter-form';
47
  $form['filter']['#attached']['css'][] = drupal_get_path('module', 'rules') . '/ui/rules.ui.css';
48
  $form['filter']['event'] = array(
49
    '#type' => 'select',
50
    '#title' => t('Filter by event'),
51
    '#options' => array(0 => t('<All>')) + RulesPluginUI::getOptions('event'),
52
    '#default_value' => $event,
53
  );
54
  $form['filter']['tag'] = array(
55
    '#type' => 'select',
56
    '#title' => t('Filter by tag'),
57
    '#options' => array(0 => t('<All>')) + RulesPluginUI::getTags(),
58
    '#default_value' => $tag,
59
  );
60
  $form['filter']['submit'] = array(
61
    '#type' => 'submit',
62
    '#value' => t('Filter'),
63
    '#name' => '', // prevent from showing up in $_GET.
64
  );
65

    
66
  $options = array('show plugin' => FALSE, 'base path' => $base_path);
67
  $form['active'] = rules_ui()->overviewTable($conditions, $options);
68
  $form['active']['#caption'] = t('Active rules');
69
  $form['active']['#empty'] = t('There are no active rules. <a href="!url">Add new rule</a>.', array('!url' => url('admin/config/workflow/rules/reaction/add')));
70

    
71
  $conditions['active'] = FALSE;
72
  $form['inactive'] = rules_ui()->overviewTable($conditions, $options);
73
  $form['inactive']['#caption'] = t('Inactive rules');
74
  $form['inactive']['#empty'] = t('There are no inactive rules.');
75

    
76
  $form['filter']['#collapsed'] = $collapsed;
77
  $form['#submit'][] = 'rules_form_submit_rebuild';
78
  $form['#method'] = 'get';
79
  return $form;
80
}
81

    
82
/**
83
 * Components overview.
84
 */
85
function rules_admin_components_overview($form, &$form_state, $base_path) {
86
  RulesPluginUI::formDefaults($form, $form_state);
87

    
88
  $collapsed = TRUE;
89
  if (empty($_GET['tag'])) {
90
    $tag = 0;
91
  }
92
  else {
93
    $tag = $_GET['tag'];
94
    $conditions['tags'] = array($tag);
95
    $collapsed = FALSE;
96
  }
97
  if (empty($_GET['plugin'])) {
98
    // Get the plugin name usable as component.
99
    $conditions['plugin'] = array_keys(rules_filter_array(rules_fetch_data('plugin_info'), 'component', TRUE));
100
    $plugin = 0;
101
  }
102
  else {
103
    $plugin = $_GET['plugin'];
104
    $conditions['plugin'] = $plugin;
105
    $collapsed = FALSE;
106
  }
107
  $form['help'] = array(
108
    '#type' => 'markup',
109
    '#markup' => t('Components are stand-alone sets of Rules configuration that can be used by Rules and other modules on your site. Components are for example useful if you want to use the same conditions, actions or rules in multiple places, or call them from your custom module. You may also export each component separately. See <a href="@url">the online documentation</a> for more information about how to use components.',
110
      array('@url' => rules_external_help('components'))),
111
  );
112
  $form['filter'] = array(
113
    '#type' => 'fieldset',
114
    '#title' => t('Filter'),
115
    '#collapsible' => TRUE,
116
  );
117
  $form['filter']['#id'] = 'rules-filter-form';
118
  $form['filter']['#attached']['css'][] = drupal_get_path('module', 'rules') . '/ui/rules.ui.css';
119
  $form['filter']['plugin'] = array(
120
    '#type' => 'select',
121
    '#title' => t('Filter by plugin'),
122
    '#options' => array(0 => t('<All>')) + rules_admin_component_options(),
123
    '#default_value' => $plugin,
124
  );
125
  $form['filter']['tag'] = array(
126
    '#type' => 'select',
127
    '#title' => t('Filter by tag'),
128
    '#options' => array(0 => '<All>') + RulesPluginUI::getTags(),
129
    '#default_value' => $tag,
130
  );
131
  $form['filter']['submit'] = array(
132
    '#type' => 'submit',
133
    '#value' => t('Filter'),
134
    '#name' => '', // prevent from showing up in $_GET.
135
  );
136

    
137
  $form['table'] = RulesPluginUI::overviewTable($conditions, array('hide status op' => TRUE));
138
  $form['table']['#empty'] = t('There are no rule components.');
139

    
140
  $form['filter']['#collapsed'] = $collapsed;
141
  $form['#submit'][] = 'rules_form_submit_rebuild';
142
  $form['#method'] = 'get';
143
  return $form;
144
}
145

    
146
/**
147
 * Rules settings form.
148
 */
149
function rules_admin_settings($form, &$form_state) {
150

    
151
  if (module_exists('path')) {
152
    // Present a list of available path cleaning callbacks.
153
    // @see rules_clean_path()
154
    $options = array(
155
      'rules_path_default_cleaning_method' => t('Rules (built in)'),
156
    );
157
    if (module_exists('ctools')) {
158
      $options['rules_path_clean_ctools'] = t('CTools');
159
    }
160
    if (module_exists('pathauto')) {
161
      $options['rules_path_clean_pathauto'] = t('Pathauto');
162
      $pathauto_help = t("Note that Pathauto's URL path cleaning method can be configured at <a href='!url'>admin/config/search/path/settings</a>.", array('!url' => url('admin/config/search/path/settings')));
163
    }
164
    else {
165
      $pathauto_help = t('Install the <a href="http://drupal.org/project/pathauto">Pathauto module</a> in order to get a configurable URL path cleaning method.');
166
    }
167

    
168
    $form['path']['rules_path_cleaning_callback'] = array(
169
      '#type' => 'select',
170
      '#title' => t('URL path cleaning method'),
171
      '#description' => t('Choose the path cleaning method to be applied when generating URL path aliases.') . ' ' . $pathauto_help,
172
      '#default_value' => variable_get('rules_path_cleaning_callback', 'rules_path_default_cleaning_method'),
173
      '#options' => $options,
174
    );
175
  }
176

    
177
  $form['rules_log_errors'] = array(
178
    '#type' => 'radios',
179
    '#title' => t('Logging of Rules evaluation errors'),
180
    '#options' => array(
181
      RulesLog::WARN => t('Log all warnings and errors'),
182
      RulesLog::ERROR => t('Log errors only'),
183
    ),
184
    '#default_value' => variable_get('rules_log_errors', RulesLog::WARN),
185
    '#description' => t('Evaluations errors are logged to the system log.'),
186
  );
187

    
188
  $form['debug'] = array(
189
    '#type' => 'fieldset',
190
    '#title' => t('Debugging'),
191
  );
192
  $form['debug']['rules_debug_log'] = array(
193
    '#type' => 'checkbox',
194
    '#title' => t('Log debug information to the system log'),
195
    '#default_value' => variable_get('rules_debug_log', 0),
196
  );
197
  $form['debug']['rules_debug'] = array(
198
    '#type' => 'radios',
199
    '#title' => t('Show debug information'),
200
    '#default_value' => variable_get('rules_debug', 0),
201
    '#options' => array(
202
      0 => t('Never'),
203
      RulesLog::WARN => t('In case of errors'),
204
      RulesLog::INFO => t('Always'),
205
    ),
206
    '#description' => t('Debug information is only shown when rules are evaluated and is visible for users having the permission <a href="!url">%link</a>.', array('%link' => t('Access the Rules debug log'), '!url' => url('admin/people/permissions', array('fragment' => 'module-rules')))),
207
  );
208

    
209
  $form['debug']['regions'] = array(
210
    '#type' => 'container',
211
    '#states' => array(
212
      // Hide the regions settings when the debug log is disabled.
213
      'invisible' => array(
214
        'input[name="rules_debug"]' => array('value' => '0'),
215
      ),
216
    ),
217
  );
218

    
219
  $theme_default = variable_get('theme_default', 'bartik');
220
  $admin_theme = variable_get('admin_theme', 'seven');
221

    
222
  $form['debug']['regions']['rules_debug_region_' . $theme_default] = array(
223
    '#type' => 'select',
224
    '#title' => t('Default theme region'),
225
    '#description' => t("The region, where the debug log should be displayed on the default theme %theme. For other themes, Rules will try to display the log on the same region, or hide it in case it doesn't exist.", array('%theme' => $theme_default)),
226
    '#options' => system_region_list($theme_default, REGIONS_VISIBLE),
227
    '#default_value' => variable_get('rules_debug_region_' . $theme_default, 'help'),
228
  );
229

    
230
  $form['debug']['regions']['rules_debug_region_' . $admin_theme] = array(
231
    '#type' => 'select',
232
    '#title' => t('Admin theme region'),
233
    '#description' => t('The region, where the debug log should be displayed on the admin theme %theme.', array('%theme' => $admin_theme)),
234
    '#options' => system_region_list($admin_theme, REGIONS_VISIBLE),
235
    '#default_value' => variable_get('rules_debug_region_' . $admin_theme, 'help'),
236
  );
237
  if (db_table_exists('rules_rules')) {
238
    drupal_set_message(t('There are left over rule configurations from a previous Rules 1.x installation. Proceed to the <a href="!url">upgrade page</a> to convert them and consult the README.txt for more details.', array('!url' => url('admin/config/workflow/rules/upgrade'))), 'warning');
239
  }
240

    
241
  return system_settings_form($form);
242
}
243

    
244
/**
245
 * Advanced settings form.
246
 */
247
function rules_admin_settings_advanced($form, &$form_state) {
248

    
249
  $form['integrity'] = array(
250
    '#type' => 'fieldset',
251
    '#title' => t('Integrity'),
252
    '#description' => t('Rules checks the integrity of your configurations to discover and exclude broken configurations from evaluation.'),
253
  );
254
  $form['integrity']['start_integrity_check'] = array(
255
    '#type' => 'submit',
256
    '#value' => t('Recheck integrity'),
257
    '#submit' => array('rules_admin_settings_integrity_check_submit'),
258
  );
259
  $form['cache'] = array(
260
    '#type' => 'fieldset',
261
    '#title' => t('Cache'),
262
    '#description' => t('Rules caches information about available actions, conditions and data types. Additionally all components and reaction rules are cached for efficient evaluation.'),
263
  );
264
  $form['cache']['rebuild_rules_cache'] = array(
265
    '#type' => 'submit',
266
    '#value' => t("Rebuild Rules' cache"),
267
    '#weight' => 2,
268
    '#submit' => array('rules_admin_settings_cache_rebuild_submit'),
269
  );
270
  return $form;
271
}
272

    
273
/**
274
 * Form submit callback to check the integrity of all configurations.
275
 */
276
function rules_admin_settings_integrity_check_submit($form, &$form_state) {
277
  $start = microtime(TRUE);
278
  $count = 0;
279
  $rules_configs = rules_config_load_multiple(FALSE);
280
  foreach ($rules_configs as $rules_config) {
281
    rules_config_update_dirty_flag($rules_config, TRUE, TRUE);
282
    if ($rules_config->dirty) {
283
      $count++;
284
      $variables = array('%label' => $rules_config->label(), '%name' => $rules_config->name, '@plugin' => $rules_config->plugin(), '!uri'=> url(RulesPluginUI::path($rules_config->name)));
285
      drupal_set_message(t('The @plugin <a href="!uri">%label (%name)</a> fails the integrity check and cannot be executed.', $variables), 'error');
286
    }
287

    
288
  }
289
  drupal_set_message(t('Integrity of %count configurations checked in %duration seconds. %count_failed broken configurations found.', array(
290
    '%count' => count($rules_configs),
291
    '%count_failed' => $count,
292
    '%duration' => round(microtime(TRUE) - $start, 2),
293
  )));
294
}
295

    
296
/**
297
 * Form submit callback: Rebuild the Rules' cache.
298
 */
299
function rules_admin_settings_cache_rebuild_submit($form, &$form_state) {
300
  $start = microtime(TRUE);
301
  rules_clear_cache();
302
  // Manually trigger cache rebuilding of all caches.
303
  rules_get_cache();
304
  _rules_rebuild_component_cache();
305
  RulesEventSet::rebuildEventCache();
306
  drupal_set_message(t('Rules cache rebuilt in %duration seconds.', array(
307
    '%duration' => round(microtime(TRUE) - $start, 2),
308
  )));
309
}
310

    
311
/**
312
 * Add reaction rules form.
313
 */
314
function rules_admin_add_reaction_rule($form, &$form_state, $base_path) {
315
  RulesPluginUI::formDefaults($form, $form_state);
316

    
317
  $rules_config = isset($form_state['rules_config']) ? $form_state['rules_config'] : rules_reaction_rule();
318
  $rules_config->form($form, $form_state, array('show settings' => TRUE, 'button' => TRUE));
319

    
320
  $form['settings']['#collapsible'] = FALSE;
321
  $form['settings']['#type'] = 'container';
322
  $form['settings']['label']['#default_value'] = '';
323

    
324
  // Hide the rule elements stuff for now.
325
  foreach (array('elements', 'conditions', 'add', 'events') as $key) {
326
    $form[$key]['#access'] = FALSE;
327
  }
328
  foreach (array('active', 'weight') as $key) {
329
    $form['settings'][$key]['#access'] = FALSE;
330
  }
331
  // Incorporate the form to add the first event.
332
  $form['settings'] += rules_ui_add_event(array(), $form_state, $rules_config, $base_path);
333
  $form['settings']['event']['#tree'] = FALSE;
334
  $form['settings']['event_settings']['#tree'] = FALSE;
335
  unset($form['settings']['help']);
336

    
337
  unset($form['settings']['submit']);
338
  $form['submit']['#value'] = t('Save');
339

    
340
  $form_state += array('rules_config' => $rules_config);
341
  $form['#validate'][] = 'rules_ui_add_reaction_rule_validate';
342
  $form['#validate'][] = 'rules_ui_edit_element_validate';
343
  $form['#submit'][] = 'rules_ui_add_reaction_rule_submit';
344
  return $form;
345
}
346

    
347
/**
348
 * Form validation callback.
349
 */
350
function rules_ui_add_reaction_rule_validate(&$form, &$form_state) {
351
  rules_ui_add_event_validate($form['settings'], $form_state);
352
}
353

    
354
/**
355
 * Form submit callback.
356
 */
357
function rules_ui_add_reaction_rule_submit(&$form, &$form_state) {
358
  rules_ui_add_event_apply($form['settings'], $form_state);
359
  rules_ui_edit_element_submit($form, $form_state);
360
}
361

    
362
/**
363
 * Add component form.
364
 */
365
function rules_admin_add_component($form, &$form_state, $base_path) {
366
  RulesPluginUI::$basePath = $base_path;
367
  RulesPluginUI::formDefaults($form, $form_state);
368

    
369
  $form['plugin_name'] = array(
370
    '#type' => 'select',
371
    '#title' => t('Component plugin'),
372
    '#options' => rules_admin_component_options(),
373
    '#description' => t('Choose which kind of component to create. Each component type is described in <a href="@url">the online documentation</a>.',
374
      array('@url' => rules_external_help('component-types'))),
375
    '#weight' => -2,
376
    '#default_value' => isset($form_state['values']['plugin_name']) ? $form_state['values']['plugin_name'] : '',
377
  );
378

    
379
  if (!isset($form_state['rules_config'])) {
380
    $form['continue'] = array(
381
      '#type' => 'submit',
382
      '#name' => 'continue',
383
      '#submit' => array('rules_admin_add_component_create_submit'),
384
      '#value' => t('Continue'),
385
    );
386
  }
387
  else {
388
    $form['plugin_name']['#disabled'] = TRUE;
389
    $form_state['rules_config']->form($form, $form_state, array('show settings' => TRUE, 'button' => TRUE, 'init' => TRUE));
390
    $form['settings']['#collapsible'] = FALSE;
391
    $form['settings']['#type'] = 'container';
392
    $form['settings']['label']['#default_value'] = '';
393
    $form['settings']['#weight'] = -1;
394

    
395
    // Hide the rule elements stuff for now.
396
    foreach (array('elements', 'negate') as $key) {
397
      $form[$key]['#access'] = FALSE;
398
    }
399
    foreach (array('active', 'weight') as $key) {
400
      $form['settings'][$key]['#access'] = FALSE;
401
    }
402
  }
403
  return $form;
404
}
405

    
406
function rules_admin_component_options() {
407
  $cache = rules_get_cache();
408
  return rules_extract_property(rules_filter_array($cache['plugin_info'], 'component', TRUE), 'label');
409
}
410

    
411
/**
412
 * Submit callback that creates the new component object initially.
413
 */
414
function rules_admin_add_component_create_submit($form, &$form_state) {
415
  $form_state['rules_config'] = rules_plugin_factory($form_state['values']['plugin_name']);
416
  $form_state['rebuild'] = TRUE;
417
}
418

    
419
/**
420
 * Validation callback for adding a component.
421
 */
422
function rules_admin_add_component_validate($form, &$form_state) {
423
  if (isset($form_state['rules_config'])) {
424
    $form_state['rules_config']->form_validate($form, $form_state);
425
  }
426
}
427

    
428
/**
429
 * Final submit callback for adding a component.
430
 */
431
function rules_admin_add_component_submit($form, &$form_state) {
432
  $rules_config = $form_state['rules_config'];
433
  $rules_config->form_submit($form, $form_state);
434
  drupal_set_message(t('Your changes have been saved.'));
435
  $form_state['redirect'] = RulesPluginUI::path($rules_config->name);
436
}