You are here

function autotag_nodeapi in Taxonomy Autotagger 6.2

Same name and namespace in other branches
  1. 6 autotag.module \autotag_nodeapi()

hook_nodeapi

File

./autotag.module, line 85

Code

function autotag_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  switch ($op) {
    case 'presave':

      // If the autotag checkbox is present and checked, we'll search the node
      // for tags, and add them to the node before saving.
      // Or, the node type allows autotagging (this may fix an issue with
      // biblio).
      $checkbox = variable_get('autotag_save_checkbox', array());
      if (isset($node->taxonomyautotagcheckbox) && $node->taxonomyautotagcheckbox || $checkbox[$node->type] && !count($_POST)) {
        module_load_include('ahah.inc', 'autotag');
        if (count($_POST)) {
          $data = $_POST;
        }
        else {
          $data = $node;
        }
        $tids = autotag_search_form_array($data, $node->type);

        // Remove the tids that we don't want adding - we can only do this if we have
        // an nid
        if ($node->nid) {
          $result = db_query('SELECT a.tid, vid FROM {autotag} a, {term_data} t WHERE nid = %d AND a.tid = t.tid', $node->nid);
          while ($row = db_fetch_array($result)) {
            $key = array_search($row, $tids);
            if ($key !== FALSE) {
              unset($tids[$key]);
            }
          }
        }
        foreach ($tids as $tid) {
          if (is_array($node->taxonomy[$tid['vid']])) {
            $node->taxonomy[$tid['vid']][$tid['tid']] = $tid['tid'];
          }
          else {
            $node->taxonomy[$tid['vid']] = array(
              $tid['tid'] => $tid['tid'],
            );
          }
        }
      }
      break;
    case 'update':
    case 'insert':

      // Here we can add the relevant tids to the autotag table.
      if (isset($_SESSION['autotag_tids'][$node->form_build_id])) {

        // Lets get the ones that were tagged to the node from the term_node table
        $result = db_query('SELECT n.tid, t.vid FROM {term_node} n, {term_data} t WHERE n.tid = t.tid AND nid = %d', $node->nid);
        while ($row = db_fetch_array($result)) {
          $key = array_search($row, $_SESSION['autotag_tids'][$node->form_build_id]);
          if ($key !== FALSE) {
            unset($_SESSION['autotag_tids'][$node->form_build_id][$key]);
          }
        }

        // Clear the current ignore tids
        db_query('DELETE FROM {autotag} WHERE nid = %d', $node->nid);
        if (count($_SESSION['autotag_tids'][$node->form_build_id])) {
          $tids_string = array();
          foreach ($_SESSION['autotag_tids'][$node->form_build_id] as $tid) {
            $tids_string[] = $node->nid . ', ' . $tid['tid'];
          }

          // Add the new ignore tids
          db_query('INSERT INTO {autotag} (nid, tid) VALUES (' . implode('),(', $tids_string) . ')');
        }
      }
      else {

        // We've probably saved without pressing the autotag button.  We need to
        // check that the autotag checkbox is checked, and if so, delete the
        // bits from the autotag table that may have been tagged..
        if (isset($node->taxonomyautotagcheckbox) && $node->taxonomyautotagcheckbox) {
          db_query('DELETE FROM {autotag} WHERE tid IN (SELECT tid FROM {term_node} WHERE {autotag}.nid = nid AND {autotag}.tid = tid) AND nid IN (SELECT nid FROM {term_node} WHERE {autotag}.nid = nid AND {autotag}.tid = tid)');
        }
      }
      break;
  }
}