1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Contains rules integration for the data module needed during evaluation.
|
6
|
*
|
7
|
* @addtogroup rules
|
8
|
*
|
9
|
* @{
|
10
|
*/
|
11
|
|
12
|
/**
|
13
|
* Action: Modify data.
|
14
|
*/
|
15
|
function rules_action_data_set($wrapper, $value, $settings, $state, $element) {
|
16
|
if ($wrapper instanceof EntityMetadataWrapper) {
|
17
|
try {
|
18
|
// Update the value first then save changes, if possible.
|
19
|
$wrapper->set($value);
|
20
|
}
|
21
|
catch (EntityMetadataWrapperException $e) {
|
22
|
throw new RulesEvaluationException('Unable to modify data "@selector": ' . $e->getMessage(), array('@selector' => $settings['data:select']));
|
23
|
}
|
24
|
// Save changes if a property of a variable has been changed.
|
25
|
if (strpos($element->settings['data:select'], ':') !== FALSE) {
|
26
|
$info = $wrapper->info();
|
27
|
// We always have to save the changes in the parent entity. E.g. when the
|
28
|
// node author is changed, we don't want to save the author but the node.
|
29
|
$state->saveChanges(implode(':', explode(':', $settings['data:select'], -1)), $info['parent']);
|
30
|
}
|
31
|
}
|
32
|
else {
|
33
|
// A not wrapped variable (e.g. a number) is being updated. Just overwrite
|
34
|
// the variable with the new value.
|
35
|
return array('data' => $value);
|
36
|
}
|
37
|
}
|
38
|
|
39
|
/**
|
40
|
* Info alter callback for the data_set action.
|
41
|
*/
|
42
|
function rules_action_data_set_info_alter(&$element_info, $element) {
|
43
|
$element->settings += array('data:select' => NULL);
|
44
|
if ($wrapper = $element->applyDataSelector($element->settings['data:select'])) {
|
45
|
$info = $wrapper->info();
|
46
|
$element_info['parameter']['value']['type'] = $wrapper->type();
|
47
|
$element_info['parameter']['value']['options list'] = !empty($info['options list']) ? 'rules_data_selector_options_list' : FALSE;
|
48
|
}
|
49
|
}
|
50
|
|
51
|
/**
|
52
|
* Action: Calculate a value.
|
53
|
*/
|
54
|
function rules_action_data_calc($input1, $op, $input2, $settings, $state, $element) {
|
55
|
$info = $element->pluginParameterInfo();
|
56
|
// Make sure to apply date offsets intelligently.
|
57
|
if ($info['input_1']['type'] == 'date' && $info['input_2']['type'] == 'duration') {
|
58
|
$input2 = ($op == '-') ? $input2 * -1 : $input2;
|
59
|
return array('result' => (int) RulesDateOffsetProcessor::applyOffset($input1, $input2));
|
60
|
}
|
61
|
|
62
|
switch ($op) {
|
63
|
case '+':
|
64
|
$result = $input1 + $input2;
|
65
|
break;
|
66
|
|
67
|
case '-':
|
68
|
$result = $input1 - $input2;
|
69
|
break;
|
70
|
|
71
|
case '*':
|
72
|
$result = $input1 * $input2;
|
73
|
break;
|
74
|
|
75
|
case '/':
|
76
|
$result = $input1 / $input2;
|
77
|
break;
|
78
|
|
79
|
case 'min':
|
80
|
$result = min($input1, $input2);
|
81
|
break;
|
82
|
|
83
|
case 'max':
|
84
|
$result = max($input1, $input2);
|
85
|
break;
|
86
|
}
|
87
|
if (isset($result)) {
|
88
|
// Ensure results are valid integer values if necessary.
|
89
|
$variables = $element->providesVariables();
|
90
|
$var_info = reset($variables);
|
91
|
if ($var_info['type'] == 'integer') {
|
92
|
$result = (int) $result;
|
93
|
}
|
94
|
return array('result' => $result);
|
95
|
}
|
96
|
}
|
97
|
|
98
|
/**
|
99
|
* Info alter callback for the data_calc action.
|
100
|
*/
|
101
|
function rules_action_data_calc_info_alter(&$element_info, RulesPlugin $element) {
|
102
|
if ($info = $element->getArgumentInfo('input_1')) {
|
103
|
// Only allow durations as offset for date values.
|
104
|
if ($info['type'] == 'date') {
|
105
|
$element_info['parameter']['input_2']['type'] = 'duration';
|
106
|
}
|
107
|
// Specify the data type of the result.
|
108
|
$element_info['provides']['result']['type'] = $info['type'];
|
109
|
|
110
|
if ($info['type'] == 'integer' && ($info2 = $element->getArgumentInfo('input_2')) && $info2['type'] == 'decimal') {
|
111
|
$element_info['provides']['result']['type'] = 'decimal';
|
112
|
}
|
113
|
// A division with two integers results in a decimal.
|
114
|
elseif (isset($element->settings['op']) && $element->settings['op'] == '/') {
|
115
|
$element_info['provides']['result']['type'] = 'decimal';
|
116
|
}
|
117
|
}
|
118
|
}
|
119
|
|
120
|
/**
|
121
|
* Action: Add a list item.
|
122
|
*/
|
123
|
function rules_action_data_list_add($list, $item, $unique = FALSE, $pos = 'end', $settings, $state) {
|
124
|
// Optionally, only add the list item if it is not yet contained.
|
125
|
if ($unique && rules_condition_data_list_contains($list, $item, $settings, $state)) {
|
126
|
return;
|
127
|
}
|
128
|
|
129
|
switch ($pos) {
|
130
|
case 'start':
|
131
|
array_unshift($list, $item);
|
132
|
break;
|
133
|
|
134
|
default:
|
135
|
$list[] = $item;
|
136
|
break;
|
137
|
}
|
138
|
return array('list' => $list);
|
139
|
}
|
140
|
|
141
|
/**
|
142
|
* Info alteration callback for the "Add and Remove a list item" actions.
|
143
|
*/
|
144
|
function rules_data_list_info_alter(&$element_info, RulesAbstractPlugin $element) {
|
145
|
// Update the required type for the list item if it is known.
|
146
|
$element->settings += array('list:select' => NULL);
|
147
|
if ($wrapper = $element->applyDataSelector($element->settings['list:select'])) {
|
148
|
if ($type = entity_property_list_extract_type($wrapper->type())) {
|
149
|
$info = $wrapper->info();
|
150
|
$element_info['parameter']['item']['type'] = $type;
|
151
|
$element_info['parameter']['item']['options list'] = !empty($info['options list']) ? 'rules_data_selector_options_list' : FALSE;
|
152
|
}
|
153
|
}
|
154
|
}
|
155
|
|
156
|
/**
|
157
|
* Action: Remove a list item.
|
158
|
*/
|
159
|
function rules_action_data_list_remove($list, $item) {
|
160
|
foreach (array_keys($list, $item) as $key) {
|
161
|
unset($list[$key]);
|
162
|
}
|
163
|
return array('list' => $list);
|
164
|
}
|
165
|
|
166
|
/**
|
167
|
* Action: Add variable.
|
168
|
*/
|
169
|
function rules_action_variable_add($args, $element) {
|
170
|
return array('variable_added' => $args['value']);
|
171
|
}
|
172
|
|
173
|
/**
|
174
|
* Info alteration callback for variable add action.
|
175
|
*/
|
176
|
function rules_action_variable_add_info_alter(&$element_info, RulesAbstractPlugin $element) {
|
177
|
if (isset($element->settings['type']) && $type = $element->settings['type']) {
|
178
|
$cache = rules_get_cache();
|
179
|
$type_info = $cache['data_info'][$type];
|
180
|
$element_info['parameter']['value']['type'] = $type;
|
181
|
$element_info['provides']['variable_added']['type'] = $type;
|
182
|
|
183
|
// For lists, we default to an empty list so subsequent actions can add
|
184
|
// items.
|
185
|
if (entity_property_list_extract_type($type)) {
|
186
|
$element_info['parameter']['value']['default value'] = array();
|
187
|
}
|
188
|
}
|
189
|
}
|
190
|
|
191
|
/**
|
192
|
* Action: Convert a value.
|
193
|
*/
|
194
|
function rules_action_data_convert($arguments, RulesPlugin $element, $state) {
|
195
|
|
196
|
$value_info = $element->getArgumentInfo('value');
|
197
|
$from_type = $value_info['type'];
|
198
|
$target_type = $arguments['type'];
|
199
|
|
200
|
// First apply the rounding behavior if given.
|
201
|
if (isset($arguments['rounding_behavior'])) {
|
202
|
switch ($arguments['rounding_behavior']) {
|
203
|
case 'up':
|
204
|
$arguments['value'] = ceil($arguments['value']);
|
205
|
break;
|
206
|
|
207
|
case 'down':
|
208
|
$arguments['value'] = floor($arguments['value']);
|
209
|
break;
|
210
|
|
211
|
default:
|
212
|
case 'round':
|
213
|
$arguments['value'] = round($arguments['value']);
|
214
|
break;
|
215
|
}
|
216
|
}
|
217
|
|
218
|
switch ($target_type) {
|
219
|
case 'decimal':
|
220
|
$result = floatval($arguments['value']);
|
221
|
break;
|
222
|
|
223
|
case 'integer':
|
224
|
$result = intval($arguments['value']);
|
225
|
break;
|
226
|
|
227
|
case 'text':
|
228
|
$result = strval($arguments['value']);
|
229
|
break;
|
230
|
|
231
|
case 'token':
|
232
|
$result = strval($arguments['value']);
|
233
|
break;
|
234
|
}
|
235
|
|
236
|
return array('conversion_result' => $result);
|
237
|
}
|
238
|
|
239
|
/**
|
240
|
* Info alteration callback for variable add action.
|
241
|
*/
|
242
|
function rules_action_data_convert_info_alter(&$element_info, RulesAbstractPlugin $element) {
|
243
|
|
244
|
if (isset($element->settings['type']) && $type = $element->settings['type']) {
|
245
|
$element_info['provides']['conversion_result']['type'] = $type;
|
246
|
|
247
|
// Only support the rounding behavior option for integers.
|
248
|
if ($type == 'integer') {
|
249
|
$element_info['parameter']['rounding_behavior'] = array(
|
250
|
'type' => 'token',
|
251
|
'label' => t('Rounding behavior'),
|
252
|
'description' => t('The rounding behavior the conversion should use.'),
|
253
|
'options list' => 'rules_action_data_convert_rounding_behavior_options',
|
254
|
'restriction' => 'input',
|
255
|
'default value' => 'round',
|
256
|
'optional' => TRUE,
|
257
|
);
|
258
|
}
|
259
|
else {
|
260
|
unset($element_info['parameter']['rounding_behavior']);
|
261
|
}
|
262
|
|
263
|
// Configure compatible source-types:
|
264
|
switch ($type) {
|
265
|
case 'integer':
|
266
|
$sources = array('decimal', 'text', 'token', 'uri', 'date', 'duration', 'boolean');
|
267
|
break;
|
268
|
|
269
|
case 'decimal':
|
270
|
$sources = array('integer', 'text', 'token', 'uri', 'date', 'duration', 'boolean');
|
271
|
break;
|
272
|
|
273
|
case 'text':
|
274
|
$sources = array('integer', 'decimal', 'token', 'uri', 'date', 'duration', 'boolean');
|
275
|
break;
|
276
|
|
277
|
case 'token':
|
278
|
$sources = array('integer', 'decimal', 'text', 'uri', 'date', 'duration', 'boolean');
|
279
|
break;
|
280
|
}
|
281
|
$element_info['parameter']['value']['type'] = $sources;
|
282
|
}
|
283
|
}
|
284
|
|
285
|
/**
|
286
|
* Action: Create data.
|
287
|
*/
|
288
|
function rules_action_data_create($args, $element) {
|
289
|
$type = $args['type'];
|
290
|
$values = array();
|
291
|
foreach ($element->pluginParameterInfo() as $name => $info) {
|
292
|
if ($name != 'type') {
|
293
|
// Remove the parameter name prefix 'param_'.
|
294
|
$values[substr($name, 6)] = $args[$name];
|
295
|
}
|
296
|
}
|
297
|
$cache = rules_get_cache();
|
298
|
$type_info = $cache['data_info'][$type];
|
299
|
if (isset($type_info['creation callback'])) {
|
300
|
try {
|
301
|
$data = $type_info['creation callback']($values, $type);
|
302
|
return array('data_created' => $data);
|
303
|
}
|
304
|
catch (EntityMetadataWrapperException $e) {
|
305
|
throw new RulesEvaluationException('Unable to create @data": ' . $e->getMessage(), array('@data' => $type), $element);
|
306
|
}
|
307
|
}
|
308
|
else {
|
309
|
throw new RulesEvaluationException('Unable to create @data, no creation callback found.', array('@data' => $type), $element, RulesLog::ERROR);
|
310
|
}
|
311
|
}
|
312
|
|
313
|
/**
|
314
|
* Info alteration callback for data create action.
|
315
|
*/
|
316
|
function rules_action_data_create_info_alter(&$element_info, RulesAbstractPlugin $element) {
|
317
|
if (!empty($element->settings['type'])) {
|
318
|
$type = $element->settings['type'];
|
319
|
$cache = rules_get_cache();
|
320
|
$type_info = $cache['data_info'][$type];
|
321
|
if (isset($type_info['property info'])) {
|
322
|
// Add the data type's properties as parameters.
|
323
|
foreach ($type_info['property info'] as $property => $property_info) {
|
324
|
// Prefix parameter names to avoid name clashes with
|
325
|
// existing parameters.
|
326
|
$element_info['parameter']['param_' . $property] = array_intersect_key($property_info, array_flip(array('type', 'label', 'allow null')));
|
327
|
if (empty($property_info['required'])) {
|
328
|
$element_info['parameter']['param_' . $property]['optional'] = TRUE;
|
329
|
$element_info['parameter']['param_' . $property]['allow null'] = TRUE;
|
330
|
}
|
331
|
}
|
332
|
}
|
333
|
$element_info['provides']['data_created']['type'] = $type;
|
334
|
}
|
335
|
}
|
336
|
|
337
|
/**
|
338
|
* Creation callback for array structured data.
|
339
|
*/
|
340
|
function rules_action_data_create_array($values = array(), $type) {
|
341
|
// $values is an array already, so we can just pass it to the wrapper.
|
342
|
return rules_wrap_data($values, array('type' => $type));
|
343
|
}
|
344
|
|
345
|
/**
|
346
|
* Condition: Compare data.
|
347
|
*/
|
348
|
function rules_condition_data_is($data, $op, $value) {
|
349
|
switch ($op) {
|
350
|
default:
|
351
|
case '==':
|
352
|
// In case both values evaluate to FALSE, further differentiate between
|
353
|
// NULL values and values evaluating to FALSE.
|
354
|
if (!$data && !$value) {
|
355
|
return (isset($data) && isset($value)) || (!isset($data) && !isset($value));
|
356
|
}
|
357
|
return $data == $value;
|
358
|
|
359
|
case '<':
|
360
|
return $data < $value;
|
361
|
|
362
|
case '>':
|
363
|
return $data > $value;
|
364
|
|
365
|
// Note: This is deprecated by the text comparison condition and IN below.
|
366
|
case 'contains':
|
367
|
return is_string($data) && strpos($data, $value) !== FALSE || is_array($data) && in_array($value, $data);
|
368
|
|
369
|
case 'IN':
|
370
|
return is_array($value) && in_array($data, $value);
|
371
|
}
|
372
|
}
|
373
|
|
374
|
/**
|
375
|
* Info alteration callback for the data_is condition.
|
376
|
*
|
377
|
* If we check the bundle property of a variable, add an assertion so that later
|
378
|
* evaluated elements can make use of this information.
|
379
|
*/
|
380
|
function rules_condition_data_is_info_alter(&$element_info, RulesAbstractPlugin $element) {
|
381
|
$element->settings += array('data:select' => NULL, 'op' => '==');
|
382
|
if ($wrapper = $element->applyDataSelector($element->settings['data:select'])) {
|
383
|
$info = $wrapper->info();
|
384
|
$element_info['parameter']['value']['type'] = $element->settings['op'] == 'IN' ? 'list<' . $wrapper->type() . '>' : $wrapper->type();
|
385
|
$element_info['parameter']['value']['options list'] = !empty($info['options list']) ? 'rules_data_selector_options_list' : FALSE;
|
386
|
}
|
387
|
}
|
388
|
|
389
|
/**
|
390
|
* Condition: List contains.
|
391
|
*/
|
392
|
function rules_condition_data_list_contains($list, $item, $settings, $state) {
|
393
|
$wrapper = $state->currentArguments['item'];
|
394
|
if ($wrapper instanceof EntityStructureWrapper && $id = $wrapper->getIdentifier()) {
|
395
|
// Check for equal items using the identifier if there is one.
|
396
|
foreach ($state->currentArguments['list'] as $i) {
|
397
|
if ($i->getIdentifier() == $id) {
|
398
|
return TRUE;
|
399
|
}
|
400
|
}
|
401
|
return FALSE;
|
402
|
}
|
403
|
return in_array($item, $list);
|
404
|
}
|
405
|
|
406
|
/**
|
407
|
* Condition: List count comparison.
|
408
|
*/
|
409
|
function rules_condition_data_list_count_is($list, $op = '==', $value) {
|
410
|
switch ($op) {
|
411
|
case '==':
|
412
|
return count($list) == $value;
|
413
|
|
414
|
case '<':
|
415
|
return count($list) < $value;
|
416
|
|
417
|
case '>':
|
418
|
return count($list) > $value;
|
419
|
}
|
420
|
}
|
421
|
|
422
|
/**
|
423
|
* Condition: Data value is empty.
|
424
|
*/
|
425
|
function rules_condition_data_is_empty($data) {
|
426
|
// Note that some primitive variables might not be wrapped at all.
|
427
|
if ($data instanceof EntityMetadataWrapper) {
|
428
|
try {
|
429
|
// We cannot use the dataAvailable() method from the wrapper because it
|
430
|
// is protected, so we catch possible exceptions with the value() method.
|
431
|
$value = $data->value();
|
432
|
return empty($value);
|
433
|
}
|
434
|
catch (EntityMetadataWrapperException $e) {
|
435
|
// An exception means that the wrapper is somehow broken and we treat
|
436
|
// that as empty.
|
437
|
return TRUE;
|
438
|
}
|
439
|
}
|
440
|
return empty($data);
|
441
|
}
|
442
|
|
443
|
/**
|
444
|
* Condition: Textual comparison.
|
445
|
*/
|
446
|
function rules_data_text_comparison($text, $text2, $op = 'contains') {
|
447
|
switch ($op) {
|
448
|
case 'contains':
|
449
|
return strpos($text, $text2) !== FALSE;
|
450
|
|
451
|
case 'starts':
|
452
|
return strpos($text, $text2) === 0;
|
453
|
|
454
|
case 'ends':
|
455
|
return strrpos($text, $text2) === (strlen($text) - strlen($text2));
|
456
|
|
457
|
case 'regex':
|
458
|
return (bool) preg_match('/' . str_replace('/', '\\/', $text2) . '/', $text);
|
459
|
}
|
460
|
}
|
461
|
|
462
|
/**
|
463
|
* @}
|
464
|
*/
|