? .DS_Store ? actions_14.patch ? actions_help.patch ? block_sane_help.patch ? comment_notice.patch ? files ? fix_garland_and_menu_theme.patch ? install_profile_notice.patch ? modules/.DS_Store ? profiles/single_user_blog ? sites/.DS_Store ? sites/all/.DS_Store ? sites/all/modules ? sites/all/views ? sites/default/settings.php ? themes/garland/.DS_Store Index: modules/actions/actions.info =================================================================== RCS file: modules/actions/actions.info diff -N modules/actions/actions.info --- modules/actions/actions.info 29 Jun 2007 18:06:50 -0000 1.1 +++ /dev/null 1 Jan 1970 00:00:00 -0000 @@ -1,6 +0,0 @@ -; $Id: actions.info,v 1.1 2007/06/29 18:06:50 dries Exp $ -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 --- modules/actions/actions.install 29 Aug 2007 14:57:49 -0000 1.3 +++ /dev/null 1 Jan 1970 00:00:00 -0000 @@ -1,21 +0,0 @@ -'. $explanation .' '. t('Below you can assign actions to run when certain comment-related operations happen. For example, you could promote a post to the front page when a comment is added.') .'

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

'. $explanation .' '. t('Below you can assign actions to run when certain post-related operations happen. For example, you could remove a post from the front page when the post is updated. Note that if you are running actions that modify the characteristics of a post (such as making a post sticky or removing a post from the front page), you will need to add the %node_save action to save the changes.', array('%node_save' => t('Save post'))) .'

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

'. $explanation .' '. t("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 post 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/assign'] = array( - 'title' => 'Assign actions', - 'description' => 'Tell Drupal when to execute actions.', - 'page callback' => 'actions_assign', - 'type' => MENU_LOCAL_TASK, - ); - $items['admin/build/actions/assign/node'] = array( - 'title' => 'Content', - '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' => 'actions_access_check', - 'access arguments' => array('comment'), - 'type' => MENU_LOCAL_TASK, - ); - $items['admin/build/actions/assign/taxonomy'] = array( - 'title' => 'Taxonomy', - 'page callback' => 'actions_assign', - 'page arguments' => array('taxonomy'), - 'access callback' => 'actions_access_check', - 'access arguments' => array('taxonomy'), - '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, - ); - - // We want contributed modules to be able to describe - // their hooks and have actions assignable to them. - $hooks = module_invoke_all('hook_info'); - foreach ($hooks as $module => $hook) { - if (in_array($module, array('node', 'comment', 'user', 'system', 'taxonomy'))) { - continue; - } - $info = db_result(db_query("SELECT info FROM {system} WHERE name = '%s'", $module)); - $info = unserialize($info); - $nice_name = $info['name']; - $items["admin/build/actions/assign/$module"] = array( - 'title' => $nice_name, - 'page callback' => 'actions_assign', - 'page arguments' => array($module), - '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_unassign'), - 'type' => MENU_CALLBACK, - ); - - return $items; -} - -/** - * Access callback for menu system. - */ -function actions_access_check($module) { - return (module_exists($module) && user_access('administer actions')); -} - - - -/** - * 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; -} - -/** - * Create the form definition for assigning an action to a hook-op combination. - * - * @param $form_state - * Information about the current form. - * @param $hook - * The name of the hook, e.g., 'nodeapi'. - * @param $op - * The name of the hook operation, e.g., 'insert'. - * @param $description - * A plain English description of what this hook operation does. - * @return - */ -function actions_assign_form($form_state, $hook, $op, $description) { - $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']['any']) || (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['callback'], $functions)) { - $options[$action['type']][$aid] = $action['description']; - } - } - - $form[$op] = array( - '#type' => 'fieldset', - '#title' => $description, - '#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 (!empty($form_values['aid'])) { - $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(array('data' => t('Name')), array('data' => 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(). We reuse code by using the - * same assignment form definition for each node-op combination. - */ -function actions_forms() { - $hooks = module_invoke_all('hook_info'); - foreach ($hooks as $module => $info) { - foreach ($hooks[$module] as $hook => $ops) { - foreach ($ops as $op => $description) { - $forms['actions_'. $hook .'_'. $op .'_assign_form'] = array('callback' => 'actions_assign_form'); - } - } - } - - return $forms; -} - -/** - * 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 specified we default to node actions, since they - // are the most common. - if (!isset($type)) { - drupal_goto('admin/build/actions/assign/node'); - } - if ($type == 'node') { - $type = 'nodeapi'; - } - - $output = ''; - $hooks = module_invoke_all('hook_info'); - foreach ($hooks as $module => $hook) { - if (isset($hook[$type])) { - foreach ($hook[$type] as $op => $description) { - $form_id = 'actions_'. $type .'_'. $op .'_assign_form'; - $output .= drupal_get_form($form_id, $type, $op, $description['runs when']); - } - } - } - return $output; -} - -/** - * Confirm removal of an assigned action. - * - * @param $hook - * @param $op - * @param $aid - * The action ID. - */ -function actions_unassign($form_state, $hook = NULL, $op = NULL, $aid = NULL) { - if (!($hook && $op && $aid)) { - drupal_goto('admin/build/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(); - - $destination = 'admin/build/actions/assign/'. ($hook == 'nodeapi' ? 'node' : $hook); - - return confirm_form($form, - t('Are you sure you want to remove the action %title?', array('%title' => $actions[$action]['description'])), - $destination, - t('You can assign it again later if you wish.'), - t('Remove'), t('Cancel') - ); -} - -function actions_unassign_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', 'Action %action has been unassigned.', array('%action' => check_plain($actions[$aid]['description']))); - drupal_set_message(t('Action %action has been unassigned.', array('%action' => $actions[$aid]['description']))); - $hook = $form_values['hook'] == 'nodeapi' ? 'node' : $form_values['hook']; - $form_state['redirect'] = 'admin/build/actions/assign/'. $hook; - } - else { - drupal_goto('admin/build/actions'); - } -} - -/** - * When an action is called in a context that does not match its type, - * the object that the action expects must be retrieved. For example, when - * an action that works on users is called during the node hook, the - * user object is not available since the node hook doesn't pass it. - * So here we load the object the action expects. - * - * @param $type - * The type of action that is about to be called. - * @param $node - * The node that was passed via the nodeapi hook. - * @return - * The object expected by the action that is about to be called. - */ -function _actions_normalize_node_context($type, $node) { - switch ($type) { - // If an action that works on comments is being called in a node context, - // the action is expecting a comment object. But we do not know which comment - // to give it. The first? The most recent? All of them? So comment actions - // in a node context are not supported. - - // 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) { - // Keep objects for reuse so that changes actions make to objects can persist. - static $objects; - // Prevent recursion by tracking which operations have already been called. - static $recursion; - // Support a subset of operations. - 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, - ); - - // 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, $objects[$action_info['type']], $context, $a4, $a4); - } - else { - actions_do($aid, $node, $context, $a3, $a4); - } - } -} - -/** - * When an action is called in a context that does not match its type, - * the object that the action expects must be retrieved. For example, when - * an action that works on nodes is called during the comment hook, the - * node object is not available since the comment hook doesn't pass it. - * So here we load the object the action expects. - * - * @param $type - * The type of action that is about to be called. - * @param $comment - * The comment that was passed via the comment hook. - * @return - * The object expected by the action that is about to be called. - */ -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) { - // Keep objects for reuse so that changes actions make to objects can persist. - static $objects; - // We support a subset of operations. - if (!in_array($op, array('insert', 'update', 'delete', 'view'))) { - return; - } - $aids = _actions_get_hook_aids('comment', $op); - $context = array( - 'hook' => 'comment', - 'op' => $op, - ); - // 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 - // in case it wants to peek at it. - $context['comment'] = (object) $a1; - actions_do($aid, $objects[$action_info['type']], $context); - } - else { - actions_do($aid, (object) $a1, $context); - } - } -} - -/** - * Implementation of hook_cron(). - */ -function actions_cron() { - $aids = _actions_get_hook_aids('cron'); - $context = array( - 'hook' => 'cron', - 'op' => '', - ); - // Cron does not act on any specific object. - $object = NULL; - actions_do(array_keys($aids), $object, $context); -} - -/** - * When an action is called in a context that does not match its type, - * the object that the action expects must be retrieved. For example, when - * an action that works on nodes is called during the user hook, the - * node object is not available since the user hook doesn't pass it. - * So here we load the object the action expects. - * - * @param $type - * The type of action that is about to be called. - * @param $account - * The account object that was passed via the user hook. - * @return - * The object expected by the action that is about to be called. - */ -function _actions_normalize_user_context($type, $account) { - switch ($type) { - // If an action that works on comments is being called in a user context, - // the action is expecting a comment object. But we have no way of - // determining the appropriate comment object to pass. So comment - // actions in a user context are not supported. - - // An action that works with nodes is being called in a user context. - // If a single node is being viewed, return the node. - case 'node': - // If we are viewing an individual node, return the node. - if ((arg(0) == 'node') && is_numeric(arg(1)) && (arg(2) == NULL)) { - return node_load(array('nid' => arg(1))); - } - } -} - -/** - * Implementation of hook_user(). - */ -function actions_user($op, &$edit, &$account, $category = NULL) { - // Keep objects for reuse so that changes actions make to objects can persist. - static $objects; - // We support a subset of operations. - 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') { - if (!isset($objects[$action_info['type']])) { - $objects[$action_info['type']] = _actions_normalize_user_context($action_info['type'], $account); - } - $context['account'] = $account; - actions_do($aid, $objects[$action_info['type']], $context); - } - else { - actions_do($aid, $account, $context, $category); - } - } -} - -/** - * Implementation of hook_taxonomy(). - */ -function actions_taxonomy($op, $type, $array) { - if ($type != 'term') { - return; - } - $aids = _actions_get_hook_aids('taxonomy', $op); - $context = array( - 'hook' => 'taxonomy', - 'op' => $op - ); - foreach ($aids as $aid => $action_info) { - actions_do($aid, (object) $array, $context); - } -} - -/** - * 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]; - } -} Index: modules/actions/actions.schema =================================================================== RCS file: modules/actions/actions.schema diff -N modules/actions/actions.schema --- modules/actions/actions.schema 29 Aug 2007 14:57:49 -0000 1.3 +++ /dev/null 1 Jan 1970 00:00:00 -0000 @@ -1,16 +0,0 @@ - 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; -} Index: modules/comment/comment.module =================================================================== RCS file: /cvs/drupal/drupal/modules/comment/comment.module,v retrieving revision 1.580 diff -u -p -r1.580 comment.module --- modules/comment/comment.module 4 Sep 2007 21:10:45 -0000 1.580 +++ modules/comment/comment.module 5 Sep 2007 04:57:37 -0000 @@ -2168,16 +2168,40 @@ function comment_action_info() { 'configurable' => FALSE, 'hooks' => array( 'comment' => array('insert', 'update', 'view'), - ) + ), + ), + 'comment_publish_action' => array( + 'description' => t('Publish comment'), + 'type' => 'comment', + 'configurable' => FALSE, + 'hooks' => array( + 'comment' => array('insert', 'update', 'view'), + ), + ), + 'comment_delete_action' => array( + 'description' => t('Delete comment'), + 'type' => 'comment', + 'configurable' => FALSE, + 'hooks' => array( + 'comment' => array('insert', 'update', 'view'), + ), ), 'comment_unpublish_by_keyword_action' => array( 'description' => t('Unpublish comment containing keyword(s)'), 'type' => 'comment', 'configurable' => TRUE, 'hooks' => array( - 'comment' => array('insert', 'update'), - ) - ) + 'comment' => array('insert', 'update', 'view'), + ), + ), + 'comment_unpublish_by_keyword_action' => array( + 'description' => t('Publish comment containing keyword(s)'), + 'type' => 'comment', + 'configurable' => TRUE, + 'hooks' => array( + 'comment' => array('insert', 'update', 'view'), + ), + ), ); } @@ -2202,6 +2226,45 @@ function comment_unpublish_action($comme watchdog('action', 'Unpublished comment %subject.', array('%subject' => $subject)); } +/** + * Drupal action to publish a comment. + * + * @param $context + * Keyed array. Must contain the id of the comment if $comment is not passed. + * @param $comment + * An optional comment object. + */ +function comment_publish_action($comment, $context = array()) { + if (isset($comment->cid)) { + $cid = $comment->cid; + $subject = $comment->subject; + } + else { + $cid = $context['cid']; + $subject = db_result(db_query("SELECT subject FROM {comments} WHERE cid = %d", $cid)); + } + db_query('UPDATE {comments} SET status = %d WHERE cid = %d', COMMENT_PUBLISHED, $cid); + watchdog('action', 'Unpublished comment %subject.', array('%subject' => $subject)); +} + +/** + * Drupal action to publish a comment. + * + * @param $context + * Keyed array. Must contain the id of the comment if $comment is not passed. + * @param $comment + * An optional comment object. + */ +function comment_delete_action($comment, $context = array()) { + if (isset($comment->cid)) { + $cid = $comment->cid; + } + else { + $cid = $context['cid']; + } + comment_delete($cid); +} + function comment_unpublish_by_keyword_action_form($context) { $form['keywords'] = array( '#title' => t('Keywords'), @@ -2236,3 +2299,38 @@ function comment_unpublish_by_keyword_ac } } } + +function comment_publish_by_keyword_action_form($context) { + $form['keywords'] = array( + '#title' => t('Keywords'), + '#type' => 'textarea', + '#description' => t('The comment will be published if it contains any of the character sequences above. Use a comma-separated list of character sequences. Example: funny, bungee jumping, "Company, Inc.".'), + '#default_value' => isset($context['keywords']) ? drupal_implode_tags($context['keywords']) : '', + ); + return $form; +} + +function comment_publish_by_keyword_action_submit($form, $form_state) { + return array('keywords' => drupal_explode_tags($form_state['values']['keywords'])); +} + +/** + * Implementation of a configurable Drupal action. + * Unpublish a comment if it contains a certain string. + * + * @param $context + * An array providing more information about the context of the call to this action. + * Unused here since this action currently only supports the insert and update ops of + * the comment hook, both of which provide a complete $comment object. + * @param $comment + * A comment object. + */ +function comment_publish_by_keyword_action($comment, $context) { + foreach ($context['keywords'] as $keyword) { + if (strstr($comment->comment, $keyword) || strstr($comment->subject, $keyword)) { + db_query('UPDATE {comments} SET status = %d WHERE cid = %d', COMMENT_PUBLISHED, $comment->cid); + watchdog('action', 'Published comment %subject.', array('%subject' => $comment->subject)); + break; + } + } +} Index: modules/node/node.module =================================================================== RCS file: /cvs/drupal/drupal/modules/node/node.module,v retrieving revision 1.877 diff -u -p -r1.877 node.module --- modules/node/node.module 2 Sep 2007 14:42:30 -0000 1.877 +++ modules/node/node.module 5 Sep 2007 04:57:38 -0000 @@ -2337,8 +2337,8 @@ function node_action_info() { 'description' => t('Publish post'), 'configurable' => FALSE, 'hooks' => array( - 'nodeapi' => array('presave','insert','update', 'view'), - 'comment' => array('delete','insert','update', 'view'), + 'nodeapi' => array('presave', 'insert', 'update', 'view'), + 'comment' => array('insert', 'update'), ), ), 'node_unpublish_action' => array( @@ -2346,8 +2346,8 @@ function node_action_info() { 'description' => t('Unpublish post'), 'configurable' => FALSE, 'hooks' => array( - 'nodeapi' => array('presave','insert','update', 'view'), - 'comment' => array('delete','insert','update', 'view'), + 'nodeapi' => array('presave', 'insert', 'update', 'view'), + 'comment' => array('delete', 'insert', 'update'), ), ), 'node_make_sticky_action' => array( @@ -2355,8 +2355,8 @@ function node_action_info() { 'description' => t('Make post sticky'), 'configurable' => FALSE, 'hooks' => array( - 'nodeapi' => array('presave','insert','update', 'view'), - 'comment' => array('delete','insert','update', 'view'), + 'nodeapi' => array('presave', 'insert', 'update', 'view'), + 'comment' => array('insert', 'update'), ), ), 'node_make_unsticky_action' => array( @@ -2364,8 +2364,8 @@ function node_action_info() { 'description' => t('Make post unsticky'), 'configurable' => FALSE, 'hooks' => array( - 'nodeapi' => array('presave','insert','update', 'view'), - 'comment' => array('delete','insert','update', 'view'), + 'nodeapi' => array('presave', 'insert', 'update', 'view'), + 'comment' => array('delete', 'insert', 'update'), ), ), 'node_promote_action' => array( @@ -2373,9 +2373,8 @@ function node_action_info() { 'description' => t('Promote post to front page'), 'configurable' => FALSE, 'hooks' => array( - 'nodeapi' => array('presave','insert','update', 'view'), - 'comment' => array('delete','insert','update', 'view'), - 'user' => array('login'), + 'nodeapi' => array('presave', 'insert', 'update', 'view'), + 'comment' => array('insert', 'update'), ), ), 'node_unpromote_action' => array( @@ -2383,8 +2382,8 @@ function node_action_info() { 'description' => t('Remove post from front page'), 'configurable' => FALSE, 'hooks' => array( - 'nodeapi' => array('presave','insert','update', 'view'), - 'comment' => array('delete','insert','update', 'view'), + 'nodeapi' => array('presave', 'insert', 'update', 'view'), + 'comment' => array('delete', 'insert', 'update'), ), ), 'node_assign_owner_action' => array( @@ -2393,8 +2392,8 @@ function node_action_info() { 'configurable' => TRUE, 'hooks' => array( 'any' => TRUE, - 'nodeapi' => array('presave','insert','update', 'view'), - 'comment' => array('delete','insert','update', 'view'), + 'nodeapi' => array('presave', 'insert', 'update', 'view'), + 'comment' => array('delete', 'insert', 'update'), ), ), 'node_save_action' => array( @@ -2402,9 +2401,8 @@ function node_action_info() { 'description' => t('Save post'), 'configurable' => FALSE, 'hooks' => array( - 'nodeapi' => array('delete','insert','update', 'view'), - 'comment' => array('delete','insert','update', 'view'), - 'user' => array('login'), + 'nodeapi' => array('insert', 'update', 'view'), + 'comment' => array('delete', 'insert', 'update'), ), ), 'node_unpublish_by_keyword_action' => array( @@ -2412,8 +2410,7 @@ function node_action_info() { 'description' => t('Unpublish post containing keyword(s)'), 'configurable' => TRUE, 'hooks' => array( - 'nodeapi' => array('presave','insert','update', 'view'), - 'comment' => array('delete','insert','update', 'view'), + 'nodeapi' => array('presave', 'insert', 'update'), ), ), ); @@ -2494,38 +2491,18 @@ function node_assign_owner_action(&$node function node_assign_owner_action_form($context) { $description = t('The username of the user to which you would like to assign ownership.'); - $count = db_result(db_query("SELECT COUNT(*) FROM {users}")); $owner_name = ''; if (isset($context['owner_uid'])) { $owner_name = db_result(db_query("SELECT name FROM {users} WHERE uid = %d", $context['owner_uid'])); } - // Use dropdown for fewer than 200 users; textbox for more than that. - if (intval($count) < 200) { - $options = array(); - $result = db_query("SELECT uid, name FROM {users} WHERE uid > 0 ORDER BY name"); - while ($data = db_fetch_object($result)) { - $options[$data->name] = $data->name; - } - $form['owner_name'] = array( - '#type' => 'select', - '#title' => t('Username'), - '#default_value' => $owner_name, - '#options' => $options, - '#description' => $description, - ); - } - else { - $form['owner_name'] = array( - '#type' => 'textfield', - '#title' => t('Username'), - '#default_value' => $owner_name, - '#autocomplete_path' => 'user/autocomplete', - '#size' => '6', - '#maxlength' => '7', - '#description' => $description, - ); - } + $form['owner_name'] = array( + '#type' => 'textfield', + '#title' => t('Username'), + '#default_value' => $owner_name, + '#autocomplete_path' => 'user/autocomplete', + '#description' => $description, + ); return $form; } Index: modules/system/system.module =================================================================== RCS file: /cvs/drupal/drupal/modules/system/system.module,v retrieving revision 1.526 diff -u -p -r1.526 system.module --- modules/system/system.module 1 Sep 2007 14:41:21 -0000 1.526 +++ modules/system/system.module 5 Sep 2007 04:57:38 -0000 @@ -52,13 +52,13 @@ function system_help($path, $arg) { return $output; case 'admin/build/modules/uninstall': return '

'. t('The uninstall process removes all data related to a module. To uninstall a module, you must first disable it. Not all modules support this feature.') .'

'; - case 'admin/build/actions': - case 'admin/build/actions/manage': + case 'admin/settings/actions': + case 'admin/settings/actions/manage': $explanation = t('Actions are functions that Drupal can execute when certain events happen, such as when a post is added or a user logs in.'); $output = '

'. $explanation .' '. t('A module, such as the actions module, may assign these actions to the specific events that will trigger them.') .'

'; - $output .= '

'. t('This page lists all actions that are available. Simple actions that do not require any configuration are listed automatically. Advanced actions that need to be configured are listed in the dropdown below. To add an advanced action, select the action and click the Configure button. After completing the configuration form, the action will be available for use by Drupal.') .'

'; + $output .= '

'. t('This page lists all actions that are available. Simple actions that do not require any configuration are listed automatically. Advanced actions that need to be configured are listed in the dropdown below. To add an advanced action, select the action and click the Configure button. After completing the configuration form, the action will be available for use by Drupal. The events that trigger these actions can be enabled and configured by enabling the actions module.') .'

'; return $output; - case 'admin/build/actions/config': + case 'admin/settings/actions/config': return '

'. 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 Joe. You would modify the description field, below, to read %send to remind you of that. The description you provide will be used to identify this action; for example, when assigning an action to a Drupal event such as a new comment being posted.', array('%send' => t('Send email to Joe'))) .'

'; case 'admin/logs/status': return '

'. t("Here you can find a short overview of your Drupal site's parameters as well as any problems detected with your installation. It is useful to copy/paste this information when you need support.") .'

'; @@ -272,40 +272,38 @@ function system_menu() { 'type' => MENU_CALLBACK, ); - // Actions: - $items['admin/build/actions'] = array( + // Settings: + $items['admin/settings/actions'] = array( 'title' => 'Actions', 'description' => 'Manage the actions defined for your site.', 'access arguments' => array('administer actions'), 'page callback' => 'system_actions_manage' ); - $items['admin/build/actions/manage'] = array( + $items['admin/settings/actions/manage'] = array( 'title' => 'Manage actions', 'description' => 'Manage the actions defined for your site.', 'page callback' => 'system_actions_manage', 'type' => MENU_DEFAULT_LOCAL_TASK, 'weight' => -2, ); - $items['admin/build/actions/config'] = array( + $items['admin/settings/actions/config'] = array( 'title' => 'Configure an action', 'page callback' => 'drupal_get_form', 'page arguments' => array('system_actions_configure'), 'type' => MENU_CALLBACK, ); - $items['admin/build/actions/delete/%actions'] = array( + $items['admin/settings/actions/delete/%actions'] = array( 'title' => 'Delete action', 'description' => 'Delete an action.', 'page callback' => 'drupal_get_form', 'page arguments' => array('system_actions_delete_form', 4), 'type' => MENU_CALLBACK, ); - $items['admin/build/actions/orphan'] = array( + $items['admin/settings/actions/orphan'] = array( 'title' => 'Remove orphans', 'page callback' => 'system_actions_remove_orphans', 'type' => MENU_CALLBACK, ); - - // Settings: $items['admin/settings/site-information'] = array( 'title' => 'Site information', 'description' => 'Change basic site information, such as the site name, slogan, e-mail address, mission, front page and more.', @@ -1101,6 +1099,17 @@ function system_action_info() { 'taxonomy' => array('insert', 'update', 'delete'), ), ), + 'system_watchdog_action' => array( + 'type' => 'system', + 'description' => t('Send a message to the log'), + 'configurable' => TRUE, + 'hooks' => array( + 'nodeapi' => array('view', 'insert', 'update', 'delete'), + 'comment' => array('view', 'insert', 'update', 'delete'), + 'user' => array('view', 'insert', 'update', 'delete', 'login'), + 'taxonomy' => array('insert', 'update', 'delete'), + ), + ), 'system_send_email_action' => array( 'description' => t('Send e-mail'), 'type' => 'system', @@ -1158,8 +1167,8 @@ function system_actions_manage() { $row[] = array( array('data' => $action->type), array('data' => $action->description), - array('data' => $action->parameters ? l(t('configure'), "admin/build/actions/config/$action->aid") : ''), - array('data' => $action->parameters ? l(t('delete'), "admin/build/actions/delete/$action->aid") : '') + array('data' => $action->parameters ? l(t('configure'), "admin/settings/actions/config/$action->aid") : ''), + array('data' => $action->parameters ? l(t('delete'), "admin/settings/actions/delete/$action->aid") : '') ); } @@ -1209,7 +1218,7 @@ function system_actions_manage_form($for function system_actions_manage_form_submit($form, &$form_state) { if ($form_state['values']['action']) { - $form_state['redirect'] = 'admin/build/actions/config/'. $form_state['values']['action']; + $form_state['redirect'] = 'admin/settings/actions/config/'. $form_state['values']['action']; } } @@ -1230,7 +1239,7 @@ function system_actions_manage_form_subm */ function system_actions_configure($form_state, $action = NULL) { if ($action === NULL) { - drupal_goto('admin/build/actions'); + drupal_goto('admin/settings/actions'); } $actions_map = actions_actions_map(actions_list()); @@ -1318,7 +1327,7 @@ function system_actions_configure_submit 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/manage'; + $form_state['redirect'] = 'admin/settings/actions/manage'; } /** @@ -1335,7 +1344,7 @@ function system_actions_delete_form($for ); return confirm_form($form, t('Are you sure you want to delete the action %action?', array('%action' => $action->description)), - 'admin/build/actions/manage', + 'admin/settings/actions/manage', t('This cannot be undone.'), t('Delete'), t('Cancel') ); @@ -1351,7 +1360,7 @@ function system_actions_delete_form_subm $description = check_plain($action->description); watchdog('user', 'Deleted action %aid (%action)', array('%aid' => $aid, '%action' => $description)); drupal_set_message(t('Action %action was deleted', array('%action' => $description))); - $form_state['redirect'] = 'admin/build/actions/manage'; + $form_state['redirect'] = 'admin/settings/actions/manage'; } /** * Post-deletion operations for deleting action orphans. @@ -1368,7 +1377,7 @@ function system_action_delete_orphans_po */ function system_actions_remove_orphans() { actions_synchronize(actions_list(), TRUE); - drupal_goto('admin/build/actions/manage'); + drupal_goto('admin/settings/actions/manage'); } /** @@ -1602,6 +1611,116 @@ function system_message_action(&$object, drupal_set_message($context['message']); } + +function system_watchdog_action_form($context) { + $form['message'] = array( + '#type' => 'textarea', + '#title' => t('Message'), + '#default_value' => isset($context['message']) ? $context['message'] : '', + '#required' => TRUE, + '#rows' => '8', + '#description' => t('The message to be logged. You may include the following variables: %site_name, %username, %node_url, %node_type, %title, %teaser, %body. Not all variables will be available in all contexts.'), + ); + $form['severity'] = array( + '#type' => 'select', + '#title' => t('Severity'), + '#default_value' => isset($context['severity']) ? $context['severity'] : 6, + '#description' => t('The severity of the message to be logged.'), + '#options' => array( + 0 => 'Emergency: system is unusable', + 1 => 'Alert: action must be taken immediately', + 2 => 'Critical: critical conditions', + 3 => 'Error: error conditions', + 4 => 'Warning: warning conditions', + 5 => 'Notice: normal but significant condition', + 6 => 'Informational: informational messages', + 7 => 'Debug: debug-level messages', + ), + ); + $form['type'] = array( + '#type' => 'textfield', + '#title' => t('Type'), + '#default_value' => isset($context['type']) ? $context['type'] : '', + '#description' => t('The "type" of the message. Can be anything (for example, content, taxonomy, or user).'), + '#size' => 40, + '#maxlength' => 255, + ); + + return $form; +} + +function system_watchdog_action_submit($form, $form_state) { + return array( + 'message' => $form_state['values']['message'], + 'severity' => $form_state['values']['severity'], + 'type' => $form_state['values']['type'], + ); +} + +/** + * A configurable Drupal action. Logs a message in watchdog + */ +function system_watchdog_action(&$object, $context = array()) { + global $user; + $variables = array( + '%site_name' => variable_get('site_name', 'Drupal'), + '%username' => $user->name ? $user->name : variable_get('anonymous', t('Anonymous')), + ); + $url = $title = NULL; + + // This action can be called in any context, but if placeholders + // are used a node object must be present to be the source + // of substituted text. + switch ($context['hook']) { + case 'nodeapi': + // Because this is not an action of type 'node' the node + // will not be passed as $object, but it will still be available + // in $context. + $node = $context['node']; + $url = url('node/'. $node->nid, array('absolute' => TRUE)); + $title = $node->title; + break; + // The comment hook also provides the node, in context. + case 'comment': + $comment = $context['comment']; + $node = node_load($comment->nid); + $url = url('node/'. $comment->nid, array('fragment' => 'comment-'. $comment->cid, 'absolute' => TRUE)); + $title = $comment->title; + break; + case 'taxonomy': + $vocabulary = taxonomy_vocabulary_load($object->vid); + $variables = array_merge($variables, array( + '%term_name' => $object->name, + '%term_description' => $object->description, + '%term_id' => $object->tid, + '%vocabulary_name' => $vocabulary->name, + '%vocabulary_description' => $vocabulary->description, + '%vocabulary_id' => $vocabulary->vid, + ) + ); + $url = url('taxonomy/term/'. $object->tid, array('absolute' => TRUE)); + $title = $object->name; + break; + default: + // We are being called directly. + $node = $object; + } + + if (isset($node) && is_object($node)) { + $variables = array_merge($variables, array( + '%uid' => $node->uid, + '%node_url' => url('node/'. $node->nid, array('absolute' => TRUE)), + '%node_type' => check_plain(node_get_types('name', $node)), + '%title' => filter_xss($node->title), + '%teaser' => filter_xss($node->teaser), + '%body' => filter_xss($node->body), + ) + ); + } + $context['message'] = strtr($context['message'], $variables); + watchdog($context['type'], $context['message'], array(), $context['severity'], l($title, $url)); +} + /** * Implementation of a configurable Drupal action. Redirect user to a URL. */ Index: modules/trigger/trigger.admin.inc =================================================================== RCS file: modules/trigger/trigger.admin.inc diff -N modules/trigger/trigger.admin.inc --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ modules/trigger/trigger.admin.inc 5 Sep 2007 04:57:38 -0000 @@ -0,0 +1,207 @@ + $hook) { + if (isset($hook[$type])) { + foreach ($hook[$type] as $op => $description) { + $form_id = 'trigger_'. $type .'_'. $op .'_assign_form'; + $output .= drupal_get_form($form_id, $type, $op, $description['runs when']); + } + } + } + return $output; +} + +/** + * Create the form definition for assigning an action to a hook-op combination. + * + * @param $form_state + * Information about the current form. + * @param $hook + * The name of the hook, e.g., 'nodeapi'. + * @param $op + * The name of the hook operation, e.g., 'insert'. + * @param $description + * A plain English description of what this hook operation does. + * @return + */ +function trigger_assign_form($form_state, $hook, $op, $description) { + $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'][] = 'trigger_assign_form_submit'; + + $options = array(); + $functions = array(); + // Restrict the options list to trigger that declare support for this hook-op combination. + foreach (actions_list() as $func => $metadata) { + if (isset($metadata['hooks']['any']) || (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['callback'], $functions)) { + $options[$action['type']][$aid] = $action['description']; + } + } + + $form[$op] = array( + '#type' => 'fieldset', + '#title' => $description, + '#theme' => 'trigger_display' + ); + // Retrieve trigger that are already assigned to this hook-op combination. + $triggers = _trigger_get_hook_trigger($hook, $op); + $form[$op]['assigned']['#type'] = 'value'; + $form[$op]['assigned']['#value'] = array(); + foreach ($triggers as $aid => $description) { + $form[$op]['assigned']['#value'][$aid] = array( + 'description' => $description, + 'link' => l(t('remove'), "admin/build/trigger/remove/$hook/$op/". md5($aid)), + ); + } + + $form[$op]['parent'] = array( + '#prefix' => "
", + '#suffix' => '
', + ); + // List possible trigger 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 action support this context.') + ); + } + return $form; +} + +function trigger_assign_form_submit($form, $form_state) { + $form_values = $form_state['values']; + if (!empty($form_values['aid'])) { + $aid = trigger_function_lookup($form_values['aid']); + $weight = db_result(db_query("SELECT MAX(weight) FROM {trigger_assignments} WHERE hook = '%s' AND op = '%s'", $form_values['hook'], $form_values['operation'])); + db_query("INSERT INTO {trigger_assignments} values ('%s', '%s', '%s', %d)", $form_values['hook'], $form_values['operation'], $aid, $weight + 1); + } +} + +/** + * Display trigger assigned to this hook-op combination in a table. + * + * @param array $element + * The fieldset including all assigned trigger. + * @return + * The rendered form with the table prepended. + */ +function theme_trigger_display($element) { + $header = array(); + $rows = array(); + if (count($element['assigned']['#value'])) { + $header = array(array('data' => t('Name')), array('data' => 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; +} + +/** + * Confirm removal of an assigned action. + * + * @param $hook + * @param $op + * @param $aid + * The action ID. + */ +function trigger_unassign($form_state, $hook = NULL, $op = NULL, $aid = NULL) { + if (!($hook && $op && $aid)) { + drupal_goto('admin/build/trigger'); + } + + $form['hook'] = array( + '#type' => 'value', + '#value' => $hook, + ); + $form['operation'] = array( + '#type' => 'value', + '#value' => $op, + ); + $form['aid'] = array( + '#type' => 'value', + '#value' => $aid, + ); + + $action = trigger_function_lookup($aid); + $triggers = trigger_get_all_triggers(); + + $destination = 'admin/build/trigger/'. ($hook == 'nodeapi' ? 'node' : $hook); + + return confirm_form($form, + t('Are you sure you want to remove the action %title?', array('%title' => $triggers[$action]['description'])), + $destination, + t('You can assign it again later if you wish.'), + t('Remove'), t('Cancel') + ); +} + +function trigger_unassign_submit($form, &$form_state) { + $form_values = $form_state['values']; + if ($form_values['confirm'] == 1) { + $aid = trigger_function_lookup($form_values['aid']); + db_query("DELETE FROM {trigger_assignments} WHERE hook = '%s' AND op = '%s' AND aid = '%s'", $form_values['hook'], $form_values['operation'], $aid); + $trigger = trigger_get_all_trigger(); + watchdog('trigger', 'Trigger %trigger has been unassigned.', array('%trigger' => check_plain($trigger[$aid]['description']))); + drupal_set_message(t('Trigger %trigger has been unassigned.', array('%trigger' => $trigger[$aid]['description']))); + $hook = $form_values['hook'] == 'nodeapi' ? 'node' : $form_values['hook']; + $form_state['redirect'] = 'admin/build/trigger/'. $hook; + } + else { + drupal_goto('admin/build/trigger'); + } +} Index: modules/trigger/trigger.info =================================================================== RCS file: modules/trigger/trigger.info diff -N modules/trigger/trigger.info --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ modules/trigger/trigger.info 5 Sep 2007 04:57:38 -0000 @@ -0,0 +1,6 @@ +; $Id: actions.info,v 1.1 2007/06/29 18:06:50 dries Exp $ +name = Trigger +description = Enables actions to be fired on certain triggers +package = Core - optional +version = VERSION +core = 6.x Index: modules/trigger/trigger.install =================================================================== RCS file: modules/trigger/trigger.install diff -N modules/trigger/trigger.install --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ modules/trigger/trigger.install 5 Sep 2007 04:57:38 -0000 @@ -0,0 +1,21 @@ +'. $explanation .' '. t('Below you can assign actions that trigger when certain comment-related operations happen. For example, you could promote a post to the front page when a comment is added.') .'

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

'. $explanation .' '. t('Below you can assign actions that trigger when certain post-related operations happen. For example, you could remove a post from the front page when the post is updated. Note that if you are running trigger that modify the characteristics of a post (such as making a post sticky or removing a post from the front page), you will need to add the %node_save action to save the changes.', array('%node_save' => t('Save post'))) .'

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

'. $explanation .' '. t("Below you can assign actions that trigger 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/trigger/cron': + $output = '

'. $explanation .' '. t('Below you can assign actions that trigger when cron runs.') .'

'; + break; + } + + return $output; +} + +/** + * Implementation of hook_menu(). + */ +function trigger_menu() { + $items['admin/build/trigger'] = array( + 'title' => 'Triggers', + 'description' => 'Assign actions to system events (triggers).', + 'page callback' => 'trigger_assign', + 'access callback' => 'trigger_access_check', + 'access arguments' => array('node'), + 'file' => 'trigger.admin.inc', + ); + // We don't use a menu wildcard here because these are tabs, + // not invisible items + $items['admin/build/trigger/node'] = array( + 'title' => 'Content', + 'page callback' => 'trigger_assign', + 'page arguments' => array('node'), + 'access arguments' => array('node'), + 'type' => MENU_LOCAL_TASK, + 'file' => 'trigger.admin.inc', + ); + $items['admin/build/trigger/user'] = array( + 'title' => 'User', + 'page callback' => 'trigger_assign', + 'page arguments' => array('user'), + 'access arguments' => array('user'), + 'type' => MENU_LOCAL_TASK, + 'file' => 'trigger.admin.inc', + ); + $items['admin/build/trigger/comment'] = array( + 'title' => 'Comment', + 'page callback' => 'trigger_assign', + 'page arguments' => array('comment'), + 'access callback' => 'trigger_access_check', + 'access arguments' => array('comment'), + 'type' => MENU_LOCAL_TASK, + 'file' => 'trigger.admin.inc', + ); + $items['admin/build/trigger/taxonomy'] = array( + 'title' => 'Taxonomy', + 'page callback' => 'trigger_assign', + 'page arguments' => array('taxonomy'), + 'access callback' => 'trigger_access_check', + 'access arguments' => array('taxonomy'), + 'access arguments' => array('taxonomy'), + 'type' => MENU_LOCAL_TASK, + 'file' => 'trigger.admin.inc', + ); + $items['admin/build/trigger/cron'] = array( + 'title' => 'Cron', + 'page callback' => 'trigger_assign', + 'page arguments' => array('cron'), + 'access arguments' => array('cron'), + 'type' => MENU_LOCAL_TASK, + 'file' => 'trigger.admin.inc', + ); + + // We want contributed modules to be able to describe + // their hooks and have trigger assignable to them. + $hooks = module_invoke_all('hook_info'); + foreach ($hooks as $module => $hook) { + // We've already done these + if (in_array($module, array('node', 'comment', 'user', 'system', 'taxonomy'))) { + continue; + } + $info = db_result(db_query("SELECT info FROM {system} WHERE name = '%s'", $module)); + $info = unserialize($info); + $nice_name = $info['name']; + $items["admin/build/trigger/$module"] = array( + 'title' => $nice_name, + 'page callback' => 'trigger_assign', + 'page arguments' => array($module), + 'access arguments' => array($module), + 'type' => MENU_LOCAL_TASK, + 'file' => 'trigger.admin.inc', + ); + } + $items['admin/build/trigger/remove'] = array( + 'title' => 'Unassign', + 'description' => 'Remove an action assignment.', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('trigger_unassign'), + 'type' => MENU_CALLBACK, + 'file' => 'trigger.admin.inc', + ); + + return $items; +} + +/** + * Access callback for menu system. + */ +function trigger_access_check($module) { + return (module_exists($module) && user_access('administer actions')); +} + + +/** + * Get the trigger 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 trigger have been assigned, + * e.g. 'nodeapi'. + * @param $op + * The hook operation for which the trigger have been assigned, + * e.g., 'view'. + * @return + * An array of action descriptions keyed by action IDs. + */ +function _trigger_get_hook_trigger($hook, $op, $type = NULL) { + $triggers = array(); + if ($type) { + $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); + } + else { + $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); + } + while ($trigger = db_fetch_object($result)) { + $triggers[$trigger->aid] = $trigger->description; + } + return $triggers; +} + +/** + * Get the aids of trigger 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 _trigger_get_hook_aids($hook, $op = '') { + $aids = array(); + $result = db_query("SELECT aa.aid, a.type FROM {trigger_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 ($trigger = db_fetch_object($result)) { + $aids[$trigger->aid]['type'] = $trigger->type; + } + return $aids; +} + +/** + * Implementation of hook_theme(). + */ +function trigger_theme() { + return array( + 'trigger_display' => array( + 'arguments' => array('element'), + 'file' => 'trigger.admin.inc', + ), + ); +} + +/** + * Implementation of hook_forms(). We reuse code by using the + * same assignment form definition for each node-op combination. + */ +function trigger_forms() { + $hooks = module_invoke_all('hook_info'); + foreach ($hooks as $module => $info) { + foreach ($hooks[$module] as $hook => $ops) { + foreach ($ops as $op => $description) { + $forms['trigger_'. $hook .'_'. $op .'_assign_form'] = array('callback' => 'trigger_assign_form'); + } + } + } + + return $forms; +} + +/** + * When an action is called in a context that does not match its type, + * the object that the action expects must be retrieved. For example, when + * an action that works on users is called during the node hook, the + * user object is not available since the node hook doesn't pass it. + * So here we load the object the action expects. + * + * @param $type + * The type of action that is about to be called. + * @param $node + * The node that was passed via the nodeapi hook. + * @return + * The object expected by the action that is about to be called. + */ +function _trigger_normalize_node_context($type, $node) { + switch ($type) { + // If an action that works on comments is being called in a node context, + // the action is expecting a comment object. But we do not know which comment + // to give it. The first? The most recent? All of them? So comment trigger + // in a node context are not supported. + + // 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 trigger_nodeapi($node, $op, $a3, $a4) { + // Keep objects for reuse so that changes trigger make to objects can persist. + static $objects; + // Prevent recursion by tracking which operations have already been called. + static $recursion; + // Support a subset of operations. + if (!in_array($op, array('view', 'update', 'presave', 'insert', 'delete')) || isset($recursion[$op])) { + return; + } + $recursion[$op] = TRUE; + + $aids = _trigger_get_hook_aids('nodeapi', $op); + if (!$aids) { + return; + } + $context = array( + 'hook' => 'nodeapi', + 'op' => $op, + ); + + // 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 trigger + // 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']] = _trigger_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, $objects[$action_info['type']], $context, $a4, $a4); + } + else { + actions_do($aid, $node, $context, $a3, $a4); + } + } +} + +/** + * When an action is called in a context that does not match its type, + * the object that the action expects must be retrieved. For example, when + * an action that works on nodes is called during the comment hook, the + * node object is not available since the comment hook doesn't pass it. + * So here we load the object the action expects. + * + * @param $type + * The type of action that is about to be called. + * @param $comment + * The comment that was passed via the comment hook. + * @return + * The object expected by the action that is about to be called. + */ +function _trigger_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 trigger_comment($a1, $op) { + // Keep objects for reuse so that changes trigger make to objects can persist. + static $objects; + // We support a subset of operations. + if (!in_array($op, array('insert', 'update', 'delete', 'view'))) { + return; + } + $aids = _trigger_get_hook_aids('comment', $op); + $context = array( + 'hook' => 'comment', + 'op' => $op, + ); + // 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 trigger + // 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']] = _trigger_normalize_comment_context($action_info['type'], $a1); + } + // Since we know about the comment, we pass it along to the action + // in case it wants to peek at it. + $context['comment'] = (object) $a1; + actions_do($aid, $objects[$action_info['type']], $context); + } + else { + actions_do($aid, (object) $a1, $context); + } + } +} + +/** + * Implementation of hook_cron(). + */ +function trigger_cron() { + $aids = _trigger_get_hook_aids('cron'); + $context = array( + 'hook' => 'cron', + 'op' => '', + ); + // Cron does not act on any specific object. + $object = NULL; + actions_do(array_keys($aids), $object, $context); +} + +/** + * When an action is called in a context that does not match its type, + * the object that the action expects must be retrieved. For example, when + * an action that works on nodes is called during the user hook, the + * node object is not available since the user hook doesn't pass it. + * So here we load the object the action expects. + * + * @param $type + * The type of action that is about to be called. + * @param $account + * The account object that was passed via the user hook. + * @return + * The object expected by the action that is about to be called. + */ +function _trigger_normalize_user_context($type, $account) { + switch ($type) { + // If an action that works on comments is being called in a user context, + // the action is expecting a comment object. But we have no way of + // determining the appropriate comment object to pass. So comment + // trigger in a user context are not supported. + + // An action that works with nodes is being called in a user context. + // If a single node is being viewed, return the node. + case 'node': + // If we are viewing an individual node, return the node. + if ((arg(0) == 'node') && is_numeric(arg(1)) && (arg(2) == NULL)) { + return node_load(array('nid' => arg(1))); + } + } +} + +/** + * Implementation of hook_user(). + */ +function trigger_user($op, &$edit, &$account, $category = NULL) { + // Keep objects for reuse so that changes trigger make to objects can persist. + static $objects; + // We support a subset of operations. + if (!in_array($op, array('login', 'logout', 'insert', 'update', 'delete', 'view'))) { + return; + } + $aids = _trigger_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') { + if (!isset($objects[$action_info['type']])) { + $objects[$action_info['type']] = _trigger_normalize_user_context($action_info['type'], $account); + } + $context['account'] = $account; + actions_do($aid, $objects[$action_info['type']], $context); + } + else { + actions_do($aid, $account, $context, $category); + } + } +} + +/** + * Implementation of hook_taxonomy(). + */ +function trigger_taxonomy($op, $type, $array) { + if ($type != 'term') { + return; + } + $aids = _trigger_get_hook_aids('taxonomy', $op); + $context = array( + 'hook' => 'taxonomy', + 'op' => $op + ); + foreach ($aids as $aid => $action_info) { + actions_do($aid, (object) $array, $context); + } +} + +/** + * 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 trigger_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]; + } +} Index: modules/trigger/trigger.schema =================================================================== RCS file: modules/trigger/trigger.schema diff -N modules/trigger/trigger.schema --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ modules/trigger/trigger.schema 5 Sep 2007 04:57:38 -0000 @@ -0,0 +1,16 @@ + 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; +} Index: modules/user/user.module =================================================================== RCS file: /cvs/drupal/drupal/modules/user/user.module,v retrieving revision 1.840 diff -u -p -r1.840 user.module --- modules/user/user.module 4 Sep 2007 14:37:48 -0000 1.840 +++ modules/user/user.module 5 Sep 2007 04:57:39 -0000 @@ -3438,8 +3438,8 @@ function user_action_info() { 'nodeapi' => array('presave', 'delete', 'insert', 'update', 'view'), 'comment' => array('view', 'insert', 'update', 'delete'), 'user' => array('logout'), - ), ), + ), 'user_block_ip_action' => array( 'description' => t('Ban IP address of current user'), 'type' => 'user', @@ -3450,6 +3450,16 @@ function user_action_info() { 'user' => array('logout'), ) ), + 'user_delete_action' => array( + 'description' => t('Delete the current user'), + 'type' => 'user', + 'configurable' => FALSE, + 'hooks' => array( + 'nodeapi' => array('presave', 'delete', 'insert', 'update', 'view'), + 'comment' => array('view', 'insert', 'update', 'delete'), + 'user' => array('logout'), + ) + ), ); } @@ -3481,3 +3491,11 @@ function user_block_ip_action() { db_query("INSERT INTO {access} (mask, type, status) VALUES ('%s', '%s', %d)", $_SERVER['REMOTE_ADDR'], 'host', 0); watchdog('action', 'Banned IP address %ip', array('%ip' => $_SERVER['REMOTE_ADDR'])); } + +/** + * Implementation of a Drupal action. + * Deletes the current user + */ +function user_delete_action(&$user) { + user_delete($user); +}