data.eval.inc 13 KB

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