function biblio_insert_keywords in Bibliography Module 7.2
Same name and namespace in other branches
- 6.2 includes/biblio.keywords.inc \biblio_insert_keywords()
- 6 biblio.keywords.inc \biblio_insert_keywords()
- 7 includes/biblio.keywords.inc \biblio_insert_keywords()
Insert keywords into the database
Parameters
$node: A node with keywords attached
$update: Set to TRUE if you are updating an existing node
Return value
An array of keyword ID's from this node
1 call to biblio_insert_keywords()
- biblio_update_keywords in includes/
biblio.keywords.inc - Update the keyword database from the supplied node
File
- includes/
biblio.keywords.inc, line 118
Code
function biblio_insert_keywords($node, $update = FALSE) {
if (empty($node->biblio_keywords)) {
return;
}
$kw_vocab = variable_get('biblio_keyword_vocabulary', 0);
$freetagging = variable_get('biblio_keyword_freetagging', 0);
$taxo_terms = $typed_keywords = array();
$freetag_vocab = FALSE;
if (!is_array($node->biblio_keywords)) {
$typed_keywords = biblio_explode_keywords($node->biblio_keywords);
}
else {
$typed_keywords = $node->biblio_keywords;
}
if ($update) {
$and = db_and()
->condition('nid', $node->nid)
->condition('vid', $node->vid);
db_delete('biblio_keyword')
->condition($and)
->execute();
}
$vocabularies = module_invoke('taxonomy', 'get_vocabularies', 'biblio');
$vid = variable_get('biblio_keyword_vocabulary', 0);
if (variable_get('biblio_keyword_freetagging', 0) && $vid) {
$freetag_vocab = $vocabularies[variable_get('biblio_keyword_vocabulary', 0)];
}
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) {
if ($vid == 'copy_to_biblio' && $term == 0) {
// 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
$taxo_terms = array();
break;
}
if (is_array($term) && !empty($term)) {
foreach ($term as $tid) {
if ($tid) {
$term_obj = taxonomy_term_load($tid);
$taxo_terms[$term_obj->tid] = $term_obj->name;
}
}
}
elseif ($term) {
$term_obj = taxonomy_term_load($term);
$taxo_terms[$term_obj->tid] = $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 ($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])) {
db_merge('biblio_keyword')
->key(array(
'kid' => $kid,
'vid' => $node->vid,
))
->fields(array(
'kid' => $kid,
'nid' => $node->nid,
'vid' => $node->vid,
))
->execute();
$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 && $freetag_vocab) {
$tids = array();
$ft_field = 'field_' . $freetag_vocab->machine_name;
$lang = isset($freetag_vocab->language) ? $freetag_vocab->language : 'und';
if (isset($node->{$ft_field}[$lang])) {
foreach ($node->{$ft_field}[$lang] as $tag) {
$tids[] = $tag['tid'];
}
}
foreach ($typed_keywords as $kw) {
if ($possibilities = taxonomy_term_load_multiple(array(), array(
'name' => trim($kw),
'vid' => $freetag_vocab->vid,
))) {
$term = array_pop($possibilities);
}
else {
$term = new stdClass();
$term->vid = $freetag_vocab->vid;
$term->name = $kw;
$term->vocabulary_machine_name = $freetag_vocab->machine_name;
taxonomy_term_save($term);
}
if (!in_array($term->tid, $tids)) {
$node->{$ft_field}[$lang][] = (array) $term;
}
}
}
return array_keys($inserted);
}