webmaster@1: $hook) {
webmaster@1: if (isset($hook[$type])) {
webmaster@1: foreach ($hook[$type] as $op => $description) {
webmaster@1: $form_id = 'trigger_'. $type .'_'. $op .'_assign_form';
webmaster@1: $output .= drupal_get_form($form_id, $type, $op, $description['runs when']);
webmaster@1: }
webmaster@1: }
webmaster@1: }
webmaster@1: return $output;
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Confirm removal of an assigned action.
webmaster@1: *
webmaster@1: * @param $hook
webmaster@1: * @param $op
webmaster@1: * @param $aid
webmaster@1: * The action ID.
webmaster@1: * @ingroup forms
webmaster@1: * @see trigger_unassign_submit()
webmaster@1: */
webmaster@1: function trigger_unassign($form_state, $hook = NULL, $op = NULL, $aid = NULL) {
webmaster@1: if (!($hook && $op && $aid)) {
webmaster@1: drupal_goto('admin/build/trigger/assign');
webmaster@1: }
webmaster@1:
webmaster@1: $form['hook'] = array(
webmaster@1: '#type' => 'value',
webmaster@1: '#value' => $hook,
webmaster@1: );
webmaster@1: $form['operation'] = array(
webmaster@1: '#type' => 'value',
webmaster@1: '#value' => $op,
webmaster@1: );
webmaster@1: $form['aid'] = array(
webmaster@1: '#type' => 'value',
webmaster@1: '#value' => $aid,
webmaster@1: );
webmaster@1:
webmaster@1: $action = actions_function_lookup($aid);
webmaster@1: $actions = actions_get_all_actions();
webmaster@1:
webmaster@1: $destination = 'admin/build/trigger/'. ($hook == 'nodeapi' ? 'node' : $hook);
webmaster@1:
webmaster@1: return confirm_form($form,
webmaster@1: t('Are you sure you want to unassign the action %title?', array('%title' => $actions[$action]['description'])),
webmaster@1: $destination,
webmaster@1: t('You can assign it again later if you wish.'),
webmaster@1: t('Unassign'), t('Cancel')
webmaster@1: );
webmaster@1: }
webmaster@1:
webmaster@1: function trigger_unassign_submit($form, &$form_state) {
webmaster@1: $form_values = $form_state['values'];
webmaster@1: if ($form_values['confirm'] == 1) {
webmaster@1: $aid = actions_function_lookup($form_values['aid']);
webmaster@1: db_query("DELETE FROM {trigger_assignments} WHERE hook = '%s' AND op = '%s' AND aid = '%s'", $form_values['hook'], $form_values['operation'], $aid);
webmaster@1: $actions = actions_get_all_actions();
webmaster@1: watchdog('actions', 'Action %action has been unassigned.', array('%action' => check_plain($actions[$aid]['description'])));
webmaster@1: drupal_set_message(t('Action %action has been unassigned.', array('%action' => $actions[$aid]['description'])));
webmaster@1: $hook = $form_values['hook'] == 'nodeapi' ? 'node' : $form_values['hook'];
webmaster@1: $form_state['redirect'] = 'admin/build/trigger/'. $hook;
webmaster@1: }
webmaster@1: else {
webmaster@1: drupal_goto('admin/build/trigger');
webmaster@1: }
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Create the form definition for assigning an action to a hook-op combination.
webmaster@1: *
webmaster@1: * @param $form_state
webmaster@1: * Information about the current form.
webmaster@1: * @param $hook
webmaster@1: * The name of the hook, e.g., 'nodeapi'.
webmaster@1: * @param $op
webmaster@1: * The name of the hook operation, e.g., 'insert'.
webmaster@1: * @param $description
webmaster@1: * A plain English description of what this hook operation does.
webmaster@1: * @return
webmaster@1: *
webmaster@1: * @ingoup forms
webmaster@1: * @see trigger_assign_form_validate()
webmaster@1: * @see trigger_assign_form_submit()
webmaster@1: */
webmaster@1: function trigger_assign_form($form_state, $hook, $op, $description) {
webmaster@1: $form['hook'] = array(
webmaster@1: '#type' => 'hidden',
webmaster@1: '#value' => $hook,
webmaster@1: );
webmaster@1: $form['operation'] = array(
webmaster@1: '#type' => 'hidden',
webmaster@1: '#value' => $op,
webmaster@1: );
webmaster@1: // All of these forms use the same validate and submit functions.
webmaster@1: $form['#validate'][] = 'trigger_assign_form_validate';
webmaster@1: $form['#submit'][] = 'trigger_assign_form_submit';
webmaster@1:
webmaster@1: $options = array();
webmaster@1: $functions = array();
webmaster@1: // Restrict the options list to actions that declare support for this hook-op
webmaster@1: // combination.
webmaster@1: foreach (actions_list() as $func => $metadata) {
webmaster@1: if (isset($metadata['hooks']['any']) || (isset($metadata['hooks'][$hook]) && is_array($metadata['hooks'][$hook]) && (in_array($op, $metadata['hooks'][$hook])))) {
webmaster@1: $functions[] = $func;
webmaster@1: }
webmaster@1: }
webmaster@1: foreach (actions_actions_map(actions_get_all_actions()) as $aid => $action) {
webmaster@1: if (in_array($action['callback'], $functions)) {
webmaster@1: $options[$action['type']][$aid] = $action['description'];
webmaster@1: }
webmaster@1: }
webmaster@1:
webmaster@1: $form[$op] = array(
webmaster@1: '#type' => 'fieldset',
webmaster@1: '#title' => t('Trigger: ') . $description,
webmaster@1: '#theme' => 'trigger_display'
webmaster@1: );
webmaster@1: // Retrieve actions that are already assigned to this hook-op combination.
webmaster@1: $actions = _trigger_get_hook_actions($hook, $op);
webmaster@1: $form[$op]['assigned']['#type'] = 'value';
webmaster@1: $form[$op]['assigned']['#value'] = array();
webmaster@1: foreach ($actions as $aid => $description) {
webmaster@1: $form[$op]['assigned']['#value'][$aid] = array(
webmaster@1: 'description' => $description,
webmaster@1: 'link' => l(t('unassign'), "admin/build/trigger/unassign/$hook/$op/". md5($aid))
webmaster@1: );
webmaster@1: }
webmaster@1:
webmaster@1: $form[$op]['parent'] = array(
webmaster@1: '#prefix' => "
",
webmaster@1: '#suffix' => '
',
webmaster@1: );
webmaster@1: // List possible actions that may be assigned.
webmaster@1: if (count($options) != 0) {
webmaster@1: array_unshift($options, t('Choose an action'));
webmaster@1: $form[$op]['parent']['aid'] = array(
webmaster@1: '#type' => 'select',
webmaster@1: '#options' => $options,
webmaster@1: );
webmaster@1: $form[$op]['parent']['submit'] = array(
webmaster@1: '#type' => 'submit',
webmaster@1: '#value' => t('Assign')
webmaster@1: );
webmaster@1: }
webmaster@1: else {
webmaster@1: $form[$op]['none'] = array(
webmaster@1: '#value' => t('No available actions for this trigger.')
webmaster@1: );
webmaster@1: }
webmaster@1: return $form;
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Validation function for trigger_assign_form().
webmaster@1: *
webmaster@1: * Makes sure that the user is not re-assigning an action to an event.
webmaster@1: */
webmaster@1: function trigger_assign_form_validate($form, $form_state) {
webmaster@1: $form_values = $form_state['values'];
webmaster@1: if (!empty($form_values['aid'])) {
webmaster@1: $aid = actions_function_lookup($form_values['aid']);
webmaster@1: 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))) {
webmaster@1: form_set_error($form_values['operation'], t('The action you chose is already assigned to that trigger.'));
webmaster@1: }
webmaster@1: }
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Submit function for trigger_assign_form().
webmaster@1: */
webmaster@1: function trigger_assign_form_submit($form, $form_state) {
webmaster@1: $form_values = $form_state['values'];
webmaster@1:
webmaster@1: if (!empty($form_values['aid'])) {
webmaster@1: $aid = actions_function_lookup($form_values['aid']);
webmaster@1: $weight = db_result(db_query("SELECT MAX(weight) FROM {trigger_assignments} WHERE hook = '%s' AND op = '%s'", $form_values['hook'], $form_values['operation']));
webmaster@1: db_query("INSERT INTO {trigger_assignments} values ('%s', '%s', '%s', %d)", $form_values['hook'], $form_values['operation'], $aid, $weight + 1);
webmaster@1: // If this action changes a node property, we need to save the node
webmaster@1: // so the change will persist.
webmaster@1: $actions = actions_list();
webmaster@1: if (isset($actions[$aid]['behavior']) && in_array('changes_node_property', $actions[$aid]['behavior']) && ($form_values['operation'] != 'presave')) {
webmaster@1: // Delete previous node_save_action if it exists, and re-add a new one at a higher weight.
webmaster@1: $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']));
webmaster@1: if ($save_post_action_assigned) {
webmaster@1: db_query("DELETE FROM {trigger_assignments} WHERE hook = '%s' AND op = '%s' AND aid = 'node_save_action'", $form_values['hook'], $form_values['operation']);
webmaster@1: }
webmaster@1: db_query("INSERT INTO {trigger_assignments} VALUES ('%s', '%s', '%s', %d)", $form_values['hook'], $form_values['operation'], 'node_save_action', $weight + 2);
webmaster@1: if (!$save_post_action_assigned) {
webmaster@1: 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.'));
webmaster@1: }
webmaster@1: }
webmaster@1: }
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Display actions assigned to this hook-op combination in a table.
webmaster@1: *
webmaster@1: * @param array $element
webmaster@1: * The fieldset including all assigned actions.
webmaster@1: * @return
webmaster@1: * The rendered form with the table prepended.
webmaster@1: *
webmaster@1: * @ingroup themeable
webmaster@1: */
webmaster@1: function theme_trigger_display($element) {
webmaster@1: $header = array();
webmaster@1: $rows = array();
webmaster@1: if (count($element['assigned']['#value'])) {
webmaster@1: $header = array(array('data' => t('Name')), array('data' => t('Operation')));
webmaster@1: $rows = array();
webmaster@1: foreach ($element['assigned']['#value'] as $aid => $info) {
webmaster@1: $rows[] = array(
webmaster@1: $info['description'],
webmaster@1: $info['link']
webmaster@1: );
webmaster@1: }
webmaster@1: }
webmaster@1:
webmaster@1: if (count($rows)) {
webmaster@1: $output = theme('table', $header, $rows) . drupal_render($element);
webmaster@1: }
webmaster@1: else {
webmaster@1: $output = drupal_render($element);
webmaster@1: }
webmaster@1: return $output;
webmaster@1: }
webmaster@1:
webmaster@1:
webmaster@1: /**
webmaster@1: * Get the actions that have already been defined for this
webmaster@1: * type-hook-op combination.
webmaster@1: *
webmaster@1: * @param $type
webmaster@1: * One of 'node', 'user', 'comment'.
webmaster@1: * @param $hook
webmaster@1: * The name of the hook for which actions have been assigned,
webmaster@1: * e.g. 'nodeapi'.
webmaster@1: * @param $op
webmaster@1: * The hook operation for which the actions have been assigned,
webmaster@1: * e.g., 'view'.
webmaster@1: * @return
webmaster@1: * An array of action descriptions keyed by action IDs.
webmaster@1: */
webmaster@1: function _trigger_get_hook_actions($hook, $op, $type = NULL) {
webmaster@1: $actions = array();
webmaster@1: if ($type) {
webmaster@1: $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);
webmaster@1: }
webmaster@1: else {
webmaster@1: $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);
webmaster@1: }
webmaster@1: while ($action = db_fetch_object($result)) {
webmaster@1: $actions[$action->aid] = $action->description;
webmaster@1: }
webmaster@1: return $actions;
webmaster@1: }