rules_admin.inc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. <?php
  2. /**
  3. * @file
  4. * Implements rule management and configuration screens.
  5. */
  6. /**
  7. * Reaction rules overview.
  8. */
  9. function rules_admin_reaction_overview($form, &$form_state, $base_path) {
  10. RulesPluginUI::formDefaults($form, $form_state);
  11. $conditions = array('plugin' => 'reaction rule', 'active' => TRUE);
  12. $collapsed = TRUE;
  13. if (empty($_GET['tag'])) {
  14. $tag = 0;
  15. }
  16. else {
  17. $tag = $_GET['tag'];
  18. $conditions['tags'] = array($tag);
  19. $collapsed = FALSE;
  20. }
  21. if (empty($_GET['event'])) {
  22. $event = 0;
  23. }
  24. else {
  25. $event = $_GET['event'];
  26. // Filter using a wildcard suffix so configured event names with suffixes
  27. // are found also.
  28. $conditions['event'] = $event . '%';
  29. $collapsed = FALSE;
  30. }
  31. $form['help'] = array(
  32. '#markup' => t('Reaction rules, listed below, react on selected events on the site. Each reaction rule may fire any number of <em>actions</em>, and may have any number of <em>conditions</em> that must be met for the actions to be executed. You can also set up <a href="@url1">components</a> – stand-alone sets of Rules configuration that can be used in Rules and other parts of your site. See <a href="@url2">the online documentation</a> for an introduction on how to use Rules.',
  33. array('@url1' => url('admin/config/workflow/rules/components'),
  34. '@url2' => rules_external_help('rules'))),
  35. );
  36. $form['filter'] = array(
  37. '#type' => 'fieldset',
  38. '#title' => t('Filter'),
  39. '#collapsible' => TRUE,
  40. );
  41. $form['filter']['#id'] = 'rules-filter-form';
  42. $form['filter']['#attached']['css'][] = drupal_get_path('module', 'rules') . '/ui/rules.ui.css';
  43. $form['filter']['event'] = array(
  44. '#type' => 'select',
  45. '#title' => t('Filter by event'),
  46. '#options' => array(0 => t('<All>')) + RulesPluginUI::getOptions('event'),
  47. '#default_value' => $event,
  48. );
  49. $form['filter']['tag'] = array(
  50. '#type' => 'select',
  51. '#title' => t('Filter by tag'),
  52. '#options' => array(0 => t('<All>')) + RulesPluginUI::getTags(),
  53. '#default_value' => $tag,
  54. );
  55. $form['filter']['submit'] = array(
  56. '#type' => 'submit',
  57. '#value' => t('Filter'),
  58. '#name' => '', // prevent from showing up in $_GET.
  59. );
  60. $options = array('show plugin' => FALSE, 'base path' => $base_path);
  61. $form['active'] = rules_ui()->overviewTable($conditions, $options);
  62. $form['active']['#caption'] = t('Active rules');
  63. $form['active']['#empty'] = t('There are no active rules. <a href="!url">Add new rule</a>.', array('!url' => url('admin/config/workflow/rules/reaction/add')));
  64. $conditions['active'] = FALSE;
  65. $form['inactive'] = rules_ui()->overviewTable($conditions, $options);
  66. $form['inactive']['#caption'] = t('Inactive rules');
  67. $form['inactive']['#empty'] = t('There are no inactive rules.');
  68. $form['filter']['#collapsed'] = $collapsed;
  69. $form['#submit'][] = 'rules_form_submit_rebuild';
  70. $form['#method'] = 'get';
  71. return $form;
  72. }
  73. /**
  74. * Components overview.
  75. */
  76. function rules_admin_components_overview($form, &$form_state, $base_path) {
  77. RulesPluginUI::formDefaults($form, $form_state);
  78. $collapsed = TRUE;
  79. if (empty($_GET['tag'])) {
  80. $tag = 0;
  81. }
  82. else {
  83. $tag = $_GET['tag'];
  84. $conditions['tags'] = array($tag);
  85. $collapsed = FALSE;
  86. }
  87. if (empty($_GET['plugin'])) {
  88. // Get the plugin name usable as component.
  89. $conditions['plugin'] = array_keys(rules_filter_array(rules_fetch_data('plugin_info'), 'component', TRUE));
  90. $plugin = 0;
  91. }
  92. else {
  93. $plugin = $_GET['plugin'];
  94. $conditions['plugin'] = $plugin;
  95. $collapsed = FALSE;
  96. }
  97. $form['help'] = array(
  98. '#markup' => t('Components are stand-alone sets of Rules configuration that can be used by Rules and other modules on your site. Components are for example useful if you want to use the same conditions, actions or rules in multiple places, or call them from your custom module. You may also export each component separately. See <a href="@url">the online documentation</a> for more information about how to use components.',
  99. array('@url' => rules_external_help('components'))),
  100. );
  101. $form['filter'] = array(
  102. '#type' => 'fieldset',
  103. '#title' => t('Filter'),
  104. '#collapsible' => TRUE,
  105. );
  106. $form['filter']['#id'] = 'rules-filter-form';
  107. $form['filter']['#attached']['css'][] = drupal_get_path('module', 'rules') . '/ui/rules.ui.css';
  108. $form['filter']['plugin'] = array(
  109. '#type' => 'select',
  110. '#title' => t('Filter by plugin'),
  111. '#options' => array(0 => t('<All>')) + rules_admin_component_options(),
  112. '#default_value' => $plugin,
  113. );
  114. $form['filter']['tag'] = array(
  115. '#type' => 'select',
  116. '#title' => t('Filter by tag'),
  117. '#options' => array(0 => '<All>') + RulesPluginUI::getTags(),
  118. '#default_value' => $tag,
  119. );
  120. $form['filter']['submit'] = array(
  121. '#type' => 'submit',
  122. '#value' => t('Filter'),
  123. '#name' => '', // prevent from showing up in $_GET.
  124. );
  125. $form['table'] = RulesPluginUI::overviewTable($conditions, array('hide status op' => TRUE));
  126. $form['table']['#empty'] = t('There are no rule components.');
  127. $form['filter']['#collapsed'] = $collapsed;
  128. $form['#submit'][] = 'rules_form_submit_rebuild';
  129. $form['#method'] = 'get';
  130. return $form;
  131. }
  132. /**
  133. * Rules settings form.
  134. */
  135. function rules_admin_settings($form, &$form_state) {
  136. if (module_exists('path')) {
  137. // Present a list of available path cleaning callbacks.
  138. // @see rules_clean_path()
  139. $options = array(
  140. 'rules_path_default_cleaning_method' => t('Rules (built in)'),
  141. );
  142. if (module_exists('ctools')) {
  143. $options['rules_path_clean_ctools'] = t('CTools');
  144. }
  145. if (module_exists('pathauto')) {
  146. $options['rules_path_clean_pathauto'] = t('Pathauto');
  147. $pathauto_help = t("Note that Pathauto's URL path cleaning method can be configured at <a href='!url'>admin/config/search/path/settings</a>.", array('!url' => url('admin/config/search/path/settings')));
  148. }
  149. else {
  150. $pathauto_help = t('Install the <a href="https://www.drupal.org/project/pathauto">Pathauto module</a> in order to get a configurable URL path cleaning method.');
  151. }
  152. $form['path']['rules_path_cleaning_callback'] = array(
  153. '#type' => 'select',
  154. '#title' => t('URL path cleaning method'),
  155. '#description' => t('Choose the path cleaning method to be applied when generating URL path aliases.') . ' ' . $pathauto_help,
  156. '#default_value' => variable_get('rules_path_cleaning_callback', 'rules_path_default_cleaning_method'),
  157. '#options' => $options,
  158. );
  159. }
  160. $form['rules_log_errors'] = array(
  161. '#type' => 'radios',
  162. '#title' => t('Logging of Rules evaluation errors'),
  163. '#options' => array(
  164. RulesLog::WARN => t('Log all warnings and errors'),
  165. RulesLog::ERROR => t('Log errors only'),
  166. ),
  167. '#default_value' => variable_get('rules_log_errors', RulesLog::WARN),
  168. '#description' => t('Evaluations errors are logged to the system log.'),
  169. );
  170. $form['debug'] = array(
  171. '#type' => 'fieldset',
  172. '#title' => t('Debugging'),
  173. );
  174. $form['debug']['rules_debug_log'] = array(
  175. '#type' => 'checkbox',
  176. '#title' => t('Log debug information to the system log'),
  177. '#default_value' => variable_get('rules_debug_log', FALSE),
  178. );
  179. $form['debug']['rules_debug'] = array(
  180. '#type' => 'radios',
  181. '#title' => t('Show debug information'),
  182. '#default_value' => variable_get('rules_debug', 0),
  183. '#options' => array(
  184. 0 => t('Never'),
  185. RulesLog::WARN => t('In case of errors'),
  186. RulesLog::INFO => t('Always'),
  187. ),
  188. '#description' => t('Debug information is only shown when rules are evaluated and is visible for users having the permission <a href="!url">%link</a>.', array('%link' => t('Access the Rules debug log'), '!url' => url('admin/people/permissions', array('fragment' => 'module-rules')))),
  189. );
  190. $form['debug']['regions'] = array(
  191. '#type' => 'container',
  192. '#states' => array(
  193. // Hide the regions settings when the debug log is disabled.
  194. 'invisible' => array(
  195. 'input[name="rules_debug"]' => array('value' => 0),
  196. ),
  197. ),
  198. );
  199. $theme_default = variable_get('theme_default', 'bartik');
  200. $admin_theme = variable_get('admin_theme', 'seven');
  201. $form['debug']['regions']['rules_debug_region_' . $theme_default] = array(
  202. '#type' => 'select',
  203. '#title' => t('Default theme region'),
  204. '#description' => t("The region, where the debug log should be displayed on the default theme %theme. For other themes, Rules will try to display the log on the same region, or hide it in case it doesn't exist.", array('%theme' => $theme_default)),
  205. '#options' => system_region_list($theme_default, REGIONS_VISIBLE),
  206. '#default_value' => variable_get('rules_debug_region_' . $theme_default, 'help'),
  207. );
  208. $form['debug']['regions']['rules_debug_region_' . $admin_theme] = array(
  209. '#type' => 'select',
  210. '#title' => t('Admin theme region'),
  211. '#description' => t('The region, where the debug log should be displayed on the admin theme %theme.', array('%theme' => $admin_theme)),
  212. '#options' => system_region_list($admin_theme, REGIONS_VISIBLE),
  213. '#default_value' => variable_get('rules_debug_region_' . $admin_theme, 'help'),
  214. );
  215. if (db_table_exists('rules_rules')) {
  216. drupal_set_message(t('There are left over rule configurations from a previous Rules 1.x installation. Proceed to the <a href="!url">upgrade page</a> to convert them and consult the README.txt for more details.', array('!url' => url('admin/config/workflow/rules/upgrade'))), 'warning');
  217. }
  218. return system_settings_form($form);
  219. }
  220. /**
  221. * Advanced settings form.
  222. */
  223. function rules_admin_settings_advanced($form, &$form_state) {
  224. $form['integrity'] = array(
  225. '#type' => 'fieldset',
  226. '#title' => t('Integrity'),
  227. '#description' => t('Rules checks the integrity of your configurations to discover and exclude broken configurations from evaluation.'),
  228. );
  229. $form['integrity']['start_integrity_check'] = array(
  230. '#type' => 'submit',
  231. '#value' => t('Recheck integrity'),
  232. '#submit' => array('rules_admin_settings_integrity_check_submit'),
  233. );
  234. $form['cache'] = array(
  235. '#type' => 'fieldset',
  236. '#title' => t('Cache'),
  237. '#description' => t('Rules caches information about available actions, conditions and data types. Additionally all components and reaction rules are cached for efficient evaluation.'),
  238. );
  239. $form['cache']['rebuild_rules_cache'] = array(
  240. '#type' => 'submit',
  241. '#value' => t("Rebuild Rules' cache"),
  242. '#weight' => 2,
  243. '#submit' => array('rules_admin_settings_cache_rebuild_submit'),
  244. );
  245. return $form;
  246. }
  247. /**
  248. * Form submit callback to check the integrity of all configurations.
  249. */
  250. function rules_admin_settings_integrity_check_submit($form, &$form_state) {
  251. $start = microtime(TRUE);
  252. $count = 0;
  253. $rules_configs = rules_config_load_multiple(FALSE);
  254. foreach ($rules_configs as $rules_config) {
  255. rules_config_update_dirty_flag($rules_config, TRUE, TRUE);
  256. if ($rules_config->dirty) {
  257. $count++;
  258. $variables = array('%label' => $rules_config->label(), '%name' => $rules_config->name, '@plugin' => $rules_config->plugin(), '!uri' => url(RulesPluginUI::path($rules_config->name)));
  259. drupal_set_message(t('The @plugin <a href="!uri">%label (%name)</a> fails the integrity check and cannot be executed.', $variables), 'error');
  260. }
  261. }
  262. drupal_set_message(t('Integrity of %count configurations checked in %duration seconds. %count_failed broken configurations found.', array(
  263. '%count' => count($rules_configs),
  264. '%count_failed' => $count,
  265. '%duration' => round(microtime(TRUE) - $start, 2),
  266. )));
  267. }
  268. /**
  269. * Form submit callback: Rebuild the Rules' cache.
  270. */
  271. function rules_admin_settings_cache_rebuild_submit($form, &$form_state) {
  272. $start = microtime(TRUE);
  273. rules_clear_cache();
  274. // Manually trigger cache rebuilding of all caches.
  275. rules_get_cache();
  276. _rules_rebuild_component_cache();
  277. RulesEventSet::rebuildEventCache();
  278. drupal_set_message(t('Rules cache rebuilt in %duration seconds.', array(
  279. '%duration' => round(microtime(TRUE) - $start, 2),
  280. )));
  281. }
  282. /**
  283. * Add reaction rules form.
  284. */
  285. function rules_admin_add_reaction_rule($form, &$form_state, $base_path) {
  286. RulesPluginUI::formDefaults($form, $form_state);
  287. $rules_config = isset($form_state['rules_config']) ? $form_state['rules_config'] : rules_reaction_rule();
  288. $rules_config->form($form, $form_state, array('show settings' => TRUE, 'button' => TRUE));
  289. $form['settings']['#collapsible'] = FALSE;
  290. $form['settings']['#type'] = 'container';
  291. $form['settings']['label']['#default_value'] = '';
  292. // Hide the rule elements stuff for now.
  293. foreach (array('elements', 'conditions', 'add', 'events') as $key) {
  294. $form[$key]['#access'] = FALSE;
  295. }
  296. foreach (array('active', 'weight') as $key) {
  297. $form['settings'][$key]['#access'] = FALSE;
  298. }
  299. // Incorporate the form to add the first event.
  300. $form['settings'] += rules_ui_add_event(array(), $form_state, $rules_config, $base_path);
  301. $form['settings']['event']['#tree'] = FALSE;
  302. $form['settings']['event_settings']['#tree'] = FALSE;
  303. unset($form['settings']['help']);
  304. unset($form['settings']['submit']);
  305. $form['submit']['#value'] = t('Save');
  306. $form_state += array('rules_config' => $rules_config);
  307. $form['#validate'][] = 'rules_ui_add_reaction_rule_validate';
  308. $form['#validate'][] = 'rules_ui_edit_element_validate';
  309. $form['#submit'][] = 'rules_ui_add_reaction_rule_submit';
  310. return $form;
  311. }
  312. /**
  313. * Form validation callback.
  314. */
  315. function rules_ui_add_reaction_rule_validate(&$form, &$form_state) {
  316. rules_ui_add_event_validate($form['settings'], $form_state);
  317. }
  318. /**
  319. * Form submit callback.
  320. */
  321. function rules_ui_add_reaction_rule_submit(&$form, &$form_state) {
  322. rules_ui_add_event_apply($form['settings'], $form_state);
  323. rules_ui_edit_element_submit($form, $form_state);
  324. }
  325. /**
  326. * Add component form.
  327. */
  328. function rules_admin_add_component($form, &$form_state, $base_path) {
  329. RulesPluginUI::$basePath = $base_path;
  330. RulesPluginUI::formDefaults($form, $form_state);
  331. $form['plugin_name'] = array(
  332. '#type' => 'select',
  333. '#title' => t('Component plugin'),
  334. '#options' => rules_admin_component_options(),
  335. '#description' => t('Choose which kind of component to create. Each component type is described in <a href="@url">the online documentation</a>.',
  336. array('@url' => rules_external_help('component-types'))),
  337. '#weight' => -2,
  338. '#default_value' => isset($form_state['values']['plugin_name']) ? $form_state['values']['plugin_name'] : '',
  339. );
  340. if (!isset($form_state['rules_config'])) {
  341. $form['continue'] = array(
  342. '#type' => 'submit',
  343. '#name' => 'continue',
  344. '#submit' => array('rules_admin_add_component_create_submit'),
  345. '#value' => t('Continue'),
  346. );
  347. }
  348. else {
  349. $form['plugin_name']['#disabled'] = TRUE;
  350. $form_state['rules_config']->form($form, $form_state, array('show settings' => TRUE, 'button' => TRUE, 'init' => TRUE));
  351. $form['settings']['#collapsible'] = FALSE;
  352. $form['settings']['#type'] = 'container';
  353. $form['settings']['label']['#default_value'] = '';
  354. $form['settings']['#weight'] = -1;
  355. // Hide the rule elements stuff for now.
  356. foreach (array('elements', 'negate') as $key) {
  357. $form[$key]['#access'] = FALSE;
  358. }
  359. foreach (array('active', 'weight') as $key) {
  360. $form['settings'][$key]['#access'] = FALSE;
  361. }
  362. }
  363. return $form;
  364. }
  365. function rules_admin_component_options() {
  366. $cache = rules_get_cache();
  367. return rules_extract_property(rules_filter_array($cache['plugin_info'], 'component', TRUE), 'label');
  368. }
  369. /**
  370. * Submit callback that creates the new component object initially.
  371. */
  372. function rules_admin_add_component_create_submit($form, &$form_state) {
  373. $form_state['rules_config'] = rules_plugin_factory($form_state['values']['plugin_name']);
  374. $form_state['rebuild'] = TRUE;
  375. }
  376. /**
  377. * Validation callback for adding a component.
  378. */
  379. function rules_admin_add_component_validate($form, &$form_state) {
  380. if (isset($form_state['rules_config'])) {
  381. $form_state['rules_config']->form_validate($form, $form_state);
  382. }
  383. }
  384. /**
  385. * Final submit callback for adding a component.
  386. */
  387. function rules_admin_add_component_submit($form, &$form_state) {
  388. $rules_config = $form_state['rules_config'];
  389. $rules_config->form_submit($form, $form_state);
  390. drupal_set_message(t('Your changes have been saved.'));
  391. $form_state['redirect'] = RulesPluginUI::path($rules_config->name);
  392. }