2007-06-29 18:06:51 +00:00
<?php
// $Id$
/**
2007-08-29 14:57:50 +00:00
* @file
* Enables functions to be stored and executed at a later time when
* triggered by other modules or by one of Drupal's core API hooks.
*/
2007-06-29 18:06:51 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_help().
2007-08-29 14:57:50 +00:00
*/
2007-09-11 14:50:05 +00:00
function trigger_help($path, $arg) {
2009-12-12 21:05:20 +00:00
$explanation = '<p>' . t('Triggers are events on your site, such as new content being added or a user logging in. The Trigger module associates these triggers with actions (functional tasks), such as unpublishing content containing certain keywords or e-mailing an administrator. The <a href="@url">Actions settings page</a> contains a list of existing actions and provides the ability to create and configure advanced actions (actions requiring configuration, such as an e-mail address or a list of banned words).', array('@url' => url('admin/config/system/actions'))) . '</p>';
2007-08-29 14:57:50 +00:00
switch ($path) {
2009-07-20 18:51:36 +00:00
case 'admin/structure/trigger/comment':
2008-04-14 17:48:46 +00:00
return $explanation . '<p>' . t('Below you can assign actions to run when certain comment-related triggers happen. For example, you could promote a post to the front page when a comment is added.') . '</p>';
2009-07-20 18:51:36 +00:00
case 'admin/structure/trigger/node':
2009-09-19 11:07:37 +00:00
return $explanation . '<p>' . t('Below you can assign actions to run when certain content-related triggers happen. For example, you could send an e-mail to an administrator when content is created or updated.') . '</p>';
case 'admin/structure/trigger/system':
2008-04-14 17:48:46 +00:00
return $explanation . '<p>' . t('Below you can assign actions to run during each pass of a <a href="@cron">cron maintenance task</a>.', array('@cron' => url('admin/reports/status'))) . '</p>';
2009-07-20 18:51:36 +00:00
case 'admin/structure/trigger/taxonomy':
2008-04-14 17:48:46 +00:00
return $explanation . '<p>' . t('Below you can assign actions to run when certain taxonomy-related triggers happen. For example, you could send an e-mail to an administrator when a term is deleted.') . '</p>';
2009-07-20 18:51:36 +00:00
case 'admin/structure/trigger/user':
2008-04-14 17:48:46 +00:00
return $explanation . '<p>' . t("Below you can assign actions to run when certain user-related triggers happen. For example, you could send an e-mail to an administrator when a user account is deleted.") . '</p>';
2009-12-06 18:46:13 +00:00
2007-09-11 14:50:05 +00:00
case 'admin/help#trigger':
2009-12-06 18:46:13 +00:00
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
2009-12-12 21:05:20 +00:00
$output .= '<p>' . t('The Trigger module provides the ability to cause <em>actions</em> to run when certain <em>triggers</em> take place on your site. Triggers are events, such as new content being added to your site or a user logging in, and actions are tasks, such as unpublishing content or e-mailing an administrator. For more information, see the online handbook entry for <a href="@trigger">Trigger module</a>.', array('@trigger' => 'http://drupal.org/handbook/modules/trigger/')) . '</p>';
2009-12-06 18:46:13 +00:00
$output .= '<h3>' . t('Uses') . '</h3>';
$output .= '<dl>';
2009-12-12 21:05:20 +00:00
$output .= '<dt>' . t('Configuring triggers and actions') . '</dt>';
$output .= '<dd>' . t('The combination of actions and triggers can perform many useful tasks, such as e-mailing an administrator if a user account is deleted, or automatically unpublishing comments that contain certain words. To set up a trigger/action combination, first visit the <a href="@actions-page">Actions configuration page</a>, where you can either verify that the action you want is already listed, or create a new <em>advanced</em> action. You will need to set up an advanced action if there are configuration options in your trigger/action combination, such as specifying an e-mail address or a list of banned words. After configuring or verifying your action, visit the <a href="@triggers-page">Triggers configuration page</a> and choose the appropriate tab (Comment, Taxonomy, etc.), where you can assign the action to run when the trigger event occurs.', array('@triggers-page' => url('admin/structure/trigger'), '@actions-page' => url('admin/config/system/actions'))) . '</dd>';
2009-12-06 18:46:13 +00:00
$output .= '</dl>';
2007-10-24 11:56:33 +00:00
return $output;
2007-06-29 18:06:51 +00:00
}
}
/**
2009-12-04 16:49:48 +00:00
* Implements hook_menu().
2007-08-29 14:57:50 +00:00
*/
2007-09-11 14:50:05 +00:00
function trigger_menu() {
2009-07-20 18:51:36 +00:00
$items['admin/structure/trigger'] = array(
2007-09-11 14:50:05 +00:00
'title' => 'Triggers',
2009-09-19 11:07:37 +00:00
'description' => 'Configure when to execute actions.',
2007-09-11 14:50:05 +00:00
'page callback' => 'trigger_assign',
2009-08-22 15:35:36 +00:00
'access arguments' => array('administer actions'),
2009-08-24 00:14:23 +00:00
'file' => 'trigger.admin.inc',
2007-06-29 18:06:51 +00:00
);
2009-08-22 15:35:36 +00:00
2007-06-29 18:06:51 +00:00
// We want contributed modules to be able to describe
// their hooks and have actions assignable to them.
2009-09-19 11:07:37 +00:00
$trigger_info = module_invoke_all('trigger_info');
drupal_alter('trigger_info', $trigger_info);
foreach ($trigger_info as $module => $hooks) {
2009-05-29 19:15:08 +00:00
$info = db_select('system')
2009-08-22 15:35:36 +00:00
->fields('system', array('info'))
2009-05-29 19:15:08 +00:00
->condition('name', $module)
2009-09-19 11:07:37 +00:00
->condition('status', 1)
2009-05-29 19:15:08 +00:00
->execute()
->fetchField();
2009-09-19 11:07:37 +00:00
if ($info) {
$info = unserialize($info);
$nice_name = $info['name'];
$items["admin/structure/trigger/$module"] = array(
'title' => $nice_name,
'page callback' => 'trigger_assign',
'page arguments' => array($module),
'access arguments' => array('administer actions'),
'type' => MENU_LOCAL_TASK,
'file' => 'trigger.admin.inc',
);
}
2007-06-29 18:06:51 +00:00
}
2009-07-20 18:51:36 +00:00
$items['admin/structure/trigger/unassign'] = array(
2007-06-29 18:06:51 +00:00
'title' => 'Unassign',
2007-09-11 14:50:05 +00:00
'description' => 'Unassign an action from a trigger.',
2007-06-29 18:06:51 +00:00
'page callback' => 'drupal_get_form',
2007-09-11 14:50:05 +00:00
'page arguments' => array('trigger_unassign'),
2008-04-23 20:01:56 +00:00
'access arguments' => array('administer actions'),
2007-06-29 18:06:51 +00:00
'type' => MENU_CALLBACK,
2009-08-24 00:14:23 +00:00
'file' => 'trigger.admin.inc',
2007-06-29 18:06:51 +00:00
);
2007-08-29 14:57:50 +00:00
return $items;
2007-06-29 18:06:51 +00:00
}
/**
2009-12-04 16:49:48 +00:00
* Implements hook_trigger_info().
2009-09-19 11:07:37 +00:00
*
* Defines all the triggers that this module implements triggers for.
*/
function trigger_trigger_info() {
return array(
'node' => array(
'node_presave' => array(
'label' => t('When either saving new content or updating existing content'),
),
'node_insert' => array(
'label' => t('After saving new content'),
),
'node_update' => array(
'label' => t('After saving updated content'),
),
'node_delete' => array(
'label' => t('After deleting content'),
),
'node_view' => array(
'label' => t('When content is viewed by an authenticated user'),
),
),
'comment' => array(
2009-11-06 03:59:06 +00:00
'comment_presave' => array(
'label' => t('When either saving a new comment or updating an existing comment'),
),
'comment_insert' => array(
2009-09-19 11:07:37 +00:00
'label' => t('After saving a new comment'),
),
'comment_update' => array(
'label' => t('After saving an updated comment'),
),
'comment_delete' => array(
'label' => t('After deleting a comment'),
),
'comment_view' => array(
'label' => t('When a comment is being viewed by an authenticated user'),
),
),
'taxonomy' => array(
'taxonomy_term_insert' => array(
'label' => t('After saving a new term to the database'),
),
'taxonomy_term_update' => array(
'label' => t('After saving an updated term to the database'),
),
'taxonomy_term_delete' => array(
'label' => t('After deleting a term'),
),
),
'system' => array(
'cron' => array(
'label' => t('When cron runs'),
),
),
'user' => array(
2009-10-10 16:48:39 +00:00
'user_presave' => array(
'label' => t('When either creating a new user account or updating an existing'),
),
2009-09-19 11:07:37 +00:00
'user_insert' => array(
2009-10-10 16:48:39 +00:00
'label' => t('After creating a new user account'),
2009-09-19 11:07:37 +00:00
),
'user_update' => array(
2009-10-10 16:48:39 +00:00
'label' => t('After updating a user account'),
2009-09-19 11:07:37 +00:00
),
'user_delete' => array(
'label' => t('After a user has been deleted'),
),
'user_login' => array(
'label' => t('After a user has logged in'),
),
'user_logout' => array(
'label' => t('After a user has logged out'),
),
'user_view' => array(
'label' => t("When a user's profile is being viewed"),
),
),
);
}
/**
* Gets the action IDs of actions to be executed for a hook.
2007-06-29 18:06:51 +00:00
*
* @param $hook
* The name of the hook being fired.
* @return
2009-09-19 11:07:37 +00:00
* An array whose keys are action IDs that the user has associated with
* this trigger, and whose values are arrays containing the action type and
* label.
2007-06-29 18:06:51 +00:00
*/
2009-09-19 11:07:37 +00:00
function trigger_get_assigned_actions($hook) {
return db_query("SELECT ta.aid, a.type, a.label FROM {trigger_assignments} ta LEFT JOIN {actions} a ON ta.aid = a.aid WHERE ta.hook = :hook ORDER BY ta.weight", array(
2009-05-29 19:15:08 +00:00
':hook' => $hook,
2009-09-19 11:07:37 +00:00
))->fetchAllAssoc( 'aid', PDO::FETCH_ASSOC);
2007-06-29 18:06:51 +00:00
}
/**
2009-12-04 16:49:48 +00:00
* Implements hook_theme().
2007-06-29 18:06:51 +00:00
*/
2007-09-11 14:50:05 +00:00
function trigger_theme() {
2007-06-29 18:06:51 +00:00
return array(
2007-09-11 14:50:05 +00:00
'trigger_display' => array(
2009-10-23 22:24:19 +00:00
'render element' => 'element',
2009-05-21 23:07:16 +00:00
'file' => 'trigger.admin.inc',
2007-06-29 18:06:51 +00:00
),
);
}
/**
2009-12-04 16:49:48 +00:00
* Implements hook_forms().
2009-09-19 11:07:37 +00:00
*
* We re-use code by using the same assignment form definition for each hook.
2007-06-29 18:06:51 +00:00
*/
2007-09-11 14:50:05 +00:00
function trigger_forms() {
2009-09-19 11:07:37 +00:00
$trigger_info = _trigger_get_all_info();
$forms = array();
foreach ($trigger_info as $module => $hooks) {
foreach ($hooks as $hook => $description) {
$forms['trigger_' . $hook . '_assign_form'] = array('callback' => 'trigger_assign_form');
2007-06-29 18:06:51 +00:00
}
}
return $forms;
}
/**
2009-09-19 11:07:37 +00:00
* Loads associated objects for node triggers.
*
* 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 a node hook implementation, the user
* object is not available since the node hook call doesn't pass it. So here we
* load the object the action expects.
2007-06-29 18:06:51 +00:00
*
* @param $type
* The type of action that is about to be called.
* @param $node
2009-03-08 04:25:07 +00:00
* The node that was passed via the node hook.
2009-09-19 11:07:37 +00:00
*
2007-06-29 18:06:51 +00:00
* @return
* The object expected by the action that is about to be called.
*/
2009-11-08 10:02:41 +00:00
function _trigger_normalize_node_context($type, $node) {
2009-09-19 11:07:37 +00:00
// Note that comment-type actions are not supported in node contexts,
// because we wouldn't know which comment to choose.
2007-06-29 18:06:51 +00:00
switch ($type) {
// An action that works on users is being called in a node context.
// Load the user object of the node's author.
case 'user':
2009-03-14 23:01:38 +00:00
return user_load($node->uid);
2007-06-29 18:06:51 +00:00
}
}
/**
2009-09-19 11:07:37 +00:00
* Calls action functions for node triggers.
2008-10-06 12:55:56 +00:00
*
2009-09-19 11:07:37 +00:00
* @param $node
* Node object.
* @param $op
* Operation to trigger.
* @param $a3
* Additional argument to action function.
* @param $a4
* Additional argument to action function.
2007-06-29 18:06:51 +00:00
*/
2009-11-08 10:02:41 +00:00
function _trigger_node($node, $hook, $a3 = NULL, $a4 = NULL) {
2007-06-29 18:06:51 +00:00
// 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;
2009-09-19 11:07:37 +00:00
if (isset($recursion[$hook])) {
2007-06-29 18:06:51 +00:00
return;
}
2009-09-19 11:07:37 +00:00
$recursion[$hook] = TRUE;
2007-06-29 18:06:51 +00:00
2009-09-19 11:07:37 +00:00
$aids = trigger_get_assigned_actions($hook);
2007-06-29 18:06:51 +00:00
if (!$aids) {
return;
}
$context = array(
2009-09-19 11:07:37 +00:00
'group' => 'node',
'hook' => $hook,
2007-06-29 18:06:51 +00:00
);
// 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.
2009-09-19 11:07:37 +00:00
foreach ($aids as $aid => $info) {
$type = $info['type'];
2009-06-05 15:41:05 +00:00
if ($type != 'node') {
if (!isset($objects[$type])) {
$objects[$type] = _trigger_normalize_node_context($type, $node);
2007-06-29 18:06:51 +00:00
}
// Since we know about the node, we pass that info along to the action.
$context['node'] = $node;
2009-06-05 15:41:05 +00:00
$result = actions_do($aid, $objects[$type], $context, $a3, $a4);
2007-06-29 18:06:51 +00:00
}
else {
actions_do($aid, $node, $context, $a3, $a4);
}
}
}
2008-11-11 16:49:38 +00:00
2008-10-06 12:55:56 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_node_view().
2008-10-06 12:55:56 +00:00
*/
2009-11-08 10:02:41 +00:00
function trigger_node_view($node, $build_mode) {
2009-09-19 11:07:37 +00:00
_trigger_node($node, 'node_view', $build_mode);
2008-10-06 12:55:56 +00:00
}
/**
2009-12-04 16:49:48 +00:00
* Implements hook_node_update().
2008-10-06 12:55:56 +00:00
*/
2009-11-08 10:02:41 +00:00
function trigger_node_update($node) {
2009-09-19 11:07:37 +00:00
_trigger_node($node, 'node_update');
2008-10-06 12:55:56 +00:00
}
/**
2009-12-04 16:49:48 +00:00
* Implements hook_node_presave().
2008-10-06 12:55:56 +00:00
*/
2009-11-08 10:02:41 +00:00
function trigger_node_presave($node) {
2009-09-19 11:07:37 +00:00
_trigger_node($node, 'node_presave');
2008-10-06 12:55:56 +00:00
}
/**
2009-12-04 16:49:48 +00:00
* Implements hook_node_insert().
2008-10-06 12:55:56 +00:00
*/
2009-11-08 10:02:41 +00:00
function trigger_node_insert($node) {
2009-09-19 11:07:37 +00:00
_trigger_node($node, 'node_insert');
2008-10-06 12:55:56 +00:00
}
/**
2009-12-04 16:49:48 +00:00
* Implements hook_node_delete().
2008-10-06 12:55:56 +00:00
*/
2009-11-08 10:02:41 +00:00
function trigger_node_delete($node) {
2009-09-19 11:07:37 +00:00
_trigger_node($node, 'node_delete');
2008-10-06 12:55:56 +00:00
}
2007-06-29 18:06:51 +00:00
/**
2009-09-19 11:07:37 +00:00
* Loads associated objects for comment triggers.
*
* 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.
2007-06-29 18:06:51 +00:00
*
* @param $type
* The type of action that is about to be called.
* @param $comment
* The comment that was passed via the comment hook.
2009-09-19 11:07:37 +00:00
*
2007-06-29 18:06:51 +00:00
* @return
* The object expected by the action that is about to be called.
*/
2007-09-11 14:50:05 +00:00
function _trigger_normalize_comment_context($type, $comment) {
2007-06-29 18:06:51 +00:00
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':
2009-03-14 23:01:38 +00:00
return user_load(is_array($comment) ? $comment['uid'] : $comment->uid);
2007-06-29 18:06:51 +00:00
}
}
2009-11-06 03:59:06 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_comment_presave().
2009-11-06 03:59:06 +00:00
*/
function trigger_comment_presave($comment) {
_trigger_comment($comment, 'comment_presave');
}
2007-06-29 18:06:51 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_comment_insert().
2007-06-29 18:06:51 +00:00
*/
2009-07-01 20:39:20 +00:00
function trigger_comment_insert($comment) {
2009-09-19 11:07:37 +00:00
_trigger_comment($comment, 'comment_insert');
2009-01-04 16:10:48 +00:00
}
/**
2009-12-04 16:49:48 +00:00
* Implements hook_comment_update().
2009-01-04 16:10:48 +00:00
*/
2009-07-01 20:39:20 +00:00
function trigger_comment_update($comment) {
2009-09-19 11:07:37 +00:00
_trigger_comment($comment, 'comment_update');
2009-01-04 16:10:48 +00:00
}
/**
2009-12-04 16:49:48 +00:00
* Implements hook_comment_delete().
2009-01-04 16:10:48 +00:00
*/
function trigger_comment_delete($comment) {
2009-09-19 11:07:37 +00:00
_trigger_comment($comment, 'comment_delete');
2009-01-04 16:10:48 +00:00
}
/**
2009-12-04 16:49:48 +00:00
* Implements hook_comment_view().
2009-01-04 16:10:48 +00:00
*/
function trigger_comment_view($comment) {
2009-09-19 11:07:37 +00:00
_trigger_comment($comment, 'comment_view');
2009-01-04 16:10:48 +00:00
}
/**
2009-09-19 11:07:37 +00:00
* Calls action functions for comment triggers.
2009-01-04 16:10:48 +00:00
*
* @param $a1
2009-09-19 11:07:37 +00:00
* Comment object or array of form values.
2009-01-04 16:10:48 +00:00
* @param $op
2009-09-19 11:07:37 +00:00
* Operation to trigger.
2009-01-04 16:10:48 +00:00
*/
2009-09-19 11:07:37 +00:00
function _trigger_comment($a1, $hook) {
2007-06-29 18:06:51 +00:00
// Keep objects for reuse so that changes actions make to objects can persist.
static $objects;
2009-09-19 11:07:37 +00:00
$aids = trigger_get_assigned_actions($hook);
2007-06-29 18:06:51 +00:00
$context = array(
2009-09-19 11:07:37 +00:00
'group' => 'comment',
'hook' => $hook,
2007-06-29 18:06:51 +00:00
);
// We need to get the expected object if the action's type is not 'comment'.
2009-09-19 11:07:37 +00:00
// 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 => $info) {
$type = $info['type'];
2009-06-05 15:41:05 +00:00
if ($type != 'comment') {
if (!isset($objects[$type])) {
$objects[$type] = _trigger_normalize_comment_context($type, $a1);
2007-06-29 18:06:51 +00:00
}
// 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;
2009-06-05 15:41:05 +00:00
actions_do($aid, $objects[$type], $context);
2007-06-29 18:06:51 +00:00
}
else {
2008-08-21 19:36:39 +00:00
actions_do($aid, $a1, $context);
2007-06-29 18:06:51 +00:00
}
}
}
/**
2009-12-04 16:49:48 +00:00
* Implements hook_cron().
2007-06-29 18:06:51 +00:00
*/
2007-09-11 14:50:05 +00:00
function trigger_cron() {
2009-09-19 11:07:37 +00:00
$aids = trigger_get_assigned_actions('cron');
2007-06-29 18:06:51 +00:00
$context = array(
2009-09-19 11:07:37 +00:00
'group' => 'cron',
2007-06-29 18:06:51 +00:00
'hook' => 'cron',
);
// Cron does not act on any specific object.
$object = NULL;
actions_do(array_keys($aids), $object, $context);
}
/**
2009-09-19 11:07:37 +00:00
* Loads associated objects for user triggers.
*
* 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.
2007-06-29 18:06:51 +00:00
*
* @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.
*/
2007-09-11 14:50:05 +00:00
function _trigger_normalize_user_context($type, $account) {
2009-09-19 11:07:37 +00:00
// Note that comment-type actions are not supported in user contexts,
// because we wouldn't know which comment to choose.
2007-06-29 18:06:51 +00:00
switch ($type) {
// 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)));
}
2009-09-19 11:07:37 +00:00
break;
2007-06-29 18:06:51 +00:00
}
}
/**
2009-12-04 16:49:48 +00:00
* Implements hook_user_login().
2007-06-29 18:06:51 +00:00
*/
2009-08-12 12:36:05 +00:00
function trigger_user_login(&$edit, $account, $category) {
2009-09-19 11:07:37 +00:00
_trigger_user('user_login', $edit, $account, $category);
2008-10-06 11:30:12 +00:00
}
/**
2009-12-04 16:49:48 +00:00
* Implements hook_user_logout().
2008-10-06 11:30:12 +00:00
*/
2009-08-12 12:36:05 +00:00
function trigger_user_logout($account) {
2009-09-19 11:07:37 +00:00
_trigger_user('user_logout', $edit = NULL, $account);
2008-10-06 11:30:12 +00:00
}
2009-10-10 16:48:39 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_user_presave().
2009-10-10 16:48:39 +00:00
*/
function trigger_user_presave(&$edit, $account, $category) {
_trigger_user('user_presave', $edit, $account, $category);
}
2008-10-06 11:30:12 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_user_insert().
2008-10-06 11:30:12 +00:00
*/
2009-08-12 12:36:05 +00:00
function trigger_user_insert(&$edit, $account, $category) {
2009-09-19 11:07:37 +00:00
_trigger_user('user_insert', $edit, $account, $category);
2008-10-06 11:30:12 +00:00
}
/**
2009-12-04 16:49:48 +00:00
* Implements hook_user_update().
2008-10-06 11:30:12 +00:00
*/
2009-08-12 12:36:05 +00:00
function trigger_user_update(&$edit, $account, $category) {
2009-09-19 11:07:37 +00:00
_trigger_user('user_update', $edit, $account, $category);
2008-10-06 11:30:12 +00:00
}
/**
2009-12-04 16:49:48 +00:00
* Implements hook_user_cancel().
2008-10-06 11:30:12 +00:00
*/
2009-01-22 12:46:07 +00:00
function trigger_user_cancel($edit, $account, $method) {
2009-01-08 08:42:13 +00:00
switch ($method) {
case 'user_cancel_reassign':
case 'user_cancel_delete':
2009-09-19 11:07:37 +00:00
_trigger_user('user_delete', $edit, $account, $method);
2009-01-08 08:42:13 +00:00
break;
}
2008-10-06 11:30:12 +00:00
}
/**
2009-12-04 16:49:48 +00:00
* Implements hook_user_view().
2008-10-06 11:30:12 +00:00
*/
2009-08-12 12:36:05 +00:00
function trigger_user_view($account) {
2009-09-19 11:07:37 +00:00
_trigger_user('user_view', $edit = NULL, $account, NULL);
2008-10-06 11:30:12 +00:00
}
/**
2009-09-19 11:07:37 +00:00
* Calls action functions for user triggers.
2008-10-06 11:30:12 +00:00
*/
2009-09-19 11:07:37 +00:00
function _trigger_user($hook, &$edit, $account, $category = NULL) {
2007-06-29 18:06:51 +00:00
// Keep objects for reuse so that changes actions make to objects can persist.
static $objects;
2009-09-19 11:07:37 +00:00
$aids = trigger_get_assigned_actions($hook);
2007-06-29 18:06:51 +00:00
$context = array(
2009-09-19 11:07:37 +00:00
'group' => 'user',
'hook' => $hook,
2007-06-29 18:06:51 +00:00
'form_values' => &$edit,
);
2009-09-19 11:07:37 +00:00
foreach ($aids as $aid => $info) {
$type = $info['type'];
2009-06-05 15:41:05 +00:00
if ($type != 'user') {
if (!isset($objects[$type])) {
$objects[$type] = _trigger_normalize_user_context($type, $account);
2007-06-29 18:06:51 +00:00
}
$context['account'] = $account;
2009-06-05 15:41:05 +00:00
actions_do($aid, $objects[$type], $context);
2007-06-29 18:06:51 +00:00
}
else {
actions_do($aid, $account, $context, $category);
}
}
}
/**
2009-09-19 11:07:37 +00:00
* Calls action functions for taxonomy triggers.
*
* @param $hook
* Hook to trigger actions for taxonomy_term_insert(),
* taxonomy_term_update(), and taxonomy_term_delete().
* @param $array
* Item on which operation is being performed, either a term or
* form values.
2007-06-29 18:06:51 +00:00
*/
2009-09-19 11:07:37 +00:00
function _trigger_taxonomy($hook, $array) {
$aids = trigger_get_assigned_actions($hook);
2007-06-29 18:06:51 +00:00
$context = array(
2009-09-19 11:07:37 +00:00
'group' => 'taxonomy',
'hook' => $hook
2007-06-29 18:06:51 +00:00
);
2009-06-05 15:41:05 +00:00
actions_do(array_keys($aids), (object) $array, $context);
2007-06-29 18:06:51 +00:00
}
/**
2009-12-04 16:49:48 +00:00
* Implements hook_taxonomy_term_insert().
2007-06-29 18:06:51 +00:00
*/
2009-09-19 11:07:37 +00:00
function trigger_taxonomy_term_insert($term) {
_trigger_taxonomy('taxonomy_term_insert', (array) $term);
}
2007-06-29 18:06:51 +00:00
2009-09-19 11:07:37 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_taxonomy_term_update().
2009-09-19 11:07:37 +00:00
*/
function trigger_taxonomy_term_update($term) {
_trigger_taxonomy('taxonomy_term_update', (array) $term);
}
/**
2009-12-04 16:49:48 +00:00
* Implements hook_taxonomy_term_delete().
2009-09-19 11:07:37 +00:00
*/
function trigger_taxonomy_term_delete($term) {
_trigger_taxonomy('taxonomy_term_delete', (array) $term);
2007-06-29 18:06:51 +00:00
}
2007-12-28 15:21:30 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_actions_delete().
2007-12-28 15:21:30 +00:00
*
2009-09-19 11:07:37 +00:00
* Removes all trigger entries for the given action, when an action is deleted.
2007-12-28 15:21:30 +00:00
*/
function trigger_actions_delete($aid) {
2009-05-29 19:15:08 +00:00
db_delete('trigger_assignments')
->condition('aid', $aid)
->execute();
2007-12-28 15:21:30 +00:00
}
2009-09-19 11:07:37 +00:00
/**
* Retrieves and caches information from hook_trigger_info() implementations.
*/
function _trigger_get_all_info() {
static $triggers = NULL;
if( $triggers ) {
return $triggers;
}
$triggers = module_invoke_all('trigger_info');
drupal_alter('trigger_info', $triggers);
return $triggers;
}