ui.controller.inc 15 KB

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