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