kid] = $term; return $keywords[$term->kid]; } else { return FALSE; } } return $keywords[$kid]; } /** * @param $kid * @return unknown_type */ function biblio_get_keyword_by_id($kid) { static $keywords = array(); if (!isset($keywords[$kid])) { $keywords[$kid] = db_fetch_object(db_query('SELECT * FROM {biblio_keyword_data} WHERE kid = %d', $kid)); } return $keywords[$kid]; } /** * @param $node * @return unknown_type */ function biblio_load_keywords($vid) { $result = db_query('SELECT bkd.* FROM {biblio_keyword} bk INNER JOIN {biblio_keyword_data} bkd ON bk.kid = bkd.kid WHERE bk.vid = %d', $vid); $keywords[$vid] = ''; while ($keyword = db_fetch_object($result)) { $keywords[$vid][$keyword->kid] = $keyword->word; } return $keywords[$vid]; } /** * @param $node * @return */ function biblio_save_keywords(&$node, $update = FALSE) { $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)) { //add any taxonomy terms to our keyword list foreach ($node->taxonomy as $term) { if (is_array($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_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 ( ($term = biblio_get_keyword_by_name($word)) ) { $kid = $term->kid; } if (!$kid) { $term = array('word' => trim($word)); $status = biblio_save_keyword($term); $kid = $term['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. $kw_vocab = variable_get('biblio_keyword_vocabulary', 0); $freetagging = variable_get('biblio_keyword_freetagging', 0); if ($freetagging && $kw_vocab ) { $node->taxonomy['tags'][$kw_vocab] = biblio_implode_keywords($typed_keywords, ','); } return count($inserted); } /** * @param $word * @return */ function biblio_save_keyword(&$form_values) { if (!empty($form_values['kid']) && $form_values['word']) { drupal_write_record('biblio_keyword_data', $form_values, 'kid'); $status = SAVED_UPDATED; } else { drupal_write_record('biblio_keyword_data', $form_values); $status = SAVED_NEW; } return $status; } /** * @param $node * @return none */ function biblio_delete_node_keywords($node) { db_query('DELETE FROM {biblio_keyword} WHERE nid = %d', $node->nid); } /** * @param $node * @return none */ function biblio_delete_revision_keywords($node) { db_query('DELETE FROM {biblio_keyword} WHERE vid = %d', $node->vid); } function biblio_delete_keyword($keyword_id) { db_query('DELETE FROM {biblio_keyword} WHERE kid = %d', $keyword_id); db_query('DELETE FROM {biblio_keyword_data} WHERE kid = %d', $keyword_id); } function biblio_explode_keywords($string) { $sep = variable_get('biblio_keyword_sep', ','); $regexp = '%(?:^|'.$sep.'\ *)("(?>[^"]*)(?>""[^"]* )*"|(?: [^"'.$sep.']*))%x'; preg_match_all($regexp, $string, $matches); $keyword_array = array_unique($matches[1]); $keywords = array(); foreach ($keyword_array as $keyword) { // If a user has escaped a term (to demonstrate that it is a group, // or includes a comma or quote character), we remove the escape // formatting so to save the term into the database as the user intends. $keyword = trim(str_replace('""', '"', preg_replace('/^"(.*)"$/', '\1', $keyword))); if ($keyword != "") { $keywords[] = $keyword; } } return $keywords; } function biblio_implode_keywords($keywords, $sep = '') { if (empty($sep)) $sep = variable_get('biblio_keyword_sep', ','); $string = ''; foreach ($keywords as $kid => $keyword) { $string .= strlen($string)?"$sep ":''; if (strpos($keyword, $sep) !== FALSE) { $string .= '"'.$keyword.'"'; } else { $string .= $keyword; } } return $string; }