Projet

Général

Profil

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

root / drupal7 / sites / all / modules / rules / ui / ui.plugins.inc @ d719f12f

1 85ad3d82 Assos Assos
<?php
2
3
/**
4
 * @file Contains UI for diverse plugins provided by Rules.
5
 */
6
7
/**
8
 * Rule specific UI.
9
 */
10
class RulesRuleUI extends RulesActionContainerUI {
11
12
  protected $rule, $conditions;
13
14
  public function __construct(FacesExtendable $object) {
15
    parent::__construct($object);
16
    $this->rule = $object;
17
    $this->conditions = $this->rule->conditionContainer();
18
  }
19
20
  public function form(&$form, &$form_state, $options = array()) {
21
    $form_state['rules_element'] = $this->rule;
22
    $label = $this->element->label();
23
    // Automatically add a counter to unlabelled rules.
24
    if ($label == t('unlabeled') && !$this->element->isRoot() && !empty($options['init'])) {
25
      $parent = $this->element->parentElement();
26
      $label .= ' ' . count($parent->getIterator());
27
    }
28
    // Components have already a label. If used inside a rule-set add a label
29
    // though. It's called 'Name' in the UI though.
30
    if (!$this->element->isRoot()) {
31
      $form['label'] = array(
32
        '#type' => 'textfield',
33
        '#title' => t('Name'),
34
        '#default_value' => empty($options['init']) ? $label : '',
35
        '#required' => TRUE,
36
        '#weight' => 5,
37
        '#description' => t('A human-readable name shortly describing the rule.'),
38
      );
39
    }
40
41
    $form += array('conditions' => array('#weight' => -5, '#tree' => TRUE));
42
    $this->conditions->form($form['conditions'], $form_state);
43
    unset($form['conditions']['negate']);
44
45
    // Add actions form.
46
    $iterator = new RecursiveIteratorIterator($this->rule->actions(), RecursiveIteratorIterator::SELF_FIRST);
47
    parent::form($form, $form_state, $options, $iterator);
48
    // Hide nested elements during creation.
49
    $form['elements']['#access'] = empty($options['init']);
50
    $form['conditions']['elements']['#access'] = empty($options['init']);
51
52
    $form_state['redirect'] = RulesPluginUI::path($this->element->root()->name, 'edit', $this->element);
53
    if (!empty($options['button'])) {
54
      $form['submit']['#value'] = t('Save changes');
55
    }
56
  }
57
58
  /**
59
   * Applies the values of the form to the rule configuration.
60
   */
61
  function form_extract_values($form, &$form_state) {
62
    $form_values = RulesPluginUI::getFormStateValues($form, $form_state);
63
    // Run condition and action container value extraction.
64
    if (isset($form['conditions'])) {
65
      $this->conditions->extender('RulesConditionContainerUI')->form_extract_values($form['conditions'], $form_state);
66
    }
67
    if (!empty($form_values['label'])) {
68
      $this->element->label = $form_values['label'];
69
    }
70
    parent::form_extract_values($form, $form_state);
71
  }
72
73
74
  public function operations() {
75
    // When rules are listed only show the edit and delete operations.
76
    $ops = parent::operations();
77
    $ops['#links'] = array_intersect_key($ops['#links'], array_flip(array('edit', 'delete')));
78
    return $ops;
79
  }
80
}
81
82
/**
83
 * Reaction rule specific UI.
84
 */
85
class RulesReactionRuleUI extends RulesRuleUI {
86
87
  public function form(&$form, &$form_state, $options = array()) {
88
    $form['events'] = array(
89
      '#type' => 'container',
90
      '#weight' => -10,
91
      '#access' => empty($options['init']),
92
    );
93
94
    $form['events']['table'] = array(
95
      '#theme' => 'table',
96
      '#caption' => 'Events',
97
      '#header' => array('Event', 'Operations'),
98
      '#empty' => t('None'),
99
    );
100
    $form['events']['table']['#attributes']['class'][] = 'rules-elements-table';
101
    foreach ($this->rule->events() as $event_name) {
102
      $event_handler = rules_get_event_handler($event_name, $this->rule->getEventSettings($event_name));
103
104
      $event_operations = array(
105
        '#theme' => 'links__rules',
106
        '#attributes' => array(
107
          'class' => array(
108
            'rules-operations',
109
            'action-links',
110
            'rules_rule_event',
111
          ),
112
        ),
113
        '#links' => array(
114
          'delete_event' => array(
115
            'title' => t('delete'),
116
            'href' => RulesPluginUI::path($this->rule->name, 'delete/event/' . $event_name),
117
            'query' => drupal_get_destination(),
118
          ),
119
        ),
120
      );
121
122
      $form['events']['table']['#rows'][$event_name] = array(
123
        'data' => array(
124
          $event_handler->summary(),
125
          array('data' => $event_operations),
126
        ),
127
      );
128
    }
129
130
    // Add the "add event" row.
131
    $cell['colspan'] = 3;
132
    $cell['data']['#theme'] = 'links__rules';
133
    $cell['data']['#attributes']['class'][] = 'rules-operations-add';
134
    $cell['data']['#attributes']['class'][] = 'action-links';
135
    $cell['data']['#links']['add_event'] = array(
136
      'title' => t('Add event'),
137
      'href' => RulesPluginUI::path($this->rule->name, 'add/event'),
138
      'query' => drupal_get_destination(),
139
    );
140
    $form['events']['table']['#rows'][] = array('data' => array($cell), 'class' => array('rules-elements-add'));
141
142
    parent::form($form, $form_state, $options);
143
    unset($form['label']);
144
  }
145
146
  /**
147
   * Adds the configuration settings form (label, tags, description, ..).
148
   */
149
  public function settingsForm(&$form, &$form_state) {
150
    parent::settingsForm($form, $form_state);
151
    $form['settings']['active'] = array(
152
      '#type' => 'checkbox',
153
      '#title' => t('Active'),
154
      '#default_value' => !isset($this->rule->active) || $this->rule->active,
155
    );
156
    $form['settings']['weight'] = array(
157
      '#type' => 'weight',
158
      '#title' => t('Weight'),
159
      '#default_value' => $this->element->weight,
160
      '#weight' => 5,
161
      '#delta' => 10,
162
      '#description' => t('Order rules that react on the same event. Rules with a higher weight are evaluated after rules with less weight.'),
163
    );
164
    unset($form['settings']['component_provides']);
165
  }
166
167
  public function settingsFormExtractValues($form, &$form_state) {
168
    $form_values = RulesPluginUI::getFormStateValues($form['settings'], $form_state);
169
    parent::settingsFormExtractValues($form, $form_state);
170
    $this->rule->active = $form_values['active'];
171
    $this->rule->weight = $form_values['weight'];
172
  }
173
}
174
175
/**
176
 * Rule set specific UI.
177
 */
178
class RulesRuleSetUI extends RulesActionContainerUI {
179
180
  public function form(&$form, &$form_state, $options = array(), $iterator = NULL) {
181
    // Pass an iterator just iterating over the rules, thus no further child
182
    // elements will be displayed.
183
    parent::form($form, $form_state, $options, $this->element->getIterator());
184
    // Only show the add rule link.
185
    $form['elements']['#add']['#links'] = array_intersect_key($form['elements']['#add']['#links'], array('add_rule' => 1));
186
    $form['elements']['#attributes']['class'][] = 'rules-rule-set';
187
    $form['elements']['#caption'] = t('Rules');
188
  }
189
}
190
191
/**
192
 * UI for Rules loops.
193
 */
194
class RulesLoopUI extends RulesActionContainerUI {
195
196
  public function form(&$form, &$form_state, $options = array()) {
197
    parent::form($form, $form_state, $options);
198
    $settings = $this->element->settings;
199
200
    $form['item'] = array(
201
      '#type' => 'fieldset',
202
      '#title' => t('Current list item'),
203
      '#description' => t('The variable used for holding each list item in the loop. This variable will be available inside the loop only.'),
204
      '#tree' => TRUE,
205
    );
206
    $form['item']['label'] = array(
207
      '#type' => 'textfield',
208
      '#title' => t('Variable label'),
209
      '#default_value' => $settings['item:label'],
210
      '#required' => TRUE,
211
    );
212
    $form['item']['var'] = array(
213
      '#type' => 'textfield',
214
      '#title' => t('Variable name'),
215
      '#default_value' => $settings['item:var'],
216
      '#description' => t('The variable name must contain only lowercase letters, numbers, and underscores and must be unique in the current scope.'),
217
      '#element_validate' => array('rules_ui_element_machine_name_validate'),
218
      '#required' => TRUE,
219
    );
220
  }
221
222
  function form_extract_values($form, &$form_state) {
223
    parent::form_extract_values($form, $form_state);
224
    $form_values = RulesPluginUI::getFormStateValues($form, $form_state);
225
226
    $this->element->settings['item:var'] = $form_values['item']['var'];
227
    $this->element->settings['item:label'] = $form_values['item']['label'];
228
  }
229
230
  public function form_validate($form, &$form_state) {
231
    parent::form_validate($form, $form_state);
232
233
    $vars = $this->element->availableVariables();
234
    $name = $this->element->settings['item:var'];
235
    if (isset($vars[$name])) {
236
      form_error($form['item']['var'], t('The variable name %name is already taken.', array('%name' => $name)));
237
    }
238
  }
239
240
  public function buildContent() {
241
    $content = parent::buildContent();
242
243
    $content['description']['item'] = array(
244
      '#caption' => t('List item'),
245
      '#theme' => 'rules_content_group',
246
    );
247
    $content['description']['item']['var'] = array(
248
      '#theme' => 'rules_variable_view',
249
      '#info' => $this->element->listItemInfo(),
250
      '#name' => $this->element->settings['item:var'],
251
    );
252
    return $content;
253
  }
254
}