trigger.admin.inc

  1. 7.x drupal-7.x/modules/trigger/trigger.admin.inc
  2. 6.x drupal-6.x/modules/trigger/trigger.admin.inc

Admin page callbacks for the trigger module.

File

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