data.eval.inc 14 KB

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