PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/modules/trigger/trigger.admin.inc

http://github.com/drupal/drupal
Pascal | 309 lines | 157 code | 22 blank | 130 comment | 22 complexity | 648360ea1ffc65f7171e3c9048cb22df MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * @file
  4. * Admin page callbacks for the trigger module.
  5. */
  6. /**
  7. * Builds the form that allows users to assign actions to triggers.
  8. *
  9. * @param $module_to_display
  10. * Which tab of triggers to display. E.g., 'node' for all
  11. * node-related triggers.
  12. * @return
  13. * HTML form.
  14. */
  15. function trigger_assign($module_to_display = NULL) {
  16. // If no type is specified we default to node actions, since they
  17. // are the most common.
  18. if (!isset($module_to_display)) {
  19. drupal_goto('admin/structure/trigger/node');
  20. }
  21. $build = array();
  22. $trigger_info = module_invoke_all('trigger_info');
  23. drupal_alter('trigger_info', $trigger_info);
  24. foreach ($trigger_info as $module => $hooks) {
  25. if ($module == $module_to_display) {
  26. foreach ($hooks as $hook => $description) {
  27. $form_id = 'trigger_' . $hook . '_assign_form';
  28. $build[$form_id] = drupal_get_form($form_id, $module, $hook, $description['label']);
  29. }
  30. }
  31. }
  32. return $build;
  33. }
  34. /**
  35. * Confirm removal of an assigned action.
  36. *
  37. * @param $module
  38. * The tab of triggers the user will be directed to after successful
  39. * removal of the action, or if the confirmation form is cancelled.
  40. * @param $hook
  41. * @param $aid
  42. * The action ID.
  43. * @ingroup forms
  44. * @see trigger_unassign_submit()
  45. */
  46. function trigger_unassign($form, $form_state, $module, $hook = NULL, $aid = NULL) {
  47. if (!($hook && $aid)) {
  48. drupal_goto('admin/structure/trigger');
  49. }
  50. $form['hook'] = array(
  51. '#type' => 'value',
  52. '#value' => $hook,
  53. );
  54. $form['module'] = array(
  55. '#type' => 'value',
  56. '#value' => $module,
  57. );
  58. $form['aid'] = array(
  59. '#type' => 'value',
  60. '#value' => $aid,
  61. );
  62. $action = actions_function_lookup($aid);
  63. $actions = actions_get_all_actions();
  64. $destination = 'admin/structure/trigger/' . $module;
  65. return confirm_form($form,
  66. t('Are you sure you want to unassign the action %title?', array('%title' => $actions[$action]['label'])),
  67. $destination,
  68. t('You can assign it again later if you wish.'),
  69. t('Unassign'), t('Cancel')
  70. );
  71. }
  72. /**
  73. * Submit callback for trigger_unassign() form.
  74. */
  75. function trigger_unassign_submit($form, &$form_state) {
  76. if ($form_state['values']['confirm'] == 1) {
  77. $aid = actions_function_lookup($form_state['values']['aid']);
  78. db_delete('trigger_assignments')
  79. ->condition('hook', $form_state['values']['hook'])
  80. ->condition('aid', $aid)
  81. ->execute();
  82. $actions = actions_get_all_actions();
  83. watchdog('actions', 'Action %action has been unassigned.', array('%action' => $actions[$aid]['label']));
  84. drupal_set_message(t('Action %action has been unassigned.', array('%action' => $actions[$aid]['label'])));
  85. $form_state['redirect'] = 'admin/structure/trigger/' . $form_state['values']['module'];
  86. }
  87. else {
  88. drupal_goto('admin/structure/trigger');
  89. }
  90. }
  91. /**
  92. * Returns the form for assigning an action to a trigger.
  93. *
  94. * @param $module
  95. * The name of the trigger group, e.g., 'node'.
  96. * @param $hook
  97. * The name of the trigger hook, e.g., 'node_insert'.
  98. * @param $label
  99. * A plain English description of what this trigger does.
  100. *
  101. * @ingoup forms
  102. * @see trigger_assign_form_validate()
  103. * @see trigger_assign_form_submit()
  104. */
  105. function trigger_assign_form($form, $form_state, $module, $hook, $label) {
  106. $form['module'] = array(
  107. '#type' => 'hidden',
  108. '#value' => $module,
  109. );
  110. $form['hook'] = array(
  111. '#type' => 'hidden',
  112. '#value' => $hook,
  113. );
  114. // All of these forms use the same validate and submit functions.
  115. $form['#validate'][] = 'trigger_assign_form_validate';
  116. $form['#submit'][] = 'trigger_assign_form_submit';
  117. $options = array();
  118. $functions = array();
  119. // Restrict the options list to actions that declare support for this hook.
  120. foreach (actions_list() as $func => $metadata) {
  121. if (isset($metadata['triggers']) && array_intersect(array($hook, 'any'), $metadata['triggers'])) {
  122. $functions[] = $func;
  123. }
  124. }
  125. foreach (actions_actions_map(actions_get_all_actions()) as $aid => $action) {
  126. if (in_array($action['callback'], $functions)) {
  127. $options[$action['type']][$aid] = $action['label'];
  128. }
  129. }
  130. $form[$hook] = array(
  131. '#type' => 'fieldset',
  132. // !description is correct, since these labels are passed through t() in
  133. // hook_trigger_info().
  134. '#title' => t('Trigger: !description', array('!description' => $label)),
  135. '#theme' => 'trigger_display',
  136. );
  137. // Retrieve actions that are already assigned to this hook combination.
  138. $actions = trigger_get_assigned_actions($hook);
  139. $form[$hook]['assigned']['#type'] = 'value';
  140. $form[$hook]['assigned']['#value'] = array();
  141. foreach ($actions as $aid => $info) {
  142. // If action is defined unassign it, otherwise offer to delete all orphaned
  143. // actions.
  144. $hash = drupal_hash_base64($aid, TRUE);
  145. if (actions_function_lookup($hash)) {
  146. $form[$hook]['assigned']['#value'][$aid] = array(
  147. 'label' => $info['label'],
  148. 'link' => l(t('unassign'), "admin/structure/trigger/unassign/$module/$hook/$hash"),
  149. );
  150. }
  151. else {
  152. // Link to system_actions_remove_orphans() to do the clean up.
  153. $form[$hook]['assigned']['#value'][$aid] = array(
  154. 'label' => $info['label'],
  155. 'link' => l(t('Remove orphaned actions'), "admin/config/system/actions/orphan"),
  156. );
  157. }
  158. }
  159. $form[$hook]['parent'] = array(
  160. '#type' => 'container',
  161. '#attributes' => array('class' => array('container-inline')),
  162. );
  163. // List possible actions that may be assigned.
  164. if (count($options) != 0) {
  165. $form[$hook]['parent']['aid'] = array(
  166. '#type' => 'select',
  167. '#title' => t('List of trigger actions when !description', array('!description' => $label)),
  168. '#title_display' => 'invisible',
  169. '#options' => $options,
  170. '#empty_option' => t('Choose an action'),
  171. );
  172. $form[$hook]['parent']['submit'] = array(
  173. '#type' => 'submit',
  174. '#value' => t('Assign')
  175. );
  176. }
  177. else {
  178. $form[$hook]['none'] = array(
  179. '#markup' => t('No actions available for this trigger. <a href="@link">Add action</a>.', array('@link' => url('admin/config/system/actions/manage')))
  180. );
  181. }
  182. return $form;
  183. }
  184. /**
  185. * Validation function for trigger_assign_form().
  186. *
  187. * Makes sure that the user is not re-assigning an action to an event.
  188. */
  189. function trigger_assign_form_validate($form, $form_state) {
  190. $form_values = $form_state['values'];
  191. if (!empty($form_values['aid'])) {
  192. $aid = actions_function_lookup($form_values['aid']);
  193. $aid_exists = db_query("SELECT aid FROM {trigger_assignments} WHERE hook = :hook AND aid = :aid", array(
  194. ':hook' => $form_values['hook'],
  195. ':aid' => $aid,
  196. ))->fetchField();
  197. if ($aid_exists) {
  198. form_set_error($form_values['hook'], t('The action you chose is already assigned to that trigger.'));
  199. }
  200. }
  201. }
  202. /**
  203. * Submit function for trigger_assign_form().
  204. */
  205. function trigger_assign_form_submit($form, &$form_state) {
  206. if (!empty($form_state['values']['aid'])) {
  207. $aid = actions_function_lookup($form_state['values']['aid']);
  208. $weight = db_query("SELECT MAX(weight) FROM {trigger_assignments} WHERE hook = :hook", array(':hook' => $form_state['values']['hook']))->fetchField();
  209. // Insert the new action.
  210. db_insert('trigger_assignments')
  211. ->fields(array(
  212. 'hook' => $form_state['values']['hook'],
  213. 'aid' => $aid,
  214. 'weight' => $weight + 1,
  215. ))
  216. ->execute();
  217. // If we are not configuring an action for a "presave" hook and this action
  218. // changes an object property, then we need to save the object, so the
  219. // property change will persist.
  220. $actions = actions_list();
  221. if (strpos($form_state['values']['hook'], 'presave') === FALSE && isset($actions[$aid]['behavior']) && in_array('changes_property', $actions[$aid]['behavior'])) {
  222. // Determine the corresponding save action name for this action.
  223. $save_action = strtok($aid, '_') . '_save_action';
  224. // If no corresponding save action exists, we need to bail out.
  225. if (!isset($actions[$save_action])) {
  226. throw new Exception(t('Missing/undefined save action (%save_aid) for %aid action.', array('%save_aid' => $aid, '%aid' => $aid)));
  227. }
  228. // Delete previous save action if it exists, and re-add it using a higher
  229. // weight.
  230. $save_action_assigned = db_query("SELECT aid FROM {trigger_assignments} WHERE hook = :hook AND aid = :aid", array(':hook' => $form_state['values']['hook'], ':aid' => $save_action))->fetchField();
  231. if ($save_action_assigned) {
  232. db_delete('trigger_assignments')
  233. ->condition('hook', $form_state['values']['hook'])
  234. ->condition('aid', $save_action)
  235. ->execute();
  236. }
  237. db_insert('trigger_assignments')
  238. ->fields(array(
  239. 'hook' => $form_state['values']['hook'],
  240. 'aid' => $save_action,
  241. 'weight' => $weight + 2,
  242. ))
  243. ->execute();
  244. // If no save action existed before, inform the user about it.
  245. if (!$save_action_assigned) {
  246. drupal_set_message(t('The %label action has been appended, which is required to save the property change.', array('%label' => $actions[$save_action]['label'])));
  247. }
  248. // Otherwise, just inform about the new weight.
  249. else {
  250. drupal_set_message(t('The %label action was moved to save the property change.', array('%label' => $actions[$save_action]['label'])));
  251. }
  252. }
  253. }
  254. }
  255. /**
  256. * Returns HTML for the form showing actions assigned to a trigger.
  257. *
  258. * @param $variables
  259. * An associative array containing:
  260. * - element: The fieldset including all assigned actions.
  261. *
  262. * @ingroup themeable
  263. */
  264. function theme_trigger_display($variables) {
  265. $element = $variables['element'];
  266. $header = array();
  267. $rows = array();
  268. if (isset($element['assigned']) && count($element['assigned']['#value'])) {
  269. $header = array(array('data' => t('Name')), array('data' => t('Operation')));
  270. $rows = array();
  271. foreach ($element['assigned']['#value'] as $aid => $info) {
  272. $rows[] = array(
  273. check_plain($info['label']),
  274. $info['link']
  275. );
  276. }
  277. }
  278. if (count($rows)) {
  279. $output = theme('table', array('header' => $header, 'rows' => $rows)) . drupal_render_children($element);
  280. }
  281. else {
  282. $output = drupal_render_children($element);
  283. }
  284. return $output;
  285. }