rules_admin.inc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. <?php
  2. /**
  3. * @file Rules Admin UI
  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. $conditions['event'] = $event;
  27. $collapsed = FALSE;
  28. }
  29. $form['help'] = array(
  30. '#type' => 'markup',
  31. '#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.',
  32. array('@url1' => url('admin/config/workflow/rules/components'),
  33. '@url2' => rules_external_help('rules'))),
  34. );
  35. $form['filter'] = array(
  36. '#type' => 'fieldset',
  37. '#title' => t('Filter'),
  38. '#collapsible' => TRUE,
  39. );
  40. $form['filter']['#id'] = 'rules-filter-form';
  41. $form['filter']['#attached']['css'][] = drupal_get_path('module', 'rules') . '/ui/rules.ui.css';
  42. $form['filter']['event'] = array(
  43. '#type' => 'select',
  44. '#title' => t('Filter by event'),
  45. '#options' => array(0 => t('<All>')) + RulesPluginUI::getOptions('event'),
  46. '#default_value' => $event,
  47. );
  48. $form['filter']['tag'] = array(
  49. '#type' => 'select',
  50. '#title' => t('Filter by tag'),
  51. '#options' => array(0 => t('<All>')) + RulesPluginUI::getTags(),
  52. '#default_value' => $tag,
  53. );
  54. $form['filter']['submit'] = array(
  55. '#type' => 'submit',
  56. '#value' => t('Filter'),
  57. '#name' => '', // prevent from showing up in $_GET.
  58. );
  59. $options = array('show plugin' => FALSE, 'base path' => $base_path);
  60. $form['active'] = rules_ui()->overviewTable($conditions, $options);
  61. $form['active']['#caption'] = t('Active rules');
  62. $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')));
  63. $conditions['active'] = FALSE;
  64. $form['inactive'] = rules_ui()->overviewTable($conditions, $options);
  65. $form['inactive']['#caption'] = t('Inactive rules');
  66. $form['inactive']['#empty'] = t('There are no inactive rules.');
  67. $form['filter']['#collapsed'] = $collapsed;
  68. $form['#submit'][] = 'rules_form_submit_rebuild';
  69. $form['#method'] = 'get';
  70. return $form;
  71. }
  72. /**
  73. * Components overview.
  74. */
  75. function rules_admin_components_overview($form, &$form_state, $base_path) {
  76. RulesPluginUI::formDefaults($form, $form_state);
  77. $collapsed = TRUE;
  78. if (empty($_GET['tag'])) {
  79. $tag = 0;
  80. }
  81. else {
  82. $tag = $_GET['tag'];
  83. $conditions['tags'] = array($tag);
  84. $collapsed = FALSE;
  85. }
  86. if (empty($_GET['plugin'])) {
  87. // Get the plugin name usable as component.
  88. $conditions['plugin'] = array_keys(rules_filter_array(rules_fetch_data('plugin_info'), 'component', TRUE));
  89. $plugin = 0;
  90. }
  91. else {
  92. $plugin = $_GET['plugin'];
  93. $conditions['plugin'] = $plugin;
  94. $collapsed = FALSE;
  95. }
  96. $form['help'] = array(
  97. '#type' => 'markup',
  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="http://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', 0),
  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 = 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. unset($form['settings']['help']);
  303. unset($form['settings']['submit']);
  304. $form['submit']['#value'] = t('Save');
  305. $form_state += array('rules_config' => $rules_config);
  306. $form['#validate'][] = 'rules_ui_edit_element_validate';
  307. $form['#submit'][] = 'rules_ui_add_event_apply';
  308. $form['#submit'][] = 'rules_ui_edit_element_submit';
  309. return $form;
  310. }
  311. /**
  312. * Add component form.
  313. */
  314. function rules_admin_add_component($form, &$form_state, $base_path) {
  315. RulesPluginUI::$basePath = $base_path;
  316. RulesPluginUI::formDefaults($form, $form_state);
  317. $form['plugin_name'] = array(
  318. '#type' => 'select',
  319. '#title' => t('Component plugin'),
  320. '#options' => rules_admin_component_options(),
  321. '#description' => t('Choose which kind of component to create. Each component type is described in <a href="@url">the online documentation</a>.',
  322. array('@url' => rules_external_help('component-types'))),
  323. '#weight' => -2,
  324. '#default_value' => isset($form_state['values']['plugin_name']) ? $form_state['values']['plugin_name'] : '',
  325. );
  326. if (!isset($form_state['rules_config'])) {
  327. $form['continue'] = array(
  328. '#type' => 'submit',
  329. '#name' => 'continue',
  330. '#submit' => array('rules_admin_add_component_create_submit'),
  331. '#value' => t('Continue'),
  332. );
  333. }
  334. else {
  335. $form['plugin_name']['#disabled'] = TRUE;
  336. $form_state['rules_config']->form($form, $form_state, array('show settings' => TRUE, 'button' => TRUE, 'init' => TRUE));
  337. $form['settings']['#collapsible'] = FALSE;
  338. $form['settings']['#type'] = 'container';
  339. $form['settings']['label']['#default_value'] = '';
  340. $form['settings']['#weight'] = -1;
  341. // Hide the rule elements stuff for now.
  342. foreach (array('elements', 'negate') as $key) {
  343. $form[$key]['#access'] = FALSE;
  344. }
  345. foreach (array('active', 'weight') as $key) {
  346. $form['settings'][$key]['#access'] = FALSE;
  347. }
  348. }
  349. return $form;
  350. }
  351. function rules_admin_component_options() {
  352. $cache = rules_get_cache();
  353. return rules_extract_property(rules_filter_array($cache['plugin_info'], 'component', TRUE), 'label');
  354. }
  355. /**
  356. * Submit callback that creates the new component object initially.
  357. */
  358. function rules_admin_add_component_create_submit($form, &$form_state) {
  359. $form_state['rules_config'] = rules_plugin_factory($form_state['values']['plugin_name']);
  360. $form_state['rebuild'] = TRUE;
  361. }
  362. /**
  363. * Validation callback for adding a component.
  364. */
  365. function rules_admin_add_component_validate($form, &$form_state) {
  366. if (isset($form_state['rules_config'])) {
  367. $form_state['rules_config']->form_validate($form, $form_state);
  368. }
  369. }
  370. /**
  371. * Final submit callback for adding a component.
  372. */
  373. function rules_admin_add_component_submit($form, &$form_state) {
  374. $rules_config = $form_state['rules_config'];
  375. $rules_config->form_submit($form, $form_state);
  376. drupal_set_message(t('Your changes have been saved.'));
  377. $form_state['redirect'] = RulesPluginUI::path($rules_config->name);
  378. }