You are here

function flag_nodeapi in Flag 5

Same name and namespace in other branches
  1. 6.2 flag.module \flag_nodeapi()
  2. 6 flag.module \flag_nodeapi()

Implementation of hook_nodeapi().

File

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

Code

function flag_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
  global $user;
  switch ($op) {
    case 'update':
    case 'insert':

      // Response to the flag checkboxes added to the form in flag_form_alter().
      $remembered = FALSE;
      if (isset($node->flag)) {
        foreach ($node->flag as $name => $state) {
          $flag = flag_get_flag($name);

          // Flagging may trigger actions. We want actions to get the current
          // node, not a stale database-loaded one:
          if (!$remembered) {
            $flag
              ->remember_content($node->nid, $node);

            // Actions may modify a node, and we don't want to overwrite this
            // modification:
            $remembered = TRUE;
          }
          flag($state ? 'flag' : 'unflag', $name, $node->nid);
        }
      }
      break;
    case 'delete':
      foreach (flag_get_flags('node') as $flag) {

        // If the flag is being tracked by translation set and the node is part
        // of a translation set, don't delete the flagging record.
        // Instead, data will be updated in the 'translation_change' op, below.
        if (!$flag->i18n || empty($node->tnid)) {
          db_query("DELETE FROM {flag_content} WHERE fid = %d AND content_id = %d", $flag->fid, $node->nid);
          db_query("DELETE FROM {flag_counts} WHERE fid = %d AND content_id = %d", $flag->fid, $node->nid);
        }
      }
      break;
    case 'translation_change':
      if (isset($node->translation_change)) {

        // If there is only one node remaining, track by nid rather than tnid.
        // Otherwise, use the new tnid.
        $content_id = $node->translation_change['new_tnid'] == 0 ? $node->translation_change['remaining_nid'] : $node->translation_change['new_tnid'];
        foreach (flag_get_flags('node') as $flag) {
          if ($flag->i18n) {
            db_query("UPDATE {flag_content} SET content_id = %d WHERE fid = %d AND content_id = %d", $content_id, $flag->fid, $node->translation_change['old_tnid']);
            db_query("UPDATE {flag_counts} SET content_id = %d WHERE fid = %d AND content_id = %d", $content_id, $flag->fid, $node->translation_change['old_tnid']);
          }
        }
      }
      break;
  }
}