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-6.x/modules/trigger/trigger.admin.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Admin page callbacks for the trigger module.
  5. */
  6. /**
  7. * Build the form that allows users to assign actions to hooks.
  8. *
  9. * @param $type
  10. * Name of hook.
  11. * @return
  12. * HTML form.
  13. */
  14. function trigger_assign($type = NULL) {
  15. // If no type is specified we default to node actions, since they
  16. // are the most common.
  17. if (!isset($type)) {
  18. drupal_goto('admin/build/trigger/node');
  19. }
  20. if ($type == 'node') {
  21. $type = 'nodeapi';
  22. }
  23. $output = '';
  24. $hooks = module_invoke_all('hook_info');
  25. foreach ($hooks as $module => $hook) {
  26. if (isset($hook[$type])) {
  27. foreach ($hook[$type] as $op => $description) {
  28. $form_id = 'trigger_'. $type .'_'. $op .'_assign_form';
  29. $output .= drupal_get_form($form_id, $type, $op, $description['runs when']);
  30. }
  31. }
  32. }
  33. return $output;
  34. }
  35. /**
  36. * Confirm removal of an assigned action.
  37. *
  38. * @param $hook
  39. * @param $op
  40. * @param $aid
  41. * The action ID.
  42. * @ingroup forms
  43. * @see trigger_unassign_submit()
  44. */
  45. function trigger_unassign($form_state, $hook = NULL, $op = NULL, $aid = NULL) {
  46. if (!($hook && $op && $aid)) {
  47. drupal_goto('admin/build/trigger/assign');
  48. }
  49. $form['hook'] = array(
  50. '#type' => 'value',
  51. '#value' => $hook,
  52. );
  53. $form['operation'] = array(
  54. '#type' => 'value',
  55. '#value' => $op,
  56. );
  57. $form['aid'] = array(
  58. '#type' => 'value',
  59. '#value' => $aid,
  60. );
  61. $action = actions_function_lookup($aid);
  62. $actions = actions_get_all_actions();
  63. $destination = 'admin/build/trigger/'. ($hook == 'nodeapi' ? 'node' : $hook);
  64. return confirm_form($form,
  65. t('Are you sure you want to unassign the action %title?', array('%title' => $actions[$action]['description'])),
  66. $destination,
  67. t('You can assign it again later if you wish.'),
  68. t('Unassign'), t('Cancel')
  69. );
  70. }
  71. function trigger_unassign_submit($form, &$form_state) {
  72. $form_values = $form_state['values'];
  73. if ($form_values['confirm'] == 1) {
  74. $aid = actions_function_lookup($form_values['aid']);
  75. db_query("DELETE FROM {trigger_assignments} WHERE hook = '%s' AND op = '%s' AND aid = '%s'", $form_values['hook'], $form_values['operation'], $aid);
  76. $actions = actions_get_all_actions();
  77. watchdog('actions', 'Action %action has been unassigned.', array('%action' => $actions[$aid]['description']));
  78. drupal_set_message(t('Action %action has been unassigned.', array('%action' => $actions[$aid]['description'])));
  79. $hook = $form_values['hook'] == 'nodeapi' ? 'node' : $form_values['hook'];
  80. $form_state['redirect'] = 'admin/build/trigger/'. $hook;
  81. }
  82. else {
  83. drupal_goto('admin/build/trigger');
  84. }
  85. }
  86. /**
  87. * Create the form definition for assigning an action to a hook-op combination.
  88. *
  89. * @param $form_state
  90. * Information about the current form.
  91. * @param $hook
  92. * The name of the hook, e.g., 'nodeapi'.
  93. * @param $op
  94. * The name of the hook operation, e.g., 'insert'.
  95. * @param $description
  96. * A plain English description of what this hook operation does.
  97. * @return
  98. *
  99. * @ingoup forms
  100. * @see trigger_assign_form_validate()
  101. * @see trigger_assign_form_submit()
  102. */
  103. function trigger_assign_form($form_state, $hook, $op, $description) {
  104. $form['hook'] = array(
  105. '#type' => 'hidden',
  106. '#value' => $hook,
  107. );
  108. $form['operation'] = array(
  109. '#type' => 'hidden',
  110. '#value' => $op,
  111. );
  112. // All of these forms use the same validate and submit functions.
  113. $form['#validate'][] = 'trigger_assign_form_validate';
  114. $form['#submit'][] = 'trigger_assign_form_submit';
  115. $options = array();
  116. $functions = array();
  117. // Restrict the options list to actions that declare support for this hook-op
  118. // combination.
  119. foreach (actions_list() as $func => $metadata) {
  120. if (isset($metadata['hooks']['any']) || (isset($metadata['hooks'][$hook]) && is_array($metadata['hooks'][$hook]) && (in_array($op, $metadata['hooks'][$hook])))) {
  121. $functions[] = $func;
  122. }
  123. }
  124. foreach (actions_actions_map(actions_get_all_actions()) as $aid => $action) {
  125. if (in_array($action['callback'], $functions)) {
  126. $options[$action['type']][$aid] = $action['description'];
  127. }
  128. }
  129. $form[$op] = array(
  130. '#type' => 'fieldset',
  131. '#title' => t('Trigger: ') . $description,
  132. '#theme' => 'trigger_display'
  133. );
  134. // Retrieve actions that are already assigned to this hook-op combination.
  135. $actions = _trigger_get_hook_actions($hook, $op);
  136. $form[$op]['assigned']['#type'] = 'value';
  137. $form[$op]['assigned']['#value'] = array();
  138. foreach ($actions as $aid => $description) {
  139. $form[$op]['assigned']['#value'][$aid] = array(
  140. 'description' => $description,
  141. 'link' => l(t('unassign'), "admin/build/trigger/unassign/$hook/$op/". md5($aid))
  142. );
  143. }
  144. $form[$op]['parent'] = array(
  145. '#prefix' => "<div class='container-inline'>",
  146. '#suffix' => '</div>',
  147. );
  148. // List possible actions that may be assigned.
  149. if (count($options) != 0) {
  150. array_unshift($options, t('Choose an action'));
  151. $form[$op]['parent']['aid'] = array(
  152. '#type' => 'select',
  153. '#options' => $options,
  154. );
  155. $form[$op]['parent']['submit'] = array(
  156. '#type' => 'submit',
  157. '#value' => t('Assign')
  158. );
  159. }
  160. else {
  161. $form[$op]['none'] = array(
  162. '#value' => t('No available actions for this trigger.')
  163. );
  164. }
  165. return $form;
  166. }
  167. /**
  168. * Validation function for trigger_assign_form().
  169. *
  170. * Makes sure that the user is not re-assigning an action to an event.
  171. */
  172. function trigger_assign_form_validate($form, $form_state) {
  173. $form_values = $form_state['values'];
  174. if (!empty($form_values['aid'])) {
  175. $aid = actions_function_lookup($form_values['aid']);
  176. if (db_result(db_query("SELECT aid FROM {trigger_assignments} WHERE hook = '%s' AND op = '%s' AND aid = '%s'", $form_values['hook'], $form_values['operation'], $aid))) {
  177. form_set_error($form_values['operation'], t('The action you chose is already assigned to that trigger.'));
  178. }
  179. }
  180. }
  181. /**
  182. * Submit function for trigger_assign_form().
  183. */
  184. function trigger_assign_form_submit($form, $form_state) {
  185. $form_values = $form_state['values'];
  186. if (!empty($form_values['aid'])) {
  187. $aid = actions_function_lookup($form_values['aid']);
  188. $weight = db_result(db_query("SELECT MAX(weight) FROM {trigger_assignments} WHERE hook = '%s' AND op = '%s'", $form_values['hook'], $form_values['operation']));
  189. db_query("INSERT INTO {trigger_assignments} values ('%s', '%s', '%s', %d)", $form_values['hook'], $form_values['operation'], $aid, $weight + 1);
  190. // If this action changes a node property, we need to save the node
  191. // so the change will persist.
  192. $actions = actions_list();
  193. if (isset($actions[$aid]['behavior']) && in_array('changes_node_property', $actions[$aid]['behavior']) && ($form_values['operation'] != 'presave')) {
  194. // Delete previous node_save_action if it exists, and re-add a new one at a higher weight.
  195. $save_post_action_assigned = db_result(db_query("SELECT aid FROM {trigger_assignments} WHERE hook = '%s' AND op = '%s' AND aid = 'node_save_action'", $form_values['hook'], $form_values['operation']));
  196. if ($save_post_action_assigned) {
  197. db_query("DELETE FROM {trigger_assignments} WHERE hook = '%s' AND op = '%s' AND aid = 'node_save_action'", $form_values['hook'], $form_values['operation']);
  198. }
  199. db_query("INSERT INTO {trigger_assignments} VALUES ('%s', '%s', '%s', %d)", $form_values['hook'], $form_values['operation'], 'node_save_action', $weight + 2);
  200. if (!$save_post_action_assigned) {
  201. drupal_set_message(t('You have added an action that changes a the property of a post. A Save post action has been added so that the property change will be saved.'));
  202. }
  203. }
  204. }
  205. }
  206. /**
  207. * Display actions assigned to this hook-op combination in a table.
  208. *
  209. * @param array $element
  210. * The fieldset including all assigned actions.
  211. * @return
  212. * The rendered form with the table prepended.
  213. *
  214. * @ingroup themeable
  215. */
  216. function theme_trigger_display($element) {
  217. $header = array();
  218. $rows = array();
  219. if (count($element['assigned']['#value'])) {
  220. $header = array(array('data' => t('Name')), array('data' => t('Operation')));
  221. $rows = array();
  222. foreach ($element['assigned']['#value'] as $aid => $info) {
  223. $rows[] = array(
  224. filter_xss_admin($info['description']),
  225. $info['link']
  226. );
  227. }
  228. }
  229. if (count($rows)) {
  230. $output = theme('table', $header, $rows) . drupal_render($element);
  231. }
  232. else {
  233. $output = drupal_render($element);
  234. }
  235. return $output;
  236. }
  237. /**
  238. * Get the actions that have already been defined for this
  239. * type-hook-op combination.
  240. *
  241. * @param $type
  242. * One of 'node', 'user', 'comment'.
  243. * @param $hook
  244. * The name of the hook for which actions have been assigned,
  245. * e.g. 'nodeapi'.
  246. * @param $op
  247. * The hook operation for which the actions have been assigned,
  248. * e.g., 'view'.
  249. * @return
  250. * An array of action descriptions keyed by action IDs.
  251. */
  252. function _trigger_get_hook_actions($hook, $op, $type = NULL) {
  253. $actions = array();
  254. if ($type) {
  255. $result = db_query("SELECT h.aid, a.description FROM {trigger_assignments} h LEFT JOIN {actions} a on a.aid = h.aid WHERE a.type = '%s' AND h.hook = '%s' AND h.op = '%s' ORDER BY h.weight", $type, $hook, $op);
  256. }
  257. else {
  258. $result = db_query("SELECT h.aid, a.description FROM {trigger_assignments} h LEFT JOIN {actions} a on a.aid = h.aid WHERE h.hook = '%s' AND h.op = '%s' ORDER BY h.weight", $hook, $op);
  259. }
  260. while ($action = db_fetch_object($result)) {
  261. $actions[$action->aid] = $action->description;
  262. }
  263. return $actions;
  264. }