rules.module 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756
  1. <?php
  2. /**
  3. * @file
  4. * Rules engine module.
  5. */
  6. // The class autoloader may fail for classes added in 7.x-2.4 (Issue 2090511).
  7. if (!drupal_autoload_class('RulesEventHandlerEntityBundle')) {
  8. require_once dirname(__FILE__) . '/includes/rules.event.inc';
  9. }
  10. // Include our hook implementations early, as they can be called even before
  11. // hook_init().
  12. require_once dirname(__FILE__) . '/modules/events.inc';
  13. /**
  14. * Implements hook_module_implements_alter().
  15. */
  16. function rules_module_implements_alter(&$implementations, $hook) {
  17. // Ensures the invocation of hook_menu_get_item_alter() triggers
  18. // rules_menu_get_item_alter() first so the rules invocation is ready for all
  19. // sub-sequent hook implementations.
  20. if ($hook == 'menu_get_item_alter' && array_key_exists('rules', $implementations)) {
  21. $group = $implementations['rules'];
  22. unset($implementations['rules']);
  23. $implementations = array_merge(array('rules' => $group), $implementations);
  24. }
  25. }
  26. /**
  27. * Implements hook_menu_get_item_alter().
  28. */
  29. function rules_menu_get_item_alter() {
  30. // Make sure that event invocation is enabled before menu items are loaded.
  31. // But make sure later calls to menu_get_item() won't automatically re-enabled
  32. // the rules invocation.
  33. // Example: modules that implement hook_entity_ENTITY_TYPE_load() might want
  34. // to invoke Rules events in that load hook, which is also invoked for menu
  35. // item loading. Since this can happen even before hook_init() we need to make
  36. // sure that firing Rules events is enabled at that point. A typical use case
  37. // for this is Drupal Commerce with commerce_cart_commerce_order_load().
  38. if (!drupal_static('rules_init', FALSE)) {
  39. rules_event_invocation_enabled(TRUE);
  40. }
  41. }
  42. /**
  43. * Implements hook_init().
  44. */
  45. function rules_init() {
  46. // See rules_menu_get_item_alter().
  47. $rules_init = &drupal_static(__FUNCTION__, FALSE);
  48. $rules_init = TRUE;
  49. // Enable event invocation once hook_init() was invoked for Rules.
  50. rules_event_invocation_enabled(TRUE);
  51. rules_invoke_event('init');
  52. }
  53. /**
  54. * Returns an instance of the rules UI controller.
  55. *
  56. * This function is for convenience, to ease re-using the Rules UI.
  57. * See the rules_admin.module for example usage.
  58. *
  59. * @return RulesUIController
  60. */
  61. function rules_ui() {
  62. $static = drupal_static(__FUNCTION__);
  63. if (!isset($static)) {
  64. $static = new RulesUIController();
  65. }
  66. return $static;
  67. }
  68. /**
  69. * Returns a new rules action.
  70. *
  71. * @param $name
  72. * The action's name.
  73. * @param array $settings
  74. * The action's settings array.
  75. *
  76. * @return RulesAction
  77. */
  78. function rules_action($name, $settings = array()) {
  79. return rules_plugin_factory('action', $name, $settings);
  80. }
  81. /**
  82. * Returns a new rules condition.
  83. *
  84. * @param $name
  85. * The condition's name.
  86. * @param array $settings
  87. * The condition's settings array.
  88. *
  89. * @return RulesCondition
  90. */
  91. function rules_condition($name, $settings = array()) {
  92. return rules_plugin_factory('condition', $name, $settings);
  93. }
  94. /**
  95. * Creates a new rule.
  96. *
  97. * @param array $variables
  98. * The array of variables to setup in the evaluation state, making them
  99. * available for the configuration elements. Values for the variables need to
  100. * be passed as argument when the rule is executed. Only Rule instances with
  101. * no variables can be embedded in other configurations, e.g. rule sets.
  102. * The array has to be keyed by variable name and contain a sub-array for each
  103. * variable that has the same structure as the arrays used for describing
  104. * parameters of an action, see hook_rules_action_info(). However, in addition
  105. * to that the following keys are supported:
  106. * - parameter: (optional) If set to FALSE, no parameter for the variable
  107. * is created - thus no argument needs to be passed to the rule for the
  108. * variable upon execution. As a consequence no value will be set
  109. * initially, but the "Set data value" action may be used to do so. This is
  110. * in particular useful for defining variables which can be provided to the
  111. * caller (see $provides argument) but need not be passed in as parameter.
  112. * @param array $provides
  113. * The names of variables which should be provided to the caller. Only
  114. * variables contained in $variables may be specified.
  115. *
  116. * @return Rule
  117. */
  118. function rule($variables = NULL, $provides = array()) {
  119. return rules_plugin_factory('rule', $variables, $provides);
  120. }
  121. /**
  122. * Creates a new reaction rule.
  123. *
  124. * @return RulesReactionRule
  125. */
  126. function rules_reaction_rule() {
  127. return rules_plugin_factory('reaction rule');
  128. }
  129. /**
  130. * Creates a logical OR condition container.
  131. *
  132. * @param array $variables
  133. * An optional array as for rule().
  134. *
  135. * @return RulesOr
  136. */
  137. function rules_or($variables = NULL) {
  138. return rules_plugin_factory('or', $variables);
  139. }
  140. /**
  141. * Creates a logical AND condition container.
  142. *
  143. * @param array $variables
  144. * An optional array as for rule().
  145. *
  146. * @return RulesAnd
  147. */
  148. function rules_and($variables = NULL) {
  149. return rules_plugin_factory('and', $variables);
  150. }
  151. /**
  152. * Creates a loop.
  153. *
  154. * @param array $settings
  155. * The loop settings, containing
  156. * 'list:select': The data selector for the list to loop over.
  157. * 'item:var': Optionally a name for the list item variable.
  158. * 'item:label': Optionally a label for the list item variable.
  159. * @param array $variables
  160. * An optional array as for rule().
  161. *
  162. * @return RulesLoop
  163. */
  164. function rules_loop($settings = array(), $variables = NULL) {
  165. return rules_plugin_factory('loop', $settings, $variables);
  166. }
  167. /**
  168. * Creates a rule set.
  169. *
  170. * @param array $variables
  171. * An array as for rule().
  172. * @param array $provides
  173. * The names of variables which should be provided to the caller. See rule().
  174. *
  175. * @return RulesRuleSet
  176. */
  177. function rules_rule_set($variables = array(), $provides = array()) {
  178. return rules_plugin_factory('rule set', $variables, $provides);
  179. }
  180. /**
  181. * Creates an action set.
  182. *
  183. * @param array $variables
  184. * An array as for rule().
  185. * @param array $provides
  186. * The names of variables which should be provided to the caller. See rule().
  187. *
  188. * @return RulesActionSet
  189. */
  190. function rules_action_set($variables = array(), $provides = array()) {
  191. return rules_plugin_factory('action set', $variables, $provides);
  192. }
  193. /**
  194. * Log a message to the rules logger.
  195. *
  196. * @param $msg
  197. * The message to log.
  198. * @param array $args
  199. * An array of placeholder arguments as used by t().
  200. * @param $priority
  201. * A priority as defined by the RulesLog class.
  202. * @param RulesPlugin $element
  203. * (optional) The RulesElement causing the log entry.
  204. * @param bool $scope
  205. * (optional) This may be used to denote the beginning (TRUE) or the end
  206. * (FALSE) of a new execution scope.
  207. */
  208. function rules_log($msg, $args = array(), $priority = RulesLog::INFO, RulesPlugin $element = NULL, $scope = NULL) {
  209. static $logger, $settings;
  210. // Statically cache the variable settings as this is called very often.
  211. if (!isset($settings)) {
  212. $settings['rules_log_errors'] = variable_get('rules_log_errors', RulesLog::WARN);
  213. $settings['rules_debug_log'] = variable_get('rules_debug_log', FALSE);
  214. $settings['rules_debug'] = variable_get('rules_debug', FALSE);
  215. }
  216. if ($priority >= $settings['rules_log_errors']) {
  217. $link = NULL;
  218. if (isset($element) && isset($element->root()->name)) {
  219. $link = l(t('edit configuration'), RulesPluginUI::path($element->root()->name, 'edit', $element));
  220. }
  221. // Disabled rules invocation to avoid an endless loop when using
  222. // watchdog - which would trigger a rules event.
  223. rules_event_invocation_enabled(FALSE);
  224. watchdog('rules', $msg, $args, $priority == RulesLog::WARN ? WATCHDOG_WARNING : WATCHDOG_ERROR, $link);
  225. rules_event_invocation_enabled(TRUE);
  226. }
  227. // Do nothing in case debugging is totally disabled.
  228. if (!$settings['rules_debug_log'] && !$settings['rules_debug']) {
  229. return;
  230. }
  231. if (!isset($logger)) {
  232. $logger = RulesLog::logger();
  233. }
  234. $path = isset($element) && isset($element->root()->name) ? RulesPluginUI::path($element->root()->name, 'edit', $element) : NULL;
  235. $logger->log($msg, $args, $priority, $scope, $path);
  236. }
  237. /**
  238. * Fetches module definitions for the given hook name.
  239. *
  240. * Used for collecting events, rules, actions and condition from other modules.
  241. *
  242. * @param $hook
  243. * The hook of the definitions to get from invoking hook_rules_{$hook}.
  244. */
  245. function rules_fetch_data($hook) {
  246. $data = &drupal_static(__FUNCTION__, array());
  247. static $discover = array(
  248. 'action_info' => 'RulesActionHandlerInterface',
  249. 'condition_info' => 'RulesConditionHandlerInterface',
  250. 'event_info' => 'RulesEventHandlerInterface',
  251. );
  252. if (!isset($data[$hook])) {
  253. $data[$hook] = array();
  254. foreach (module_implements('rules_' . $hook) as $module) {
  255. $result = call_user_func($module . '_rules_' . $hook);
  256. if (isset($result) && is_array($result)) {
  257. foreach ($result as $name => $item) {
  258. $item += array('module' => $module);
  259. $data[$hook][$name] = $item;
  260. }
  261. }
  262. }
  263. // Support class discovery.
  264. if (isset($discover[$hook])) {
  265. $data[$hook] += rules_discover_plugins($discover[$hook]);
  266. }
  267. drupal_alter('rules_' . $hook, $data[$hook]);
  268. }
  269. return $data[$hook];
  270. }
  271. /**
  272. * Discover plugin implementations.
  273. *
  274. * Class based plugin handlers must be loaded when rules caches are rebuilt,
  275. * such that they get discovered properly. You have the following options:
  276. * - Put it into a regular module file (discouraged)
  277. * - Put it into your module.rules.inc file
  278. * - Put it in any file and declare it using hook_rules_file_info()
  279. * - Put it in any file and declare it using hook_rules_directory()
  280. *
  281. * In addition to that, the class must be loadable via regular class
  282. * auto-loading, thus put the file holding the class in your info file or use
  283. * another class-loader.
  284. *
  285. * @param string $class
  286. * The class or interface the plugins must implement. For a plugin to be
  287. * discovered it must have a static getInfo() method also.
  288. *
  289. * @return array
  290. * An info-hook style array containing info about discovered plugins.
  291. *
  292. * @see RulesActionHandlerInterface
  293. * @see RulesConditionHandlerInterface
  294. * @see RulesEventHandlerInterface
  295. */
  296. function rules_discover_plugins($class) {
  297. // Make sure all files possibly holding plugins are included.
  298. RulesAbstractPlugin::includeFiles();
  299. $items = array();
  300. foreach (get_declared_classes() as $plugin_class) {
  301. if (is_subclass_of($plugin_class, $class) && method_exists($plugin_class, 'getInfo')) {
  302. $info = call_user_func(array($plugin_class, 'getInfo'));
  303. $info['class'] = $plugin_class;
  304. $info['module'] = _rules_discover_module($plugin_class);
  305. $items[$info['name']] = $info;
  306. }
  307. }
  308. return $items;
  309. }
  310. /**
  311. * Determines the module providing the given class.
  312. *
  313. * @param string $class
  314. * The name of the class or interface plugins to discover.
  315. *
  316. * @return string|false
  317. * The path of the class, relative to the Drupal installation root,
  318. * or FALSE if not discovered.
  319. */
  320. function _rules_discover_module($class) {
  321. $paths = &drupal_static(__FUNCTION__);
  322. if (!isset($paths)) {
  323. // Build up a map of modules keyed by their directory.
  324. foreach (system_list('module_enabled') as $name => $module_info) {
  325. $paths[dirname($module_info->filename)] = $name;
  326. }
  327. }
  328. // Retrieve the class file and convert its absolute path to a regular Drupal
  329. // path relative to the installation root.
  330. $reflection = new ReflectionClass($class);
  331. $path = str_replace(realpath(DRUPAL_ROOT) . DIRECTORY_SEPARATOR, '', realpath(dirname($reflection->getFileName())));
  332. $path = DIRECTORY_SEPARATOR != '/' ? str_replace(DIRECTORY_SEPARATOR, '/', $path) : $path;
  333. // Go up the path until we match a module.
  334. $parts = explode('/', $path);
  335. while (!isset($paths[$path]) && array_pop($parts)) {
  336. $path = dirname($path);
  337. }
  338. return isset($paths[$path]) ? $paths[$path] : FALSE;
  339. }
  340. /**
  341. * Gets a rules cache entry.
  342. */
  343. function &rules_get_cache($cid = 'data') {
  344. // Make use of the fast, advanced drupal static pattern.
  345. static $drupal_static_fast;
  346. if (!isset($drupal_static_fast)) {
  347. $drupal_static_fast['cache'] = &drupal_static(__FUNCTION__, array());
  348. }
  349. $cache = &$drupal_static_fast['cache'];
  350. if (!isset($cache[$cid])) {
  351. // The main 'data' cache includes translated strings, so each language is
  352. // cached separately.
  353. $cid_suffix = $cid == 'data' ? ':' . $GLOBALS['language']->language : '';
  354. if ($get = cache_get($cid . $cid_suffix, 'cache_rules')) {
  355. $cache[$cid] = $get->data;
  356. }
  357. else {
  358. // Prevent stampeding by ensuring the cache is rebuilt just once at the
  359. // same time.
  360. while (!lock_acquire(__FUNCTION__ . $cid . $cid_suffix, 60)) {
  361. // Now wait until the lock is released.
  362. lock_wait(__FUNCTION__ . $cid . $cid_suffix, 30);
  363. // If the lock is released it's likely the cache was rebuild. Thus check
  364. // again if we can fetch it from the persistent cache.
  365. if ($get = cache_get($cid . $cid_suffix, 'cache_rules')) {
  366. $cache[$cid] = $get->data;
  367. return $cache[$cid];
  368. }
  369. }
  370. if ($cid === 'data') {
  371. // There is no 'data' cache so we need to rebuild it. Make sure
  372. // subsequent cache gets of the main 'data' cache during rebuild get
  373. // the interim cache by passing in the reference of the static cache
  374. // variable.
  375. _rules_rebuild_cache($cache['data']);
  376. }
  377. elseif (strpos($cid, 'comp_') === 0) {
  378. $cache[$cid] = FALSE;
  379. _rules_rebuild_component_cache();
  380. }
  381. elseif (strpos($cid, 'event_') === 0 || $cid == 'rules_event_whitelist') {
  382. $cache[$cid] = FALSE;
  383. RulesEventSet::rebuildEventCache();
  384. }
  385. else {
  386. $cache[$cid] = FALSE;
  387. }
  388. // Ensure a set lock is released.
  389. lock_release(__FUNCTION__ . $cid . $cid_suffix);
  390. }
  391. }
  392. return $cache[$cid];
  393. }
  394. /**
  395. * Rebuilds the rules cache.
  396. *
  397. * This rebuilds the rules 'data' cache and invokes rebuildCache() methods on
  398. * all plugin classes, which allows plugins to add their own data to the cache.
  399. * The cache is rebuilt in the order the plugins are defined.
  400. *
  401. * Note that building the action/condition info cache triggers loading of all
  402. * components, thus depends on entity-loading and so syncing entities in code
  403. * to the database.
  404. *
  405. * @see rules_rules_plugin_info()
  406. * @see entity_defaults_rebuild()
  407. */
  408. function _rules_rebuild_cache(&$cache) {
  409. foreach (array('data_info', 'plugin_info') as $hook) {
  410. $cache[$hook] = rules_fetch_data($hook);
  411. }
  412. foreach ($cache['plugin_info'] as $name => &$info) {
  413. // Let the items add something to the cache.
  414. $item = new $info['class']();
  415. $item->rebuildCache($info, $cache);
  416. }
  417. $cid_suffix = ':' . $GLOBALS['language']->language;
  418. cache_set('data' . $cid_suffix, $cache, 'cache_rules');
  419. }
  420. /**
  421. * Cache components to allow efficient usage via rules_invoke_component().
  422. *
  423. * @see rules_invoke_component()
  424. * @see rules_get_cache()
  425. */
  426. function _rules_rebuild_component_cache() {
  427. $components = rules_get_components();
  428. foreach ($components as $id => $component) {
  429. // If a component is marked as dirty, check if this still applies.
  430. if ($component->dirty) {
  431. rules_config_update_dirty_flag($component);
  432. }
  433. if (!$component->dirty) {
  434. // Clone the component to avoid modules getting the to be cached
  435. // version from the static loading cache.
  436. $component = clone $component;
  437. $component->optimize();
  438. // Allow modules to alter the cached component.
  439. drupal_alter('rules_component', $component->plugin, $component);
  440. rules_set_cache('comp_' . $component->name, $component);
  441. }
  442. }
  443. }
  444. /**
  445. * Sets a rules cache item.
  446. *
  447. * In addition to calling cache_set(), this function makes sure the cache item
  448. * is immediately available via rules_get_cache() by keeping all cache items
  449. * in memory. That way we can guarantee rules_get_cache() is able to retrieve
  450. * any cache item, even if all cache gets fail.
  451. *
  452. * @see rules_get_cache()
  453. */
  454. function rules_set_cache($cid, $data) {
  455. $cache = &drupal_static('rules_get_cache', array());
  456. $cache[$cid] = $data;
  457. cache_set($cid, $data, 'cache_rules');
  458. }
  459. /**
  460. * Implements hook_flush_caches().
  461. */
  462. function rules_flush_caches() {
  463. return array('cache_rules');
  464. }
  465. /**
  466. * Clears the rule set cache.
  467. */
  468. function rules_clear_cache() {
  469. cache_clear_all('*', 'cache_rules', TRUE);
  470. drupal_static_reset('rules_get_cache');
  471. drupal_static_reset('rules_fetch_data');
  472. drupal_static_reset('rules_config_update_dirty_flag');
  473. entity_get_controller('rules_config')->resetCache();
  474. }
  475. /**
  476. * Imports the given export and returns the imported configuration.
  477. *
  478. * @param string $export
  479. * A serialized string in JSON format as produced by the RulesPlugin::export()
  480. * method, or the PHP export as usual PHP array.
  481. * @param string $error_msg
  482. *
  483. * @return RulesPlugin
  484. */
  485. function rules_import($export, &$error_msg = '') {
  486. return entity_get_controller('rules_config')->import($export, $error_msg);
  487. }
  488. /**
  489. * Wraps the given data.
  490. *
  491. * @param $data
  492. * If available, the actual data, else NULL.
  493. * @param $info
  494. * An array of info about this data.
  495. * @param bool $force
  496. * Usually data is only wrapped if really needed. If set to TRUE, wrapping the
  497. * data is forced, so primitive data types are also wrapped.
  498. *
  499. * @return EntityMetadataWrapper
  500. * An EntityMetadataWrapper or the unwrapped data.
  501. *
  502. * @see hook_rules_data_info()
  503. */
  504. function &rules_wrap_data($data = NULL, $info, $force = FALSE) {
  505. // If the data is already wrapped, use the existing wrapper.
  506. if ($data instanceof EntityMetadataWrapper) {
  507. return $data;
  508. }
  509. $cache = rules_get_cache();
  510. // Define the keys to be passed through to the metadata wrapper.
  511. $wrapper_keys = array_flip(array('property info', 'property defaults'));
  512. if (isset($cache['data_info'][$info['type']])) {
  513. $info += array_intersect_key($cache['data_info'][$info['type']], $wrapper_keys);
  514. }
  515. // If a list is given, also add in the info of the item type.
  516. $list_item_type = entity_property_list_extract_type($info['type']);
  517. if ($list_item_type && isset($cache['data_info'][$list_item_type])) {
  518. $info += array_intersect_key($cache['data_info'][$list_item_type], $wrapper_keys);
  519. }
  520. // By default we do not wrap the data, except for completely unknown types.
  521. if (!empty($cache['data_info'][$info['type']]['wrap']) || $list_item_type || $force || empty($cache['data_info'][$info['type']])) {
  522. unset($info['handler']);
  523. // Allow data types to define custom wrapper classes.
  524. if (!empty($cache['data_info'][$info['type']]['wrapper class'])) {
  525. $class = $cache['data_info'][$info['type']]['wrapper class'];
  526. $wrapper = new $class($info['type'], $data, $info);
  527. }
  528. else {
  529. $wrapper = entity_metadata_wrapper($info['type'], $data, $info);
  530. }
  531. return $wrapper;
  532. }
  533. return $data;
  534. }
  535. /**
  536. * Unwraps the given data, if it's wrapped.
  537. *
  538. * @param array $data
  539. * An array of wrapped data.
  540. * @param array $info
  541. * Optionally an array of info about how to unwrap the data. Keyed as $data.
  542. *
  543. * @return array
  544. * An array containing unwrapped or passed through data.
  545. */
  546. function rules_unwrap_data(array $data, $info = array()) {
  547. $cache = rules_get_cache();
  548. foreach ($data as $key => $entry) {
  549. // If it's a wrapper, unwrap unless specified otherwise.
  550. if ($entry instanceof EntityMetadataWrapper) {
  551. if (!isset($info[$key]['allow null'])) {
  552. $info[$key]['allow null'] = FALSE;
  553. }
  554. if (!isset($info[$key]['wrapped'])) {
  555. // By default, do not unwrap special data types that are always wrapped.
  556. $info[$key]['wrapped'] = (isset($info[$key]['type']) && is_string($info[$key]['type']) && !empty($cache['data_info'][$info[$key]['type']]['is wrapped']));
  557. }
  558. // Activate the decode option by default if 'sanitize' is not enabled, so
  559. // any text is either sanitized or decoded.
  560. // @see EntityMetadataWrapper::value()
  561. $options = $info[$key] + array('decode' => empty($info[$key]['sanitize']));
  562. try {
  563. if (!($info[$key]['allow null'] && $info[$key]['wrapped'])) {
  564. $value = $entry->value($options);
  565. if (!$info[$key]['wrapped']) {
  566. $data[$key] = $value;
  567. }
  568. if (!$info[$key]['allow null'] && !isset($value)) {
  569. throw new RulesEvaluationException('The variable or parameter %name is empty.', array('%name' => $key));
  570. }
  571. }
  572. }
  573. catch (EntityMetadataWrapperException $e) {
  574. throw new RulesEvaluationException('Unable to get the data value for the variable or parameter %name. Error: !error', array('%name' => $key, '!error' => $e->getMessage()));
  575. }
  576. }
  577. }
  578. return $data;
  579. }
  580. /**
  581. * Gets event info for a given event.
  582. *
  583. * @param string $event_name
  584. * A (configured) event name.
  585. *
  586. * @return array
  587. * An array of event info. If the event is unknown, a suiting info array is
  588. * generated and returned
  589. */
  590. function rules_get_event_info($event_name) {
  591. $base_event_name = rules_get_event_base_name($event_name);
  592. $events = rules_fetch_data('event_info');
  593. if (isset($events[$base_event_name])) {
  594. return $events[$base_event_name] + array('name' => $base_event_name);
  595. }
  596. return array(
  597. 'label' => t('Unknown event "!event_name"', array('!event_name' => $base_event_name)),
  598. 'name' => $base_event_name,
  599. );
  600. }
  601. /**
  602. * Returns the base name of a configured event name.
  603. *
  604. * For a configured event name like node_view--article the base event name
  605. * node_view is returned.
  606. *
  607. * @param string $event_name
  608. * A (configured) event name.
  609. *
  610. * @return string
  611. * The event base name.
  612. */
  613. function rules_get_event_base_name($event_name) {
  614. // Cut off any suffix from a configured event name.
  615. if (strpos($event_name, '--') !== FALSE) {
  616. $parts = explode('--', $event_name, 2);
  617. return $parts[0];
  618. }
  619. return $event_name;
  620. }
  621. /**
  622. * Returns the rule event handler for the given event.
  623. *
  624. * Events having no settings are handled via the class RulesEventSettingsNone.
  625. *
  626. * @param string $event_name
  627. * The event name (base or configured).
  628. * @param array $settings
  629. * (optional) An array of event settings to set on the handler.
  630. *
  631. * @return RulesEventHandlerInterface
  632. * The event handler.
  633. */
  634. function rules_get_event_handler($event_name, array $settings = NULL) {
  635. $event_name = rules_get_event_base_name($event_name);
  636. $event_info = rules_get_event_info($event_name);
  637. $class = !empty($event_info['class']) ? $event_info['class'] : 'RulesEventDefaultHandler';
  638. $handler = new $class($event_name, $event_info);
  639. return isset($settings) ? $handler->setSettings($settings) : $handler;
  640. }
  641. /**
  642. * Creates a new instance of a the given rules plugin.
  643. *
  644. * @return RulesPlugin
  645. */
  646. function rules_plugin_factory($plugin_name, $arg1 = NULL, $arg2 = NULL) {
  647. $cache = rules_get_cache();
  648. if (isset($cache['plugin_info'][$plugin_name]['class'])) {
  649. return new $cache['plugin_info'][$plugin_name]['class']($arg1, $arg2);
  650. }
  651. }
  652. /**
  653. * Implements hook_rules_plugin_info().
  654. *
  655. * Note that the cache is rebuilt in the order of the plugins. Therefore the
  656. * condition and action plugins must be at the top, so that any components
  657. * re-building their cache can create configurations including properly setup-ed
  658. * actions and conditions.
  659. */
  660. function rules_rules_plugin_info() {
  661. return array(
  662. 'condition' => array(
  663. 'class' => 'RulesCondition',
  664. 'embeddable' => 'RulesConditionContainer',
  665. 'extenders' => array(
  666. 'RulesPluginImplInterface' => array(
  667. 'class' => 'RulesAbstractPluginDefaults',
  668. ),
  669. 'RulesPluginFeaturesIntegrationInterface' => array(
  670. 'methods' => array(
  671. 'features_export' => 'rules_features_abstract_default_features_export',
  672. ),
  673. ),
  674. 'RulesPluginUIInterface' => array(
  675. 'class' => 'RulesAbstractPluginUI',
  676. ),
  677. ),
  678. ),
  679. 'action' => array(
  680. 'class' => 'RulesAction',
  681. 'embeddable' => 'RulesActionContainer',
  682. 'extenders' => array(
  683. 'RulesPluginImplInterface' => array(
  684. 'class' => 'RulesAbstractPluginDefaults',
  685. ),
  686. 'RulesPluginFeaturesIntegrationInterface' => array(
  687. 'methods' => array(
  688. 'features_export' => 'rules_features_abstract_default_features_export',
  689. ),
  690. ),
  691. 'RulesPluginUIInterface' => array(
  692. 'class' => 'RulesAbstractPluginUI',
  693. ),
  694. ),
  695. ),
  696. 'or' => array(
  697. 'label' => t('Condition set (OR)'),
  698. 'class' => 'RulesOr',
  699. 'embeddable' => 'RulesConditionContainer',
  700. 'component' => TRUE,
  701. 'extenders' => array(
  702. 'RulesPluginUIInterface' => array(
  703. 'class' => 'RulesConditionContainerUI',
  704. ),
  705. ),
  706. ),
  707. 'and' => array(
  708. 'label' => t('Condition set (AND)'),
  709. 'class' => 'RulesAnd',
  710. 'embeddable' => 'RulesConditionContainer',
  711. 'component' => TRUE,
  712. 'extenders' => array(
  713. 'RulesPluginUIInterface' => array(
  714. 'class' => 'RulesConditionContainerUI',
  715. ),
  716. ),
  717. ),
  718. 'action set' => array(
  719. 'label' => t('Action set'),
  720. 'class' => 'RulesActionSet',
  721. 'embeddable' => FALSE,
  722. 'component' => TRUE,
  723. 'extenders' => array(
  724. 'RulesPluginUIInterface' => array(
  725. 'class' => 'RulesActionContainerUI',
  726. ),
  727. ),
  728. ),
  729. 'rule' => array(
  730. 'label' => t('Rule'),
  731. 'class' => 'Rule',
  732. 'embeddable' => 'RulesRuleSet',
  733. 'component' => TRUE,
  734. 'extenders' => array(
  735. 'RulesPluginUIInterface' => array(
  736. 'class' => 'RulesRuleUI',
  737. ),
  738. ),
  739. ),
  740. 'loop' => array(
  741. 'class' => 'RulesLoop',
  742. 'embeddable' => 'RulesActionContainer',
  743. 'extenders' => array(
  744. 'RulesPluginUIInterface' => array(
  745. 'class' => 'RulesLoopUI',
  746. ),
  747. ),
  748. ),
  749. 'reaction rule' => array(
  750. 'class' => 'RulesReactionRule',
  751. 'embeddable' => FALSE,
  752. 'extenders' => array(
  753. 'RulesPluginUIInterface' => array(
  754. 'class' => 'RulesReactionRuleUI',
  755. ),
  756. ),
  757. ),
  758. 'event set' => array(
  759. 'class' => 'RulesEventSet',
  760. 'embeddable' => FALSE,
  761. ),
  762. 'rule set' => array(
  763. 'label' => t('Rule set'),
  764. 'class' => 'RulesRuleSet',
  765. 'component' => TRUE,
  766. // Rule sets don't get embedded - we use a separate action to execute.
  767. 'embeddable' => FALSE,
  768. 'extenders' => array(
  769. 'RulesPluginUIInterface' => array(
  770. 'class' => 'RulesRuleSetUI',
  771. ),
  772. ),
  773. ),
  774. );
  775. }
  776. /**
  777. * Implements hook_entity_info().
  778. */
  779. function rules_entity_info() {
  780. return array(
  781. 'rules_config' => array(
  782. 'label' => t('Rules configuration'),
  783. 'controller class' => 'RulesEntityController',
  784. 'base table' => 'rules_config',
  785. 'fieldable' => TRUE,
  786. 'entity keys' => array(
  787. 'id' => 'id',
  788. 'name' => 'name',
  789. 'label' => 'label',
  790. ),
  791. 'module' => 'rules',
  792. 'static cache' => TRUE,
  793. 'bundles' => array(),
  794. 'configuration' => TRUE,
  795. 'exportable' => TRUE,
  796. 'export' => array(
  797. 'default hook' => 'default_rules_configuration',
  798. ),
  799. 'access callback' => 'rules_config_access',
  800. 'features controller class' => 'RulesFeaturesController',
  801. ),
  802. );
  803. }
  804. /**
  805. * Implements hook_hook_info().
  806. */
  807. function rules_hook_info() {
  808. foreach (array('plugin_info', 'rules_directory', 'data_info', 'condition_info', 'action_info', 'event_info', 'file_info', 'evaluator_info', 'data_processor_info') as $hook) {
  809. $hooks['rules_' . $hook] = array(
  810. 'group' => 'rules',
  811. );
  812. $hooks['rules_' . $hook . '_alter'] = array(
  813. 'group' => 'rules',
  814. );
  815. }
  816. $hooks['default_rules_configuration'] = array(
  817. 'group' => 'rules_defaults',
  818. );
  819. $hooks['default_rules_configuration_alter'] = array(
  820. 'group' => 'rules_defaults',
  821. );
  822. return $hooks;
  823. }
  824. /**
  825. * Load rule configurations from the database.
  826. *
  827. * This function should be used whenever you need to load more than one entity
  828. * from the database. The entities are loaded into memory and will not require
  829. * database access if loaded again during the same page request.
  830. *
  831. * @see hook_entity_info()
  832. * @see RulesEntityController
  833. *
  834. * @param array|false $names
  835. * An array of rules configuration names or FALSE to load all.
  836. * @param array $conditions
  837. * An array of conditions in the form 'field' => $value.
  838. *
  839. * @return array
  840. * An array of rule configurations indexed by their ids.
  841. */
  842. function rules_config_load_multiple($names = array(), $conditions = array()) {
  843. return entity_load_multiple_by_name('rules_config', $names, $conditions);
  844. }
  845. /**
  846. * Loads a single rule configuration from the database.
  847. *
  848. * @see rules_config_load_multiple()
  849. *
  850. * @return RulesPlugin
  851. */
  852. function rules_config_load($name) {
  853. return entity_load_single('rules_config', $name);
  854. }
  855. /**
  856. * Returns an array of configured components.
  857. *
  858. * For actually executing a component use rules_invoke_component(), as this
  859. * retrieves the component from cache instead.
  860. *
  861. * @param $label
  862. * Whether to return only the label or the whole component object.
  863. * @param $type
  864. * Optionally filter for 'action' or 'condition' components.
  865. * @param array $conditions
  866. * An array of additional conditions as required by rules_config_load().
  867. *
  868. * @return array
  869. * An array keyed by component name containing either the label or the config.
  870. */
  871. function rules_get_components($label = FALSE, $type = NULL, $conditions = array()) {
  872. $cache = rules_get_cache();
  873. $plugins = array_keys(rules_filter_array($cache['plugin_info'], 'component', TRUE));
  874. $conditions = $conditions + array('plugin' => $plugins);
  875. $faces = array(
  876. 'action' => 'RulesActionInterface',
  877. 'condition' => 'RulesConditionInterface',
  878. );
  879. $items = array();
  880. foreach (rules_config_load_multiple(FALSE, $conditions) as $name => $config) {
  881. if (!isset($type) || $config instanceof $faces[$type]) {
  882. $items[$name] = $label ? $config->label() : $config;
  883. }
  884. }
  885. return $items;
  886. }
  887. /**
  888. * Delete rule configurations from database.
  889. *
  890. * @param array $ids
  891. * An array of entity IDs.
  892. */
  893. function rules_config_delete(array $ids) {
  894. return entity_get_controller('rules_config')->delete($ids);
  895. }
  896. /**
  897. * Ensures the configuration's 'dirty' flag is up to date by running an integrity check.
  898. *
  899. * @param bool $update
  900. * (optional) Whether the dirty flag is also updated in the database if
  901. * necessary. Defaults to TRUE.
  902. */
  903. function rules_config_update_dirty_flag($rules_config, $update = TRUE) {
  904. // Keep a log of already check configurations to avoid repetitive checks on
  905. // often used components.
  906. // @see rules_element_invoke_component_validate()
  907. $checked = &drupal_static(__FUNCTION__, array());
  908. if (!empty($checked[$rules_config->name])) {
  909. return;
  910. }
  911. $checked[$rules_config->name] = TRUE;
  912. $was_dirty = !empty($rules_config->dirty);
  913. try {
  914. // First set the rule to dirty, so any repetitive checks give green light
  915. // for this configuration.
  916. $rules_config->dirty = FALSE;
  917. $rules_config->integrityCheck();
  918. if ($was_dirty) {
  919. $variables = array('%label' => $rules_config->label(), '%name' => $rules_config->name, '@plugin' => $rules_config->plugin());
  920. watchdog('rules', 'The @plugin %label (%name) was marked dirty, but passes the integrity check now and is active again.', $variables, WATCHDOG_INFO);
  921. }
  922. }
  923. catch (RulesIntegrityException $e) {
  924. $rules_config->dirty = TRUE;
  925. if (!$was_dirty) {
  926. $variables = array('%label' => $rules_config->label(), '%name' => $rules_config->name, '!message' => $e->getMessage(), '@plugin' => $rules_config->plugin());
  927. watchdog('rules', 'The @plugin %label (%name) fails the integrity check and cannot be executed. Error: !message', $variables, WATCHDOG_ERROR);
  928. }
  929. }
  930. // Save the updated dirty flag to the database.
  931. if ($was_dirty != $rules_config->dirty) {
  932. db_update('rules_config')
  933. ->fields(array('dirty' => (int) $rules_config->dirty))
  934. ->condition('id', $rules_config->id)
  935. ->execute();
  936. }
  937. }
  938. /**
  939. * Invokes a hook and the associated rules event.
  940. *
  941. * Calling this function does the same as calling module_invoke_all() and
  942. * rules_invoke_event() separately, however merges both functions into one in
  943. * order to ease usage and to work efficiently.
  944. *
  945. * @param $hook
  946. * The name of the hook / event to invoke.
  947. * @param ...
  948. * Arguments to pass to the hook / event.
  949. *
  950. * @return array
  951. * An array of return values of the hook implementations. If modules return
  952. * arrays from their implementations, those are merged into one array.
  953. */
  954. function rules_invoke_all() {
  955. // Copied code from module_invoke_all().
  956. $args = func_get_args();
  957. $hook = $args[0];
  958. unset($args[0]);
  959. $return = array();
  960. foreach (module_implements($hook) as $module) {
  961. $function = $module . '_' . $hook;
  962. if (function_exists($function)) {
  963. $result = call_user_func_array($function, $args);
  964. if (isset($result) && is_array($result)) {
  965. $return = array_merge_recursive($return, $result);
  966. }
  967. elseif (isset($result)) {
  968. $return[] = $result;
  969. }
  970. }
  971. }
  972. // Invoke the event.
  973. rules_invoke_event_by_args($hook, $args);
  974. return $return;
  975. }
  976. /**
  977. * Invokes configured rules for the given event.
  978. *
  979. * @param $event_name
  980. * The event's name.
  981. * @param ...
  982. * Pass parameters for the variables provided by this event, as defined in
  983. * hook_rules_event_info(). Example given:
  984. * @code
  985. * rules_invoke_event('node_view', $node, $view_mode);
  986. * @endcode
  987. *
  988. * @see rules_invoke_event_by_args()
  989. */
  990. function rules_invoke_event() {
  991. $args = func_get_args();
  992. $event_name = $args[0];
  993. unset($args[0]);
  994. // We maintain a whitelist of configured events to reduces the number of cache
  995. // reads. If the whitelist is not in the cache we proceed and it is rebuilt.
  996. if (rules_event_invocation_enabled()) {
  997. $whitelist = rules_get_cache('rules_event_whitelist');
  998. if ((($whitelist === FALSE) || isset($whitelist[$event_name])) && $event = rules_get_cache('event_' . $event_name)) {
  999. $event->executeByArgs($args);
  1000. }
  1001. }
  1002. }
  1003. /**
  1004. * Invokes configured rules for the given event.
  1005. *
  1006. * @param $event_name
  1007. * The event's name.
  1008. * @param array $args
  1009. * An array of parameters for the variables provided by the event, as defined
  1010. * in hook_rules_event_info(). Either pass an array keyed by the variable
  1011. * names or a numerically indexed array, in which case the ordering of the
  1012. * passed parameters has to match the order of the specified variables.
  1013. * Example given:
  1014. * @code
  1015. * rules_invoke_event_by_args('node_view', array('node' => $node, 'view_mode' => $view_mode));
  1016. * @endcode
  1017. *
  1018. * @see rules_invoke_event()
  1019. */
  1020. function rules_invoke_event_by_args($event_name, $args = array()) {
  1021. // We maintain a whitelist of configured events to reduces the number of cache
  1022. // reads. If the whitelist is empty we proceed and it is rebuilt.
  1023. if (rules_event_invocation_enabled()) {
  1024. $whitelist = rules_get_cache('rules_event_whitelist');
  1025. if ((empty($whitelist) || isset($whitelist[$event_name])) && $event = rules_get_cache('event_' . $event_name)) {
  1026. $event->executeByArgs($args);
  1027. }
  1028. }
  1029. }
  1030. /**
  1031. * Invokes a rule component, e.g. a rule set.
  1032. *
  1033. * @param $component_name
  1034. * The component's name.
  1035. * @param $args
  1036. * Pass further parameters as required for the invoked component.
  1037. *
  1038. * @return array
  1039. * An array of variables as provided by the component, or FALSE in case the
  1040. * component could not be executed.
  1041. */
  1042. function rules_invoke_component() {
  1043. $args = func_get_args();
  1044. $name = array_shift($args);
  1045. if ($component = rules_get_cache('comp_' . $name)) {
  1046. return $component->executeByArgs($args);
  1047. }
  1048. return FALSE;
  1049. }
  1050. /**
  1051. * Filters the given array of arrays.
  1052. *
  1053. * This filter operates by keeping only entries which have $key set to the
  1054. * value of $value.
  1055. *
  1056. * @param array $array
  1057. * The array of arrays to filter.
  1058. * @param $key
  1059. * The key used for the comparison.
  1060. * @param $value
  1061. * The value to compare the array's entry to.
  1062. *
  1063. * @return array
  1064. * The filtered array.
  1065. */
  1066. function rules_filter_array($array, $key, $value) {
  1067. $return = array();
  1068. foreach ($array as $i => $entry) {
  1069. $entry += array($key => NULL);
  1070. if ($entry[$key] == $value) {
  1071. $return[$i] = $entry;
  1072. }
  1073. }
  1074. return $return;
  1075. }
  1076. /**
  1077. * Merges the $update array into $array.
  1078. *
  1079. * Makes sure no values of $array not appearing in $update are lost.
  1080. *
  1081. * @return array
  1082. * The updated array.
  1083. */
  1084. function rules_update_array(array $array, array $update) {
  1085. foreach ($update as $key => $data) {
  1086. if (isset($array[$key]) && is_array($array[$key]) && is_array($data)) {
  1087. $array[$key] = rules_update_array($array[$key], $data);
  1088. }
  1089. else {
  1090. $array[$key] = $data;
  1091. }
  1092. }
  1093. return $array;
  1094. }
  1095. /**
  1096. * Extracts the property with the given name.
  1097. *
  1098. * @param array $arrays
  1099. * An array of arrays from which a property is to be extracted.
  1100. * @param $key
  1101. * The name of the property to extract.
  1102. *
  1103. * @return array
  1104. * An array of extracted properties, keyed as in $arrays.
  1105. */
  1106. function rules_extract_property($arrays, $key) {
  1107. $data = array();
  1108. foreach ($arrays as $name => $item) {
  1109. $data[$name] = $item[$key];
  1110. }
  1111. return $data;
  1112. }
  1113. /**
  1114. * Returns the first key of the array.
  1115. */
  1116. function rules_array_key($array) {
  1117. reset($array);
  1118. return key($array);
  1119. }
  1120. /**
  1121. * Clean replacements so they are URL friendly.
  1122. *
  1123. * Can be used as 'cleaning callback' for action or condition parameters.
  1124. *
  1125. * @param $replacements
  1126. * An array of token replacements that need to be "cleaned" for use in the URL.
  1127. * @param array $data
  1128. * An array of objects used to generate the replacements.
  1129. * @param array $options
  1130. * An array of options used to generate the replacements.
  1131. *
  1132. * @see rules_path_action_info()
  1133. */
  1134. function rules_path_clean_replacement_values(&$replacements, $data = array(), $options = array()) {
  1135. // Include path.eval.inc which contains path cleaning functions.
  1136. module_load_include('inc', 'rules', 'modules/path.eval');
  1137. foreach ($replacements as $token => $value) {
  1138. $replacements[$token] = rules_clean_path($value);
  1139. }
  1140. }
  1141. /**
  1142. * Implements hook_theme().
  1143. */
  1144. function rules_theme() {
  1145. return array(
  1146. 'rules_elements' => array(
  1147. 'render element' => 'element',
  1148. 'file' => 'ui/ui.theme.inc',
  1149. ),
  1150. 'rules_content_group' => array(
  1151. 'render element' => 'element',
  1152. 'file' => 'ui/ui.theme.inc',
  1153. ),
  1154. 'rules_parameter_configuration' => array(
  1155. 'render element' => 'element',
  1156. 'file' => 'ui/ui.theme.inc',
  1157. ),
  1158. 'rules_variable_view' => array(
  1159. 'render element' => 'element',
  1160. 'file' => 'ui/ui.theme.inc',
  1161. ),
  1162. 'rules_data_selector_help' => array(
  1163. 'variables' => array('parameter' => NULL, 'variables' => NULL),
  1164. 'file' => 'ui/ui.theme.inc',
  1165. ),
  1166. 'rules_ui_variable_form' => array(
  1167. 'render element' => 'element',
  1168. 'file' => 'ui/ui.theme.inc',
  1169. ),
  1170. 'rules_log' => array(
  1171. 'render element' => 'element',
  1172. 'file' => 'ui/ui.theme.inc',
  1173. ),
  1174. 'rules_autocomplete' => array(
  1175. 'render element' => 'element',
  1176. 'file' => 'ui/ui.theme.inc',
  1177. ),
  1178. 'rules_debug_element' => array(
  1179. 'render element' => 'element',
  1180. 'file' => 'ui/ui.theme.inc',
  1181. ),
  1182. 'rules_settings_help' => array(
  1183. 'variables' => array('text' => '', 'heading' => ''),
  1184. 'file' => 'ui/ui.theme.inc',
  1185. ),
  1186. );
  1187. }
  1188. /**
  1189. * Implements hook_permission().
  1190. */
  1191. function rules_permission() {
  1192. $perms = array(
  1193. 'administer rules' => array(
  1194. 'title' => t('Administer rule configurations'),
  1195. 'description' => t('Administer rule configurations including events, conditions and actions for which the user has sufficient access permissions.'),
  1196. ),
  1197. 'bypass rules access' => array(
  1198. 'title' => t('Bypass Rules access control'),
  1199. 'description' => t('Control all configurations regardless of permission restrictions of events, conditions or actions.'),
  1200. 'restrict access' => TRUE,
  1201. ),
  1202. 'access rules debug' => array(
  1203. 'title' => t('Access the Rules debug log'),
  1204. ),
  1205. );
  1206. // Fetch all components to generate the access keys.
  1207. $conditions['plugin'] = array_keys(rules_filter_array(rules_fetch_data('plugin_info'), 'component', TRUE));
  1208. $conditions['access_exposed'] = 1;
  1209. $components = entity_load('rules_config', FALSE, $conditions);
  1210. $perms += rules_permissions_by_component($components);
  1211. return $perms;
  1212. }
  1213. /**
  1214. * Helper function to get all the permissions for components that have access exposed.
  1215. */
  1216. function rules_permissions_by_component(array $components = array()) {
  1217. $perms = array();
  1218. foreach ($components as $component) {
  1219. $perms += array(
  1220. "use Rules component $component->name" => array(
  1221. 'title' => t('Use Rules component %component', array('%component' => $component->label())),
  1222. 'description' => t('Controls access for using the component %component via the provided action or condition. <a href="@component-edit-url">Edit this component.</a>', array('%component' => $component->label(), '@component-edit-url' => url(RulesPluginUI::path($component->name)))),
  1223. ),
  1224. );
  1225. }
  1226. return $perms;
  1227. }
  1228. /**
  1229. * Menu callback for loading rules configuration elements.
  1230. *
  1231. * @see RulesUIController::config_menu()
  1232. */
  1233. function rules_element_load($element_id, $config_name) {
  1234. $config = rules_config_load($config_name);
  1235. return $config->elementMap()->lookup($element_id);
  1236. }
  1237. /**
  1238. * Menu callback for getting the title as configured.
  1239. *
  1240. * @see RulesUIController::config_menu()
  1241. */
  1242. function rules_get_title($text, $element) {
  1243. if ($element instanceof RulesPlugin) {
  1244. $cache = rules_get_cache();
  1245. $plugin = $element->plugin();
  1246. $plugin = isset($cache['plugin_info'][$plugin]['label']) ? $cache['plugin_info'][$plugin]['label'] : $plugin;
  1247. $plugin = drupal_strtolower(drupal_substr($plugin, 0, 1)) . drupal_substr($plugin, 1);
  1248. return t($text, array('!label' => $element->label(), '!plugin' => $plugin));
  1249. }
  1250. // As fallback treat $element as simple string.
  1251. return t($text, array('!plugin' => $element));
  1252. }
  1253. /**
  1254. * Menu callback for getting the title for the add element page.
  1255. *
  1256. * Uses a work-a-round for accessing the plugin name.
  1257. *
  1258. * @see RulesUIController::config_menu()
  1259. */
  1260. function rules_menu_add_element_title($array) {
  1261. $plugin_name = arg($array[0]);
  1262. $cache = rules_get_cache();
  1263. if (isset($cache['plugin_info'][$plugin_name]['class'])) {
  1264. $info = $cache['plugin_info'][$plugin_name] + array('label' => $plugin_name);
  1265. $label = drupal_strtolower(drupal_substr($info['label'], 0, 1)) . drupal_substr($info['label'], 1);
  1266. return t('Add a new !plugin', array('!plugin' => $label));
  1267. }
  1268. }
  1269. /**
  1270. * Returns the current region for the debug log.
  1271. */
  1272. function rules_debug_log_region() {
  1273. // If there is no setting for the current theme use the default theme setting.
  1274. global $theme_key;
  1275. $theme_default = variable_get('theme_default', 'bartik');
  1276. return variable_get('rules_debug_region_' . $theme_key, variable_get('rules_debug_region_' . $theme_default, 'help'));
  1277. }
  1278. /**
  1279. * Implements hook_page_build() to add the rules debug log to the page bottom.
  1280. */
  1281. function rules_page_build(&$page) {
  1282. // Invoke a the page redirect, in case the action has been executed.
  1283. // @see rules_action_drupal_goto()
  1284. if (isset($GLOBALS['_rules_action_drupal_goto_do'])) {
  1285. list($url, $force) = $GLOBALS['_rules_action_drupal_goto_do'];
  1286. drupal_goto($url);
  1287. }
  1288. if (isset($_SESSION['rules_debug'])) {
  1289. $region = rules_debug_log_region();
  1290. foreach ($_SESSION['rules_debug'] as $log) {
  1291. $page[$region]['rules_debug'][] = array(
  1292. '#markup' => $log,
  1293. );
  1294. $page[$region]['rules_debug']['#theme_wrappers'] = array('rules_log');
  1295. }
  1296. unset($_SESSION['rules_debug']);
  1297. }
  1298. if (rules_show_debug_output()) {
  1299. $region = rules_debug_log_region();
  1300. $page[$region]['rules_debug']['#pre_render'] = array('rules_debug_log_pre_render');
  1301. }
  1302. }
  1303. /**
  1304. * Pre-render callback for the debug log, which renders and then clears it.
  1305. */
  1306. function rules_debug_log_pre_render($elements) {
  1307. $logger = RulesLog::logger();
  1308. if ($log = $logger->render()) {
  1309. $logger = RulesLog::logger();
  1310. $logger->clear();
  1311. $elements[] = array('#markup' => $log);
  1312. $elements['#theme_wrappers'] = array('rules_log');
  1313. // Log the rules log to the system log if enabled.
  1314. if (variable_get('rules_debug_log', FALSE)) {
  1315. watchdog('rules', 'Rules debug information: !log', array('!log' => $log), WATCHDOG_NOTICE);
  1316. }
  1317. }
  1318. return $elements;
  1319. }
  1320. /**
  1321. * Implements hook_drupal_goto_alter().
  1322. *
  1323. * @see rules_action_drupal_goto()
  1324. */
  1325. function rules_drupal_goto_alter(&$path, &$options, &$http_response_code) {
  1326. // Invoke a the page redirect, in case the action has been executed.
  1327. if (isset($GLOBALS['_rules_action_drupal_goto_do'])) {
  1328. list($url, $force) = $GLOBALS['_rules_action_drupal_goto_do'];
  1329. if ($force || !isset($_GET['destination'])) {
  1330. $url = drupal_parse_url($url);
  1331. $path = $url['path'];
  1332. $options['query'] = $url['query'];
  1333. $options['fragment'] = $url['fragment'];
  1334. $http_response_code = 302;
  1335. }
  1336. }
  1337. }
  1338. /**
  1339. * Returns whether the debug log should be shown.
  1340. */
  1341. function rules_show_debug_output() {
  1342. // For performance avoid unnecessary auto-loading of the RulesLog class.
  1343. if (!class_exists('RulesLog', FALSE)) {
  1344. return FALSE;
  1345. }
  1346. if (variable_get('rules_debug', 0) == RulesLog::INFO && user_access('access rules debug')) {
  1347. return TRUE;
  1348. }
  1349. return variable_get('rules_debug', 0) == RulesLog::WARN && user_access('access rules debug') && RulesLog::logger()->hasErrors();
  1350. }
  1351. /**
  1352. * Implements hook_exit().
  1353. */
  1354. function rules_exit() {
  1355. // Bail out if this is cached request and modules are not loaded.
  1356. if (!module_exists('rules') || !module_exists('user')) {
  1357. return;
  1358. }
  1359. if (rules_show_debug_output()) {
  1360. if ($log = RulesLog::logger()->render()) {
  1361. // Keep the log in the session so we can show it on the next page.
  1362. $_SESSION['rules_debug'][] = $log;
  1363. }
  1364. }
  1365. // Log the rules log to the system log if enabled.
  1366. if (variable_get('rules_debug_log', FALSE) && $log = RulesLog::logger()->render()) {
  1367. watchdog('rules', 'Rules debug information: !log', array('!log' => $log), WATCHDOG_NOTICE);
  1368. }
  1369. }
  1370. /**
  1371. * Implements hook_element_info().
  1372. */
  1373. function rules_element_info() {
  1374. // A duration form element for rules. Needs ui.forms.inc included.
  1375. $types['rules_duration'] = array(
  1376. '#input' => TRUE,
  1377. '#tree' => TRUE,
  1378. '#default_value' => 0,
  1379. '#value_callback' => 'rules_ui_element_duration_value',
  1380. '#process' => array('rules_ui_element_duration_process', 'ajax_process_form'),
  1381. '#after_build' => array('rules_ui_element_duration_after_build'),
  1382. '#pre_render' => array('form_pre_render_conditional_form_element'),
  1383. );
  1384. $types['rules_data_selection'] = array(
  1385. '#input' => TRUE,
  1386. '#pre_render' => array('form_pre_render_conditional_form_element'),
  1387. '#process' => array('rules_data_selection_process', 'ajax_process_form'),
  1388. '#theme' => 'rules_autocomplete',
  1389. );
  1390. return $types;
  1391. }
  1392. /**
  1393. * Implements hook_modules_enabled().
  1394. */
  1395. function rules_modules_enabled($modules) {
  1396. // Re-enable Rules configurations that are dirty, because they require one of
  1397. // the enabled the modules.
  1398. $query = db_select('rules_dependencies', 'rd');
  1399. $query->join('rules_config', 'rc', 'rd.id = rc.id');
  1400. $query->fields('rd', array('id'))
  1401. ->condition('rd.module', $modules, 'IN')
  1402. ->condition('rc.dirty', 1);
  1403. $ids = $query->execute()->fetchCol();
  1404. // If there are some configurations that might work again, re-check all dirty
  1405. // configurations as others might work again too, e.g. consider a rule that is
  1406. // dirty because it requires a dirty component.
  1407. if ($ids) {
  1408. $rules_configs = rules_config_load_multiple(FALSE, array('dirty' => 1));
  1409. foreach ($rules_configs as $rules_config) {
  1410. try {
  1411. $rules_config->integrityCheck();
  1412. // If no exceptions were thrown we can set the configuration back to OK.
  1413. db_update('rules_config')
  1414. ->fields(array('dirty' => 0))
  1415. ->condition('id', $rules_config->id)
  1416. ->execute();
  1417. if ($rules_config->active) {
  1418. drupal_set_message(t('All dependencies for the Rules configuration %config are met again, so it has been re-activated.', array('%config' => $rules_config->label())));
  1419. }
  1420. }
  1421. catch (RulesIntegrityException $e) {
  1422. // The rule is still dirty, so do nothing.
  1423. }
  1424. }
  1425. }
  1426. rules_clear_cache();
  1427. }
  1428. /**
  1429. * Implements hook_modules_disabled().
  1430. */
  1431. function rules_modules_disabled($modules) {
  1432. // Disable Rules configurations that depend on one of the disabled modules.
  1433. $query = db_select('rules_dependencies', 'rd');
  1434. $query->join('rules_config', 'rc', 'rd.id = rc.id');
  1435. $query->fields('rd', array('id'))
  1436. ->distinct()
  1437. ->condition('rd.module', $modules, 'IN')
  1438. ->condition('rc.dirty', 0);
  1439. $ids = $query->execute()->fetchCol();
  1440. if (!empty($ids)) {
  1441. db_update('rules_config')
  1442. ->fields(array('dirty' => 1))
  1443. ->condition('id', $ids, 'IN')
  1444. ->execute();
  1445. // Tell the user about enabled rules that have been marked as dirty.
  1446. $count = db_select('rules_config', 'r')
  1447. ->fields('r')
  1448. ->condition('id', $ids, 'IN')
  1449. ->condition('active', 1)
  1450. ->countQuery()
  1451. ->execute()
  1452. ->fetchField();
  1453. if ($count > 0) {
  1454. $message = format_plural($count,
  1455. '1 Rules configuration requires some of the disabled modules to function and cannot be executed any more.',
  1456. '@count Rules configurations require some of the disabled modules to function and cannot be executed any more.'
  1457. );
  1458. drupal_set_message($message, 'warning');
  1459. }
  1460. }
  1461. rules_clear_cache();
  1462. }
  1463. /**
  1464. * Access callback for dealing with Rules configurations.
  1465. *
  1466. * @see entity_access()
  1467. */
  1468. function rules_config_access($op, $rules_config = NULL, $account = NULL) {
  1469. if (user_access('bypass rules access', $account)) {
  1470. return TRUE;
  1471. }
  1472. // Allow modules to grant / deny access.
  1473. $access = module_invoke_all('rules_config_access', $op, $rules_config, $account);
  1474. // Only grant access if at least one module granted access and no one denied
  1475. // access.
  1476. if (in_array(FALSE, $access, TRUE)) {
  1477. return FALSE;
  1478. }
  1479. elseif (in_array(TRUE, $access, TRUE)) {
  1480. return TRUE;
  1481. }
  1482. return FALSE;
  1483. }
  1484. /**
  1485. * Implements hook_rules_config_access().
  1486. */
  1487. function rules_rules_config_access($op, $rules_config = NULL, $account = NULL) {
  1488. // Instead of returning FALSE return nothing, so others still can grant
  1489. // access.
  1490. if (!isset($rules_config) || (isset($account) && $account->uid != $GLOBALS['user']->uid)) {
  1491. return;
  1492. }
  1493. if (user_access('administer rules', $account) && ($op == 'view' || $rules_config->access())) {
  1494. return TRUE;
  1495. }
  1496. }
  1497. /**
  1498. * Implements hook_menu().
  1499. */
  1500. function rules_menu() {
  1501. $items['admin/config/workflow/rules/upgrade'] = array(
  1502. 'title' => 'Upgrade',
  1503. 'page callback' => 'drupal_get_form',
  1504. 'page arguments' => array('rules_upgrade_form'),
  1505. 'access arguments' => array('administer rules'),
  1506. 'file' => 'includes/rules.upgrade.inc',
  1507. 'file path' => drupal_get_path('module', 'rules'),
  1508. 'type' => MENU_CALLBACK,
  1509. );
  1510. $items['admin/config/workflow/rules/upgrade/clear'] = array(
  1511. 'title' => 'Clear',
  1512. 'page callback' => 'drupal_get_form',
  1513. 'page arguments' => array('rules_upgrade_confirm_clear_form'),
  1514. 'access arguments' => array('administer rules'),
  1515. 'file' => 'includes/rules.upgrade.inc',
  1516. 'file path' => drupal_get_path('module', 'rules'),
  1517. 'type' => MENU_CALLBACK,
  1518. );
  1519. $items['admin/config/workflow/rules/autocomplete_tags'] = array(
  1520. 'title' => 'Rules tags autocomplete',
  1521. 'page callback' => 'rules_autocomplete_tags',
  1522. 'page arguments' => array(5),
  1523. 'access arguments' => array('administer rules'),
  1524. 'file' => 'ui/ui.forms.inc',
  1525. 'type' => MENU_CALLBACK,
  1526. );
  1527. return $items;
  1528. }
  1529. /**
  1530. * Helper function to keep track of external documentation pages for Rules.
  1531. *
  1532. * @param string $topic
  1533. * The topic key for used for identifying help pages.
  1534. *
  1535. * @return string|array|false
  1536. * Either a URL for the given page, or the full list of external help pages.
  1537. */
  1538. function rules_external_help($topic = NULL) {
  1539. $help = array(
  1540. 'rules' => 'https://www.drupal.org/node/298480',
  1541. 'terminology' => 'https://www.drupal.org/node/1299990',
  1542. 'condition-components' => 'https://www.drupal.org/node/1300034',
  1543. 'data-selection' => 'https://www.drupal.org/node/1300042',
  1544. 'chained-tokens' => 'https://www.drupal.org/node/1300042',
  1545. 'loops' => 'https://www.drupal.org/node/1300058',
  1546. 'components' => 'https://www.drupal.org/node/1300024',
  1547. 'component-types' => 'https://www.drupal.org/node/1300024',
  1548. 'variables' => 'https://www.drupal.org/node/1300024',
  1549. 'scheduler' => 'https://www.drupal.org/node/1300068',
  1550. 'coding' => 'https://www.drupal.org/node/878720',
  1551. );
  1552. if (isset($topic)) {
  1553. return isset($help[$topic]) ? $help[$topic] : FALSE;
  1554. }
  1555. return $help;
  1556. }
  1557. /**
  1558. * Implements hook_help().
  1559. */
  1560. function rules_help($path, $arg) {
  1561. // Only enable the help if the admin module is active.
  1562. if ($path == 'admin/help#rules' && module_exists('rules_admin')) {
  1563. $output['header'] = array(
  1564. '#markup' => t('Rules documentation is kept online. Please use the links below for more information about Rules. Feel free to contribute to improving the online documentation!'),
  1565. );
  1566. // Build a list of essential Rules help pages, formatted as a bullet list.
  1567. $link_list['rules'] = l(t('Rules introduction'), rules_external_help('rules'));
  1568. $link_list['terminology'] = l(t('Rules terminology'), rules_external_help('terminology'));
  1569. $link_list['scheduler'] = l(t('Rules Scheduler'), rules_external_help('scheduler'));
  1570. $link_list['coding'] = l(t('Coding for Rules'), rules_external_help('coding'));
  1571. $output['topic-list'] = array(
  1572. '#markup' => theme('item_list', array('items' => $link_list)),
  1573. );
  1574. return render($output);
  1575. }
  1576. }
  1577. /**
  1578. * Implements hook_token_info().
  1579. */
  1580. function rules_token_info() {
  1581. $cache = rules_get_cache();
  1582. $data_info = $cache['data_info'];
  1583. $types = array('text', 'integer', 'uri', 'token', 'decimal', 'date', 'duration');
  1584. foreach ($types as $type) {
  1585. $token_type = $data_info[$type]['token type'];
  1586. $token_info['types'][$token_type] = array(
  1587. 'name' => $data_info[$type]['label'],
  1588. 'description' => t('Tokens related to %label Rules variables.', array('%label' => $data_info[$type]['label'])),
  1589. 'needs-data' => $token_type,
  1590. );
  1591. $token_info['tokens'][$token_type]['value'] = array(
  1592. 'name' => t("Value"),
  1593. 'description' => t('The value of the variable.'),
  1594. );
  1595. }
  1596. return $token_info;
  1597. }
  1598. /**
  1599. * Implements hook_tokens().
  1600. */
  1601. function rules_tokens($type, $tokens, $data, $options = array()) {
  1602. // Handle replacements of primitive variable types.
  1603. if (substr($type, 0, 6) == 'rules_' && !empty($data[$type])) {
  1604. // Leverage entity tokens token processor by passing on as struct.
  1605. $info['property info']['value'] = array(
  1606. 'type' => substr($type, 6),
  1607. 'label' => '',
  1608. );
  1609. // Entity tokens uses metadata wrappers as values for 'struct' types.
  1610. $wrapper = entity_metadata_wrapper('struct', array('value' => $data[$type]), $info);
  1611. return entity_token_tokens('struct', $tokens, array('struct' => $wrapper), $options);
  1612. }
  1613. }
  1614. /**
  1615. * Helper function that retrieves a metadata wrapper with all properties.
  1616. *
  1617. * Note that without this helper, bundle-specific properties aren't added.
  1618. */
  1619. function rules_get_entity_metadata_wrapper_all_properties(RulesAbstractPlugin $element) {
  1620. return entity_metadata_wrapper($element->settings['type'], NULL, array(
  1621. 'property info alter' => 'rules_entity_metadata_wrapper_all_properties_callback',
  1622. ));
  1623. }
  1624. /**
  1625. * Callback that returns a metadata wrapper with all properties.
  1626. */
  1627. function rules_entity_metadata_wrapper_all_properties_callback(EntityMetadataWrapper $wrapper, $property_info) {
  1628. $info = $wrapper->info();
  1629. $properties = entity_get_all_property_info($info['type']);
  1630. $property_info['properties'] += $properties;
  1631. return $property_info;
  1632. }
  1633. /**
  1634. * Helper to enable or disable the invocation of rules events.
  1635. *
  1636. * Rules invocation is disabled by default, such that Rules does not operate
  1637. * when Drupal is not fully bootstrapped. It gets enabled in rules_init() and
  1638. * rules_enable().
  1639. *
  1640. * @param bool|null $enable
  1641. * NULL to leave the setting as is and TRUE / FALSE to change the behaviour.
  1642. *
  1643. * @return bool
  1644. * Whether the rules invocation is enabled or disabled.
  1645. */
  1646. function rules_event_invocation_enabled($enable = NULL) {
  1647. static $invocation_enabled = FALSE;
  1648. if (isset($enable)) {
  1649. $invocation_enabled = (bool) $enable;
  1650. }
  1651. // Disable invocation if configured or if site runs in maintenance mode.
  1652. return $invocation_enabled && !defined('MAINTENANCE_MODE');
  1653. }