123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388 |
- <?php
- function actions_do($action_ids, $object = NULL, $context = NULL, $a1 = NULL, $a2 = NULL) {
-
- static $stack;
- $stack++;
- if ($stack > variable_get('actions_max_stack', 35)) {
- watchdog('actions', 'Stack overflow: too many calls to actions_do(). Aborting to prevent infinite recursion.', array(), WATCHDOG_ERROR);
- return;
- }
- $actions = array();
- $available_actions = actions_list();
- $actions_result = array();
- if (is_array($action_ids)) {
- $conditions = array();
- foreach ($action_ids as $action_id) {
- if (is_numeric($action_id)) {
- $conditions[] = $action_id;
- }
- elseif (isset($available_actions[$action_id])) {
- $actions[$action_id] = $available_actions[$action_id];
- }
- }
-
-
- if (!empty($conditions)) {
- $query = db_select('actions');
- $query->addField('actions', 'aid');
- $query->addField('actions', 'type');
- $query->addField('actions', 'callback');
- $query->addField('actions', 'parameters');
- $query->condition('aid', $conditions, 'IN');
- $result = $query->execute();
- foreach ($result as $action) {
- $actions[$action->aid] = $action->parameters ? unserialize($action->parameters) : array();
- $actions[$action->aid]['callback'] = $action->callback;
- $actions[$action->aid]['type'] = $action->type;
- }
- }
-
- foreach ($actions as $action_id => $params) {
-
- if (is_numeric($action_id)) {
- $function = $params['callback'];
- if (function_exists($function)) {
- $context = array_merge($context, $params);
- $actions_result[$action_id] = $function($object, $context, $a1, $a2);
- }
- else {
- $actions_result[$action_id] = FALSE;
- }
- }
-
- else {
- $actions_result[$action_id] = $action_id($object, $context, $a1, $a2);
- }
- }
- }
-
- else {
-
- if (is_numeric($action_ids)) {
- $action = db_query("SELECT callback, parameters FROM {actions} WHERE aid = :aid", array(':aid' => $action_ids))->fetchObject();
- $function = $action->callback;
- if (function_exists($function)) {
- $context = array_merge($context, unserialize($action->parameters));
- $actions_result[$action_ids] = $function($object, $context, $a1, $a2);
- }
- else {
- $actions_result[$action_ids] = FALSE;
- }
- }
-
- else {
- if (function_exists($action_ids)) {
- $actions_result[$action_ids] = $action_ids($object, $context, $a1, $a2);
- }
- else {
-
- $actions_result[$action_ids] = FALSE;
- }
- }
- }
- $stack--;
- return $actions_result;
- }
- function actions_list($reset = FALSE) {
- $actions = &drupal_static(__FUNCTION__);
- if (!isset($actions) || $reset) {
- $actions = module_invoke_all('action_info');
- drupal_alter('action_info', $actions);
- }
-
- return (array) $actions;
- }
- function actions_get_all_actions() {
- $actions = db_query("SELECT aid, type, callback, parameters, label FROM {actions}")->fetchAllAssoc('aid', PDO::FETCH_ASSOC);
- foreach ($actions as &$action) {
- $action['configurable'] = (bool) $action['parameters'];
- unset($action['parameters']);
- unset($action['aid']);
- }
- return $actions;
- }
- function actions_actions_map($actions) {
- $actions_map = array();
- foreach ($actions as $callback => $array) {
- $key = drupal_hash_base64($callback);
- $actions_map[$key]['callback'] = isset($array['callback']) ? $array['callback'] : $callback;
- $actions_map[$key]['label'] = $array['label'];
- $actions_map[$key]['type'] = $array['type'];
- $actions_map[$key]['configurable'] = $array['configurable'];
- }
- return $actions_map;
- }
- function actions_function_lookup($hash) {
-
- $actions_list = actions_list();
- foreach ($actions_list as $function => $array) {
- if (drupal_hash_base64($function) == $hash) {
- return $function;
- }
- }
- $aid = FALSE;
-
- $result = db_query("SELECT aid FROM {actions} WHERE parameters <> ''")->fetchAll(PDO::FETCH_ASSOC);
- foreach ($result as $row) {
- if (drupal_hash_base64($row['aid']) == $hash) {
- $aid = $row['aid'];
- break;
- }
- }
- return $aid;
- }
- function actions_synchronize($delete_orphans = FALSE) {
- $actions_in_code = actions_list(TRUE);
- $actions_in_db = db_query("SELECT aid, callback, label FROM {actions} WHERE parameters = ''")->fetchAllAssoc('callback', PDO::FETCH_ASSOC);
-
- foreach ($actions_in_code as $callback => $array) {
-
-
- if (!$array['configurable']) {
-
- if (isset($actions_in_db[$callback])) {
- unset($actions_in_db[$callback]);
- }
- else {
-
- db_insert('actions')
- ->fields(array(
- 'aid' => $callback,
- 'type' => $array['type'],
- 'callback' => $callback,
- 'parameters' => '',
- 'label' => $array['label'],
- ))
- ->execute();
- watchdog('actions', "Action '%action' added.", array('%action' => $array['label']));
- }
- }
- }
-
- if ($actions_in_db) {
- $orphaned = array_keys($actions_in_db);
- if ($delete_orphans) {
- $actions = db_query('SELECT aid, label FROM {actions} WHERE callback IN (:orphaned)', array(':orphaned' => $orphaned))->fetchAll();
- foreach ($actions as $action) {
- actions_delete($action->aid);
- watchdog('actions', "Removed orphaned action '%action' from database.", array('%action' => $action->label));
- }
- }
- else {
- $link = l(t('Remove orphaned actions'), 'admin/config/system/actions/orphan');
- $count = count($actions_in_db);
- $orphans = implode(', ', $orphaned);
- watchdog('actions', '@count orphaned actions (%orphans) exist in the actions table. !link', array('@count' => $count, '%orphans' => $orphans, '!link' => $link), WATCHDOG_INFO);
- }
- }
- }
- function actions_save($function, $type, $params, $label, $aid = NULL) {
-
-
- if (!$aid) {
- $aid = db_next_id();
- }
- db_merge('actions')
- ->key(array('aid' => $aid))
- ->fields(array(
- 'callback' => $function,
- 'type' => $type,
- 'parameters' => serialize($params),
- 'label' => $label,
- ))
- ->execute();
- watchdog('actions', 'Action %action saved.', array('%action' => $label));
- return $aid;
- }
- function actions_load($aid) {
- return db_query("SELECT aid, type, callback, parameters, label FROM {actions} WHERE aid = :aid", array(':aid' => $aid))->fetchObject();
- }
- function actions_delete($aid) {
- db_delete('actions')
- ->condition('aid', $aid)
- ->execute();
- module_invoke_all('actions_delete', $aid);
- }
|