ui.controller.inc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. <?php
  2. /**
  3. * @file Contains the UI controller for Rules.
  4. */
  5. /**
  6. * Controller class for the Rules UI.
  7. *
  8. * The Rules UI controller defines the methods other modules may use in order
  9. * to easily re-use the UI, regardless whether the rules admin module is
  10. * enabled.
  11. */
  12. class RulesUIController {
  13. /**
  14. * Generates menu items to manipulate rules configurations.
  15. *
  16. * @param $base_path
  17. * The path to the overview page from where the configurations are edited.
  18. */
  19. public function config_menu($base_path) {
  20. $items = array();
  21. $base_count = count(explode('/', $base_path));
  22. $items[$base_path . '/manage/%rules_config'] = array(
  23. 'title callback' => 'rules_get_title',
  24. 'title arguments' => array('Editing !plugin "!label"', $base_count + 1),
  25. 'page callback' => 'drupal_get_form',
  26. 'page arguments' => array('rules_ui_form_edit_rules_config', $base_count + 1, $base_path),
  27. 'access callback' => 'rules_config_access',
  28. 'access arguments' => array('update', $base_count + 1),
  29. 'type' => MENU_VISIBLE_IN_BREADCRUMB,
  30. 'file' => 'ui/ui.forms.inc',
  31. 'file path' => drupal_get_path('module', 'rules'),
  32. );
  33. $items[$base_path . '/manage/%rules_config/add/%rules_element'] = array(
  34. // Adding another part to the path would hit the menu path-part-limit
  35. // for base paths like admin/config/workflow/rules. Therefor we have to
  36. // use this fugly way for setting the title.
  37. 'title callback' => 'rules_menu_add_element_title',
  38. // Wrap the integer in an array, so it is passed as is.
  39. 'title arguments' => array(array($base_count + 4)),
  40. 'page callback' => 'drupal_get_form',
  41. 'page arguments' => array('rules_ui_add_element', $base_count + 1, $base_count + 4, $base_count + 3, $base_path),
  42. 'access callback' => 'rules_config_access',
  43. 'access arguments' => array('update', $base_count + 1),
  44. 'load arguments' => array($base_count + 1),
  45. 'file' => 'ui/ui.forms.inc',
  46. 'file path' => drupal_get_path('module', 'rules'),
  47. );
  48. $items[$base_path . '/manage/%rules_config/add/event'] = array(
  49. 'title callback' => 'rules_get_title',
  50. 'title arguments' => array('Adding event to !plugin "!label"', $base_count + 1),
  51. 'page callback' => 'drupal_get_form',
  52. 'page arguments' => array('rules_ui_add_event_page', $base_count + 1, $base_path),
  53. 'access callback' => 'rules_config_access',
  54. 'access arguments' => array('update', $base_count + 1),
  55. 'load arguments' => array($base_count + 1),
  56. 'file' => 'ui/ui.forms.inc',
  57. 'file path' => drupal_get_path('module', 'rules'),
  58. );
  59. $items[$base_path . '/manage/%rules_config/delete/event'] = array(
  60. //@todo: improve title.
  61. 'title' => 'Remove event',
  62. 'page callback' => 'drupal_get_form',
  63. 'page arguments' => array('rules_ui_remove_event', $base_count + 1, $base_count + 4, $base_path),
  64. 'access callback' => 'rules_config_access',
  65. 'access arguments' => array('update', $base_count + 1),
  66. 'description' => 'Remove an event from a reaction rule.',
  67. 'file' => 'ui/ui.forms.inc',
  68. 'file path' => drupal_get_path('module', 'rules'),
  69. );
  70. $items[$base_path . '/manage/%rules_config/edit/%rules_element'] = array(
  71. 'title callback' => 'rules_get_title',
  72. 'title arguments' => array('Editing !plugin "!label"', $base_count + 3),
  73. 'page callback' => 'drupal_get_form',
  74. 'page arguments' => array('rules_ui_edit_element', $base_count + 1, $base_count + 3, $base_path),
  75. 'access callback' => 'rules_config_access',
  76. 'access arguments' => array('update', $base_count + 1),
  77. 'load arguments' => array($base_count + 1),
  78. 'file' => 'ui/ui.forms.inc',
  79. 'file path' => drupal_get_path('module', 'rules'),
  80. );
  81. $items[$base_path . '/manage/%rules_config/autocomplete'] = array(
  82. 'page callback' => 'rules_ui_form_data_selection_auto_completion',
  83. 'page arguments' => array($base_count + 3, $base_count + 4, $base_count + 5),
  84. 'access callback' => 'rules_config_access',
  85. 'access arguments' => array('update', $base_count + 1),
  86. 'type' => MENU_CALLBACK,
  87. 'file' => 'ui/ui.forms.inc',
  88. 'file path' => drupal_get_path('module', 'rules'),
  89. );
  90. $items[$base_path . '/manage/%rules_config/delete/%rules_element'] = array(
  91. 'title callback' => 'rules_get_title',
  92. 'title arguments' => array('Editing !plugin "!label"', $base_count + 3),
  93. 'page callback' => 'drupal_get_form',
  94. 'page arguments' => array('rules_ui_delete_element', $base_count + 1, $base_count + 3, $base_path),
  95. 'access callback' => 'rules_config_access',
  96. 'access arguments' => array('update', $base_count + 1),
  97. 'load arguments' => array($base_count + 1),
  98. 'file' => 'ui/ui.forms.inc',
  99. 'file path' => drupal_get_path('module', 'rules'),
  100. );
  101. $items[$base_path . '/manage/%rules_config/%'] = array(
  102. 'page callback' => 'drupal_get_form',
  103. 'page arguments' => array('rules_ui_form_rules_config_confirm_op', $base_count + 1, $base_count + 2, $base_path),
  104. 'access callback' => 'rules_config_access',
  105. 'access arguments' => array('update', $base_count + 1),
  106. 'file' => 'ui/ui.forms.inc',
  107. 'file path' => drupal_get_path('module', 'rules'),
  108. );
  109. $items[$base_path . '/manage/%rules_config/clone'] = array(
  110. 'title callback' => 'rules_get_title',
  111. 'title arguments' => array('Cloning !plugin "!label"', $base_count + 1),
  112. 'page callback' => 'drupal_get_form',
  113. 'page arguments' => array('rules_ui_form_clone_rules_config', $base_count + 1, $base_path),
  114. 'access callback' => 'rules_config_access',
  115. 'access arguments' => array('update', $base_count + 1),
  116. 'file' => 'ui/ui.forms.inc',
  117. 'file path' => drupal_get_path('module', 'rules'),
  118. );
  119. $items[$base_path . '/manage/%rules_config/export'] = array(
  120. 'title callback' => 'rules_get_title',
  121. 'title arguments' => array('Export of !plugin "!label"', $base_count + 1),
  122. 'page callback' => 'drupal_get_form',
  123. 'page arguments' => array('rules_ui_form_export_rules_config', $base_count + 1, $base_path),
  124. 'access callback' => 'rules_config_access',
  125. 'access arguments' => array('view', $base_count + 1),
  126. 'file' => 'ui/ui.forms.inc',
  127. 'file path' => drupal_get_path('module', 'rules'),
  128. );
  129. $items[$base_path . '/manage/%rules_config/execute'] = array(
  130. 'title callback' => 'rules_get_title',
  131. 'title arguments' => array('Executing !plugin "!label"', $base_count + 1),
  132. 'page callback' => 'drupal_get_form',
  133. 'page arguments' => array('rules_ui_form_execute_rules_config', $base_count + 1, $base_path),
  134. 'access callback' => 'rules_config_access',
  135. 'access arguments' => array('update', $base_count + 1),
  136. 'file' => 'ui/ui.forms.inc',
  137. 'file path' => drupal_get_path('module', 'rules'),
  138. );
  139. drupal_alter('rules_ui_menu', $items, $base_path, $base_count);
  140. if (module_exists('rules_scheduler')) {
  141. $items[$base_path . '/manage/%rules_config/schedule'] = array(
  142. 'title callback' => 'rules_get_title',
  143. 'title arguments' => array('Schedule !plugin "!label"', $base_count + 1),
  144. 'page callback' => 'drupal_get_form',
  145. 'page arguments' => array('rules_scheduler_schedule_form', $base_count + 1, $base_path),
  146. 'access callback' => 'rules_config_access',
  147. 'access arguments' => array('update', $base_count + 1),
  148. 'file' => 'rules_scheduler.admin.inc',
  149. 'file path' => drupal_get_path('module', 'rules_scheduler'),
  150. );
  151. }
  152. return $items;
  153. }
  154. /**
  155. * Generates the render array for a overview configuration table for arbitrary
  156. * rule configs that match the given conditions.
  157. *
  158. * Note: The generated overview table contains multiple links for editing the
  159. * rule configurations. For the links to properly work use
  160. * RulesUIController::config_menu($base_path) to generate appropriate menu
  161. * items for the path at which the overview table is displayed.
  162. *
  163. * @param $conditions
  164. * An array of conditions as needed by rules_config_load_multiple().
  165. * @param $options
  166. * An array with optional options. Known keys are:
  167. * - 'hide status op': If set to TRUE, enable/disable links are not added.
  168. * Defaults to FALSE.
  169. * - 'show plugin': If set to FALSE, the plugin is not shown. Defaults to
  170. * TRUE.
  171. * - 'show events': If set to TRUE, the event column is shown. Defaults to
  172. * TRUE if only reaction rules are listed.
  173. * - 'show execution op': If set to TRUE an operation for execution a
  174. * component is shown for components, as well as a link to schedule a
  175. * component if the rules scheduler module is enabled.
  176. * - 'base path': Optionally, a different base path to use instead of the
  177. * currently set RulesPluginUI::$basePath. If no base path has been set
  178. * yet, the current path is used by default.
  179. *
  180. * @return Array
  181. * A renderable array.
  182. */
  183. public function overviewTable($conditions = array(), $options = array()) {
  184. $options += array(
  185. 'hide status op' => FALSE,
  186. 'show plugin' => TRUE,
  187. 'show events' => isset($conditions['plugin']) && $conditions['plugin'] == 'reaction rule',
  188. 'show execution op' => !(isset($conditions['plugin']) && $conditions['plugin'] == 'reaction rule'),
  189. );
  190. // By default show only configurations owned by rules.
  191. $conditions += array(
  192. 'owner' => 'rules',
  193. );
  194. if (!empty($options['base path'])) {
  195. RulesPluginUI::$basePath = $options['base path'];
  196. }
  197. else if (!isset(RulesPluginUI::$basePath)) {
  198. // Default to the current path, only if no path has been set yet.
  199. RulesPluginUI::$basePath = current_path();
  200. }
  201. $entities = entity_load('rules_config', FALSE, $conditions);
  202. ksort($entities);
  203. // Prepare some variables used by overviewTableRow().
  204. $this->event_info = rules_fetch_data('event_info');
  205. $this->cache = rules_get_cache();
  206. $rows = array();
  207. foreach ($entities as $id => $entity) {
  208. if (user_access('bypass rules access') || $entity->access()) {
  209. $rows[] = $this->overviewTableRow($conditions, $id, $entity, $options);
  210. }
  211. }
  212. // Assemble the right table header.
  213. $header = array(t('Name'), t('Event'), t('Plugin'), t('Status'), array('data' => t('Operations')));
  214. if (!$options['show events']) {
  215. // Remove the event heading as there is no such column.
  216. unset($header[1]);
  217. }
  218. if (!$options['show plugin']) {
  219. unset($header[2]);
  220. }
  221. // Fix the header operation column colspan.
  222. $num_cols = isset($rows[0]) ? count($rows[0]) : 0;
  223. if (($addition = $num_cols - count($header)) > 0) {
  224. $header[4]['colspan'] = $addition + 1;
  225. }
  226. $table = array(
  227. '#theme' => 'table',
  228. '#header' => $header,
  229. '#rows' => $rows,
  230. '#empty' => t('None.'),
  231. );
  232. $table['#attributes']['class'][] = 'rules-overview-table';
  233. $table['#attached']['css'][] = drupal_get_path('module', 'rules') . '/ui/rules.ui.css';
  234. // TODO: hide configs where access() is FALSE.
  235. return $table;
  236. }
  237. /**
  238. * Generates the row for a single rules config.
  239. *
  240. * @param $additional_cols
  241. * Additional columns to be added after the entity label column.
  242. */
  243. protected function overviewTableRow($conditions, $name, $config, $options) {
  244. // Build content includes the label, as well as a short overview including
  245. // the machine name.
  246. $row[] = array('data' => $config->buildContent());
  247. // Add events if the configs are assigned to events.
  248. if ($options['show events']) {
  249. $events = array();
  250. if ($config instanceof RulesTriggerableInterface) {
  251. foreach ($config->events() as $event_name) {
  252. $event_handler = rules_get_event_handler($event_name, $config->getEventSettings($event_name));
  253. $events[] = $event_handler->summary();
  254. }
  255. }
  256. $row[] = implode(", ", $events);
  257. }
  258. if ($options['show plugin']) {
  259. $plugin = $config->plugin();
  260. $row[] = isset($this->cache['plugin_info'][$plugin]['label']) ? $this->cache['plugin_info'][$plugin]['label'] : $plugin;
  261. }
  262. $row[] = array('data' => array(
  263. '#theme' => 'entity_status',
  264. '#status' => $config->status,
  265. ));
  266. // Add operations depending on the options and the exportable status.
  267. if (!$config->hasStatus(ENTITY_FIXED)) {
  268. $row[] = l(t('edit'), RulesPluginUI::path($name), array('attributes' => array('class' => array('edit', 'action'))));
  269. if (module_exists('rules_i18n')) {
  270. $row[] = l(t('translate'), RulesPluginUI::path($name, 'translate'), array('attributes' => array('class' => array('translate', 'action'))));
  271. }
  272. }
  273. else {
  274. $row[] = '';
  275. if (module_exists('rules_i18n')) {
  276. $row[] = '';
  277. }
  278. }
  279. if (!$options['hide status op']) {
  280. // Add either an enable or disable link.
  281. $text = $config->active ? t('disable') : t('enable');
  282. $active_class = $config->active ? 'disable' : 'enable';
  283. $link_path = RulesPluginUI::path($name, $active_class);
  284. $row[] = $config->hasStatus(ENTITY_FIXED) ? '' : l($text, $link_path, array('attributes' => array('class' => array($active_class, 'action')), 'query' => drupal_get_destination()));
  285. }
  286. $row[] = l(t('clone'), RulesPluginUI::path($name, 'clone'), array('attributes' => array('class' => array('clone', 'action'))));
  287. // Add execute link for for components.
  288. if ($options['show execution op']) {
  289. $row[] = ($config instanceof RulesTriggerableInterface) ? '' : l(t('execute'), RulesPluginUI::path($name, 'execute'), array('attributes' => array('class' => array('execute', 'action')), 'query' => drupal_get_destination()));
  290. if (module_exists('rules_scheduler')) {
  291. // Add schedule link for action components only.
  292. $row[] = $config instanceof RulesActionInterface ? l(t('schedule'), RulesPluginUI::path($name, 'schedule'), array('attributes' => array('class' => array('schedule', 'action')), 'query' => drupal_get_destination())) : '';
  293. }
  294. }
  295. if (!$config->hasStatus(ENTITY_IN_CODE) && !$config->hasStatus(ENTITY_FIXED)) {
  296. $row[] = l(t('delete'), RulesPluginUI::path($name, 'delete'), array('attributes' => array('class' => array('delete', 'action')), 'query' => drupal_get_destination()));
  297. }
  298. elseif ($config->hasStatus(ENTITY_OVERRIDDEN) && !$config->hasStatus(ENTITY_FIXED)) {
  299. $row[] = l(t('revert'), RulesPluginUI::path($name, 'revert'), array('attributes' => array('class' => array('revert', 'action')), 'query' => drupal_get_destination()));
  300. }
  301. else {
  302. $row[] = '';
  303. }
  304. $row[] = l(t('export'), RulesPluginUI::path($name, 'export'), array('attributes' => array('class' => array('export', 'action'))));
  305. return $row;
  306. }
  307. }