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 @ 950416da

1
<?php
2

    
3
/**
4
 * @file
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
    '#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.',
36
      array('@url1' => url('admin/config/workflow/rules/components'),
37
            '@url2' => rules_external_help('rules'))),
38
  );
39

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

    
65
  $options = array('show plugin' => FALSE, 'base path' => $base_path);
66
  $form['active'] = rules_ui()->overviewTable($conditions, $options);
67
  $form['active']['#caption'] = t('Active rules');
68
  $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')));
69

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

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

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

    
87
  $collapsed = TRUE;
88
  if (empty($_GET['tag'])) {
89
    $tag = 0;
90
  }
91
  else {
92
    $tag = $_GET['tag'];
93
    $conditions['tags'] = array($tag);
94
    $collapsed = FALSE;
95
  }
96
  if (empty($_GET['plugin'])) {
97
    // Get the plugin name usable as component.
98
    $conditions['plugin'] = array_keys(rules_filter_array(rules_fetch_data('plugin_info'), 'component', TRUE));
99
    $plugin = 0;
100
  }
101
  else {
102
    $plugin = $_GET['plugin'];
103
    $conditions['plugin'] = $plugin;
104
    $collapsed = FALSE;
105
  }
106
  $form['help'] = array(
107
    '#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.',
108
      array('@url' => rules_external_help('components'))),
109
  );
110
  $form['filter'] = array(
111
    '#type' => 'fieldset',
112
    '#title' => t('Filter'),
113
    '#collapsible' => TRUE,
114
  );
115
  $form['filter']['#id'] = 'rules-filter-form';
116
  $form['filter']['#attached']['css'][] = drupal_get_path('module', 'rules') . '/ui/rules.ui.css';
117
  $form['filter']['plugin'] = array(
118
    '#type' => 'select',
119
    '#title' => t('Filter by plugin'),
120
    '#options' => array(0 => t('<All>')) + rules_admin_component_options(),
121
    '#default_value' => $plugin,
122
  );
123
  $form['filter']['tag'] = array(
124
    '#type' => 'select',
125
    '#title' => t('Filter by tag'),
126
    '#options' => array(0 => '<All>') + RulesPluginUI::getTags(),
127
    '#default_value' => $tag,
128
  );
129
  $form['filter']['submit'] = array(
130
    '#type' => 'submit',
131
    '#value' => t('Filter'),
132
    '#name' => '', // prevent from showing up in $_GET.
133
  );
134

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

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

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

    
149
  if (module_exists('path')) {
150
    // Present a list of available path cleaning callbacks.
151
    // @see rules_clean_path()
152
    $options = array(
153
      'rules_path_default_cleaning_method' => t('Rules (built in)'),
154
    );
155
    if (module_exists('ctools')) {
156
      $options['rules_path_clean_ctools'] = t('CTools');
157
    }
158
    if (module_exists('pathauto')) {
159
      $options['rules_path_clean_pathauto'] = t('Pathauto');
160
      $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')));
161
    }
162
    else {
163
      $pathauto_help = t('Install the <a href="https://www.drupal.org/project/pathauto">Pathauto module</a> in order to get a configurable URL path cleaning method.');
164
    }
165

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

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

    
186
  $form['debug'] = array(
187
    '#type' => 'fieldset',
188
    '#title' => t('Debugging'),
189
  );
190
  $form['debug']['rules_debug_log'] = array(
191
    '#type' => 'checkbox',
192
    '#title' => t('Log debug information to the system log'),
193
    '#default_value' => variable_get('rules_debug_log', FALSE),
194
  );
195
  $form['debug']['rules_debug'] = array(
196
    '#type' => 'radios',
197
    '#title' => t('Show debug information'),
198
    '#default_value' => variable_get('rules_debug', 0),
199
    '#options' => array(
200
      0 => t('Never'),
201
      RulesLog::WARN => t('In case of errors'),
202
      RulesLog::INFO => t('Always'),
203
    ),
204
    '#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')))),
205
  );
206

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

    
217
  $theme_default = variable_get('theme_default', 'bartik');
218
  $admin_theme = variable_get('admin_theme', 'seven');
219

    
220
  $form['debug']['regions']['rules_debug_region_' . $theme_default] = array(
221
    '#type' => 'select',
222
    '#title' => t('Default theme region'),
223
    '#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)),
224
    '#options' => system_region_list($theme_default, REGIONS_VISIBLE),
225
    '#default_value' => variable_get('rules_debug_region_' . $theme_default, 'help'),
226
  );
227

    
228
  $form['debug']['regions']['rules_debug_region_' . $admin_theme] = array(
229
    '#type' => 'select',
230
    '#title' => t('Admin theme region'),
231
    '#description' => t('The region, where the debug log should be displayed on the admin theme %theme.', array('%theme' => $admin_theme)),
232
    '#options' => system_region_list($admin_theme, REGIONS_VISIBLE),
233
    '#default_value' => variable_get('rules_debug_region_' . $admin_theme, 'help'),
234
  );
235
  if (db_table_exists('rules_rules')) {
236
    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');
237
  }
238

    
239
  return system_settings_form($form);
240
}
241

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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