| 1 |
<?php |
<?php |
| 2 |
// $Id: similar.module,v 1.7.2.6 2007/11/20 05:26:30 deekayen Exp $ |
// $Id: similar.module,v 1.7.2.7 2007/11/29 16:17:37 deekayen Exp $ |
| 3 |
|
|
| 4 |
/** |
/** |
| 5 |
* @file |
* @file |
| 52 |
$form['similar_cache'] = array( |
$form['similar_cache'] = array( |
| 53 |
'#type' => 'fieldset', |
'#type' => 'fieldset', |
| 54 |
'#title' => t('Cache settings'), |
'#title' => t('Cache settings'), |
| 55 |
'#collapsible' => true, |
'#collapsible' => TRUE, |
| 56 |
'#collapsed' => false); |
'#collapsed' => FALSE |
| 57 |
|
); |
| 58 |
|
|
| 59 |
$form['similar_cache']['similar_cache'] = array( |
$form['similar_cache']['similar_cache'] = array( |
| 60 |
'#type' => 'radios', |
'#type' => 'radios', |
| 108 |
'#type' => 'radios', |
'#type' => 'radios', |
| 109 |
'#title' => t('Force clear on deletion'), |
'#title' => t('Force clear on deletion'), |
| 110 |
'#default_value' => variable_get('similar_clear_on_delete', 1), |
'#default_value' => variable_get('similar_clear_on_delete', 1), |
| 111 |
'#options' => array(0 => t('Disabled'), 1 => t('Enabled')) |
'#options' => array(0 => t('Disabled'),1 => t('Enabled')) |
| 112 |
); |
); |
| 113 |
return system_settings_form($form); |
return system_settings_form($form); |
| 114 |
} |
} |
| 120 |
switch ($op) { |
switch ($op) { |
| 121 |
case 'insert': |
case 'insert': |
| 122 |
if (variable_get('similar_clear_on_insert', 0)) { |
if (variable_get('similar_clear_on_insert', 0)) { |
| 123 |
cache_clear_all('similar_block_0:', 'cache', true); |
cache_clear_all('similar_block_0:', 'cache', TRUE); |
| 124 |
} |
} |
| 125 |
break; |
break; |
| 126 |
case 'update': |
case 'update': |
| 129 |
cache_clear_all("similar_block_0:$node->nid", 'cache'); |
cache_clear_all("similar_block_0:$node->nid", 'cache'); |
| 130 |
} |
} |
| 131 |
else { |
else { |
| 132 |
cache_clear_all('similar_block_0:', 'cache', true); |
cache_clear_all('similar_block_0:', 'cache', TRUE); |
| 133 |
} |
} |
| 134 |
} |
} |
| 135 |
break; |
break; |
| 136 |
case 'delete': |
case 'delete': |
| 137 |
if (variable_get('similar_clear_on_delete', 1)) { |
if (variable_get('similar_clear_on_delete', 1)) { |
| 138 |
cache_clear_all('similar_block_0:', 'cache', true); |
cache_clear_all('similar_block_0:', 'cache', TRUE); |
| 139 |
} |
} |
| 140 |
else { |
else { |
| 141 |
cache_clear_all("similar_block_0:$node->nid", 'cache'); |
cache_clear_all("similar_block_0:$node->nid", 'cache'); |
| 197 |
$form['similar_taxonomy'] = array( |
$form['similar_taxonomy'] = array( |
| 198 |
'#type' => 'fieldset', |
'#type' => 'fieldset', |
| 199 |
'#title' => t('Taxonomy category filter'), |
'#title' => t('Taxonomy category filter'), |
| 200 |
'#collapsible' => true, '#collapsed' => true |
'#collapsible' => TRUE, |
| 201 |
|
'#collapsed' => TRUE |
| 202 |
); |
); |
| 203 |
$form['similar_taxonomy']['similar_taxonomy_filter'] = array( |
$form['similar_taxonomy']['similar_taxonomy_filter'] = array( |
| 204 |
'#type' => 'radios', |
'#type' => 'radios', |
| 210 |
$form['similar_taxonomy']['similar_taxonomy_select'] = array( |
$form['similar_taxonomy']['similar_taxonomy_select'] = array( |
| 211 |
'#type' => 'fieldset', |
'#type' => 'fieldset', |
| 212 |
'#title' => t('Taxonomy categories to display'), |
'#title' => t('Taxonomy categories to display'), |
| 213 |
'#collapsible' => true, '#collapsed' => true |
'#collapsible' => TRUE, |
| 214 |
|
'#collapsed' => TRUE |
| 215 |
); |
); |
| 216 |
$form['similar_taxonomy']['similar_taxonomy_select']['similar_taxonomy_tids'] = array( |
$form['similar_taxonomy']['similar_taxonomy_select']['similar_taxonomy_tids'] = array( |
| 217 |
'#type' => 'select', |
'#type' => 'select', |
| 218 |
'#default_value' => variable_get('similar_taxonomy_tids', array_keys($names)), |
'#default_value' => variable_get('similar_taxonomy_tids', array_keys($names)), |
| 219 |
'#description' => t('Hold the CTRL key to (de)select multiple options.'), |
'#description' => t('Hold the CTRL key to (de)select multiple options.'), |
| 220 |
'#options' => $names, '#multiple' => true |
'#options' => $names, |
| 221 |
|
'#multiple' => TRUE |
| 222 |
|
); |
| 223 |
|
$vocs = _similar_taxonomy_get_vocabularies(); |
| 224 |
|
$form['similar_taxonomy']['similar_taxonomy_exclude'] = array( |
| 225 |
|
'#type' => 'select', |
| 226 |
|
'#multiple' => TRUE, |
| 227 |
|
'#title' => t('Exclude vocabularies'), |
| 228 |
|
'#default_value' => variable_get('similar_taxonomy_exclude', ''), |
| 229 |
|
'#options' => $vocs |
| 230 |
); |
); |
| 231 |
} |
} |
| 232 |
} |
} |
| 241 |
if (module_exists('taxonomy')) { |
if (module_exists('taxonomy')) { |
| 242 |
variable_set('similar_taxonomy_filter', $edit['similar_taxonomy_filter']); |
variable_set('similar_taxonomy_filter', $edit['similar_taxonomy_filter']); |
| 243 |
variable_set('similar_taxonomy_tids', $edit['similar_taxonomy_tids']); |
variable_set('similar_taxonomy_tids', $edit['similar_taxonomy_tids']); |
| 244 |
|
variable_set('similar_taxonomy_exclude', $edit['similar_taxonomy_exclude']); |
| 245 |
} |
} |
| 246 |
} |
} |
| 247 |
return; |
return; |
| 250 |
default: |
default: |
| 251 |
if (arg(0) == 'node' && is_numeric(arg(1)) && arg(2) != 'edit') { |
if (arg(0) == 'node' && is_numeric(arg(1)) && arg(2) != 'edit') { |
| 252 |
$node = node_load(array('nid' => arg(1))); |
$node = node_load(array('nid' => arg(1))); |
| 253 |
} else { |
} |
| 254 |
|
else { |
| 255 |
return; |
return; |
| 256 |
} |
} |
| 257 |
|
|
| 301 |
function _similar_published_node_types() { |
function _similar_published_node_types() { |
| 302 |
$types = array(); |
$types = array(); |
| 303 |
$result = db_query('SELECT DISTINCT(n.type) FROM {node} n WHERE n.status <> 0 ORDER BY n.type ASC'); |
$result = db_query('SELECT DISTINCT(n.type) FROM {node} n WHERE n.status <> 0 ORDER BY n.type ASC'); |
| 304 |
while($type = db_fetch_object($result)) { |
while ($type = db_fetch_object($result)) { |
| 305 |
$types[$type->type] = $type->type; |
$types[$type->type] = $type->type; |
| 306 |
} |
} |
| 307 |
return $types; |
return $types; |
| 316 |
function _similar_taxonomy_names() { |
function _similar_taxonomy_names() { |
| 317 |
$names = array(); |
$names = array(); |
| 318 |
$result = db_query('SELECT d.tid, v.vid, v.name AS vocab_name, d.name AS data_name FROM {term_data} d, {vocabulary} v WHERE v.vid = d.vid ORDER BY v.name, d.name ASC'); |
$result = db_query('SELECT d.tid, v.vid, v.name AS vocab_name, d.name AS data_name FROM {term_data} d, {vocabulary} v WHERE v.vid = d.vid ORDER BY v.name, d.name ASC'); |
| 319 |
while($data = db_fetch_object($result)) { |
while ($data = db_fetch_object($result)) { |
| 320 |
$names[$data->tid] = $data->vocab_name .': '. $data->data_name; |
$names[$data->tid] = $data->vocab_name .': '. $data->data_name; |
| 321 |
} |
} |
| 322 |
return $names; |
return $names; |
| 329 |
* @return array |
* @return array |
| 330 |
*/ |
*/ |
| 331 |
function _similar_taxonomy_membership($nid) { |
function _similar_taxonomy_membership($nid) { |
| 332 |
|
$tids_exclude = array(); |
| 333 |
|
$vid = variable_get('similar_taxonomy_exclude', ''); |
| 334 |
|
if (count($vid) > 0) { |
| 335 |
|
$result = db_query('SELECT t.tid FROM {term_data} t WHERE t.vid IN (%s)', implode(',', $vid)); |
| 336 |
|
while ($data = db_fetch_object($result)) { |
| 337 |
|
$tids_exclude[$data->tid] = $data->tid; |
| 338 |
|
} |
| 339 |
|
} |
| 340 |
|
|
| 341 |
$tids = array(); |
$tids = array(); |
| 342 |
$result = db_query('SELECT t.tid FROM {term_node} t WHERE t.nid = %d', $nid); |
$result = db_query('SELECT t.tid FROM {term_node} t WHERE t.nid = %d', $nid); |
| 343 |
while($data = db_fetch_object($result)) { |
while($data = db_fetch_object($result)) { |
| 344 |
$tids[$data->tid] = $data->tid; |
if (!in_array($data->tid, $tidsExclude)) { |
| 345 |
|
$tids[$data->tid] = $data->tid; |
| 346 |
|
} |
| 347 |
} |
} |
| 348 |
return $tids; |
return $tids; |
| 349 |
} |
} |
| 371 |
function theme_similar_content($node) { |
function theme_similar_content($node) { |
| 372 |
$items = array(); |
$items = array(); |
| 373 |
|
|
| 374 |
$text = $node->title . ' ' . $node->body; |
$text = $node->title .' '. $node->body; |
| 375 |
$teaser = variable_get('similar_teaser_enabled', 0); |
$teaser = variable_get('similar_teaser_enabled', 0); |
| 376 |
|
|
| 377 |
$types = _similar_published_node_types(); |
$types = _similar_published_node_types(); |
| 397 |
} |
} |
| 398 |
|
|
| 399 |
$result = db_query_range("SELECT r.nid, MATCH(r.body, r.title) AGAINST ('%s') AS score FROM {node_revisions} r INNER JOIN {node} n ON r.nid = n.nid AND r.vid = n.vid INNER JOIN {term_node} t ON n.nid = t.nid AND t.tid IN ($taxonomy_tids) WHERE MATCH(r.body, r.title) AGAINST ('%s') AND n.status <> 0 AND r.nid <> %d AND n.type IN ($types) GROUP BY n.nid ORDER BY score DESC, r.vid DESC", $text, $text, $node->nid, 0, variable_get('similar_num_display', 5)); |
$result = db_query_range("SELECT r.nid, MATCH(r.body, r.title) AGAINST ('%s') AS score FROM {node_revisions} r INNER JOIN {node} n ON r.nid = n.nid AND r.vid = n.vid INNER JOIN {term_node} t ON n.nid = t.nid AND t.tid IN ($taxonomy_tids) WHERE MATCH(r.body, r.title) AGAINST ('%s') AND n.status <> 0 AND r.nid <> %d AND n.type IN ($types) GROUP BY n.nid ORDER BY score DESC, r.vid DESC", $text, $text, $node->nid, 0, variable_get('similar_num_display', 5)); |
| 400 |
} else { |
} |
| 401 |
|
else { |
| 402 |
$result = db_query_range("SELECT r.nid, MATCH(r.body, r.title) AGAINST ('%s') AS score FROM {node_revisions} r INNER JOIN {node} n ON r.nid = n.nid AND r.vid = n.vid WHERE MATCH(r.body, r.title) AGAINST ('%s') AND n.status <> 0 AND r.nid <> %d AND n.type IN ($types) GROUP BY n.nid ORDER BY score DESC, r.vid DESC", $text, $text, $node->nid, 0, variable_get('similar_num_display', 5)); |
$result = db_query_range("SELECT r.nid, MATCH(r.body, r.title) AGAINST ('%s') AS score FROM {node_revisions} r INNER JOIN {node} n ON r.nid = n.nid AND r.vid = n.vid WHERE MATCH(r.body, r.title) AGAINST ('%s') AND n.status <> 0 AND r.nid <> %d AND n.type IN ($types) GROUP BY n.nid ORDER BY score DESC, r.vid DESC", $text, $text, $node->nid, 0, variable_get('similar_num_display', 5)); |
| 403 |
} |
} |
| 404 |
while ($node = db_fetch_object($result)) { |
while ($node = db_fetch_object($result)) { |
| 411 |
NULL, |
NULL, |
| 412 |
NULL, |
NULL, |
| 413 |
NULL, |
NULL, |
| 414 |
true |
TRUE |
| 415 |
) . |
) . |
| 416 |
'</div><div class="similar-teaser">'. check_markup($content->teaser, $content->format, false) .'</div>'; |
'</div><div class="similar-teaser">'. check_markup($content->teaser, $content->format, FALSE) .'</div>'; |
| 417 |
} else { |
} |
| 418 |
|
else { |
| 419 |
$items[] = l( |
$items[] = l( |
| 420 |
$content->title, |
$content->title, |
| 421 |
'node/'.$node->nid, |
'node/'. $node->nid, |
| 422 |
variable_get('similar_rel_nofollow', 0) ? array('rel' => 'nofollow') : NULL |
variable_get('similar_rel_nofollow', 0) ? array('rel' => 'nofollow') : NULL |
| 423 |
); |
); |
| 424 |
} |
} |
| 426 |
|
|
| 427 |
return sizeof($items) > 0 ? theme('item_list', $items) : ''; |
return sizeof($items) > 0 ? theme('item_list', $items) : ''; |
| 428 |
} |
} |
| 429 |
|
|
| 430 |
|
/** |
| 431 |
|
* Get all vocabularies |
| 432 |
|
* |
| 433 |
|
* @return array |
| 434 |
|
*/ |
| 435 |
|
function _similar_taxonomy_get_vocabularies() { |
| 436 |
|
$v = array(); |
| 437 |
|
$vocs = taxonomy_get_vocabularies(); |
| 438 |
|
$result = db_query("SELECT DISTINCT(n.type) FROM {node} n WHERE n.status <> 0 ORDER BY n.type ASC"); |
| 439 |
|
foreach ($vocs as $voc) { |
| 440 |
|
$v[$voc->vid] = $voc->name;; |
| 441 |
|
} |
| 442 |
|
return $v; |
| 443 |
|
} |