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