Index: modules/actions/actions.info =================================================================== RCS file: modules/actions/actions.info diff -N modules/actions/actions.info --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ modules/actions/actions.info 8 Jun 2007 14:42:40 -0000 @@ -0,0 +1,6 @@ +; $Id$ +name = Actions +description = Enables triggerable Drupal actions. +package = Core - optional +version = VERSION +core = 6.x Index: modules/actions/actions.install =================================================================== RCS file: modules/actions/actions.install diff -N modules/actions/actions.install --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ modules/actions/actions.install 8 Jun 2007 14:42:40 -0000 @@ -0,0 +1,25 @@ +' . t('Actions are functions that Drupal can execute when certain events happen, such as when a node is added or a user logs in. This page lists all actions that are available. Simple actions that do not require any configuration are listed automatically. Actions that need to be configured are listed in the dropdown below. To add a configurable action, select the action and click the Configure button. After completing the configuration form, the action will be available for use by Drupal.') . '

'; + break; + case 'admin/build/actions/config': + $output = '

' . t('This is where you configure a certain action that will be performed at some time in the future. For example, you might configure an action to send email to your friend Royce. Your entry in the description field, below, should be descriptive enough to remind you of that.') . '

'; + break; + case 'admin/build/actions/assign/comment': + $output = '

' . t('Actions are functions that Drupal can execute when certain events happen, such as when a node is added or a user logs in. Below you can assign actions to run when certain comment-related operations happen. For example, you could unpublish a node when a comment is added.') . '

'; + break; + case 'admin/build/actions/assign/node': + $output = '

' . t('Actions are functions that Drupal can execute when certain events happen, such as when a node is added or a user logs in. Below you can assign actions to run when certain node-related operations happen. For example, you could remove a node from the front page when the node is updated. Note that if you are running actions that modify the characteristics of a node, you will need to add the %node_save action to save the changes.', array('%node_save' => t('Save node'))) . '

'; + break; + case 'admin/build/actions/assign/user': + $output = '

' . t("Actions are functions that Drupal can execute when certain events happen, such as when a node is added or a user logs in. Below you can assign actions to run when certain user-related operations happen. For example, you could block a user when the user's account is edited by assigning the %block_user action to the user %update operation.", array('%block_user' => t('Block user'), '%update' => t('update'))) . '

'; + break; + case 'admin/build/actions/assign/cron': + $output = '

' . t('Actions are functions that Drupal can execute when certain events happen, such as when a node is added or a user logs in. Below you can assign actions to run when cron runs.') . '

'; + break; + } + + return $output; +} + +/** +* Implementation of hook_menu(). +*/ +function actions_menu() { + $items['admin/build/actions'] = array( + 'title' => 'Actions', + 'description' => 'Manage the actions defined for your site.', + 'access arguments' => array('administer actions'), + 'page callback' => 'actions_assign' + ); + $items['admin/build/actions/manage'] = array( + 'title' => 'Manage actions', + 'page callback' => 'actions_manage', + 'type' => MENU_LOCAL_TASK, + 'weight' => 2, + ); + $items['admin/build/actions/assign'] = array( + 'title' => 'Assign actions', + 'description' => 'Tell Drupal when to execute actions.', + 'page callback' => 'actions_assign', + 'type' => MENU_DEFAULT_LOCAL_TASK, + ); + $items['admin/build/actions/assign/node'] = array( + 'title' => 'Node', + 'page callback' => 'actions_assign', + 'page arguments' => array('node'), + 'type' => MENU_LOCAL_TASK, + ); + $items['admin/build/actions/assign/user'] = array( + 'title' => 'User', + 'page callback' => 'actions_assign', + 'page arguments' => array('user'), + 'type' => MENU_LOCAL_TASK, + ); + $items['admin/build/actions/assign/comment'] = array( + 'title' => 'Comment', + 'page callback' => 'actions_assign', + 'page arguments' => array('comment'), + 'access callback' => 'module_exists', + 'access arguments' => array('comment'), + 'type' => MENU_LOCAL_TASK, + ); + $items['admin/build/actions/assign/cron'] = array( + 'title' => 'Cron', + 'page callback' => 'actions_assign', + 'page arguments' => array('cron'), + 'type' => MENU_LOCAL_TASK, + ); + $items['admin/build/actions/assign/remove'] = array( + 'title' => 'Unassign', + 'description' => 'Remove an action assignment.', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('actions_assign_remove'), + 'type' => MENU_CALLBACK, + ); + $items['admin/build/actions/config'] = array( + 'title' => 'Configure an action', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('actions_configure'), + 'type' => MENU_CALLBACK, + ); + $items['admin/build/actions/assign/delete'] = array( + 'title' => 'Delete action', + 'description' => 'Delete an action.', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('actions_delete_form'), + 'type' => MENU_CALLBACK, + ); + $items['admin/build/actions/orphan'] = array( + 'title' => 'Remove orphans', + 'page callback' => 'actions_remove_orphans', + 'type' => MENU_CALLBACK, + ); + return $items; +} + +/** +* Implementation of hook_perm(). +*/ +function actions_perm() { + return array('administer actions'); +} + +/** + * Menu callback. + * Display an overview of available and configured actions. + */ +function actions_manage() { + $output = ''; + $actions = actions_list(); + actions_synchronize($actions); + $actions_map = actions_actions_map($actions); + $options = array(t('Choose a configurable action')); + $unconfigurable = array(); + + foreach ($actions_map as $key => $array) { + if ($array['configurable']) { + $options[$key] = $array['description'] . '...'; + } + else { + $unconfigurable[] = $array; + } + } + + $row = array(); + $instances_present = db_fetch_object(db_query("SELECT aid FROM {actions} WHERE params != ''")); + $header = array( + array('data' => t('Action Type'), 'field' => 'type'), + array('data' => t('Description'), 'field' => 'description'), + array('data' => $instances_present ? t('Operations') : '', 'colspan' => '2') + ); + $sql = 'SELECT * FROM {actions}'; + $result = pager_query($sql . tablesort_sql($header), 50); + while ($data = db_fetch_object($result)) { + $row[] = array( + array('data' => $data->type), + array('data' => $data->description), + array('data' => $data->params ? l(t('configure'), "admin/build/actions/config/$data->aid") : ''), + array('data' => $data->params ? l(t('delete'), "admin/build/actions/delete/$data->aid") : '') + ); + } + + if ($row) { + $pager = theme('pager', NULL, 50, 0); + if (!empty($pager)) { + $row[] = array(array('data' => $pager, 'colspan' => '3')); + } + $output .= '

' . t('Actions available to Drupal:') . '

'; + $output .= theme('table', $header, $row); + } + + if ($actions_map) { + $output .= '

' . t('Make a new configurable action instance available:') . '

'; + $output .= '

' . drupal_get_form('actions_manage_form', $options) . '

'; + } + + return $output; +} + +/** + * Define the form for the actions overview page. + * + * @param $options + * An array of configurable actions. + * @return + * Form definition. + */ +function actions_manage_form($form_state, $options = array()) { + $form['parent'] = array( + '#prefix' => "
", + '#suffix' => '
', + ); + $form['parent']['action'] = array( + '#type' => 'select', + '#default_value' => '', + '#options' => $options, + '#description' => '', + ); + $form['parent']['buttons']['submit'] = array( + '#type' => 'submit', + '#value' => t('Configure'), + ); + return $form; +} + +function actions_manage_form_submit($form, &$form_state) { + if ($form_state['values']['action']) { + $form_state['redirect'] = 'admin/build/actions/config/' . $form_state['values']['action']; + } +} + +/** + * Create the form for configuration of a single action. + * We provide the "Description" field. The rest of the form + * is provided by the action. We then provide the Save button. + * Because we are combining unknown form elements with the action + * configuration form, we use actions_ prefix on our elements. + * + * @param $action + * md5 hash of action ID + */ +function actions_configure($form_state, $action = NULL) { + if ($action === NULL) { + drupal_goto('admin/build/actions'); + } + + $actions_map = actions_actions_map(actions_list()); + $edit = array(); + + if (is_numeric($action)) { // Editing saved instance of a configurable action. + $aid = $action; + // Load stored parameter values from database. + $data = db_fetch_object(db_query("SELECT * FROM {actions} WHERE aid = %d", intval($aid))); + $edit['actions_description'] = $data->description; + $edit['actions_type'] = $data->type; + $function = $data->func; + $action = md5($data->func); + $params = unserialize($data->params); + if ($params) { + foreach ($params as $name => $val) { + $edit[$name] = $val; + } + } + } + else { // Creating a new action instance. + $function = $actions_map[$action]['function']; + $edit['actions_description'] = $actions_map[$action]['description']; + $edit['actions_type'] = $actions_map[$action]['type']; + } + + + $form['actions_description'] = array( + '#type' => 'textfield', + '#title' => t('Description'), + '#default_value' => $edit['actions_description'], + '#size' => '70', + '#maxlength' => '255', + '#description' => t('A unique description for this configuration of this action. This will be used to describe this action when assigning actions.'), + '#weight' => -10 + ); + $action_form = $function . '_form'; + $form = array_merge($form, $action_form($edit)); + $form['actions_type'] = array( + '#type' => 'value', + '#value' => $edit['actions_type'], + ); + $form['actions_action'] = array( + '#type' => 'hidden', + '#value' => $action, + ); + if (isset($aid)) { // Configuring an existing action. + $form['actions_aid'] = array( + '#type' => 'hidden', + '#value' => $aid, + ); + } + $form['actions_configured'] = array( + '#type' => 'hidden', + '#value' => '1', + ); + $form['buttons']['submit'] = array( + '#type' => 'submit', + '#value' => t('Save'), + '#weight' => 13 + ); + + return $form; +} + +function actions_configure_validate($form, $form_state) { + $function = actions_function_lookup($form_state['values']['actions_action']) . '_validate'; + // Hand off validation to the action. + if (function_exists($function)) { + $function($form, $form_state); + } +} + +function actions_configure_submit($form, &$form_state) { + $function = actions_function_lookup($form_state['values']['actions_action']); + $submit_function = $function . '_submit'; + + // Action will return keyed array of values to store. + $params = $submit_function($form, $form_state); + $aid = isset($form_state['values']['actions_aid']) ? $form_state['values']['actions_aid'] : NULL; + + actions_save($function, $form_state['values']['actions_type'], $params, $form_state['values']['actions_description'], $aid); + drupal_set_message(t('The action has been successfully saved.')); + + $form_state['redirect'] = 'admin/build/actions'; +} + +/** + * Create the form for confirmation of deleting an action. + * + * @param $aid + * The action ID. + */ +function actions_delete_form($aid) { + if (!$aid) drupal_goto('admin/build/actions'); + $action = actions_load($aid); + $form['aid'] = array( + '#type' => 'hidden', + '#value' => $aid + ); + + return confirm_form( + $form, + t('Really delete action !action?', array('!action' => theme('placeholder', $action->description))), + 'admin/build/actions', + t('This cannot be undone.'), + t('Delete'), + t('Cancel') + ); +} + +function actions_delete_form_submit($form, &$form_state) { + // note that in the future we need to check for dependencies here + $aid = $form_state['values']['aid']; + $action = actions_load($aid); + actions_delete($aid); + $description = check_plain($action->description); + watchdog('user', t('Deleted action %aid (%action)', array('%aid' => $aid, '%action' => $description))); + drupal_set_message(t('Action !action was deleted', array('!action' => theme('placeholder', $description)))); + $form_state['redirect'] = 'admin/build/actions'; +} + + +/** + * Save an action and its associated user-supplied parameter values to + * the database. + * + * @param $function + * The name of the function to be called when this action is performed. + * @param $params + * An associative array with parameter names as keys and parameter values + * as values. + * @param $desc + * A user-supplied description of this particular action, e.g., 'Send + * e-mail to Jim'. + * @param $aid + * The ID of this action. If omitted, a new action is created. + * + * @return + * The ID of the action. + */ +function actions_save($function, $type, $params, $desc, $aid = NULL) { + $serialized = serialize($params); + if ($aid) { + db_query("UPDATE {actions} SET func = '%s', type = '%s', params = '%s', description = '%s' WHERE aid = %d", $function, $type, $serialized, $desc, $aid); + watchdog('user', t("Action '@action' saved.", array('%action' => $desc))); + } + else { + $aid = variable_get('actions_next_id', 0); + variable_set('actions_next_id', $aid++); + db_query("INSERT INTO {actions} (aid, func, type, params, description) VALUES (%d, '%s', '%s', '%s', '%s')", $aid, $function, $type, $serialized, $desc); + watchdog('user', t("Action '@action' created.", array('@action' => $desc))); + } + + return $aid; +} + +/** + * Retrieve a single action from the database. + * + * @param $aid + * integer The ID of the action to retrieve. + * + * @return + * The appropriate action row from the database as an object. + */ +function actions_load($aid) { + return db_fetch_object(db_query("SELECT * FROM {actions} WHERE aid = %d", $aid)); +} + +/** + * Delete a single action from the database. + * + * @param $aid + * integer The ID of the action to delete. + * + */ +function actions_delete($aid) { + db_query("DELETE FROM {actions} WHERE aid = %d", $aid); +} + + +/** + * Synchronize actions that are provided by modules with actions + * that are stored in the actions table. This is necessary so that + * actions that do not require configuration can receive action IDs. + * This is not necessarily the best approach, but it is the most + * straightforward. + * + */ +function actions_synchronize($actions_in_code = array(), $delete_orphans = FALSE) { + if (!$actions_in_code) { + $actions_in_code = actions_list(); + } + $actions_in_db = array(); + $result = db_query("SELECT * FROM {actions} WHERE params = ''"); + while ($data = db_fetch_object($result)) { + $actions_in_db[$data->func] = array('aid' => $data->aid, 'description' => $data->description); + } + + // Go through all the actions provided by modules. + foreach ($actions_in_code as $func => $array) { + // Ignore configurable actions since their instances get put in + // when the user adds the action. + if (!$array['configurable']) { + // If we already have an action ID for this action, no need to assign aid. + if (array_key_exists($func, $actions_in_db)) { + unset($actions_in_db[$func]); + } + else { + // This is a new singleton that we don't have an aid for; assign one. + db_query("INSERT INTO {actions} (aid, type, func, params, description) VALUES ('%s', '%s', '%s', '%s', '%s')", $func, $array['type'], $func, '', $array['description']); + drupal_set_message(t("Action '%action' added.", array('%action' => filter_xss_admin($array['description'])))); + } + } + } + + // Any actions that we have left in $actions_in_db are orphaned. + if ($actions_in_db) { + $orphaned = ''; + + foreach ($actions_in_db as $func => $array) { + if ($delete_orphans) { + db_query("DELETE FROM {actions} WHERE func = '%s'", $func); + drupal_set_message(t("Deleted orphaned action '%action'.", array('%action' => $func))); + } + else { + $orphaned .= $func . ', '; + } + } + + if (!$delete_orphans) { + $orphaned = rtrim(rtrim($orphaned, ' '), ','); + $link = l(t('Remove orphaned actions'), 'admin/build/actions/orphan'); + $count = count($actions_in_db); + $phrase = format_plural($count, 'One orphaned action exists', '@count orphaned actions exist', array('@count' => $count)); + drupal_set_message(t("%phrase in the actions table", array('%phrase' => $phrase)) . ' (' . $orphaned . '). ' . $link, 'warning'); + } + } +} + +/** + * Menu callback. Remove any actions that are in the database but not supported + * by any currently enabled module. + */ +function actions_remove_orphans() { + actions_synchronize(actions_list(), TRUE); + drupal_goto('admin/build/actions/manage'); +} + +/** + * Get the actions that have already been defined for this + * type/hook/op combination. + * + * @param $type + * One of 'node', 'user', 'comment'. + * @param $hook + * The name of the hook for which actions have been assigned, + * e.g. 'nodeapi'. + * @param $op + * The hook operation for which the actions have been assigned, + * e.g., 'view'. + * @return + * An array of action descriptions keyed by action IDs. + */ +function _actions_get_hook_actions($hook, $op, $type = NULL) { + $actions = array(); + if ($type) { + $result = db_query("SELECT h.aid, a.description FROM {actions_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); + } + else { + $result = db_query("SELECT h.aid, a.description FROM {actions_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); + } + while ($action = db_fetch_object($result)) { + $actions[$action->aid] = $action->description; + } + return $actions; +} + +/** + * Get the aids of actions to be executed for a hook/op combination. + * + * @param $hook + * The name of the hook being fired. + * @param $op + * The name of the operation being executed. Defaults to an empty string + * because some hooks (e.g., hook_cron()) do not have operations. + * @return + * An array of action IDs. + */ +function _actions_get_hook_aids($hook, $op = '') { + $aids = array(); + $result = db_query("SELECT aa.aid, a.type FROM {actions_assignments} aa LEFT JOIN {actions} a ON aa.aid = a.aid WHERE aa.hook = '%s' AND aa.op = '%s' ORDER BY weight", $hook, $op); + while ($action = db_fetch_object($result)) { + $aids[$action->aid]['type'] = $action->type; + } + return $aids; +} + +function actions_assign_form($form_state, $hook, $op) { + $form['hook'] = array( + '#type' => 'hidden', + '#value' => $hook + ); + $form['operation'] = array( + '#type' => 'hidden', + '#value' => $op + ); + // All of these forms use the same #submit function. + $form['#submit'][] = 'actions_assign_form_submit'; + + $options = array(); + $functions = array(); + // Restrict the options list to actions that declare support for this hook-op combination. + foreach (actions_list() as $func => $metadata) { + if (isset($metadata['hooks'][$hook]) && is_array($metadata['hooks'][$hook]) && in_array($op, $metadata['hooks'][$hook])) { + $functions[] = $func; + } + } + foreach (actions_actions_map(actions_get_all_actions()) as $aid => $action) { + if (in_array($action['function'], $functions)) { + $options[$action['type']][$aid] = $action['description']; + } + } + + $form[$op] = array( + '#type' => 'fieldset', + '#title' => t('Actions to execute during the @hook %view operation:', array('@hook' => $hook, '%view' => $op)), + '#theme' => 'actions_display' + ); + // Retrieve actions that are already assigned to this hook-op combination. + $actions = _actions_get_hook_actions($hook, $op); + $form[$op]['assigned']['#type'] = 'value'; + $form[$op]['assigned']['#value'] = array(); + foreach ($actions as $aid => $description) { + $form[$op]['assigned']['#value'][$aid] = array( + 'description' => $description, + 'link' => l(t('remove'), "admin/build/actions/assign/remove/$hook/$op/" . md5($aid)) + ); + } + + $form[$op]['parent'] = array( + '#prefix' => "
", + '#suffix' => '
', + ); + // List possible actions that may be assigned. + if (count($options) != 0) { + array_unshift($options, t('Choose an action')); + $form[$op]['parent']['aid'] = array( + '#type' => 'select', + '#options' => $options, + ); + $form[$op]['parent']['submit'] = array( + '#type' => 'submit', + '#value' => t('Assign') + ); + } + else { + $form[$op]['none'] = array( + '#value' => t('No available actions support this context.') + ); + } + return $form; +} + +function actions_assign_form_submit($form, $form_state) { + $form_values = $form_state['values']; + if (isset($form_values['aid']) && $form_values['aid'] != '0') { + $aid = actions_function_lookup($form_values['aid']); + $weight = db_result(db_query("SELECT MAX(weight) FROM {actions_assignments} WHERE hook = '%s' AND op = '%s'", $form_values['hook'], $form_values['operation'])); + db_query("INSERT INTO {actions_assignments} values ('%s', '%s', '%s', %d)", $form_values['hook'], $form_values['operation'], $aid, $weight + 1); + } +} + +/** + * Implementation of hook_theme(). + */ +function actions_theme() { + return array( + 'actions_display' => array( + 'arguments' => array('element') + ), + ); +} + +/** + * Display actions assigned to this hook/op combination in a table. + * + * @param array $element + * The fieldset including all assigned actions. + * @return + * The rendered form with the table prepended. + */ +function theme_actions_display($element) { + $header = array(); + $rows = array(); + if (count($element['assigned']['#value'])) { + $header = array(t('Name'), t('Operation')); + $rows = array(); + foreach ($element['assigned']['#value'] as $aid => $info) { + $rows[] = array( + $info['description'], + $info['link'] + ); + } + } + + $output = theme('table', $header, $rows) . drupal_render($element); + return $output; +} + +/** + * Implementation of hook_forms(). + */ +function actions_forms() { + $callback = array('callback' => 'actions_assign_form'); + return array( + 'actions_nodeapi_presave_assign_form' => $callback, + 'actions_nodeapi_insert_assign_form' => $callback, + 'actions_nodeapi_update_assign_form' => $callback, + 'actions_nodeapi_delete_assign_form' => $callback, + 'actions_nodeapi_view_assign_form' => $callback, + 'actions_comment_insert_assign_form' => $callback, + 'actions_comment_update_assign_form' => $callback, + 'actions_comment_delete_assign_form' => $callback, + 'actions_comment_view_assign_form' => $callback, + 'actions_user_login_assign_form' => $callback, + 'actions_user_logout_assign_form' => $callback, + 'actions_user_insert_assign_form' => $callback, + 'actions_user_update_assign_form' => $callback, + 'actions_user_delete_assign_form' => $callback, + 'actions_user_view_assign_form' => $callback, + ); +} + +/** + * Build the form that allows users to assign actions to hooks. + * + * @param $type + * Name of hook. + * @return + * HTML form. + */ +function actions_assign($type = NULL) { + // If no type is specificied we default to node actions, since they + // are the most common. + if (!isset($type)) { + drupal_goto('admin/build/actions/assign/node'); + } + + switch($type) { + case 'nodeapi': + case 'node': + $output = drupal_get_form('actions_nodeapi_presave_assign_form', 'nodeapi', 'presave'); + $output .= drupal_get_form('actions_nodeapi_insert_assign_form', 'nodeapi', 'insert'); + $output .= drupal_get_form('actions_nodeapi_update_assign_form', 'nodeapi', 'update'); + $output .= drupal_get_form('actions_nodeapi_delete_assign_form', 'nodeapi', 'delete'); + $output .= drupal_get_form('actions_nodeapi_view_assign_form', 'nodeapi', 'view'); + break; + case 'comment': + $output = drupal_get_form('actions_comment_insert_assign_form', 'comment', 'insert'); + $output .= drupal_get_form('actions_comment_update_assign_form', 'comment', 'update'); + $output .= drupal_get_form('actions_comment_delete_assign_form', 'comment', 'delete'); + $output .= drupal_get_form('actions_comment_view_assign_form', 'comment', 'view'); + break; + case 'user': + $output = drupal_get_form('actions_user_login_assign_form', 'user', 'login'); + $output .= drupal_get_form('actions_user_logout_assign_form', 'user', 'logout'); + $output .= drupal_get_form('actions_user_insert_assign_form', 'user', 'insert'); + $output .= drupal_get_form('actions_user_update_assign_form', 'user', 'update'); + $output .= drupal_get_form('actions_user_delete_assign_form', 'user', 'delete'); + $output .= drupal_get_form('actions_user_view_assign_form', 'user', 'view'); + break; + case 'cron': + $output = drupal_get_form('actions_cron_run_assign_form', 'cron', 'run'); + break; + } + + return $output; +} + +/** + * Confirm removal of an assigned action. + * + * @param $hook + * @param $op + * @param $aid + * The action ID. + */ +function actions_assign_remove($hook = NULL, $op = NULL, $aid = NULL) { + if (!($hook && $op && $aid)) { + drupal_goto('admin/actions/assign'); + } + $form['hook'] = array( + '#type' => 'value', + '#value' => $hook + ); + $form['operation'] = array( + '#type' => 'value', + '#value' => $op + ); + $form['aid'] = array( + '#type' => 'value', + '#value' => $aid + ); + $action = actions_function_lookup($aid); + $actions = actions_get_all_actions(); + $description = $actions[$action]['description']; + return confirm_form( + $form, + t('Are you sure you want to delete the action %title?', array('%title' => $description)), + 'admin/build/actions/assign/'. $actions[$action]['type'], + t('You can add it again later if you wish.'), + t('Delete'), + t('Cancel') + ); +} + +function actions_assign_remove_submit($form, &$form_state) { + $form_values = $form_state['values']; + if ($form_values['confirm'] == 1) { + $aid = actions_function_lookup($form_values['aid']); + db_query("DELETE FROM {actions_assignments} WHERE hook = '%s' AND op = '%s' AND aid = '%s'", $form_values['hook'], $form_values['operation'], $aid); + $actions = actions_get_all_actions(); + watchdog('actions', t('Action %action deleted', array('%action' => check_plain($actions[$aid]['description'])))); + drupal_set_message(t('Action %action deleted.', array('%action' => $actions[$aid]['description']))); + $form_state['redirect'] = 'admin/build/actions/assign/'. $form_values['hook']; + } + else { + drupal_goto('admin/build/actions'); + } +} + +function _actions_normalize_node_context($type, $node) { + switch($type) { + // An action that works on comments is being called in a node context. + case 'comment': + + // An action that works on users is being called in a node context. + // Load the user object of the node's author. + case 'user': + return user_load(array('uid' => $node->uid)); + } +} + +/** + * Implementation of hook_nodeapi(). + */ +function actions_nodeapi($node, $op, $a3, $a4) { + // We prevent recursion by tracking which operations have already been called. + static $recursion; + if (!in_array($op, array('view', 'update', 'presave','insert', 'delete')) || isset($recursion[$op])) { + return; + } + $recursion[$op] = TRUE; + + $aids = _actions_get_hook_aids('nodeapi', $op); + if (!$aids) { + return; + } + $context = array( + 'hook' => 'nodeapi', + 'op' => $op, + ); + + static $objects; + // We need to get the expected object if the action's type is not 'node'. + // We keep the object in $objects so we can reuse it if we have multiple actions + // that make changes to an object. + foreach ($aids as $aid => $action_info) { + if ($action_info['type'] != 'node') { + if (!isset($objects[$action_info['type']])) { + $objects[$action_info['type']] = _actions_normalize_node_context($action_info['type'], $node); + } + // Since we know about the node, we pass that info along to the action. + $context['node'] = $node; + $result = actions_do($aid, $context, $objects[$action_info['type']], $a4, $a4); + } + else { + actions_do($aid, $context, $node, $a3, $a4); + } + + } +} + +function _actions_normalize_comment_context($type, $comment) { + switch ($type) { + // An action that works with nodes is being called in a comment context. + case 'node': + return node_load(is_array($comment) ? $comment['nid'] : $comment->nid); + + // An action that works on users is being called in a comment context. + case 'user': + return user_load(array('uid' => is_array($comment) ? $comment['uid'] : $comment->uid)); + } +} + +/** + * Implementation of hook_comment(). + */ +function actions_comment($a1, $op) { + // We support a subset of ops. + if (!in_array($op, array('insert', 'update', 'delete', 'view'))) { + return; + } + $aids = _actions_get_hook_aids('comment', $op); + $context = array( + 'hook' => 'comment', + 'op' => $op, + ); + static $objects; + // We need to get the expected object if the action's type is not 'comment'. + // We keep the object in $objects so we can reuse it if we have multiple actions + // that make changes to an object. + foreach ($aids as $aid => $action_info) { + if ($action_info['type'] != 'comment') { + // + if (!isset($objects[$action_info['type']])) { + $objects[$action_info['type']] = _actions_normalize_comment_context($action_info['type'], $a1); + } + // Since we know about the comment, we pass it along to the action. + // + $context['comment'] = (object) $a1; + actions_do($aid, $context, $objects[$action_info['type']]); + } + else { + actions_do($aid, $context, (object) $a1); + } + } +} + +/** + * Implementation of hook_cron(). + */ +function actions_cron() { + $aids = _actions_get_hook_aids('cron'); + $context = array( + 'hook' => 'cron', + 'op' => '', + ); + + actions_do(array_keys($aids), $context); +} + +function _actions_normalize_user_context($type, $account) { + switch($type) { + // An action that works with nodes is being called in a user context. + case 'node': + + // An action that works with comments is being called in a user context + case 'comment': + + } +} + +/** + * Implementation of hook_user(). + */ +function actions_user($op, &$edit, &$account, $category = NULL) { + // We support a subset of ops. + if (!in_array($op, array('login', 'logout', 'insert', 'update', 'delete', 'view'))) { + return; + } + $aids = _actions_get_hook_aids('user', $op); + $context = array( + 'hook' => 'user', + 'op' => $op, + 'form_values' => &$edit, + ); + foreach ($aids as $aid => $action_info) { + if ($action_info['type'] != 'user') { + $object = _actions_normalize_user_context($account); + $context['account'] = $account; + actions_do($aid, $context, $object); + } + } + + actions_do($aids, $context, $account, $category); +} + +/** + * Often we generate a select field of all actions. This function + * generates the options for that select. + * + * @param $type + * One of 'node', 'user', 'comment'. + * @return + * Array keyed by action ID. + */ +function actions_options($type = 'all') { + $options = array(t('Choose an action')); + foreach (actions_actions_map(actions_get_all_actions()) as $aid => $action) { + $options[$action['type']][$aid] = $action['description']; + } + + if ($type == 'all') { + return $options; + } + else { + return $options[$type]; + } +} \ No newline at end of file Index: modules/actions/actions.schema =================================================================== RCS file: modules/actions/actions.schema diff -N modules/actions/actions.schema --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ modules/actions/actions.schema 8 Jun 2007 14:42:40 -0000 @@ -0,0 +1,26 @@ + array( + 'aid' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => '0'), + 'type' => array('type' => 'varchar', 'length' => 32, 'not null' => TRUE, 'default' => ''), + 'func' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''), + 'params' => array('type' => 'text', 'not null' => TRUE, 'size' => 'big', 'default' => ''), + 'description' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => '0'), + ), + 'primary key' => array('aid'), + ); + $schema['actions_assignments'] = array( + 'fields' => array( + 'hook' => array('type' => 'varchar', 'length' => 32, 'not null' => TRUE, 'default' => ''), + 'op' => array('type' => 'varchar', 'length' => 32, 'not null' => TRUE, 'default' => ''), + 'aid' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''), + 'weight' => array('type' => 'int', 'not null' => TRUE, 'default' => 0), + ), + 'index keys' => array( + 'hook_op' => array('hook', 'op')) + ); + return $schema; +} \ No newline at end of file