You are here

function flag_actions_do in Flag 6.2

Same name and namespace in other branches
  1. 5 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
Implements hook_flag(). Trigger actions if any are available.

File

./flag_actions.module, line 175
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);
  $object_changed = FALSE;
  foreach ($actions as $aid => $action) {
    if ($action->event == 'flag') {
      $at_threshold = $count == $action->threshold;
      $repeat = $action->repeat_threshold ? $count > $action->threshold && ($count - $action->threshold) % $action->repeat_threshold == 0 : FALSE;
    }
    elseif ($action->event == 'unflag') {
      $at_threshold = $count == $action->threshold - 1;
      $repeat = $action->repeat_threshold ? $count < $action->threshold - 1 && ($count - $action->threshold - 1) % $action->repeat_threshold == 0 : FALSE;
    }
    if (($at_threshold || $repeat) && $action->event == $event && !$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_' . $action->type . '_property', $action->behavior)) {
        $object_changed = TRUE;
      }
    }
  }

  // Actions by default do not save elements unless the save action is
  // explicitly added. We run it automatically upon flagging.
  if ($object_changed) {
    $save_action = $action->type . '_save_action';
    if (function_exists($save_action)) {
      $save_action($relevant_objects[$action->type]);
    }
  }
}