| 1 |
<?php |
<?php |
| 2 |
// $Id: biblio.keywords.inc,v 1.16.2.3 2009/11/09 01:55:56 rjerome Exp $ |
// $Id: biblio.keywords.inc,v 1.16.2.4 2009/11/09 21:22:48 rjerome Exp $ |
| 3 |
/** |
/** |
| 4 |
* biblio.module for Drupal |
* biblio.module for Drupal |
| 5 |
* |
* |
| 61 |
* @return unknown_type |
* @return unknown_type |
| 62 |
*/ |
*/ |
| 63 |
function biblio_load_keywords($vid) { |
function biblio_load_keywords($vid) { |
| 64 |
|
$keywords = array(); |
| 65 |
$result = db_query('SELECT bkd.* FROM {biblio_keyword} bk INNER JOIN {biblio_keyword_data} bkd ON bk.kid = bkd.kid WHERE bk.vid = :vid ORDER BY bkd.word ASC', array(':vid' => $vid)); |
if (!isset($keywords[$vid])) { |
| 66 |
$keywords[$vid] = ''; |
$query = db_select('biblio_keyword', 'bk'); |
| 67 |
foreach ($result as $keyword) { |
$query->join('biblio_keyword_data', 'bkd', 'bk.kid = bkd.kid'); |
| 68 |
$keywords[$vid][$keyword->kid] = $keyword->word; |
$query->fields('bkd'); |
| 69 |
} |
$query->condition('bk.vid', $vid)->orderBy('bkd.word', 'ASC'); |
| 70 |
return $keywords[$vid]; |
|
| 71 |
|
$result = $query->execute(); |
| 72 |
|
|
| 73 |
|
$keywords[$vid] = array(); |
| 74 |
|
foreach ($result as $keyword) { |
| 75 |
|
$keywords[$vid][$keyword->kid] = $keyword->word; |
| 76 |
|
} |
| 77 |
|
} |
| 78 |
|
return $keywords[$vid]; |
| 79 |
} |
} |
| 80 |
|
|
|
function biblio_delete_keywords($node) { |
|
|
db_query('DELETE FROM {biblio_keyword} WHERE nid = :nid', array(':nid' => $node->nid)); |
|
|
biblio_delete_orphan_keywords(); |
|
|
} |
|
| 81 |
|
|
| 82 |
|
/** |
| 83 |
|
* Update the keyword database from the supplied node |
| 84 |
|
* |
| 85 |
|
* @param stdClass $node |
| 86 |
|
* @return |
| 87 |
|
* An array of keyword ID's |
| 88 |
|
*/ |
| 89 |
function biblio_update_keywords($node) { |
function biblio_update_keywords($node) { |
| 90 |
biblio_insert_keywords($node, TRUE); |
$kids = biblio_insert_keywords($node, TRUE); |
| 91 |
biblio_delete_orphan_keywords(); |
biblio_delete_orphan_keywords(); |
| 92 |
|
return $kids; |
| 93 |
} |
} |
| 94 |
|
|
|
function biblio_delete_orphan_keywords () { |
|
|
if (variable_get('biblio_auto_orphaned_keyword_delete', 0)) { |
|
|
db_query('DELETE FROM {biblio_keyword_data} WHERE kid NOT IN (SELECT DISTINCT(kid) FROM {biblio_keyword})'); |
|
|
} |
|
|
} |
|
| 95 |
/** |
/** |
| 96 |
|
* Insert keywords into the database |
| 97 |
|
* |
| 98 |
* @param $node |
* @param $node |
| 99 |
|
* A node with keywords attached |
| 100 |
|
* @param $update |
| 101 |
|
* Set to true if you are updating an existing node |
| 102 |
* @return |
* @return |
| 103 |
|
* An array of keyword ID's from this node |
| 104 |
*/ |
*/ |
| 105 |
function biblio_insert_keywords($node, $update = FALSE) { |
function biblio_insert_keywords($node, $update = FALSE) { |
| 106 |
if (empty($node->biblio_keywords)) return; |
if (empty($node->biblio_keywords)) return; |
| 114 |
$typed_keywords = $node->biblio_keywords; |
$typed_keywords = $node->biblio_keywords; |
| 115 |
} |
} |
| 116 |
|
|
| 117 |
if (!$update) db_query('DELETE FROM {biblio_keyword} WHERE nid = :nid AND vid = :vid', array(':nid' => $node->nid, ':vid' => $node->vid)); |
if (!$update) { |
| 118 |
|
$and = db_and()->condition('nid', $node->nid) |
| 119 |
|
->condition('vid', $node->vid); |
| 120 |
|
db_delete('biblio_keyword') |
| 121 |
|
->condition($and) |
| 122 |
|
->execute(); |
| 123 |
|
} |
| 124 |
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 |
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 |
| 125 |
foreach ($node->taxonomy as $vid => $term) { |
foreach ($node->taxonomy as $vid => $term) { |
| 126 |
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 |
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 |
| 149 |
if (!strlen(trim($word))) continue; //skip if we have a blank |
if (!strlen(trim($word))) continue; //skip if we have a blank |
| 150 |
$kid = FALSE; |
$kid = FALSE; |
| 151 |
// See if the term exists |
// See if the term exists |
| 152 |
if ( ($term = biblio_get_keyword_by_name($word)) ) { |
if ( ($kw = biblio_get_keyword_by_name($word)) ) { |
| 153 |
$kid = $term->kid; |
$kid = $kw->kid; |
| 154 |
} |
} |
| 155 |
if (!$kid) { |
if (!$kid) { |
| 156 |
$term = array('word' => trim($word)); |
$kw = array('word' => trim($word)); |
| 157 |
$status = biblio_save_keyword($term); |
$status = biblio_save_keyword($kw); |
| 158 |
$kid = $term['kid']; |
$kid = $kw['kid']; |
| 159 |
} |
} |
| 160 |
// Defend against duplicate, differently cased tags |
// Defend against duplicate, differently cased tags |
| 161 |
if (!isset($inserted[$kid])) { |
if (!isset($inserted[$kid])) { |
| 162 |
if ($update) db_query('DELETE FROM {biblio_keyword} WHERE nid = :nid AND vid = :vid AND kid = :kid', array(':nid' => $node->nid, ':vid' => $node->vid, ':kid' => $kid)); |
if ($update) { |
| 163 |
//db_query('INSERT INTO {biblio_keyword} (kid, nid, vid) VALUES (%d, %d, %d)', $kid, $node->nid, $node->vid); |
$and = db_and()->condition('nid', $node->nid) |
| 164 |
$inserted[$kid] = db_insert('biblio_keyword')->fields(array( |
->condition('vid', $node->vid) |
| 165 |
'kid' => $kid, |
->condition('kid', $kid); |
| 166 |
'nid' => $node->nid, |
db_delete('biblio_keyword') |
| 167 |
'vid' => $node->vid)) |
->condition($and) |
| 168 |
->execute(); |
->execute(); |
| 169 |
|
} |
| 170 |
|
$inserted[$kid] = db_insert('biblio_keyword')->fields( |
| 171 |
|
array( |
| 172 |
|
'kid' => $kid, |
| 173 |
|
'nid' => $node->nid, |
| 174 |
|
'vid' => $node->vid |
| 175 |
|
))->execute(); |
| 176 |
} |
} |
| 177 |
} |
} |
| 178 |
|
|
| 182 |
$node->taxonomy['tags'][$kw_vocab] = biblio_implode_keywords($typed_keywords, ','); |
$node->taxonomy['tags'][$kw_vocab] = biblio_implode_keywords($typed_keywords, ','); |
| 183 |
} |
} |
| 184 |
|
|
| 185 |
return count($inserted); |
return array_keys($inserted); |
| 186 |
} |
} |
| 187 |
/** |
/** |
| 188 |
* @param $word |
* @param $word |
| 189 |
* @return |
* @return |
| 190 |
*/ |
*/ |
| 191 |
function biblio_save_keyword(&$form_values) { |
function biblio_save_keyword(&$keyword) { |
| 192 |
if (!empty($form_values['kid']) && $form_values['word']) { |
if (!empty($keyword['kid']) && $keyword['word']) { |
| 193 |
drupal_write_record('biblio_keyword_data', $form_values, 'kid'); |
drupal_write_record('biblio_keyword_data', $keyword, 'kid'); |
| 194 |
$status = SAVED_UPDATED; |
$status = SAVED_UPDATED; |
| 195 |
} else { |
} else { |
| 196 |
drupal_write_record('biblio_keyword_data', $form_values); |
drupal_write_record('biblio_keyword_data', $keyword); |
| 197 |
$status = SAVED_NEW; |
$status = SAVED_NEW; |
| 198 |
} |
} |
| 199 |
|
|
| 200 |
return $status; |
return $status; |
| 201 |
} |
} |
| 202 |
|
|
| 203 |
|
function biblio_delete_orphan_keywords ($force = FALSE) { |
| 204 |
|
if (variable_get('biblio_auto_orphaned_keyword_delete', 0) || $force) { |
| 205 |
|
// $sub_select = db_select('biblio_keyword', 'bk'); |
| 206 |
|
// $sub_select->addField('bk', 'kid'); |
| 207 |
|
// $sub_select->distinct(); |
| 208 |
|
// return db_delete('biblio_keyword_data') |
| 209 |
|
// ->condition('kid', $sub_select, 'NOT IN') |
| 210 |
|
// ->execute(); |
| 211 |
|
db_query('DELETE FROM {biblio_keyword_data} WHERE kid NOT IN (SELECT DISTINCT(kid) FROM {biblio_keyword})'); |
| 212 |
|
} |
| 213 |
|
return; |
| 214 |
|
} |
| 215 |
/** |
/** |
| 216 |
|
* Delete all keywords references from a given node, but the actual keywords remain in the biblio_keyword_data table |
| 217 |
|
* Also remove orphan keywords if this option is enabled. Orphan keywords |
| 218 |
|
* are keywords which remain in the biblio_keyword_data table but are not refferenced |
| 219 |
|
* by any nodes through the biblio_keyword table. |
| 220 |
|
* |
| 221 |
* @param $node |
* @param $node |
| 222 |
* @return none |
* @return |
| 223 |
|
* The number of links removed |
| 224 |
*/ |
*/ |
| 225 |
function biblio_delete_node_keywords($node) { |
function biblio_delete_keywords($node) { |
| 226 |
db_query('DELETE FROM {biblio_keyword} WHERE nid = :nid', array(':nid' => $node->nid)); |
$count = db_delete('biblio_keyword') |
| 227 |
|
->condition('nid', $node->nid) |
| 228 |
|
->execute(); |
| 229 |
|
biblio_delete_orphan_keywords(); |
| 230 |
|
return $count; |
| 231 |
} |
} |
| 232 |
|
|
| 233 |
/** |
/** |
| 234 |
|
* Delete "node revision to keyword" links from the biblio_keyword table |
| 235 |
|
* |
| 236 |
* @param $node |
* @param $node |
| 237 |
* @return none |
* @return |
| 238 |
|
* The number of links removed |
| 239 |
*/ |
*/ |
| 240 |
function biblio_delete_revision_keywords($node) { |
function biblio_delete_revision_keywords($node) { |
| 241 |
db_query('DELETE FROM {biblio_keyword} WHERE vid = :vid', array(':vid' => $node->vid)); |
return db_delete('biblio_keyword') |
| 242 |
|
->condition('vid', $node->vid) |
| 243 |
|
->execute(); |
| 244 |
} |
} |
| 245 |
|
|
| 246 |
|
/** |
| 247 |
|
* Delete multiple keywords from both the biblio_keyword and biblio_keyword_data tables |
| 248 |
|
* This will remove the keywords referenced by the supplied ID's from ALL nodes which reference them. |
| 249 |
|
* |
| 250 |
|
* @param array $keywords |
| 251 |
|
* An array of keyword id's to delete |
| 252 |
|
* @return |
| 253 |
|
* The number of keywords deleted |
| 254 |
|
*/ |
| 255 |
|
function biblio_delete_multiple_keywords($keywords) { |
| 256 |
|
$count = 0; |
| 257 |
|
foreach ($keywords as $kid) { |
| 258 |
|
$count += biblio_delete_keyword($kid); |
| 259 |
|
} |
| 260 |
|
return $count; |
| 261 |
|
} |
| 262 |
|
/** |
| 263 |
|
* Delete a keyword from both the biblio_keyword and biblio_keyword_data tables |
| 264 |
|
* This will remove the keyword referenced by the supplied ID from ALL nodes which reference them. |
| 265 |
|
* |
| 266 |
|
* @param $keyword_id |
| 267 |
|
* The keyword id to delete |
| 268 |
|
* @return |
| 269 |
|
* The number of keywords deleted (should always be one) |
| 270 |
|
*/ |
| 271 |
function biblio_delete_keyword($keyword_id) { |
function biblio_delete_keyword($keyword_id) { |
| 272 |
db_query('DELETE FROM {biblio_keyword} WHERE kid = :kid', array(':kid' => $keyword_id)); |
db_delete('biblio_keyword') |
| 273 |
db_query('DELETE FROM {biblio_keyword_data} WHERE kid = :kid', array(':kid' => $keyword_id)); |
->condition('kid', $keyword_id) |
| 274 |
|
->execute(); |
| 275 |
|
|
| 276 |
|
return db_delete('biblio_keyword_data') |
| 277 |
|
->condition('kid', $keyword_id) |
| 278 |
|
->execute(); |
| 279 |
} |
} |
| 280 |
|
|
| 281 |
function biblio_explode_keywords($string) { |
function biblio_explode_keywords($string) { |