| 1 |
<?php
|
| 2 |
// $Id: biblio.keywords.inc,v 1.15 2009/06/25 18:23:09 rjerome Exp $
|
| 3 |
/**
|
| 4 |
* biblio.module for Drupal
|
| 5 |
*
|
| 6 |
* Copyright (C) 2006-2009 Ron Jerome
|
| 7 |
*
|
| 8 |
* This program is free software; you can redistribute it and/or modify
|
| 9 |
* it under the terms of the GNU General Public License as published by
|
| 10 |
* the Free Software Foundation; either version 2 of the License, or
|
| 11 |
* (at your option) any later version.
|
| 12 |
*
|
| 13 |
* This program is distributed in the hope that it will be useful,
|
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 16 |
* GNU General Public License for more details.
|
| 17 |
*
|
| 18 |
* You should have received a copy of the GNU General Public License along
|
| 19 |
* with this program; if not, write to the Free Software Foundation, Inc.,
|
| 20 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
| 21 |
*
|
| 22 |
*/
|
| 23 |
|
| 24 |
/**
|
| 25 |
* @param $name
|
| 26 |
* @return array of keywords
|
| 27 |
*/
|
| 28 |
function biblio_get_keyword_by_name($name) {
|
| 29 |
static $keywords = array();
|
| 30 |
|
| 31 |
if (!$kid = array_search($name, $keywords)) {
|
| 32 |
$term = db_fetch_object(db_query("SELECT * FROM {biblio_keyword_data} k WHERE LOWER(k.word) = LOWER('%s')", trim($name)));
|
| 33 |
if ($term) {
|
| 34 |
$keywords[$term->kid] = $term;
|
| 35 |
return $keywords[$term->kid];
|
| 36 |
}
|
| 37 |
else {
|
| 38 |
return FALSE;
|
| 39 |
}
|
| 40 |
}
|
| 41 |
|
| 42 |
return $keywords[$kid];
|
| 43 |
}
|
| 44 |
|
| 45 |
/**
|
| 46 |
* @param $kid
|
| 47 |
* @return unknown_type
|
| 48 |
*/
|
| 49 |
function biblio_get_keyword_by_id($kid) {
|
| 50 |
static $keywords = array();
|
| 51 |
|
| 52 |
if (!isset($keywords[$kid])) {
|
| 53 |
$keywords[$kid] = db_fetch_object(db_query('SELECT * FROM {biblio_keyword_data} WHERE kid = %d', $kid));
|
| 54 |
}
|
| 55 |
|
| 56 |
return $keywords[$kid];
|
| 57 |
|
| 58 |
}
|
| 59 |
|
| 60 |
/**
|
| 61 |
* @param $node
|
| 62 |
* @return unknown_type
|
| 63 |
*/
|
| 64 |
function biblio_load_keywords($vid) {
|
| 65 |
|
| 66 |
$result = db_query('SELECT bkd.* FROM {biblio_keyword} bk INNER JOIN {biblio_keyword_data} bkd ON bk.kid = bkd.kid WHERE bk.vid = %d ORDER BY bkd.word ASC', $vid);
|
| 67 |
$keywords[$vid] = '';
|
| 68 |
while ($keyword = db_fetch_object($result)) {
|
| 69 |
$keywords[$vid][$keyword->kid] = $keyword->word;
|
| 70 |
}
|
| 71 |
return $keywords[$vid];
|
| 72 |
|
| 73 |
}
|
| 74 |
|
| 75 |
/**
|
| 76 |
* @param $node
|
| 77 |
* @return
|
| 78 |
*/
|
| 79 |
function biblio_save_keywords(&$node, $update = FALSE) {
|
| 80 |
$kw_vocab = variable_get('biblio_keyword_vocabulary', 0);
|
| 81 |
$freetagging = variable_get('biblio_keyword_freetagging', 0);
|
| 82 |
$taxo_terms = $typed_keywords = array();
|
| 83 |
if (!is_array($node->biblio_keywords)) {
|
| 84 |
$typed_keywords = biblio_explode_keywords($node->biblio_keywords);
|
| 85 |
}
|
| 86 |
else {
|
| 87 |
$typed_keywords = $node->biblio_keywords;
|
| 88 |
}
|
| 89 |
|
| 90 |
if (!$update) db_query('DELETE FROM {biblio_keyword} WHERE nid = %d AND vid = %d', array($node->nid, $node->vid));
|
| 91 |
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
|
| 92 |
foreach ($node->taxonomy as $vid => $term) {
|
| 93 |
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
|
| 94 |
$taxo_terms = array();
|
| 95 |
break;
|
| 96 |
}
|
| 97 |
if (is_array($term) && !empty($term)) {
|
| 98 |
foreach ($term as $tid) {
|
| 99 |
if ($tid) {
|
| 100 |
$term_obj = taxonomy_get_term($tid);
|
| 101 |
$taxo_terms[$term_obj->tid] = $term_obj->name;
|
| 102 |
}
|
| 103 |
}
|
| 104 |
}
|
| 105 |
else if ($term) {
|
| 106 |
$term_obj = taxonomy_get_term($term);
|
| 107 |
$taxo_terms[$term_obj->tid] = $term_obj->name;
|
| 108 |
}
|
| 109 |
}
|
| 110 |
}
|
| 111 |
|
| 112 |
$keywords = array_merge($typed_keywords, $taxo_terms);
|
| 113 |
|
| 114 |
foreach ($keywords as $keyword) {
|
| 115 |
$word = (is_object($keyword)) ? trim($keyword->word) : trim($keyword);
|
| 116 |
if (!strlen(trim($word))) continue; //skip if we have a blank
|
| 117 |
$kid = FALSE;
|
| 118 |
// See if the term exists
|
| 119 |
if ( ($term = biblio_get_keyword_by_name($word)) ) {
|
| 120 |
$kid = $term->kid;
|
| 121 |
}
|
| 122 |
if (!$kid) {
|
| 123 |
$term = array('word' => trim($word));
|
| 124 |
$status = biblio_save_keyword($term);
|
| 125 |
$kid = $term['kid'];
|
| 126 |
}
|
| 127 |
// Defend against duplicate, differently cased tags
|
| 128 |
if (!isset($inserted[$kid])) {
|
| 129 |
if ($update) db_query('DELETE FROM {biblio_keyword} WHERE nid = %d AND vid = %d AND kid = %d', array($node->nid, $node->vid, $kid));
|
| 130 |
db_query('INSERT INTO {biblio_keyword} (kid, nid, vid) VALUES (%d, %d, %d)', $kid, $node->nid, $node->vid);
|
| 131 |
$inserted[$kid] = TRUE;
|
| 132 |
}
|
| 133 |
}
|
| 134 |
|
| 135 |
// now if we are saving keywords into a taxonomy freetagging vocabulary, then create the tags string and add it to the node object.
|
| 136 |
|
| 137 |
if ($freetagging && $kw_vocab ) {
|
| 138 |
$node->taxonomy['tags'][$kw_vocab] = biblio_implode_keywords($typed_keywords, ',');
|
| 139 |
}
|
| 140 |
|
| 141 |
return count($inserted);
|
| 142 |
}
|
| 143 |
/**
|
| 144 |
* @param $word
|
| 145 |
* @return
|
| 146 |
*/
|
| 147 |
function biblio_save_keyword(&$form_values) {
|
| 148 |
if (!empty($form_values['kid']) && $form_values['word']) {
|
| 149 |
drupal_write_record('biblio_keyword_data', $form_values, 'kid');
|
| 150 |
$status = SAVED_UPDATED;
|
| 151 |
} else {
|
| 152 |
drupal_write_record('biblio_keyword_data', $form_values);
|
| 153 |
$status = SAVED_NEW;
|
| 154 |
}
|
| 155 |
|
| 156 |
return $status;
|
| 157 |
}
|
| 158 |
|
| 159 |
/**
|
| 160 |
* @param $node
|
| 161 |
* @return none
|
| 162 |
*/
|
| 163 |
function biblio_delete_node_keywords($node) {
|
| 164 |
db_query('DELETE FROM {biblio_keyword} WHERE nid = %d', $node->nid);
|
| 165 |
}
|
| 166 |
|
| 167 |
/**
|
| 168 |
* @param $node
|
| 169 |
* @return none
|
| 170 |
*/
|
| 171 |
function biblio_delete_revision_keywords($node) {
|
| 172 |
db_query('DELETE FROM {biblio_keyword} WHERE vid = %d', $node->vid);
|
| 173 |
}
|
| 174 |
|
| 175 |
function biblio_delete_keyword($keyword_id) {
|
| 176 |
db_query('DELETE FROM {biblio_keyword} WHERE kid = %d', $keyword_id);
|
| 177 |
db_query('DELETE FROM {biblio_keyword_data} WHERE kid = %d', $keyword_id);
|
| 178 |
}
|
| 179 |
|
| 180 |
function biblio_explode_keywords($string) {
|
| 181 |
$sep = variable_get('biblio_keyword_sep', ',');
|
| 182 |
$regexp = '%(?:^|'.$sep.'\ *)("(?>[^"]*)(?>""[^"]* )*"|(?: [^"'.$sep.']*))%x';
|
| 183 |
preg_match_all($regexp, $string, $matches);
|
| 184 |
$keyword_array = array_unique($matches[1]);
|
| 185 |
|
| 186 |
$keywords = array();
|
| 187 |
foreach ($keyword_array as $keyword) {
|
| 188 |
// If a user has escaped a term (to demonstrate that it is a group,
|
| 189 |
// or includes a comma or quote character), we remove the escape
|
| 190 |
// formatting so to save the term into the database as the user intends.
|
| 191 |
$keyword = trim(str_replace('""', '"', preg_replace('/^"(.*)"$/', '\1', $keyword)));
|
| 192 |
if ($keyword != "") {
|
| 193 |
$keywords[] = $keyword;
|
| 194 |
}
|
| 195 |
}
|
| 196 |
return $keywords;
|
| 197 |
}
|
| 198 |
function biblio_implode_keywords($keywords, $sep = '') {
|
| 199 |
|
| 200 |
if (empty($sep)) $sep = variable_get('biblio_keyword_sep', ',');
|
| 201 |
$string = '';
|
| 202 |
foreach ($keywords as $kid => $keyword) {
|
| 203 |
$string .= strlen($string)?"$sep ":'';
|
| 204 |
if (strpos($keyword, $sep) !== FALSE) {
|
| 205 |
$string .= '"'.$keyword.'"';
|
| 206 |
}
|
| 207 |
else {
|
| 208 |
$string .= $keyword;
|
| 209 |
}
|
| 210 |
}
|
| 211 |
return $string;
|
| 212 |
}
|