Projet

Général

Profil

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

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

1
<?php
2

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

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

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

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

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

    
207
    $entities = entity_load('rules_config', FALSE, $conditions);
208
    ksort($entities);
209

    
210
    // Prepare some variables used by overviewTableRow().
211
    $this->event_info = rules_fetch_data('event_info');
212
    $this->cache = rules_get_cache();
213

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

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

    
244
    // TODO: hide configs where access() is FALSE.
245
    return $table;
246
  }
247

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

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

    
275
    $row[] = array('data' => array(
276
      '#theme' => 'entity_status',
277
      '#status' => $config->status,
278
    ));
279

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

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

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

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