rules_core.rules.inc 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. <?php
  2. /**
  3. * @file Rules core integration providing data types and conditions and
  4. * actions to invoke configured components.
  5. *
  6. * @addtogroup rules
  7. * @{
  8. */
  9. /**
  10. * Implements hook_rules_file_info() on behalf of the pseudo rules_core module.
  11. *
  12. * @see rules_core_modules()
  13. */
  14. function rules_rules_core_file_info() {
  15. return array('modules/rules_core.eval');
  16. }
  17. /**
  18. * Implements hook_rules_data_info() on behalf of the pseudo rules_core module.
  19. *
  20. * @see rules_core_modules()
  21. */
  22. function rules_rules_core_data_info() {
  23. $return = array(
  24. 'text' => array(
  25. 'label' => t('text'),
  26. 'ui class' => 'RulesDataUIText',
  27. 'token type' => 'rules_text',
  28. ),
  29. 'token' => array(
  30. 'label' => t('text token'),
  31. 'parent' => 'text',
  32. 'ui class' => 'RulesDataUITextToken',
  33. 'token type' => 'rules_token',
  34. ),
  35. // A formatted text as used by entity metadata.
  36. 'text_formatted' => array(
  37. 'label' => t('formatted text'),
  38. 'ui class' => 'RulesDataUITextFormatted',
  39. 'wrap' => TRUE,
  40. 'property info' => entity_property_text_formatted_info(),
  41. ),
  42. 'decimal' => array(
  43. 'label' => t('decimal number'),
  44. 'parent' => 'text',
  45. 'ui class' => 'RulesDataUIDecimal',
  46. 'token type' => 'rules_decimal',
  47. ),
  48. 'integer' => array(
  49. 'label' => t('integer'),
  50. 'class' => 'RulesIntegerWrapper',
  51. 'parent' => 'decimal',
  52. 'ui class' => 'RulesDataUIInteger',
  53. 'token type' => 'rules_integer',
  54. ),
  55. 'date' => array(
  56. 'label' => t('date'),
  57. 'ui class' => 'RulesDataUIDate',
  58. 'token type' => 'rules_date',
  59. ),
  60. 'duration' => array(
  61. 'label' => t('duration'),
  62. 'parent' => 'integer',
  63. 'ui class' => 'RulesDataUIDuration',
  64. 'token type' => 'rules_duration',
  65. ),
  66. 'boolean' => array(
  67. 'label' => t('truth value'),
  68. 'ui class' => 'RulesDataUIBoolean',
  69. 'token type' => 'rules_boolean',
  70. ),
  71. 'uri' => array(
  72. 'label' => t('URI'),
  73. 'parent' => 'text',
  74. // Clean inserted tokens with "rawurlencode".
  75. 'cleaning callback' => 'rawurlencode',
  76. 'ui class' => 'RulesDataUIURI',
  77. 'token type' => 'rules_uri',
  78. ),
  79. 'list' => array(
  80. 'label' => t('list', array(), array('context' => 'data_types')),
  81. 'wrap' => TRUE,
  82. 'group' => t('List', array(), array('context' => 'data_types')),
  83. ),
  84. 'list<text>' => array(
  85. 'label' => t('list of text'),
  86. 'ui class' => 'RulesDataUIListText',
  87. 'wrap' => TRUE,
  88. 'group' => t('List', array(), array('context' => 'data_types')),
  89. ),
  90. 'list<integer>' => array(
  91. 'label' => t('list of integer'),
  92. 'ui class' => 'RulesDataUIListInteger',
  93. 'wrap' => TRUE,
  94. 'group' => t('List', array(), array('context' => 'data_types')),
  95. ),
  96. 'list<token>' => array(
  97. 'label' => t('list of text tokens'),
  98. 'ui class' => 'RulesDataUIListToken',
  99. 'wrap' => TRUE,
  100. 'group' => t('List', array(), array('context' => 'data_types')),
  101. ),
  102. 'entity' => array(
  103. 'label' => t('any entity'),
  104. 'group' => t('Entity'),
  105. 'is wrapped' => TRUE,
  106. ),
  107. );
  108. foreach (entity_get_info() as $type => $info) {
  109. if (!empty($info['label'])) {
  110. $return[$type] = array(
  111. 'label' => strtolower($info['label'][0]) . substr($info['label'], 1),
  112. 'parent' => 'entity',
  113. 'wrap' => TRUE,
  114. 'group' => t('Entity'),
  115. 'ui class' => empty($info['exportable']) ? 'RulesDataUIEntity' : 'RulesDataUIEntityExportable',
  116. );
  117. }
  118. }
  119. if (module_exists('taxonomy')) {
  120. // For exportability identify vocabularies by name.
  121. $return['taxonomy_vocabulary']['wrapper class'] = 'RulesTaxonomyVocabularyWrapper';
  122. $return['taxonomy_vocabulary']['ui class'] = 'RulesDataUITaxonomyVocabulary';
  123. }
  124. return $return;
  125. }
  126. /**
  127. * Implements hook_rules_data_info_alter() on behalf of the pseudo rules_core module.
  128. *
  129. * Makes sure there is a list<type> data type for each type registered.
  130. *
  131. * @see rules_rules_data_info_alter()
  132. */
  133. function rules_rules_core_data_info_alter(&$data_info) {
  134. foreach ($data_info as $type => $info) {
  135. if (!entity_property_list_extract_type($type)) {
  136. $list_type = "list<$type>";
  137. if (!isset($data_info[$list_type])) {
  138. $data_info[$list_type] = array(
  139. 'label' => t('list of @type_label items', array('@type_label' => $info['label'])),
  140. 'wrap' => TRUE,
  141. 'group' => t('List', array(), array('context' => 'data_types')),
  142. );
  143. if (isset($info['parent']) && $info['parent'] == 'entity') {
  144. $data_info[$list_type]['ui class'] = 'RulesDataUIListEntity';
  145. }
  146. }
  147. }
  148. }
  149. }
  150. /**
  151. * Implements hook_rules_evaluator_info() on behalf of the pseudo rules_core
  152. * module.
  153. *
  154. * @see rules_core_modules()
  155. */
  156. function rules_rules_core_evaluator_info() {
  157. return array(
  158. // Process strtotime() inputs to timestamps.
  159. 'date' => array(
  160. 'class' => 'RulesDateInputEvaluator',
  161. 'type' => 'date',
  162. 'weight' => -10,
  163. ),
  164. // Post-process any input value to absolute URIs.
  165. 'uri' => array(
  166. 'class' => 'RulesURIInputEvaluator',
  167. 'type' => 'uri',
  168. 'weight' => 50,
  169. ),
  170. );
  171. }
  172. /**
  173. * Implements hook_rules_data_processor_info() on behalf of the pseudo
  174. * rules_core module.
  175. *
  176. * @see rules_core_modules()
  177. */
  178. function rules_rules_core_data_processor_info() {
  179. return array(
  180. 'date_offset' => array(
  181. 'class' => 'RulesDateOffsetProcessor',
  182. 'type' => 'date',
  183. 'weight' => -2,
  184. ),
  185. 'num_offset' => array(
  186. 'class' => 'RulesNumericOffsetProcessor',
  187. 'type' => array('integer', 'decimal'),
  188. 'weight' => -2,
  189. ),
  190. );
  191. }
  192. /**
  193. * Implements hook_rules_condition_info() on behalf of the pseudo rules_core
  194. * module.
  195. *
  196. * @see rules_core_modules()
  197. */
  198. function rules_rules_core_condition_info() {
  199. $defaults = array(
  200. 'group' => t('Components'),
  201. 'base' => 'rules_element_invoke_component',
  202. 'named parameter' => TRUE,
  203. 'access callback' => 'rules_element_invoke_component_access_callback',
  204. );
  205. $items = array();
  206. foreach (rules_get_components(FALSE, 'condition') as $name => $config) {
  207. $items['component_' . $name] = $defaults + array(
  208. 'label' => $config->plugin() . ': ' . drupal_ucfirst($config->label()),
  209. 'parameter' => $config->parameterInfo(),
  210. );
  211. $items['component_' . $name]['#config_name'] = $name;
  212. }
  213. return $items;
  214. }
  215. /**
  216. * Implements hook_rules_action_info() on behalf of the pseudo rules_core
  217. * module.
  218. *
  219. * @see rules_core_modules()
  220. */
  221. function rules_rules_core_action_info() {
  222. $defaults = array(
  223. 'group' => t('Components'),
  224. 'base' => 'rules_element_invoke_component',
  225. 'named parameter' => TRUE,
  226. 'access callback' => 'rules_element_invoke_component_access_callback',
  227. );
  228. $items = array();
  229. foreach (rules_get_components(FALSE, 'action') as $name => $config) {
  230. $items['component_' . $name] = $defaults + array(
  231. 'label' => $config->plugin() . ': ' . drupal_ucfirst($config->label()),
  232. 'parameter' => $config->parameterInfo(),
  233. 'provides' => $config->providesVariables(),
  234. );
  235. $items['component_' . $name]['#config_name'] = $name;
  236. }
  237. return $items;
  238. }
  239. /**
  240. * Implements RulesPluginUIInterface::operations() for the action.
  241. */
  242. function rules_element_invoke_component_operations(RulesPlugin $element) {
  243. $defaults = $element->extender('RulesPluginUI')->operations();
  244. $info = $element->info();
  245. // Add an operation for editing the component.
  246. $defaults['#links']['component'] = array(
  247. 'title' => t('edit component'),
  248. 'href' => RulesPluginUI::path($info['#config_name']),
  249. );
  250. return $defaults;
  251. }
  252. /**
  253. * Validate callback to make sure the invoked component exists and is not dirty.
  254. *
  255. * @see rules_scheduler_action_schedule_validate()
  256. */
  257. function rules_element_invoke_component_validate(RulesPlugin $element) {
  258. $info = $element->info();
  259. $component = rules_config_load($info['#config_name']);
  260. // Check if a component exists.
  261. if (!$component) {
  262. throw new RulesIntegrityException(t('The component %config does not exist.', array('%config' => $info['#config_name'])), $element);
  263. }
  264. // Check if a component is marked as dirty.
  265. rules_config_update_dirty_flag($component);
  266. if (!empty($component->dirty)) {
  267. throw new RulesIntegrityException(t('The utilized component %config fails the integrity check.', array('%config' => $info['#config_name'])), $element);
  268. }
  269. }
  270. /**
  271. * Implements the features export callback of the RulesPluginFeaturesIntegrationInterace.
  272. */
  273. function rules_element_invoke_component_features_export(&$export, &$pipe, $module_name = '', $element) {
  274. // Add the used component to the pipe.
  275. $info = $element->info();
  276. $pipe['rules_config'][] = $info['#config_name'];
  277. }
  278. /**
  279. * Access callback for the invoke component condition/action.
  280. */
  281. function rules_element_invoke_component_access_callback($type, $name) {
  282. // Cut of the leading 'component_' from the action name.
  283. $component = rules_config_load(substr($name, 10));
  284. if (!$component) {
  285. // Missing component.
  286. return FALSE;
  287. }
  288. // If access is not exposed for this component, default to component access.
  289. if (empty($component->access_exposed)) {
  290. return $component->access();
  291. }
  292. // Apply the permissions.
  293. return user_access('bypass rules access') || user_access("use Rules component $component->name");
  294. }
  295. /**
  296. * @}
  297. */