Projet

Général

Profil

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

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

1
<?php
2

    
3
/**
4
 * @file
5
 * Contains UI for diverse plugins provided by Rules.
6
 */
7

    
8
/**
9
 * Rule specific UI.
10
 */
11
class RulesRuleUI extends RulesActionContainerUI {
12

    
13
  protected $rule;
14
  protected $conditions;
15

    
16
  /**
17
   * Constructs a RulesRuleUI object.
18
   *
19
   * @param FacesExtendable $object
20
   */
21
  public function __construct(FacesExtendable $object) {
22
    parent::__construct($object);
23
    $this->rule = $object;
24
    $this->conditions = $this->rule->conditionContainer();
25
  }
26

    
27
  public function form(&$form, &$form_state, $options = array(), $iterator = NULL) {
28
    $form_state['rules_element'] = $this->rule;
29
    $label = $this->element->label();
30
    // Automatically add a counter to unlabelled rules.
31
    if ($label == t('unlabeled') && !$this->element->isRoot() && !empty($options['init'])) {
32
      $parent = $this->element->parentElement();
33
      $label .= ' ' . count($parent->getIterator());
34
    }
35
    // Components have already a label. If used inside a rule-set add a label
36
    // though. It's called 'Name' in the UI though.
37
    if (!$this->element->isRoot()) {
38
      $form['label'] = array(
39
        '#type' => 'textfield',
40
        '#title' => t('Name'),
41
        '#default_value' => empty($options['init']) ? $label : '',
42
        '#required' => TRUE,
43
        '#weight' => 5,
44
        '#description' => t('A human-readable name shortly describing the rule.'),
45
      );
46
    }
47

    
48
    $form += array('conditions' => array('#weight' => -5, '#tree' => TRUE));
49
    $this->conditions->form($form['conditions'], $form_state);
50
    unset($form['conditions']['negate']);
51

    
52
    // Add actions form.
53
    $iterator = new RecursiveIteratorIterator($this->rule->actions(), RecursiveIteratorIterator::SELF_FIRST);
54
    parent::form($form, $form_state, $options, $iterator);
55
    // Hide nested elements during creation.
56
    $form['elements']['#access'] = empty($options['init']);
57
    $form['conditions']['elements']['#access'] = empty($options['init']);
58

    
59
    $form_state['redirect'] = RulesPluginUI::path($this->element->root()->name, 'edit', $this->element);
60
    if (!empty($options['button'])) {
61
      $form['submit']['#value'] = t('Save changes');
62
    }
63
  }
64

    
65
  /**
66
   * Applies the values of the form to the rule configuration.
67
   */
68
  public function form_extract_values($form, &$form_state) {
69
    $form_values = RulesPluginUI::getFormStateValues($form, $form_state);
70
    // Run condition and action container value extraction.
71
    if (isset($form['conditions'])) {
72
      $this->conditions->extender('RulesConditionContainerUI')->form_extract_values($form['conditions'], $form_state);
73
    }
74
    if (!empty($form_values['label'])) {
75
      $this->element->label = $form_values['label'];
76
    }
77
    parent::form_extract_values($form, $form_state);
78
  }
79

    
80
  public function operations() {
81
    // When rules are listed only show the edit and delete operations.
82
    $ops = parent::operations();
83
    $ops['#links'] = array_intersect_key($ops['#links'], array_flip(array('edit', 'delete')));
84
    return $ops;
85
  }
86

    
87
}
88

    
89
/**
90
 * Reaction rule specific UI.
91
 */
92
class RulesReactionRuleUI extends RulesRuleUI {
93

    
94
  public function form(&$form, &$form_state, $options = array(), $iterator = NULL) {
95
    $form['events'] = array(
96
      '#type' => 'container',
97
      '#weight' => -10,
98
      '#access' => empty($options['init']),
99
    );
100

    
101
    $form['events']['table'] = array(
102
      '#theme' => 'table',
103
      '#caption' => 'Events',
104
      '#header' => array(t('Event'), t('Operations')),
105
      '#empty' => t('None'),
106
    );
107
    $form['events']['table']['#attributes']['class'][] = 'rules-elements-table';
108
    foreach ($this->rule->events() as $event_name) {
109
      $event_handler = rules_get_event_handler($event_name, $this->rule->getEventSettings($event_name));
110

    
111
      $event_operations = array(
112
        '#theme' => 'links__rules',
113
        '#attributes' => array(
114
          'class' => array(
115
            'rules-operations',
116
            'action-links',
117
            'rules_rule_event',
118
          ),
119
        ),
120
        '#links' => array(
121
          'delete_event' => array(
122
            'title' => t('delete'),
123
            'href' => RulesPluginUI::path($this->rule->name, 'delete/event/' . $event_name),
124
            'query' => drupal_get_destination(),
125
          ),
126
        ),
127
      );
128

    
129
      $form['events']['table']['#rows'][$event_name] = array(
130
        'data' => array(
131
          $event_handler->summary(),
132
          array('data' => $event_operations),
133
        ),
134
      );
135
    }
136

    
137
    // Add the "add event" row.
138
    $cell['colspan'] = 3;
139
    $cell['data']['#theme'] = 'links__rules';
140
    $cell['data']['#attributes']['class'][] = 'rules-operations-add';
141
    $cell['data']['#attributes']['class'][] = 'action-links';
142
    $cell['data']['#links']['add_event'] = array(
143
      'title' => t('Add event'),
144
      'href' => RulesPluginUI::path($this->rule->name, 'add/event'),
145
      'query' => drupal_get_destination(),
146
    );
147
    $form['events']['table']['#rows'][] = array('data' => array($cell), 'class' => array('rules-elements-add'));
148

    
149
    parent::form($form, $form_state, $options);
150
    unset($form['label']);
151
  }
152

    
153
  /**
154
   * Adds the configuration settings form (label, tags, description, ..).
155
   */
156
  public function settingsForm(&$form, &$form_state) {
157
    parent::settingsForm($form, $form_state);
158
    $form['settings']['active'] = array(
159
      '#type' => 'checkbox',
160
      '#title' => t('Active'),
161
      '#default_value' => !isset($this->rule->active) || $this->rule->active,
162
    );
163
    $form['settings']['weight'] = array(
164
      '#type' => 'weight',
165
      '#title' => t('Weight'),
166
      '#default_value' => $this->element->weight,
167
      '#weight' => 5,
168
      '#delta' => 10,
169
      '#description' => t('Order rules that react on the same event. Rules with a higher weight are evaluated after rules with less weight.'),
170
    );
171
    unset($form['settings']['component_provides']);
172
  }
173

    
174
  public function settingsFormExtractValues($form, &$form_state) {
175
    $form_values = RulesPluginUI::getFormStateValues($form['settings'], $form_state);
176
    parent::settingsFormExtractValues($form, $form_state);
177
    $this->rule->active = $form_values['active'];
178
    $this->rule->weight = $form_values['weight'];
179
  }
180

    
181
}
182

    
183
/**
184
 * Rule set specific UI.
185
 */
186
class RulesRuleSetUI extends RulesActionContainerUI {
187

    
188
  public function form(&$form, &$form_state, $options = array(), $iterator = NULL) {
189
    // Pass an iterator just iterating over the rules, thus no further child
190
    // elements will be displayed.
191
    parent::form($form, $form_state, $options, $this->element->getIterator());
192
    // Only show the add rule link.
193
    $form['elements']['#add']['#links'] = array_intersect_key($form['elements']['#add']['#links'], array('add_rule' => 1));
194
    $form['elements']['#attributes']['class'][] = 'rules-rule-set';
195
    $form['elements']['#caption'] = t('Rules');
196
  }
197

    
198
}
199

    
200
/**
201
 * UI for Rules loops.
202
 */
203
class RulesLoopUI extends RulesActionContainerUI {
204

    
205
  public function form(&$form, &$form_state, $options = array(), $iterator = NULL) {
206
    parent::form($form, $form_state, $options);
207
    $settings = $this->element->settings;
208

    
209
    $form['item'] = array(
210
      '#type' => 'fieldset',
211
      '#title' => t('Current list item'),
212
      '#description' => t('The variable used for holding each list item in the loop. This variable will be available inside the loop only.'),
213
      '#tree' => TRUE,
214
    );
215
    $form['item']['label'] = array(
216
      '#type' => 'textfield',
217
      '#title' => t('Variable label'),
218
      '#default_value' => $settings['item:label'],
219
      '#required' => TRUE,
220
    );
221
    $form['item']['var'] = array(
222
      '#type' => 'textfield',
223
      '#title' => t('Variable name'),
224
      '#default_value' => $settings['item:var'],
225
      '#description' => t('The variable name must contain only lowercase letters, numbers, and underscores and must be unique in the current scope.'),
226
      '#element_validate' => array('rules_ui_element_machine_name_validate'),
227
      '#required' => TRUE,
228
    );
229
  }
230

    
231
  public function form_extract_values($form, &$form_state) {
232
    parent::form_extract_values($form, $form_state);
233
    $form_values = RulesPluginUI::getFormStateValues($form, $form_state);
234

    
235
    $this->element->settings['item:var'] = $form_values['item']['var'];
236
    $this->element->settings['item:label'] = $form_values['item']['label'];
237
  }
238

    
239
  public function form_validate($form, &$form_state) {
240
    parent::form_validate($form, $form_state);
241

    
242
    $vars = $this->element->availableVariables();
243
    $name = $this->element->settings['item:var'];
244
    if (isset($vars[$name])) {
245
      form_error($form['item']['var'], t('The variable name %name is already taken.', array('%name' => $name)));
246
    }
247
  }
248

    
249
  public function buildContent() {
250
    $content = parent::buildContent();
251

    
252
    $content['description']['item'] = array(
253
      '#caption' => t('List item'),
254
      '#theme' => 'rules_content_group',
255
    );
256
    $content['description']['item']['var'] = array(
257
      '#theme' => 'rules_variable_view',
258
      '#info' => $this->element->listItemInfo(),
259
      '#name' => $this->element->settings['item:var'],
260
    );
261
    return $content;
262
  }
263

    
264
}