00001 <?php
00002
00003
00040 function actions_do($action_ids, $object = NULL, $context = NULL, $a1 = NULL, $a2 = NULL) {
00041 static $stack;
00042 $stack++;
00043 if ($stack > variable_get('actions_max_stack', 35)) {
00044 watchdog('actions', 'Stack overflow: too many calls to actions_do(). Aborting to prevent infinite recursion.', array(), WATCHDOG_ERROR);
00045 return;
00046 }
00047 $actions = array();
00048 $available_actions = actions_list();
00049 $result = array();
00050 if (is_array($action_ids)) {
00051 $where = array();
00052 $where_values = array();
00053 foreach ($action_ids as $action_id) {
00054 if (is_numeric($action_id)) {
00055 $where[] = 'OR aid = %d';
00056 $where_values[] = $action_id;
00057 }
00058 elseif (isset($available_actions[$action_id])) {
00059 $actions[$action_id] = $available_actions[$action_id];
00060 }
00061 }
00062
00063
00064
00065 if ($where) {
00066 $where_clause = implode(' ', $where);
00067
00068 $where_clause = '(' . strstr($where_clause, " ") . ')';
00069 $result_db = db_query('SELECT * FROM {actions} WHERE ' . $where_clause, $where_values);
00070 while ($action = db_fetch_object($result_db)) {
00071 $actions[$action->aid] = $action->parameters ? unserialize($action->parameters) : array();
00072 $actions[$action->aid]['callback'] = $action->callback;
00073 $actions[$action->aid]['type'] = $action->type;
00074 }
00075 }
00076
00077
00078 foreach ($actions as $action_id => $params) {
00079 if (is_numeric($action_id)) {
00080 $function = $params['callback'];
00081 $context = array_merge($context, $params);
00082 $result[$action_id] = $function($object, $context, $a1, $a2);
00083 }
00084
00085 else {
00086 $result[$action_id] = $action_id($object, $context, $a1, $a2);
00087 }
00088 }
00089 }
00090
00091 else {
00092
00093 if (is_numeric($action_ids)) {
00094 $action = db_fetch_object(db_query("SELECT * FROM {actions} WHERE aid = %d", $action_ids));
00095 $function = $action->callback;
00096 $context = array_merge($context, unserialize($action->parameters));
00097 $result[$action_ids] = $function($object, $context, $a1, $a2);
00098 }
00099
00100 else {
00101 $result[$action_ids] = $action_ids($object, $context, $a1, $a2);
00102 }
00103 }
00104 return $result;
00105 }
00106
00107
00152 function actions_list($reset = FALSE) {
00153 static $actions;
00154 if (!isset($actions) || $reset) {
00155 $actions = module_invoke_all('action_info');
00156 drupal_alter('action_info', $actions);
00157 }
00158
00159
00160 return (array)$actions;
00161 }
00162
00176 function actions_get_all_actions() {
00177 $actions = array();
00178 $result = db_query("SELECT * FROM {actions}");
00179 while ($action = db_fetch_object($result)) {
00180 $actions[$action->aid] = array(
00181 'callback' => $action->callback,
00182 'description' => $action->description,
00183 'type' => $action->type,
00184 'configurable' => (bool) $action->parameters,
00185 );
00186 }
00187 return $actions;
00188 }
00189
00207 function actions_actions_map($actions) {
00208 $actions_map = array();
00209 foreach ($actions as $callback => $array) {
00210 $key = md5($callback);
00211 $actions_map[$key]['callback'] = isset($array['callback']) ? $array['callback'] : $callback;
00212 $actions_map[$key]['description'] = $array['description'];
00213 $actions_map[$key]['type'] = $array['type'];
00214 $actions_map[$key]['configurable'] = $array['configurable'];
00215 }
00216 return $actions_map;
00217 }
00218
00230 function actions_function_lookup($hash) {
00231 $actions_list = actions_list();
00232 foreach ($actions_list as $function => $array) {
00233 if (md5($function) == $hash) {
00234 return $function;
00235 }
00236 }
00237
00238
00239 $aid = db_result(db_query("SELECT aid FROM {actions} WHERE MD5(aid) = '%s' AND parameters != ''", $hash));
00240 return $aid;
00241 }
00242
00251 function actions_synchronize($actions_in_code = array(), $delete_orphans = FALSE) {
00252 if (!$actions_in_code) {
00253 $actions_in_code = actions_list();
00254 }
00255 $actions_in_db = array();
00256 $result = db_query("SELECT * FROM {actions} WHERE parameters = ''");
00257 while ($action = db_fetch_object($result)) {
00258 $actions_in_db[$action->callback] = array('aid' => $action->aid, 'description' => $action->description);
00259 }
00260
00261
00262 foreach ($actions_in_code as $callback => $array) {
00263
00264
00265 if (!$array['configurable']) {
00266
00267 if (array_key_exists($callback, $actions_in_db)) {
00268 unset($actions_in_db[$callback]);
00269 }
00270 else {
00271
00272 db_query("INSERT INTO {actions} (aid, type, callback, parameters, description) VALUES ('%s', '%s', '%s', '%s', '%s')", $callback, $array['type'], $callback, '', $array['description']);
00273 watchdog('actions', "Action '%action' added.", array('%action' => filter_xss_admin($array['description'])));
00274 }
00275 }
00276 }
00277
00278
00279 if ($actions_in_db) {
00280 $orphaned = array();
00281 $placeholder = array();
00282
00283 foreach ($actions_in_db as $callback => $array) {
00284 $orphaned[] = $callback;
00285 $placeholder[] = "'%s'";
00286 }
00287
00288 $orphans = implode(', ', $orphaned);
00289
00290 if ($delete_orphans) {
00291 $placeholders = implode(', ', $placeholder);
00292 $results = db_query("SELECT a.aid, a.description FROM {actions} a WHERE callback IN ($placeholders)", $orphaned);
00293 while ($action = db_fetch_object($results)) {
00294 actions_delete($action->aid);
00295 watchdog('actions', "Removed orphaned action '%action' from database.", array('%action' => filter_xss_admin($action->description)));
00296 }
00297 }
00298 else {
00299 $link = l(t('Remove orphaned actions'), 'admin/build/actions/orphan');
00300 $count = count($actions_in_db);
00301 watchdog('actions', format_plural($count, 'One orphaned action (%orphans) exists in the actions table. !link', '@count orphaned actions (%orphans) exist in the actions table. !link', array('@count' => $count, '%orphans' => $orphans, '!link' => $link), 'warning'));
00302 }
00303 }
00304 }
00305
00323 function actions_save($function, $type, $params, $desc, $aid = NULL) {
00324 $serialized = serialize($params);
00325 if ($aid) {
00326 db_query("UPDATE {actions} SET callback = '%s', type = '%s', parameters = '%s', description = '%s' WHERE aid = %d", $function, $type, $serialized, $desc, $aid);
00327 watchdog('actions', 'Action %action saved.', array('%action' => $desc));
00328 }
00329 else {
00330
00331
00332 db_query('INSERT INTO {actions_aid} VALUES (default)');
00333 $aid = db_last_insert_id('actions_aid', 'aid');
00334 db_query("INSERT INTO {actions} (aid, callback, type, parameters, description) VALUES (%d, '%s', '%s', '%s', '%s')", $aid, $function, $type, $serialized, $desc);
00335 watchdog('actions', 'Action %action created.', array('%action' => $desc));
00336 }
00337
00338 return $aid;
00339 }
00340
00350 function actions_load($aid) {
00351 return db_fetch_object(db_query("SELECT * FROM {actions} WHERE aid = %d", $aid));
00352 }
00353
00360 function actions_delete($aid) {
00361 db_query("DELETE FROM {actions} WHERE aid = %d", $aid);
00362 module_invoke_all('actions_delete', $aid);
00363 }