You are here

function flag_flag in Flag 6.2

Same name and namespace in other branches
  1. 5 flag.module \flag_flag()
  2. 6 flag.module \flag_flag()
  3. 7.2 flag.module \flag_flag()

Implementation of hook_flag(). Trigger actions if any are available.

File

./flag.module, line 754
The Flag module.

Code

function flag_flag($action, $flag, $content_id, $account) {
  if (module_exists('trigger')) {
    $context['hook'] = 'flag';
    $context['account'] = $account;
    $context['flag'] = $flag;
    $context['op'] = $action;

    // We add to the $context all the objects we know about:
    $context = array_merge($flag
      ->get_relevant_action_objects($content_id), $context);

    // The primary object the actions work on.
    $object = $flag
      ->fetch_content($content_id);

    // Generic "all flags" actions.
    foreach (_trigger_get_hook_aids('flag', $action) as $aid => $action_info) {

      // The 'if ($aid)' is a safeguard against http://drupal.org/node/271460#comment-886564
      if ($aid) {
        actions_do($aid, $object, $context);
      }
    }

    // Actions specifically for this flag.
    foreach (_trigger_get_hook_aids('flag', $action . '_' . $flag->name) as $aid => $action_info) {
      if ($aid) {
        actions_do($aid, $object, $context);
      }
    }
  }
  if (module_exists('rules')) {
    $event_name = ($action == 'flag' ? 'flag_flagged_' : 'flag_unflagged_') . $flag->name;
    $arguments = array(
      'flag' => $flag,
      'flag_content_id' => $content_id,
      'flagging_user' => $account,
    );
    rules_invoke_event($event_name, $arguments);
  }
}