data.eval.inc 13 KB

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