#333658 by sun: Code clean-up for actions.inc.
parent
36bcedc486
commit
5a53f843d7
|
|
@ -49,7 +49,6 @@ function actions_do($action_ids, $object = NULL, $context = NULL, $a1 = NULL, $a
|
||||||
$available_actions = actions_list();
|
$available_actions = actions_list();
|
||||||
$result = array();
|
$result = array();
|
||||||
if (is_array($action_ids)) {
|
if (is_array($action_ids)) {
|
||||||
|
|
||||||
$conditions = array();
|
$conditions = array();
|
||||||
foreach ($action_ids as $action_id) {
|
foreach ($action_ids as $action_id) {
|
||||||
if (is_numeric($action_id)) {
|
if (is_numeric($action_id)) {
|
||||||
|
|
@ -60,8 +59,8 @@ function actions_do($action_ids, $object = NULL, $context = NULL, $a1 = NULL, $a
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// When we have action instances we must go to the database to
|
// When we have action instances we must go to the database to retrieve
|
||||||
// retrieve instance data.
|
// instance data.
|
||||||
if (!empty($conditions)) {
|
if (!empty($conditions)) {
|
||||||
$query = db_select('actions');
|
$query = db_select('actions');
|
||||||
$query->addField('actions', 'aid');
|
$query->addField('actions', 'aid');
|
||||||
|
|
@ -79,7 +78,8 @@ function actions_do($action_ids, $object = NULL, $context = NULL, $a1 = NULL, $a
|
||||||
|
|
||||||
// Fire actions, in no particular order.
|
// Fire actions, in no particular order.
|
||||||
foreach ($actions as $action_id => $params) {
|
foreach ($actions as $action_id => $params) {
|
||||||
if (is_numeric($action_id)) { // Configurable actions need parameters.
|
// Configurable actions need parameters.
|
||||||
|
if (is_numeric($action_id)) {
|
||||||
$function = $params['callback'];
|
$function = $params['callback'];
|
||||||
$context = array_merge($context, $params);
|
$context = array_merge($context, $params);
|
||||||
$result[$action_id] = $function($object, $context, $a1, $a2);
|
$result[$action_id] = $function($object, $context, $a1, $a2);
|
||||||
|
|
@ -90,7 +90,7 @@ function actions_do($action_ids, $object = NULL, $context = NULL, $a1 = NULL, $a
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Optimized execution of single action.
|
// Optimized execution of a single action.
|
||||||
else {
|
else {
|
||||||
// If it's a configurable action, retrieve stored parameters.
|
// If it's a configurable action, retrieve stored parameters.
|
||||||
if (is_numeric($action_ids)) {
|
if (is_numeric($action_ids)) {
|
||||||
|
|
@ -108,10 +108,10 @@ function actions_do($action_ids, $object = NULL, $context = NULL, $a1 = NULL, $a
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Discover all action functions by invoking hook_action_info().
|
* Discover all action functions by invoking hook_action_info().
|
||||||
*
|
*
|
||||||
|
* @code
|
||||||
* mymodule_action_info() {
|
* mymodule_action_info() {
|
||||||
* return array(
|
* return array(
|
||||||
* 'mymodule_functiondescription_action' => array(
|
* 'mymodule_functiondescription_action' => array(
|
||||||
|
|
@ -125,6 +125,7 @@ function actions_do($action_ids, $object = NULL, $context = NULL, $a1 = NULL, $a
|
||||||
* )
|
* )
|
||||||
* );
|
* );
|
||||||
* }
|
* }
|
||||||
|
* @endcode
|
||||||
*
|
*
|
||||||
* The description is used in presenting possible actions to the user for
|
* The description is used in presenting possible actions to the user for
|
||||||
* configuration. The type is used to present these actions in a logical
|
* configuration. The type is used to present these actions in a logical
|
||||||
|
|
@ -139,8 +140,7 @@ function actions_do($action_ids, $object = NULL, $context = NULL, $a1 = NULL, $a
|
||||||
* @return
|
* @return
|
||||||
* An associative array keyed on function name. The value of each key is
|
* An associative array keyed on function name. The value of each key is
|
||||||
* an array containing information about the action, such as type of
|
* an array containing information about the action, such as type of
|
||||||
* action and description of the action, e.g.,
|
* action and description of the action, e.g.:
|
||||||
*
|
|
||||||
* @code
|
* @code
|
||||||
* $actions['node_publish_action'] = array(
|
* $actions['node_publish_action'] = array(
|
||||||
* 'type' => 'node',
|
* 'type' => 'node',
|
||||||
|
|
@ -160,22 +160,21 @@ function actions_list($reset = FALSE) {
|
||||||
drupal_alter('action_info', $actions);
|
drupal_alter('action_info', $actions);
|
||||||
}
|
}
|
||||||
|
|
||||||
// See module_implements for explanations of this cast.
|
// See module_implements() for an explanation of this cast.
|
||||||
return (array)$actions;
|
return (array)$actions;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieve all action instances from the database.
|
* Retrieve all action instances from the database.
|
||||||
*
|
*
|
||||||
* Compare with actions_list() which gathers actions by
|
* Compare with actions_list() which gathers actions by invoking
|
||||||
* invoking hook_action_info(). The two are synchronized
|
* hook_action_info(). The two are synchronized by visiting
|
||||||
* by visiting /admin/build/actions (when actions.module is
|
* /admin/build/actions (when actions.module is enabled) which runs
|
||||||
* enabled) which runs actions_synchronize().
|
* actions_synchronize().
|
||||||
*
|
*
|
||||||
* @return
|
* @return
|
||||||
* Associative array keyed by action ID. Each value is
|
* Associative array keyed by action ID. Each value is an associative array
|
||||||
* an associative array with keys 'callback', 'description',
|
* with keys 'callback', 'description', 'type' and 'configurable'.
|
||||||
* 'type' and 'configurable'.
|
|
||||||
*/
|
*/
|
||||||
function actions_get_all_actions() {
|
function actions_get_all_actions() {
|
||||||
$actions = db_query("SELECT aid, type, callback, parameters, description FROM {actions}")->fetchAllAssoc('aid', PDO::FETCH_ASSOC);
|
$actions = db_query("SELECT aid, type, callback, parameters, description FROM {actions}")->fetchAllAssoc('aid', PDO::FETCH_ASSOC);
|
||||||
|
|
@ -190,19 +189,19 @@ function actions_get_all_actions() {
|
||||||
/**
|
/**
|
||||||
* Create an associative array keyed by md5 hashes of function names.
|
* Create an associative array keyed by md5 hashes of function names.
|
||||||
*
|
*
|
||||||
* Hashes are used to prevent actual function names from going out into
|
* Hashes are used to prevent actual function names from going out into HTML
|
||||||
* HTML forms and coming back.
|
* forms and coming back.
|
||||||
*
|
*
|
||||||
* @param $actions
|
* @param $actions
|
||||||
* An associative array with function names as keys and associative
|
* An associative array with function names as keys and associative arrays
|
||||||
* arrays with keys 'description', 'type', etc. as values. Generally
|
* with keys 'description', 'type', etc. as values. Generally the output of
|
||||||
* the output of actions_list() or actions_get_all_actions() is given
|
* actions_list() or actions_get_all_actions() is given as input to this
|
||||||
* as input to this function.
|
* function.
|
||||||
*
|
*
|
||||||
* @return
|
* @return
|
||||||
* An associative array keyed on md5 hash of function name. The value of
|
* An associative array keyed on md5 hash of function names. The value of
|
||||||
* each key is an associative array of function, description, and type
|
* each key is an associative array of function, description, and type for
|
||||||
* for the action.
|
* the action.
|
||||||
*/
|
*/
|
||||||
function actions_actions_map($actions) {
|
function actions_actions_map($actions) {
|
||||||
$actions_map = array();
|
$actions_map = array();
|
||||||
|
|
@ -222,10 +221,10 @@ function actions_actions_map($actions) {
|
||||||
* Faster than actions_actions_map() when you only need the function name.
|
* Faster than actions_actions_map() when you only need the function name.
|
||||||
*
|
*
|
||||||
* @param $hash
|
* @param $hash
|
||||||
* MD5 hash of a function name
|
* MD5 hash of a function name.
|
||||||
*
|
*
|
||||||
* @return
|
* @return
|
||||||
* Function name
|
* The corresponding function name or FALSE if none is found.
|
||||||
*/
|
*/
|
||||||
function actions_function_lookup($hash) {
|
function actions_function_lookup($hash) {
|
||||||
$actions_list = actions_list();
|
$actions_list = actions_list();
|
||||||
|
|
@ -242,10 +241,10 @@ function actions_function_lookup($hash) {
|
||||||
/**
|
/**
|
||||||
* Synchronize actions that are provided by modules.
|
* Synchronize actions that are provided by modules.
|
||||||
*
|
*
|
||||||
* They are synchronized with actions that are stored in the actions table.
|
* Actions provided by modules are synchronized with actions that are stored in
|
||||||
* This is necessary so that actions that do not require configuration can
|
* the actions table. This is necessary so that actions that do not require
|
||||||
* receive action IDs. This is not necessarily the best approach,
|
* configuration can receive action IDs. This is not necessarily the best
|
||||||
* but it is the most straightforward.
|
* approach, but it is the most straightforward.
|
||||||
*/
|
*/
|
||||||
function actions_synchronize($actions_in_code = array(), $delete_orphans = FALSE) {
|
function actions_synchronize($actions_in_code = array(), $delete_orphans = FALSE) {
|
||||||
if (!$actions_in_code) {
|
if (!$actions_in_code) {
|
||||||
|
|
@ -255,8 +254,8 @@ function actions_synchronize($actions_in_code = array(), $delete_orphans = FALSE
|
||||||
|
|
||||||
// Go through all the actions provided by modules.
|
// Go through all the actions provided by modules.
|
||||||
foreach ($actions_in_code as $callback => $array) {
|
foreach ($actions_in_code as $callback => $array) {
|
||||||
// Ignore configurable actions since their instances get put in
|
// Ignore configurable actions since their instances get put in when the
|
||||||
// when the user adds the action.
|
// user adds the action.
|
||||||
if (!$array['configurable']) {
|
if (!$array['configurable']) {
|
||||||
// If we already have an action ID for this action, no need to assign aid.
|
// If we already have an action ID for this action, no need to assign aid.
|
||||||
if (array_key_exists($callback, $actions_in_db)) {
|
if (array_key_exists($callback, $actions_in_db)) {
|
||||||
|
|
@ -308,11 +307,11 @@ function actions_synchronize($actions_in_code = array(), $delete_orphans = FALSE
|
||||||
* @param $function
|
* @param $function
|
||||||
* The name of the function to be called when this action is performed.
|
* The name of the function to be called when this action is performed.
|
||||||
* @param $params
|
* @param $params
|
||||||
* An associative array with parameter names as keys and parameter values
|
* An associative array with parameter names as keys and parameter values as
|
||||||
* as values.
|
* values.
|
||||||
* @param $desc
|
* @param $desc
|
||||||
* A user-supplied description of this particular action, e.g., 'Send
|
* A user-supplied description of this particular action, e.g., 'Send e-mail
|
||||||
* e-mail to Jim'.
|
* to Jim'.
|
||||||
* @param $aid
|
* @param $aid
|
||||||
* The ID of this action. If omitted, a new action is created.
|
* The ID of this action. If omitted, a new action is created.
|
||||||
*
|
*
|
||||||
|
|
@ -320,8 +319,8 @@ function actions_synchronize($actions_in_code = array(), $delete_orphans = FALSE
|
||||||
* The ID of the action.
|
* The ID of the action.
|
||||||
*/
|
*/
|
||||||
function actions_save($function, $type, $params, $desc, $aid = NULL) {
|
function actions_save($function, $type, $params, $desc, $aid = NULL) {
|
||||||
// aid is the callback for singleton actions so we need to keep a
|
// aid is the callback for singleton actions so we need to keep a separate
|
||||||
// separate table for numeric aids.
|
// table for numeric aids.
|
||||||
if (!$aid) {
|
if (!$aid) {
|
||||||
$aid = db_insert('actions_aid')->useDefaults(array('aid'))->execute();
|
$aid = db_insert('actions_aid')->useDefaults(array('aid'))->execute();
|
||||||
}
|
}
|
||||||
|
|
@ -344,7 +343,7 @@ function actions_save($function, $type, $params, $desc, $aid = NULL) {
|
||||||
* Retrieve a single action from the database.
|
* Retrieve a single action from the database.
|
||||||
*
|
*
|
||||||
* @param $aid
|
* @param $aid
|
||||||
* integer The ID of the action to retrieve.
|
* The ID of the action to retrieve.
|
||||||
*
|
*
|
||||||
* @return
|
* @return
|
||||||
* The appropriate action row from the database as an object.
|
* The appropriate action row from the database as an object.
|
||||||
|
|
@ -357,7 +356,7 @@ function actions_load($aid) {
|
||||||
* Delete a single action from the database.
|
* Delete a single action from the database.
|
||||||
*
|
*
|
||||||
* @param $aid
|
* @param $aid
|
||||||
* integer The ID of the action to delete.
|
* The ID of the action to delete.
|
||||||
*/
|
*/
|
||||||
function actions_delete($aid) {
|
function actions_delete($aid) {
|
||||||
db_delete('actions')
|
db_delete('actions')
|
||||||
|
|
@ -365,3 +364,4 @@ function actions_delete($aid) {
|
||||||
->execute();
|
->execute();
|
||||||
module_invoke_all('actions_delete', $aid);
|
module_invoke_all('actions_delete', $aid);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue