function biblio_insert_keywords in Bibliography Module 7
Same name and namespace in other branches
- 6.2 includes/biblio.keywords.inc \biblio_insert_keywords()
- 6 biblio.keywords.inc \biblio_insert_keywords()
- 7.2 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
2 calls to biblio_insert_keywords()
- biblio_insert in ./
biblio.module - Implements hook_insert().
- biblio_update_keywords in includes/
biblio.keywords.inc - Update the keyword database from the supplied node.
File
- includes/
biblio.keywords.inc, line 134 - Contains all keyword related functions.
Code
function biblio_insert_keywords($node, $update = FALSE) {
$kw_vocab = variable_get('biblio_keyword_vocabulary', 0);
$taxo_terms = $typed_keywords = array();
$freetag_vocab = FALSE;
if (isset($node->biblio_keywords) && !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();
}
if (empty($node->biblio_keywords)) {
return;
}
$vocabularies = module_invoke('taxonomy', 'get_vocabularies');
$vid = variable_get('biblio_keyword_vocabulary', 0);
if (variable_get('biblio_keyword_freetagging', 0) && $vid) {
$freetag_vocab = $vocabularies[variable_get('biblio_keyword_vocabulary', 0)];
}
// Add any taxonomy terms to our keyword list.
if (isset($node->taxonomy) && is_array($node->taxonomy) && variable_get('biblio_copy_taxo_terms_to_keywords', 0)) {
foreach ($node->taxonomy as $vid => $term) {
// 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.
if ($vid == 'copy_to_biblio' && $term == 0) {
$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))) {
// Skip if we have a blank.
continue;
}
$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.
$vocabularies = module_invoke('taxonomy', 'get_vocabularies');
$vid = variable_get('biblio_keyword_vocabulary', 0);
$freetagging = variable_get('biblio_keyword_freetagging', 0);
if ($freetagging && $vid) {
$freetag_vocab = $vocabularies[$vid];
$term_refs = biblio_get_term_ref_fields();
$ft_field = 'field_' . $freetag_vocab->machine_name . '_ref';
if (!isset($term_refs[$ft_field])) {
biblio_create_term_ref($freetag_vocab);
}
}
if ($freetagging && $freetag_vocab) {
$tids = array();
$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'];
}
}
if (is_array($typed_keywords) && !empty($typed_keywords)) {
$query = new EntityFieldQuery();
$query
->entityCondition('entity_type', 'taxonomy_term')
->propertyCondition('vid', $vid)
->propertyCondition('name', $typed_keywords);
$result = $query
->execute();
if (isset($result['taxonomy_term'])) {
$existing_tids = array_keys($result['taxonomy_term']);
$existing_terms = taxonomy_term_load_multiple($existing_tids);
$existing_words = array();
foreach ($existing_terms as $term) {
$existing_words[$term->name] = $term;
}
}
}
foreach ($typed_keywords as $kw) {
if (isset($existing_words[$kw])) {
$term = $existing_words[$kw];
}
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);
}