You are here

function biblio_insert_keywords in Bibliography Module 6

Same name and namespace in other branches
  1. 6.2 includes/biblio.keywords.inc \biblio_insert_keywords()
  2. 7 includes/biblio.keywords.inc \biblio_insert_keywords()
  3. 7.2 includes/biblio.keywords.inc \biblio_insert_keywords()

Parameters

$node:

3 calls to biblio_insert_keywords()
biblio_insert in ./biblio.module
Implementation of hook_insert().
biblio_update_6015 in ./biblio.install
biblio_update_keywords in ./biblio.keywords.inc

File

./biblio.keywords.inc, line 122

Code

function biblio_insert_keywords($node, $update = FALSE) {
  $kw_vocab = variable_get('biblio_keyword_vocabulary', 0);
  $freetagging = variable_get('biblio_keyword_freetagging', 0);
  $taxo_terms = $typed_keywords = array();
  if (!is_array($node->biblio_keywords)) {
    $typed_keywords = biblio_explode_keywords($node->biblio_keywords);
  }
  else {
    $typed_keywords = $node->biblio_keywords;
  }
  if ($update) {
    db_query('DELETE FROM {biblio_keyword} WHERE nid = %d AND vid = %d', array(
      $node->nid,
      $node->vid,
    ));
  }
  if (isset($node->taxonomy) && is_array($node->taxonomy) && variable_get('biblio_copy_taxo_terms_to_keywords', 0)) {

    //add any taxonomy terms to our keyword list
    foreach ($node->taxonomy as $vid => $term) {
      switch ($vid) {
        case 'tags':
          if (is_array($term)) {
            foreach ($term as $vid => $terms) {
              $taxo_terms = array_merge($taxo_terms, biblio_explode_keywords($terms, ','));
            }
          }
          break;
        case 'copy_to_biblio':
          if ($term == 0) {
            $taxo_terms = array();

            // don't copy if user overrides the default to copy, just set the $taxo_terms to an empty array and break out of the for loop
          }
          unset($node->taxonomy['copy_to_biblio']);
          break;
        default:
          if (is_array($term) && !empty($term)) {
            foreach ($term as $tid) {
              if ($tid) {
                $term_obj = taxonomy_get_term($tid);
                $taxo_terms[] = $term_obj->name;
              }
            }
          }
          else {
            if ($term) {
              $term_obj = taxonomy_get_term($term);
              $taxo_terms[] = $term_obj->name;
            }
          }
      }
    }
  }
  $keywords = array_unique(array_merge($typed_keywords, $taxo_terms));
  foreach ($keywords as $keyword) {
    $word = is_object($keyword) ? trim($keyword->word) : trim($keyword);
    if (!strlen(trim($word))) {
      continue;
    }

    //skip if we have a blank
    $kid = FALSE;

    // See if the term exists
    if ($kw = biblio_get_keyword_by_name($word)) {
      $kid = $kw->kid;
    }
    if (!$kid) {
      $kw = array(
        'word' => trim($word),
      );
      $status = biblio_save_keyword($kw);
      $kid = $kw['kid'];
    }

    // Defend against duplicate, differently cased tags
    if (!isset($inserted[$kid])) {
      if ($update) {
        db_query('DELETE FROM {biblio_keyword} WHERE nid = %d AND vid = %d AND kid = %d', array(
          $node->nid,
          $node->vid,
          $kid,
        ));
      }
      db_query('INSERT INTO {biblio_keyword} (kid, nid, vid) VALUES (%d, %d, %d)', $kid, $node->nid, $node->vid);
      $inserted[$kid] = TRUE;
    }
  }

  // now if we are saving keywords into a taxonomy freetagging vocabulary, then create the tags string and add it to the node object.
  if ($freetagging && $kw_vocab) {
    $node->taxonomy['tags'][$kw_vocab] = biblio_implode_keywords($typed_keywords, ',');
  }
  return;
}