You are here

function flag_actions_do in Flag 5

Same name and namespace in other branches
  1. 6.2 flag_actions.module \flag_actions_do()
  2. 6 flag_actions.module \flag_actions_do()
  3. 7.3 flag_actions.module \flag_actions_do()
  4. 7.2 flag_actions.module \flag_actions_do()

Perform flag actions.

1 call to flag_actions_do()
flag_actions_flag in ./flag_actions.module
Implementation of hook_flag(). Trigger actions if any are available.

File

./flag_actions.module, line 152
Actions support for the Flag module.

Code

function flag_actions_do($event, $flag, $content_id, $account) {
  $actions = flag_actions_get_actions($flag->name);
  if (!$actions) {
    return;
  }
  $flag_action = $flag
    ->get_flag_action($content_id);
  $flag_action->action = $event;
  $flag_action->count = $count = $flag
    ->get_count($content_id);
  $relevant_objects = $flag
    ->get_relevant_action_objects($content_id);
  $node_changed = FALSE;
  foreach ($actions as $aid => $action) {
    $threshold = $action->event == $event && ($action->event == 'flag' && $count == $action->threshold || $action->event == 'unflag' && $count == $action->threshold - 1);
    if ($threshold && !$action->missing) {
      $context = $action->parameters;
      $context['callback'] = $action->callback;

      // We're setting 'hook' to something, to prevent PHP warnings by actions
      // who read it. Maybe we should set it to nodeapi/comment/user, depending
      // on the flag, because these three are among the only hooks some actions
      // in system.module "know" to work with.
      $context['hook'] = 'flag';
      $context['type'] = $action->type;
      $context['account'] = $account;
      $context['flag'] = $flag;
      $context['flag-action'] = $flag_action;

      // We add to the $context all the objects we know about:
      $context = array_merge($relevant_objects, $context);
      $callback = $action->callback;
      if (isset($relevant_objects[$action->type])) {
        $callback($relevant_objects[$action->type], $context);
      }
      else {

        // What object shall we send as last resort? Let's send a node, or
        // the flag's object.
        if (isset($relevant_objects['node'])) {
          $callback($relevant_objects['node'], $context);
        }
        else {
          $callback($relevant_objects[$flag->content_type], $context);
        }
      }
      if (is_array($action->behavior) && in_array('changes_node_property', $action->behavior)) {
        $node_changed = TRUE;
      }
    }
  }
  if ($node_changed) {
    node_save_action($relevant_objects['node']);

    // Remember this modified node for the next round, if there's any.
    $flag
      ->remember_content($content_id, $relevant_objects['node']);
  }
}