Projet

Général

Profil

Paste
Télécharger (14,7 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / rules / ui / ui.controller.inc @ 950416da

1
<?php
2

    
3
/**
4
 * @file
5
 * Contains the UI controller for Rules.
6
 */
7

    
8
/**
9
 * Controller class for the Rules UI.
10
 *
11
 * The Rules UI controller defines the methods other modules may use in order
12
 * to easily re-use the UI, regardless whether the rules admin module is
13
 * enabled.
14
 */
15
class RulesUIController {
16

    
17
  /**
18
   * Generates menu items to manipulate rules configurations.
19
   *
20
   * @param $base_path
21
   *   The path to the overview page from where the configurations are edited.
22
   */
23
  public function config_menu($base_path) {
24
    $items = array();
25
    $base_count = count(explode('/', $base_path));
26
    $items[$base_path . '/manage/%rules_config'] = array(
27
      'title callback' => 'rules_get_title',
28
      'title arguments' => array('Editing !plugin "!label"', $base_count + 1),
29
      'page callback' => 'drupal_get_form',
30
      'page arguments' => array('rules_ui_form_edit_rules_config', $base_count + 1, $base_path),
31
      'access callback' => 'rules_config_access',
32
      'access arguments' => array('update', $base_count + 1),
33
      'type' => MENU_VISIBLE_IN_BREADCRUMB,
34
      'file' => 'ui/ui.forms.inc',
35
      'file path' => drupal_get_path('module', 'rules'),
36
    );
37
    $items[$base_path . '/manage/%rules_config/add/%rules_element'] = array(
38
      // Adding another part to the path would hit the menu path-part-limit
39
      // for base paths like admin/config/workflow/rules. Therefore we have to
40
      // use this ugly way for setting the title.
41
      'title callback' => 'rules_menu_add_element_title',
42
      // Wrap the integer in an array, so it is passed as is.
43
      'title arguments' => array(array($base_count + 4)),
44
      'page callback' => 'drupal_get_form',
45
      'page arguments' => array('rules_ui_add_element', $base_count + 1, $base_count + 4, $base_count + 3, $base_path),
46
      'access callback' => 'rules_config_access',
47
      'access arguments' => array('update', $base_count + 1),
48
      'load arguments' => array($base_count + 1),
49
      'file' => 'ui/ui.forms.inc',
50
      'file path' => drupal_get_path('module', 'rules'),
51
    );
52
    $items[$base_path . '/manage/%rules_config/add/event'] = array(
53
      'title callback' => 'rules_get_title',
54
      'title arguments' => array('Adding event to !plugin "!label"', $base_count + 1),
55
      'page callback' => 'drupal_get_form',
56
      'page arguments' => array('rules_ui_add_event_page', $base_count + 1, $base_path),
57
      'access callback' => 'rules_config_access',
58
      'access arguments' => array('update', $base_count + 1),
59
      'load arguments' => array($base_count + 1),
60
      'file' => 'ui/ui.forms.inc',
61
      'file path' => drupal_get_path('module', 'rules'),
62
    );
63
    $items[$base_path . '/manage/%rules_config/delete/event'] = array(
64
      // @todo Improve title.
65
      'title' => 'Remove event',
66
      'page callback' => 'drupal_get_form',
67
      'page arguments' => array('rules_ui_remove_event', $base_count + 1, $base_count + 4, $base_path),
68
      'access callback' => 'rules_config_access',
69
      'access arguments' => array('update', $base_count + 1),
70
      'description' => 'Remove an event from a reaction rule.',
71
      'file' => 'ui/ui.forms.inc',
72
      'file path' => drupal_get_path('module', 'rules'),
73
    );
74
    $items[$base_path . '/manage/%rules_config/edit/%rules_element'] = array(
75
      'title callback' => 'rules_get_title',
76
      'title arguments' => array('Editing !plugin "!label"', $base_count + 3),
77
      'page callback' => 'drupal_get_form',
78
      'page arguments' => array('rules_ui_edit_element', $base_count + 1, $base_count + 3, $base_path),
79
      'access callback' => 'rules_config_access',
80
      'access arguments' => array('update', $base_count + 1),
81
      'load arguments' => array($base_count + 1),
82
      'file' => 'ui/ui.forms.inc',
83
      'file path' => drupal_get_path('module', 'rules'),
84
    );
85
    $items[$base_path . '/manage/%rules_config/autocomplete'] = array(
86
      'page callback' => 'rules_ui_form_data_selection_auto_completion',
87
      'page arguments' => array($base_count + 3, $base_count + 4, $base_count + 5),
88
      'access callback' => 'rules_config_access',
89
      'access arguments' => array('update', $base_count + 1),
90
      'type' => MENU_CALLBACK,
91
      'file' => 'ui/ui.forms.inc',
92
      'file path' => drupal_get_path('module', 'rules'),
93
    );
94
    $items[$base_path . '/manage/%rules_config/delete/%rules_element'] = array(
95
      'title callback' => 'rules_get_title',
96
      'title arguments' => array('Editing !plugin "!label"', $base_count + 3),
97
      'page callback' => 'drupal_get_form',
98
      'page arguments' => array('rules_ui_delete_element', $base_count + 1, $base_count + 3, $base_path),
99
      'access callback' => 'rules_config_access',
100
      'access arguments' => array('update', $base_count + 1),
101
      'load arguments' => array($base_count + 1),
102
      'file' => 'ui/ui.forms.inc',
103
      'file path' => drupal_get_path('module', 'rules'),
104
    );
105
    $items[$base_path . '/manage/%rules_config/%'] = array(
106
      'page callback' => 'drupal_get_form',
107
      'page arguments' => array('rules_ui_form_rules_config_confirm_op', $base_count + 1, $base_count + 2, $base_path),
108
      'access callback' => 'rules_config_access',
109
      'access arguments' => array('update', $base_count + 1),
110
      'file' => 'ui/ui.forms.inc',
111
      'file path' => drupal_get_path('module', 'rules'),
112
    );
113
    $items[$base_path . '/manage/%rules_config/clone'] = array(
114
      'title callback' => 'rules_get_title',
115
      'title arguments' => array('Cloning !plugin "!label"', $base_count + 1),
116
      'page callback' => 'drupal_get_form',
117
      'page arguments' => array('rules_ui_form_clone_rules_config', $base_count + 1, $base_path),
118
      'access callback' => 'rules_config_access',
119
      'access arguments' => array('update', $base_count + 1),
120
      'file' => 'ui/ui.forms.inc',
121
      'file path' => drupal_get_path('module', 'rules'),
122
    );
123
    $items[$base_path . '/manage/%rules_config/export'] = array(
124
      'title callback' => 'rules_get_title',
125
      'title arguments' => array('Export of !plugin "!label"', $base_count + 1),
126
      'page callback' => 'drupal_get_form',
127
      'page arguments' => array('rules_ui_form_export_rules_config', $base_count + 1, $base_path),
128
      'access callback' => 'rules_config_access',
129
      'access arguments' => array('view', $base_count + 1),
130
      'file' => 'ui/ui.forms.inc',
131
      'file path' => drupal_get_path('module', 'rules'),
132
    );
133
    $items[$base_path . '/manage/%rules_config/execute'] = array(
134
      'title callback' => 'rules_get_title',
135
      'title arguments' => array('Executing !plugin "!label"', $base_count + 1),
136
      'page callback' => 'drupal_get_form',
137
      'page arguments' => array('rules_ui_form_execute_rules_config', $base_count + 1, $base_path),
138
      'access callback' => 'rules_config_access',
139
      'access arguments' => array('update', $base_count + 1),
140
      'file' => 'ui/ui.forms.inc',
141
      'file path' => drupal_get_path('module', 'rules'),
142
    );
143
    drupal_alter('rules_ui_menu', $items, $base_path, $base_count);
144

    
145
    if (module_exists('rules_scheduler')) {
146
      $items[$base_path . '/manage/%rules_config/schedule'] = array(
147
        'title callback' => 'rules_get_title',
148
        'title arguments' => array('Schedule !plugin "!label"', $base_count + 1),
149
        'page callback' => 'drupal_get_form',
150
        'page arguments' => array('rules_scheduler_schedule_form', $base_count + 1, $base_path),
151
        'access callback' => 'rules_config_access',
152
        'access arguments' => array('update', $base_count + 1),
153
        'file' => 'rules_scheduler.admin.inc',
154
        'file path' => drupal_get_path('module', 'rules_scheduler'),
155
      );
156
    }
157
    return $items;
158
  }
159

    
160
  /**
161
   * Generates the render array for an overview configuration table.
162
   *
163
   * Generates the render array for an overview configuration table for
164
   * arbitrary rule configs that match the given conditions.
165
   *
166
   * Note: The generated overview table contains multiple links for editing the
167
   * rule configurations. For the links to properly work use
168
   * RulesUIController::config_menu($base_path) to generate appropriate menu
169
   * items for the path at which the overview table is displayed.
170
   *
171
   * @param $conditions
172
   *   An array of conditions as needed by rules_config_load_multiple().
173
   * @param $options
174
   *   An array with optional options. Known keys are:
175
   *   - 'hide status op': If set to TRUE, enable/disable links are not added.
176
   *     Defaults to FALSE.
177
   *   - 'show plugin': If set to FALSE, the plugin is not shown. Defaults to
178
   *     TRUE.
179
   *   - 'show events': If set to TRUE, the event column is shown. Defaults to
180
   *     TRUE if only reaction rules are listed.
181
   *   - 'show execution op': If set to TRUE an operation for execution a
182
   *     component is shown for components, as well as a link to schedule a
183
   *     component if the rules scheduler module is enabled.
184
   *   - 'base path': Optionally, a different base path to use instead of the
185
   *     currently set RulesPluginUI::$basePath. If no base path has been set
186
   *     yet, the current path is used by default.
187
   *
188
   * @return array
189
   *   A renderable array.
190
   */
191
  public function overviewTable($conditions = array(), $options = array()) {
192
    $options += array(
193
      'hide status op' => FALSE,
194
      'show plugin' => TRUE,
195
      'show events' => isset($conditions['plugin']) && $conditions['plugin'] == 'reaction rule',
196
      'show execution op' => !(isset($conditions['plugin']) && $conditions['plugin'] == 'reaction rule'),
197
    );
198
    // By default show only configurations owned by rules.
199
    $conditions += array(
200
      'owner' => 'rules',
201
    );
202
    if (!empty($options['base path'])) {
203
      RulesPluginUI::$basePath = $options['base path'];
204
    }
205
    elseif (!isset(RulesPluginUI::$basePath)) {
206
      // Default to the current path, only if no path has been set yet.
207
      RulesPluginUI::$basePath = current_path();
208
    }
209

    
210
    $entities = entity_load('rules_config', FALSE, $conditions);
211
    ksort($entities);
212

    
213
    // Prepare some variables used by overviewTableRow().
214
    $this->event_info = rules_fetch_data('event_info');
215
    $this->cache = rules_get_cache();
216

    
217
    $rows = array();
218
    foreach ($entities as $id => $entity) {
219
      if (user_access('bypass rules access') || $entity->access()) {
220
        $rows[] = $this->overviewTableRow($conditions, $id, $entity, $options);
221
      }
222
    }
223
    // Assemble the right table header.
224
    $header = array(t('Name'), t('Event'), t('Plugin'), t('Status'), array('data' => t('Operations')));
225
    if (!$options['show events']) {
226
      // Remove the event heading as there is no such column.
227
      unset($header[1]);
228
    }
229
    if (!$options['show plugin']) {
230
      unset($header[2]);
231
    }
232
    // Fix the header operation column colspan.
233
    $num_cols = isset($rows[0]) ? count($rows[0]) : 0;
234
    if (($addition = $num_cols - count($header)) > 0) {
235
      $header[4]['colspan'] = $addition + 1;
236
    }
237

    
238
    $table = array(
239
      '#theme' => 'table',
240
      '#header' => $header,
241
      '#rows' => $rows,
242
      '#empty' => t('None.'),
243
    );
244
    $table['#attributes']['class'][] = 'rules-overview-table';
245
    $table['#attached']['css'][] = drupal_get_path('module', 'rules') . '/ui/rules.ui.css';
246

    
247
    // @todo Hide configs where access() is FALSE.
248
    return $table;
249
  }
250

    
251
  /**
252
   * Generates the row for a single rules config.
253
   *
254
   * @param $additional_cols
255
   *   Additional columns to be added after the entity label column.
256
   */
257
  protected function overviewTableRow($conditions, $name, $config, $options) {
258
    // Build content includes the label, as well as a short overview including
259
    // the machine name.
260
    $row[] = array('data' => $config->buildContent());
261

    
262
    // Add events if the configs are assigned to events.
263
    if ($options['show events']) {
264
      $events = array();
265
      if ($config instanceof RulesTriggerableInterface) {
266
        foreach ($config->events() as $event_name) {
267
          $event_handler = rules_get_event_handler($event_name, $config->getEventSettings($event_name));
268
          $events[] = $event_handler->summary();
269
        }
270
      }
271
      $row[] = implode(", ", $events);
272
    }
273
    if ($options['show plugin']) {
274
      $plugin = $config->plugin();
275
      $row[] = isset($this->cache['plugin_info'][$plugin]['label']) ? $this->cache['plugin_info'][$plugin]['label'] : $plugin;
276
    }
277

    
278
    $row[] = array('data' => array(
279
      '#theme' => 'entity_status',
280
      '#status' => $config->status,
281
    ));
282

    
283
    // Add operations depending on the options and the exportable status.
284
    if (!$config->hasStatus(ENTITY_FIXED)) {
285
      $row[] = l(t('edit'), RulesPluginUI::path($name), array('attributes' => array('class' => array('edit', 'action'))));
286
      if (module_exists('rules_i18n')) {
287
        $row[] = l(t('translate'), RulesPluginUI::path($name, 'translate'), array('attributes' => array('class' => array('translate', 'action'))));
288
      }
289
    }
290
    else {
291
      $row[] = '';
292
      if (module_exists('rules_i18n')) {
293
        $row[] = '';
294
      }
295
    }
296

    
297
    if (!$options['hide status op']) {
298
      // Add either an enable or disable link.
299
      $text = $config->active ? t('disable') : t('enable');
300
      $active_class = $config->active ? 'disable' : 'enable';
301
      $link_path = RulesPluginUI::path($name, $active_class);
302
      $row[] = $config->hasStatus(ENTITY_FIXED) ? '' : l($text, $link_path, array('attributes' => array('class' => array($active_class, 'action')), 'query' => drupal_get_destination()));
303
    }
304
    $row[] = l(t('clone'), RulesPluginUI::path($name, 'clone'), array('attributes' => array('class' => array('clone', 'action'))));
305

    
306
    // Add execute link for for components.
307
    if ($options['show execution op']) {
308
      $row[] = ($config instanceof RulesTriggerableInterface) ? '' : l(t('execute'), RulesPluginUI::path($name, 'execute'), array('attributes' => array('class' => array('execute', 'action')), 'query' => drupal_get_destination()));
309
      if (module_exists('rules_scheduler')) {
310
        // Add schedule link for action components only.
311
        $row[] = $config instanceof RulesActionInterface ? l(t('schedule'), RulesPluginUI::path($name, 'schedule'), array('attributes' => array('class' => array('schedule', 'action')), 'query' => drupal_get_destination())) : '';
312
      }
313
    }
314

    
315
    if (!$config->hasStatus(ENTITY_IN_CODE) && !$config->hasStatus(ENTITY_FIXED)) {
316
      $row[] = l(t('delete'), RulesPluginUI::path($name, 'delete'), array('attributes' => array('class' => array('delete', 'action')), 'query' => drupal_get_destination()));
317
    }
318
    elseif ($config->hasStatus(ENTITY_OVERRIDDEN) && !$config->hasStatus(ENTITY_FIXED)) {
319
      $row[] = l(t('revert'), RulesPluginUI::path($name, 'revert'), array('attributes' => array('class' => array('revert', 'action')), 'query' => drupal_get_destination()));
320
    }
321
    else {
322
      $row[] = '';
323
    }
324
    $row[] = l(t('export'), RulesPluginUI::path($name, 'export'), array('attributes' => array('class' => array('export', 'action'))));
325
    return $row;
326
  }
327

    
328
}