00001 <?php
00002
00003
00017 function trigger_assign($type = NULL) {
00018
00019
00020 if (!isset($type)) {
00021 drupal_goto('admin/build/trigger/node');
00022 }
00023 if ($type == 'node') {
00024 $type = 'nodeapi';
00025 }
00026
00027 $output = '';
00028 $hooks = module_invoke_all('hook_info');
00029 foreach ($hooks as $module => $hook) {
00030 if (isset($hook[$type])) {
00031 foreach ($hook[$type] as $op => $description) {
00032 $form_id = 'trigger_' . $type . '_' . $op . '_assign_form';
00033 $output .= drupal_get_form($form_id, $type, $op, $description['runs when']);
00034 }
00035 }
00036 }
00037 return $output;
00038 }
00039
00050 function trigger_unassign($form_state, $hook = NULL, $op = NULL, $aid = NULL) {
00051 if (!($hook && $op && $aid)) {
00052 drupal_goto('admin/build/trigger/assign');
00053 }
00054
00055 $form['hook'] = array(
00056 '#type' => 'value',
00057 '#value' => $hook,
00058 );
00059 $form['operation'] = array(
00060 '#type' => 'value',
00061 '#value' => $op,
00062 );
00063 $form['aid'] = array(
00064 '#type' => 'value',
00065 '#value' => $aid,
00066 );
00067
00068 $action = actions_function_lookup($aid);
00069 $actions = actions_get_all_actions();
00070
00071 $destination = 'admin/build/trigger/' . ($hook == 'nodeapi' ? 'node' : $hook);
00072
00073 return confirm_form($form,
00074 t('Are you sure you want to unassign the action %title?', array('%title' => $actions[$action]['description'])),
00075 $destination,
00076 t('You can assign it again later if you wish.'),
00077 t('Unassign'), t('Cancel')
00078 );
00079 }
00080
00081 function trigger_unassign_submit($form, &$form_state) {
00082 $form_values = $form_state['values'];
00083 if ($form_values['confirm'] == 1) {
00084 $aid = actions_function_lookup($form_values['aid']);
00085 db_query("DELETE FROM {trigger_assignments} WHERE hook = '%s' AND op = '%s' AND aid = '%s'", $form_values['hook'], $form_values['operation'], $aid);
00086 $actions = actions_get_all_actions();
00087 watchdog('actions', 'Action %action has been unassigned.', array('%action' => check_plain($actions[$aid]['description'])));
00088 drupal_set_message(t('Action %action has been unassigned.', array('%action' => $actions[$aid]['description'])));
00089 $hook = $form_values['hook'] == 'nodeapi' ? 'node' : $form_values['hook'];
00090 $form_state['redirect'] = 'admin/build/trigger/' . $hook;
00091 }
00092 else {
00093 drupal_goto('admin/build/trigger');
00094 }
00095 }
00096
00114 function trigger_assign_form($form_state, $hook, $op, $description) {
00115 $form['hook'] = array(
00116 '#type' => 'hidden',
00117 '#value' => $hook,
00118 );
00119 $form['operation'] = array(
00120 '#type' => 'hidden',
00121 '#value' => $op,
00122 );
00123
00124 $form['#validate'][] = 'trigger_assign_form_validate';
00125 $form['#submit'][] = 'trigger_assign_form_submit';
00126
00127 $options = array();
00128 $functions = array();
00129
00130
00131 foreach (actions_list() as $func => $metadata) {
00132 if (isset($metadata['hooks']['any']) || (isset($metadata['hooks'][$hook]) && is_array($metadata['hooks'][$hook]) && (in_array($op, $metadata['hooks'][$hook])))) {
00133 $functions[] = $func;
00134 }
00135 }
00136 foreach (actions_actions_map(actions_get_all_actions()) as $aid => $action) {
00137 if (in_array($action['callback'], $functions)) {
00138 $options[$action['type']][$aid] = $action['description'];
00139 }
00140 }
00141
00142 $form[$op] = array(
00143 '#type' => 'fieldset',
00144 '#title' => t('Trigger: ') . $description,
00145 '#theme' => 'trigger_display'
00146 );
00147
00148 $actions = _trigger_get_hook_actions($hook, $op);
00149 $form[$op]['assigned']['#type'] = 'value';
00150 $form[$op]['assigned']['#value'] = array();
00151 foreach ($actions as $aid => $description) {
00152 $form[$op]['assigned']['#value'][$aid] = array(
00153 'description' => $description,
00154 'link' => l(t('unassign'), "admin/build/trigger/unassign/$hook/$op/" . md5($aid))
00155 );
00156 }
00157
00158 $form[$op]['parent'] = array(
00159 '#prefix' => "<div class='container-inline'>",
00160 '#suffix' => '</div>',
00161 );
00162
00163 if (count($options) != 0) {
00164 array_unshift($options, t('Choose an action'));
00165 $form[$op]['parent']['aid'] = array(
00166 '#type' => 'select',
00167 '#options' => $options,
00168 );
00169 $form[$op]['parent']['submit'] = array(
00170 '#type' => 'submit',
00171 '#value' => t('Assign')
00172 );
00173 }
00174 else {
00175 $form[$op]['none'] = array(
00176 '#value' => t('No available actions for this trigger.')
00177 );
00178 }
00179 return $form;
00180 }
00181
00187 function trigger_assign_form_validate($form, $form_state) {
00188 $form_values = $form_state['values'];
00189 if (!empty($form_values['aid'])) {
00190 $aid = actions_function_lookup($form_values['aid']);
00191 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))) {
00192 form_set_error($form_values['operation'], t('The action you chose is already assigned to that trigger.'));
00193 }
00194 }
00195 }
00196
00200 function trigger_assign_form_submit($form, $form_state) {
00201 $form_values = $form_state['values'];
00202
00203 if (!empty($form_values['aid'])) {
00204 $aid = actions_function_lookup($form_values['aid']);
00205 $weight = db_result(db_query("SELECT MAX(weight) FROM {trigger_assignments} WHERE hook = '%s' AND op = '%s'", $form_values['hook'], $form_values['operation']));
00206 db_query("INSERT INTO {trigger_assignments} values ('%s', '%s', '%s', %d)", $form_values['hook'], $form_values['operation'], $aid, $weight + 1);
00207
00208
00209 $actions = actions_list();
00210 if (isset($actions[$aid]['behavior']) && in_array('changes_node_property', $actions[$aid]['behavior']) && ($form_values['operation'] != 'presave')) {
00211
00212 $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']));
00213 if ($save_post_action_assigned) {
00214 db_query("DELETE FROM {trigger_assignments} WHERE hook = '%s' AND op = '%s' AND aid = 'node_save_action'", $form_values['hook'], $form_values['operation']);
00215 }
00216 db_query("INSERT INTO {trigger_assignments} VALUES ('%s', '%s', '%s', %d)", $form_values['hook'], $form_values['operation'], 'node_save_action', $weight + 2);
00217 if (!$save_post_action_assigned) {
00218 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.'));
00219 }
00220 }
00221 }
00222 }
00223
00234 function theme_trigger_display($element) {
00235 $header = array();
00236 $rows = array();
00237 if (count($element['assigned']['#value'])) {
00238 $header = array(array('data' => t('Name')), array('data' => t('Operation')));
00239 $rows = array();
00240 foreach ($element['assigned']['#value'] as $aid => $info) {
00241 $rows[] = array(
00242 $info['description'],
00243 $info['link']
00244 );
00245 }
00246 }
00247
00248 if (count($rows)) {
00249 $output = theme('table', $header, $rows) . drupal_render($element);
00250 }
00251 else {
00252 $output = drupal_render($element);
00253 }
00254 return $output;
00255 }
00256
00257
00273 function _trigger_get_hook_actions($hook, $op, $type = NULL) {
00274 $actions = array();
00275 if ($type) {
00276 $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);
00277 }
00278 else {
00279 $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);
00280 }
00281 while ($action = db_fetch_object($result)) {
00282 $actions[$action->aid] = $action->description;
00283 }
00284 return $actions;
00285 }