data.rules.inc 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  1. <?php
  2. /**
  3. * @file General data related rules integration
  4. *
  5. * @addtogroup rules
  6. * @{
  7. */
  8. /**
  9. * Implements hook_rules_file_info() on behalf of the pseudo data module.
  10. * @see rules_core_modules()
  11. */
  12. function rules_data_file_info() {
  13. return array('modules/data.eval');
  14. }
  15. /**
  16. * Implements hook_rules_action_info() on behalf of the pseudo data module.
  17. * @see rules_core_modules()
  18. */
  19. function rules_data_action_info() {
  20. $return['data_set'] = array(
  21. 'label' => t('Set a data value'),
  22. 'parameter' => array(
  23. 'data' => array(
  24. 'type' => '*',
  25. 'label' => t('Data'),
  26. 'description' => t('Specifies the data to be modified using a data selector, e.g. "node:author:name".'),
  27. 'restriction' => 'selector',
  28. 'wrapped' => TRUE,
  29. 'allow null' => TRUE,
  30. ),
  31. 'value' => array(
  32. 'type' => '*',
  33. 'label' => t('Value'),
  34. 'description' => t('The new value to set for the specified data.'),
  35. 'allow null' => TRUE,
  36. 'optional' => TRUE,
  37. ),
  38. ),
  39. 'group' => t('Data'),
  40. 'base' => 'rules_action_data_set',
  41. );
  42. $return['data_calc'] = array(
  43. 'label' => t('Calculate a value'),
  44. 'parameter' => array(
  45. 'input_1' => array(
  46. 'type' => array('decimal', 'date'),
  47. 'label' => t('Input value 1'),
  48. 'description' => t('The first input value for the calculation.'),
  49. ),
  50. 'op' => array(
  51. 'type' => 'text',
  52. 'label' => t('Operator'),
  53. 'description' => t('The calculation operator.'),
  54. 'options list' => 'rules_action_data_calc_operator_options',
  55. 'restriction' => 'input',
  56. 'default value' => '+',
  57. ),
  58. 'input_2' => array(
  59. 'type' => 'decimal',
  60. 'label' => t('Input value 2'),
  61. 'description' => t('The second input value.'),
  62. ),
  63. ),
  64. 'group' => t('Data'),
  65. 'base' => 'rules_action_data_calc',
  66. 'provides' => array(
  67. 'result' => array(
  68. 'type' => 'unknown',
  69. 'label' => t('Calculation result'),
  70. ),
  71. ),
  72. );
  73. $return['list_add'] = array(
  74. 'label' => t('Add an item to a list'),
  75. 'parameter' => array(
  76. 'list' => array(
  77. 'type' => 'list',
  78. 'label' => t('List', array(), array('context' => 'data_types')),
  79. 'description' => t('The data list, to which an item is to be added.'),
  80. 'restriction' => 'selector',
  81. 'allow null' => TRUE,
  82. 'save' => TRUE,
  83. ),
  84. 'item' => array(
  85. 'type' => 'unknown',
  86. 'label' => t('Item to add'),
  87. ),
  88. 'unique' => array(
  89. 'type' => 'boolean',
  90. 'label' => t('Enforce uniqueness'),
  91. 'description' => t('Only add the item to the list if it is not yet contained.'),
  92. 'optional' => TRUE,
  93. 'default value' => FALSE,
  94. ),
  95. 'pos' => array(
  96. 'type' => 'text',
  97. 'label' => t('Insert position'),
  98. 'optional' => TRUE,
  99. 'default value' => 'end',
  100. 'options list' => 'rules_action_data_list_add_positions',
  101. ),
  102. ),
  103. 'group' => t('Data'),
  104. 'base' => 'rules_action_data_list_add',
  105. 'callbacks' => array(
  106. 'info_alter' => 'rules_data_list_info_alter',
  107. 'form_alter' => 'rules_data_list_form_alter',
  108. ),
  109. );
  110. $return['list_remove'] = array(
  111. 'label' => t('Remove an item from a list'),
  112. 'parameter' => array(
  113. 'list' => array(
  114. 'type' => 'list',
  115. 'label' => t('List', array(), array('context' => 'data_types')),
  116. 'description' => t('The data list for which an item is to be removed.'),
  117. 'restriction' => 'selector',
  118. 'save' => TRUE,
  119. ),
  120. 'item' => array(
  121. 'type' => 'unknown',
  122. 'label' => t('Item to remove'),
  123. ),
  124. ),
  125. 'group' => t('Data'),
  126. 'base' => 'rules_action_data_list_remove',
  127. 'callbacks' => array(
  128. 'info_alter' => 'rules_data_list_info_alter',
  129. 'form_alter' => 'rules_data_list_form_alter',
  130. ),
  131. );
  132. $return['variable_add'] = array(
  133. 'label' => t('Add a variable'),
  134. 'named parameter' => TRUE,
  135. 'parameter' => array(
  136. 'type' => array(
  137. 'type' => 'text',
  138. 'label' => t('Type'),
  139. 'options list' => 'rules_data_action_variable_add_options',
  140. 'description' => t('Specifies the type of the variable that should be added.'),
  141. 'restriction' => 'input',
  142. ),
  143. 'value' => array(
  144. 'type' => 'unknown',
  145. 'label' => t('Value'),
  146. 'optional' => TRUE,
  147. 'description' => t('Optionally, specify the initial value of the variable.')
  148. ),
  149. ),
  150. 'provides' => array(
  151. 'variable_added' => array(
  152. 'type' => 'unknown',
  153. 'label' => t('Added variable'),
  154. ),
  155. ),
  156. 'group' => t('Data'),
  157. 'base' => 'rules_action_variable_add',
  158. 'callbacks' => array(
  159. 'form_alter' => 'rules_action_type_form_alter',
  160. 'validate' => 'rules_action_create_type_validate',
  161. ),
  162. );
  163. if (rules_data_action_data_create_options()) {
  164. $return['data_create'] = array(
  165. 'label' => t('Create a data structure'),
  166. 'named parameter' => TRUE,
  167. 'parameter' => array(
  168. 'type' => array(
  169. 'type' => 'text',
  170. 'label' => t('Type'),
  171. 'options list' => 'rules_data_action_data_create_options',
  172. 'description' => t('Specifies the type of the data structure that should be created.'),
  173. 'restriction' => 'input',
  174. ),
  175. // Further needed parameters depend on the type.
  176. ),
  177. 'provides' => array(
  178. 'data_created' => array(
  179. 'type' => 'unknown',
  180. 'label' => t('Created data'),
  181. ),
  182. ),
  183. 'group' => t('Data'),
  184. 'base' => 'rules_action_data_create',
  185. 'callbacks' => array(
  186. 'form_alter' => 'rules_action_type_form_alter',
  187. 'validate' => 'rules_action_create_type_validate',
  188. ),
  189. );
  190. }
  191. $return['data_convert'] = array(
  192. 'label' => t('Convert data type'),
  193. 'parameter' => array(
  194. 'type' => array(
  195. 'type' => 'token',
  196. 'label' => t('Target type'),
  197. 'description' => t('The data type to convert a value to.'),
  198. 'options list' => 'rules_action_data_convert_types_options',
  199. 'restriction' => 'input',
  200. ),
  201. 'value' => array(
  202. 'type' => array('decimal', 'integer', 'text'),
  203. 'label' => t('Value to convert'),
  204. 'default mode' => 'selector',
  205. ),
  206. // For to-integer conversion only.
  207. 'rounding_behavior' => array(
  208. 'type' => 'token',
  209. 'label' => t('Rounding behavior'),
  210. 'description' => t('The rounding behavior the conversion should use.'),
  211. 'options list' => 'rules_action_data_convert_rounding_behavior_options',
  212. 'restriction' => 'input',
  213. 'default value' => 'round',
  214. 'optional' => TRUE,
  215. ),
  216. ),
  217. 'provides' => array(
  218. 'conversion_result' => array(
  219. 'type' => 'unknown',
  220. 'label' => t('Conversion result'),
  221. ),
  222. ),
  223. 'group' => t('Data'),
  224. 'base' => 'rules_action_data_convert',
  225. 'named parameter' => TRUE,
  226. 'callbacks' => array(
  227. 'form_alter' => 'rules_action_type_form_alter',
  228. ),
  229. );
  230. return $return;
  231. }
  232. /**
  233. * Data conversation action: Options list callback for the target type.
  234. */
  235. function rules_action_data_convert_types_options(RulesPlugin $element, $param_name) {
  236. return array(
  237. 'decimal' => t('Decimal'),
  238. 'integer' => t('Integer'),
  239. 'text' => t('Text'),
  240. );
  241. }
  242. /**
  243. * Data conversation action: Options list callback for rounding behavior.
  244. */
  245. function rules_action_data_convert_rounding_behavior_options(RulesPlugin $element, $param_name) {
  246. return array(
  247. 'down' => t('Always down (9.5 -> 9)'),
  248. 'round' => t('Round, half up (9.5 -> 10)'),
  249. 'up' => t('Always up (9.5 -> 10)'),
  250. );
  251. }
  252. /**
  253. * Customize access check for data set action.
  254. */
  255. function rules_action_data_set_access(RulesAbstractPlugin $element) {
  256. if (isset($element->settings['data:select']) && $wrapper = $element->applyDataSelector($element->settings['data:select'])) {
  257. return $wrapper instanceof EntityMetadataWrapper && $wrapper->access('edit');
  258. }
  259. }
  260. /**
  261. * Custom validation callback for the data set action.
  262. */
  263. function rules_action_data_set_validate(RulesAbstractPlugin $element) {
  264. $element->settings += array('data:select' => NULL);
  265. $info = $element->applyDataSelector($element->settings['data:select'])->info();
  266. if (strpos($element->settings['data:select'], ':') !== FALSE && empty($info['setter callback'])) {
  267. throw new RulesIntegrityException(t("The selected data property doesn't support writing."), array($element, 'parameter', 'data'));
  268. }
  269. }
  270. /**
  271. * Form alter callback for the data_set action.
  272. */
  273. function rules_action_data_set_form_alter(&$form, &$form_state, $options, RulesAbstractPlugin $element) {
  274. if (!empty($options['init']) && !isset($form_state['rules_element_step'])) {
  275. $form['negate']['#access'] = FALSE;
  276. unset($form['parameter']['value']);
  277. unset($form['parameter']['language']);
  278. $form['submit'] = array(
  279. '#type' => 'submit',
  280. '#value' => t('Continue'),
  281. '#limit_validation_errors' => array(array('parameter', 'data')),
  282. '#submit' => array('rules_form_submit_rebuild'),
  283. );
  284. $form_state['rules_element_step'] = 'data_value';
  285. // Clear the parameter mode for the value parameter, so its gets the proper
  286. // default value based upon the type of the the selected data on rebuild.
  287. unset($form_state['parameter_mode']['value']);
  288. }
  289. else {
  290. // Change the data parameter to be not editable.
  291. $form['parameter']['data']['settings']['#access'] = FALSE;
  292. // TODO: improve display
  293. $form['parameter']['data']['info'] = array(
  294. '#prefix' => '<p>',
  295. '#markup' => t('<strong>Selected data:</strong> %selector', array('%selector' => $element->settings['data:select'])),
  296. '#suffix' => '</p>',
  297. );
  298. }
  299. }
  300. /**
  301. * Form alter callback for the data calculation action.
  302. */
  303. function rules_action_data_calc_form_alter(&$form, &$form_state, $options, RulesAbstractPlugin $element) {
  304. $form['reload'] = array(
  305. '#weight' => 5,
  306. '#type' => 'submit',
  307. '#name' => 'reload',
  308. '#value' => t('Reload form'),
  309. '#limit_validation_errors' => array(array('parameter', 'input_1')),
  310. '#submit' => array('rules_form_submit_rebuild'),
  311. '#ajax' => rules_ui_form_default_ajax(),
  312. );
  313. }
  314. /**
  315. * Custom validate callback for entity create, add variable and data create
  316. * action.
  317. */
  318. function rules_action_create_type_validate($element) {
  319. if (!isset($element->settings['type'])) {
  320. throw new RulesIntegrityException(t('Invalid type specified.'), array($element, 'parameter', 'type'));
  321. }
  322. }
  323. /**
  324. * Form alter callback for the list add and remove actions.
  325. *
  326. * Use multiple steps to configure the action to update the item configuration
  327. * form once we know the data type.
  328. *
  329. * @see rules_data_list_info_alter()
  330. */
  331. function rules_data_list_form_alter(&$form, &$form_state, $options, RulesAbstractPlugin $element) {
  332. if (!empty($options['init']) && !isset($form_state['rules_element_step'])) {
  333. unset($form['parameter']['item'], $form['parameter']['pos']);
  334. $form_state['rules_element_step'] = 1;
  335. $form['negate']['#access'] = FALSE;
  336. $form['parameter']['unique']['#access'] = FALSE;
  337. $form['submit'] = array(
  338. '#type' => 'submit',
  339. '#value' => t('Continue'),
  340. '#limit_validation_errors' => array(array('parameter', 'list')),
  341. '#submit' => array('rules_form_submit_rebuild'),
  342. );
  343. }
  344. else {
  345. // Change the list parameter to be not editable any more.
  346. $form['parameter']['list']['settings']['#access'] = FALSE;
  347. $form['parameter']['list']['info'] = array(
  348. '#prefix' => '<p>',
  349. '#markup' => t('<strong>Selected list:</strong> %selector', array('%selector' => $element->settings['list:select'])),
  350. '#suffix' => '</p>',
  351. );
  352. }
  353. }
  354. /**
  355. * Form alter callback for actions relying on the entity type or the data type.
  356. */
  357. function rules_action_type_form_alter(&$form, &$form_state, $options, RulesAbstractPlugin $element) {
  358. $first_step = empty($element->settings['type']);
  359. $form['reload'] = array(
  360. '#weight' => 5,
  361. '#type' => 'submit',
  362. '#name' => 'reload',
  363. '#value' => $first_step ? t('Continue') : t('Reload form'),
  364. '#limit_validation_errors' => array(array('parameter', 'type')),
  365. '#submit' => array('rules_action_type_form_submit_rebuild'),
  366. '#ajax' => rules_ui_form_default_ajax(),
  367. );
  368. // Use ajax and trigger as the reload button.
  369. $form['parameter']['type']['settings']['type']['#ajax'] = $form['reload']['#ajax'] + array(
  370. 'event' => 'change',
  371. 'trigger_as' => array('name' => 'reload'),
  372. );
  373. if ($first_step) {
  374. // In the first step show only the type select.
  375. foreach (element_children($form['parameter']) as $key) {
  376. if ($key != 'type') {
  377. unset($form['parameter'][$key]);
  378. }
  379. }
  380. unset($form['submit']);
  381. unset($form['provides']);
  382. // Disable #ajax for the first step as it has troubles with lazy-loaded JS.
  383. // @todo: Re-enable once JS lazy-loading is fixed in core.
  384. unset($form['parameter']['type']['settings']['type']['#ajax']);
  385. unset($form['reload']['#ajax']);
  386. }
  387. else {
  388. // Hide the reload button in case js is enabled and it's not the first step.
  389. $form['reload']['#attributes'] = array('class' => array('rules-hide-js'));
  390. }
  391. }
  392. /**
  393. * FAPI submit callback for reloading the type form for entities or data types.
  394. */
  395. function rules_action_type_form_submit_rebuild($form, &$form_state) {
  396. rules_form_submit_rebuild($form, $form_state);
  397. // Clear the parameter modes for the parameters, so they get the proper
  398. // default values based upon the data types on rebuild.
  399. $form_state['parameter_mode'] = array();
  400. }
  401. /**
  402. * Options list callback for possible insertion positions.
  403. */
  404. function rules_action_data_list_add_positions() {
  405. return array(
  406. 'end' => t('Append the item to the end.'),
  407. 'start' => t('Prepend the item to the front.'),
  408. );
  409. }
  410. /**
  411. * Options list callback for variable add action.
  412. */
  413. function rules_data_action_variable_add_options() {
  414. return RulesPluginUI::getOptions('data');
  415. }
  416. /**
  417. * Options list callback for the data calculation action.
  418. */
  419. function rules_action_data_calc_operator_options(RulesPlugin $element, $param_name) {
  420. $options = array(
  421. '+' => '( + )',
  422. '-' => '( - )',
  423. '*' => '( * )',
  424. '/' => '( / )',
  425. );
  426. // Only show +/- in case a date has been selected.
  427. if (($info = $element->getArgumentInfo('input_1')) && $info['type'] == 'date') {
  428. unset($options['*']);
  429. unset($options['/']);
  430. }
  431. return $options;
  432. }
  433. /**
  434. * Options list callback for data create action.
  435. */
  436. function rules_data_action_data_create_options() {
  437. $cache = rules_get_cache();
  438. $data_info = $cache['data_info'];
  439. $entity_info = entity_get_info();
  440. // Remove entities.
  441. $data_info = array_diff_key($data_info, $entity_info);
  442. $options = array();
  443. foreach ($data_info as $type => $properties) {
  444. if (isset($properties['creation callback'])) {
  445. // Add data types with creation callback only.
  446. $options[$type] = $properties['label'];
  447. }
  448. }
  449. natcasesort($options);
  450. return $options;
  451. }
  452. /**
  453. * Implements hook_rules_condition_info() on behalf of the pseudo data module.
  454. * @see rules_core_modules()
  455. */
  456. function rules_data_condition_info() {
  457. return array(
  458. 'data_is' => array(
  459. 'label' => t('Data comparison'),
  460. 'parameter' => array(
  461. 'data' => array(
  462. 'type' => '*',
  463. 'label' => t('Data to compare'),
  464. 'description' => t('The data to be compared, specified by using a data selector, e.g. "node:author:name".'),
  465. 'allow null' => TRUE,
  466. ),
  467. 'op' => array(
  468. 'type' => 'text',
  469. 'label' => t('Operator'),
  470. 'description' => t('The comparison operator.'),
  471. 'optional' => TRUE,
  472. 'default value' => '==',
  473. 'options list' => 'rules_condition_data_is_operator_options',
  474. 'restriction' => 'input',
  475. ),
  476. 'value' => array(
  477. 'type' => '*',
  478. 'label' => t('Data value'),
  479. 'description' => t('The value to compare the data with.'),
  480. 'allow null' => TRUE,
  481. ),
  482. ),
  483. 'group' => t('Data'),
  484. 'base' => 'rules_condition_data_is',
  485. ),
  486. 'data_is_empty' => array(
  487. 'label' => t('Data value is empty'),
  488. 'parameter' => array(
  489. 'data' => array(
  490. 'type' => '*',
  491. 'label' => t('Data to check'),
  492. 'description' => t('The data to be checked to be empty, specified by using a data selector, e.g. "node:author:name".'),
  493. 'allow null' => TRUE,
  494. 'wrapped' => TRUE,
  495. ),
  496. ),
  497. 'group' => t('Data'),
  498. 'base' => 'rules_condition_data_is_empty',
  499. ),
  500. 'list_contains' => array(
  501. 'label' => t('List contains item'),
  502. 'parameter' => array(
  503. 'list' => array(
  504. 'type' => 'list',
  505. 'label' => t('List', array(), array('context' => 'data_types')),
  506. 'restriction' => 'selector',
  507. ),
  508. 'item' => array(
  509. 'type' => 'unknown',
  510. 'label' => t('Item'),
  511. 'description' => t('The item to check for.'),
  512. ),
  513. ),
  514. 'group' => t('Data'),
  515. 'base' => 'rules_condition_data_list_contains',
  516. 'callbacks' => array(
  517. 'info_alter' => 'rules_data_list_info_alter',
  518. 'form_alter' => 'rules_data_list_form_alter',
  519. ),
  520. ),
  521. 'text_matches' => array(
  522. 'label' => t('Text comparison'),
  523. 'parameter' => array(
  524. 'text' => array(
  525. 'type' => 'text',
  526. 'label' => t('Text'),
  527. 'restriction' => 'selector',
  528. ),
  529. 'match' => array(
  530. 'type' => 'text',
  531. 'label' => t('Matching text'),
  532. ),
  533. 'operation' => array(
  534. 'type' => 'text',
  535. 'label' => t('Comparison operation'),
  536. 'options list' => 'rules_data_text_comparison_operation_list',
  537. 'restriction' => 'input',
  538. 'default value' => 'contains',
  539. 'optional' => TRUE,
  540. 'description' => t('In case the comparison operation @regex is selected, the matching pattern will be interpreted as a <a href="@regex-wikipedia">regular expression</a>. Tip: <a href="@RegExr">RegExr: Online Regular Expression Testing Tool</a> is helpful for learning, writing, and testing Regular Expressions.', array('@regex-wikipedia' => 'http://en.wikipedia.org/wiki/Regular_expression', '@RegExr' => 'http://gskinner.com/RegExr/', '@regex' => t('regular expression'))),
  541. ),
  542. ),
  543. 'group' => t('Data'),
  544. 'base' => 'rules_data_text_comparison',
  545. ),
  546. );
  547. }
  548. /**
  549. * If the bundle is compared, add the metadata assertion so other elements
  550. * can make use of properties specific to the bundle.
  551. */
  552. function rules_condition_data_is_assertions($element) {
  553. // Assert the bundle of entities, if its compared.
  554. if ($wrapper = $element->applyDataSelector($element->settings['data:select'])) {
  555. $info = $wrapper->info();
  556. if (isset($info['parent']) && $info['parent'] instanceof EntityDrupalWrapper) {
  557. $entity_info = $info['parent']->entityInfo();
  558. if (isset($entity_info['entity keys']['bundle']) && $entity_info['entity keys']['bundle'] == $info['name']) {
  559. // Assert that the entity is of bundle $value.
  560. $value = is_array($element->settings['value']) ? $element->settings['value'] : array($element->settings['value']);
  561. // Chop of the last part of the selector.
  562. $parts = explode(':', $element->settings['data:select'], -1);
  563. return array(implode(':', $parts) => array('bundle' => $value));
  564. }
  565. }
  566. }
  567. }
  568. /**
  569. * Form alter callback for the condition data_is.
  570. *
  571. * Use multiple steps to configure the condition as the needed type of the value
  572. * depends on the selected data.
  573. */
  574. function rules_condition_data_is_form_alter(&$form, &$form_state, $options, RulesAbstractPlugin $element) {
  575. if (!empty($options['init']) && !isset($form_state['rules_element_step'])) {
  576. unset($form['parameter']['op'], $form['parameter']['value']);
  577. $form['negate']['#access'] = FALSE;
  578. $form_state['rules_element_step'] = 'data_value';
  579. $form['submit'] = array(
  580. '#type' => 'submit',
  581. '#value' => t('Continue'),
  582. '#limit_validation_errors' => array(array('parameter', 'data'), array('parameter', 'op')),
  583. '#submit' => array('rules_form_submit_rebuild'),
  584. );
  585. // Clear the parameter mode for the value parameter, so its gets the proper
  586. // default value based upon the type of the the selected data on rebuild.
  587. unset($form_state['parameter_mode']['value']);
  588. }
  589. else {
  590. // Change the data parameter to be not editable.
  591. $form['parameter']['data']['settings']['#access'] = FALSE;
  592. // TODO: improve display
  593. $form['parameter']['data']['info'] = array(
  594. '#prefix' => '<p>',
  595. '#markup' => t('<strong>Selected data:</strong> %selector', array('%selector' => $element->settings['data:select'])),
  596. '#suffix' => '</p>',
  597. );
  598. // Limit the operations to what makes sense for the selected data type.
  599. $info = $element->pluginParameterInfo();
  600. $data_info = $info['value'];
  601. if ($element->settings['op'] == 'IN') {
  602. $data_info['type'] = entity_property_list_extract_type($data_info['type']);
  603. }
  604. if (!RulesData::typesMatch($data_info, array('type' => array('decimal', 'date')))) {
  605. $options =& $form['parameter']['op']['settings']['op']['#options'];
  606. unset($options['<'], $options['>']);
  607. }
  608. // Remove 'contains' if it is not selected, as it is deprecated by the
  609. // text comparison condition.
  610. if ($element->settings['op'] != 'contains') {
  611. unset($form['parameter']['op']['settings']['op']['#options']['contains']);
  612. }
  613. // Auto-refresh the form if the operation is changed, so the input form
  614. // changes in case "is one of" requires a list value.
  615. $form['parameter']['op']['settings']['op']['#ajax'] = rules_ui_form_default_ajax() + array(
  616. 'trigger_as' => array('name' => 'reload'),
  617. );
  618. // Provide a reload button for non-JS users.
  619. $form['reload'] = array(
  620. '#type' => 'submit',
  621. '#value' => t('Reload form'),
  622. '#limit_validation_errors' => array(array('parameter', 'data'), array('parameter', 'op')),
  623. '#submit' => array('rules_form_submit_rebuild'),
  624. '#ajax' => rules_ui_form_default_ajax(),
  625. '#weight' => 5,
  626. );
  627. // Hide the reload button in case JS is enabled.
  628. $form['reload']['#attributes'] = array('class' => array('rules-hide-js'));
  629. }
  630. }
  631. /**
  632. * Provides configuration help for the data_is condition.
  633. */
  634. function rules_condition_data_is_help() {
  635. return array('#markup' => t('Compare two data values of the same type with each other.'));
  636. }
  637. /**
  638. * Options list callback for condition data_is.
  639. */
  640. function rules_condition_data_is_operator_options() {
  641. return array(
  642. '==' => t('equals'),
  643. 'IN' => t('is one of'),
  644. '<' => t('is lower than'),
  645. '>' => t('is greater than'),
  646. // Note: This is deprecated by the text comparison condition.
  647. 'contains' => t('contains'),
  648. );
  649. }
  650. /**
  651. * Options list callback for condition text_matches.
  652. */
  653. function rules_data_text_comparison_operation_list() {
  654. return array(
  655. 'contains' => t('contains'),
  656. 'starts' => t('starts with'),
  657. 'ends' => t('ends with'),
  658. 'regex' => t('regular expression'),
  659. );
  660. }
  661. /**
  662. * Returns the options list as specified by the selected property of the first parameter.
  663. *
  664. * @see rules_data_list_info_alter()
  665. * @see rules_action_data_set_info_alter()
  666. * @see rules_condition_data_is_info_alter()
  667. */
  668. function rules_data_selector_options_list(RulesAbstractPlugin $element) {
  669. $name = rules_array_key($element->pluginParameterInfo());
  670. // If the selected data property has an option list, make use of it.
  671. if (isset($element->settings[$name . ':select']) && $wrapper = $element->applyDataSelector($element->settings[$name . ':select'])) {
  672. return $wrapper->optionsList($element instanceof RulesActionInterface ? 'edit' : 'view');
  673. }
  674. }
  675. /**
  676. * @}
  677. */