You are here

function autotag_nodeapi in Taxonomy Autotagger 6

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

Implementation of hook_nodeapi

This saves the hidden terms in the database. This is done here, as the nid may not be known on submit (if the node is new).

File

./autotag.module, line 21
autotag.module

Code

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

      // Return if this node doesn't have any autotag vids associated with it
      $vids = _autotag_get_vids_for_type($node->type);
      if (!count($vids)) {
        return;
      }
      $all_tids = _autotag_search_node($node);
      $tids_to_ignore = explode("|", $node->hideterms);

      // Remove the ignore ones from the add ones
      $terms = $node->taxonomy;
      foreach ($all_tids as $tid) {
        if (!in_array($tid['tid'], $tids_to_ignore)) {
          if (!isset($terms[$tid['vid']]) || !in_array($tid['tid'], $terms[$tid['vid']])) {
            $terms[$tid['vid']][] = $tid['tid'];
          }
        }
      }

      // Remove the ignore ones, from terms already associated with the node
      foreach ($tids_to_ignore as $tid) {
        if (is_numeric($tid)) {

          // Remove this from $node->taxonomy
          foreach ($terms as $vocab_key => $vocab_value) {
            foreach ($vocab_value as $term_key => $term_value) {
              if ($term_value == $tid) {
                unset($terms[$vocab_key][$term_key]);
              }
            }
          }
        }
      }

      // We've also got additional tags
      $regexp = '%(?:^|,\\ *)("(?>[^"]*)(?>""[^"]* )*"|(?: [^",]*))%x';
      preg_match_all($regexp, $node->additionaltags, $matches);
      $words = $matches[1];
      if (count($words)) {
        $words_place_holders = array();
        foreach ($words as $does_not_matter) {
          $words_place_holders[] = "'%s'";
        }
        $result = db_query("SELECT tid, name FROM {term_data} WHERE vid IN (%s) AND LOWER(name) IN (" . implode(",", $words_place_holders) . ")", array_merge(array(
          implode(",", $vids),
        ), $words));
        while ($row = db_fetch_array($result)) {
          if (!isset($terms[$row['vid']]) || !in_array($row['tid'], $terms[$row['vid']])) {
            $terms[$row['vid']][] = $row['tid'];
          }
        }
      }
      taxonomy_node_save($node, $terms);
      _autotag_update_table($node->nid, $tids_to_ignore);
      break;
  }
}