PageRenderTime 75ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/rules/ui/ui.controller.inc

#
Pascal | 315 lines | 95 code | 14 blank | 206 comment | 25 complexity | f4a901df03250b28bc509a338a2bfd58 MD5 | raw file
Possible License(s): GPL-2.0
  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', $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. if (!empty($options['base path'])) {
  191. RulesPluginUI::$basePath = $options['base path'];
  192. }
  193. else if (!isset(RulesPluginUI::$basePath)) {
  194. // Default to the current path, only if no path has been set yet.
  195. RulesPluginUI::$basePath = current_path();
  196. }
  197. $entities = entity_load('rules_config', FALSE, $conditions);
  198. ksort($entities);
  199. // Prepare some variables used by overviewTableRow().
  200. $this->event_info = rules_fetch_data('event_info');
  201. $this->cache = rules_get_cache();
  202. $rows = array();
  203. foreach ($entities as $id => $entity) {
  204. if (user_access('bypass rules access') || $entity->access()) {
  205. $rows[] = $this->overviewTableRow($conditions, $id, $entity, $options);
  206. }
  207. }
  208. // Assemble the right table header.
  209. $header = array(t('Name'), t('Event'), t('Plugin'), t('Status'), array('data' => t('Operations')));
  210. if (!$options['show events']) {
  211. // Remove the event heading as there is no such column.
  212. unset($header[1]);
  213. }
  214. if (!$options['show plugin']) {
  215. unset($header[2]);
  216. }
  217. // Fix the header operation column colspan.
  218. $num_cols = isset($rows[0]) ? count($rows[0]) : 0;
  219. if (($addition = $num_cols - count($header)) > 0) {
  220. $header[4]['colspan'] = $addition + 1;
  221. }
  222. $table = array(
  223. '#theme' => 'table',
  224. '#header' => $header,
  225. '#rows' => $rows,
  226. '#empty' => t('None.'),
  227. );
  228. $table['#attributes']['class'][] = 'rules-overview-table';
  229. $table['#attached']['css'][] = drupal_get_path('module', 'rules') . '/ui/rules.ui.css';
  230. // TODO: hide configs where access() is FALSE.
  231. return $table;
  232. }
  233. /**
  234. * Generates the row for a single rules config.
  235. *
  236. * @param $additional_cols
  237. * Additional columns to be added after the entity label column.
  238. */
  239. protected function overviewTableRow($conditions, $name, $config, $options) {
  240. // Build content includes the label, as well as a short overview including
  241. // the machine name.
  242. $row[] = array('data' => $config->buildContent());
  243. // Add events if the configs are assigned to events.
  244. if ($options['show events']) {
  245. $events = array();
  246. if ($config instanceof RulesTriggerableInterface) {
  247. foreach ($config->events() as $event_name) {
  248. $this->event_info += array($event_name => array('label' => t('Unknown event "!event_name"', array('!event_name' => $event_name))));
  249. $events[] = check_plain($this->event_info[$event_name]['label']);
  250. }
  251. }
  252. $row[] = implode(", ", $events);
  253. }
  254. if ($options['show plugin']) {
  255. $plugin = $config->plugin();
  256. $row[] = isset($this->cache['plugin_info'][$plugin]['label']) ? $this->cache['plugin_info'][$plugin]['label'] : $plugin;
  257. }
  258. $row[] = array('data' => array(
  259. '#theme' => 'entity_status',
  260. '#status' => $config->status,
  261. ));
  262. // Add operations depending on the options and the exportable status.
  263. if (!$config->hasStatus(ENTITY_FIXED)) {
  264. $row[] = l(t('edit'), RulesPluginUI::path($name));
  265. $row[] = l(t('translate'), RulesPluginUI::path($name, 'translate'));
  266. }
  267. else {
  268. $row[] = '';
  269. $row[] = '';
  270. }
  271. if (!$options['hide status op']) {
  272. // Add either an enable or disable link.
  273. $text = $config->active ? t('disable') : t('enable');
  274. $link_path = RulesPluginUI::path($name, $config->active ? 'disable' : 'enable');
  275. $row[] = $config->hasStatus(ENTITY_FIXED) ? '' : l($text, $link_path, array('query' => drupal_get_destination()));
  276. }
  277. $row[] = l(t('clone'), RulesPluginUI::path($name, 'clone'));
  278. // Add execute link for for components.
  279. if ($options['show execution op']) {
  280. $row[] = ($config instanceof RulesTriggerableInterface) ? '' : l(t('execute'), RulesPluginUI::path($name, 'execute'), array('query' => drupal_get_destination()));
  281. if (module_exists('rules_scheduler')) {
  282. // Add schedule link for action components only.
  283. $row[] = $config instanceof RulesActionInterface ? l(t('schedule'), RulesPluginUI::path($name, 'schedule'), array('query' => drupal_get_destination())) : '';
  284. }
  285. }
  286. if (!$config->hasStatus(ENTITY_IN_CODE) && !$config->hasStatus(ENTITY_FIXED)) {
  287. $row[] = l(t('delete'), RulesPluginUI::path($name, 'delete'), array('query' => drupal_get_destination()));
  288. }
  289. elseif ($config->hasStatus(ENTITY_OVERRIDDEN) && !$config->hasStatus(ENTITY_FIXED)) {
  290. $row[] = l(t('revert'), RulesPluginUI::path($name, 'revert'), array('query' => drupal_get_destination()));
  291. }
  292. else {
  293. $row[] = '';
  294. }
  295. $row[] = l(t('export'), RulesPluginUI::path($name, 'export'));
  296. return $row;
  297. }
  298. }