function similarterms_list in Similar By Terms 6
Same name and namespace in other branches
- 5 similarterms.module \similarterms_list()
- 7 similarterms.module \similarterms_list()
Output the block
Parameters
$vocid: integer - vocabulary id, leave out to use ALL terms for this node
$nid: integer - nid, leave out to use the current node
Return value
an array of node objects
1 call to similarterms_list()
- similarterms_block_view in ./
similarterms.module - Perform the "view" op for hook_block().
File
- ./
similarterms.module, line 189 - Similar By Terms module displays a block with similar content based on taxonomy terms.
Code
function similarterms_list($vocid = 0, $nid = NULL) {
$lists = array();
$nodes = array();
$args = array();
$list_num = 0;
$sql = "";
$cache_lifetime = variable_get('similarterms_cache_options', 3600);
$count = variable_get('simterms_count_' . $vocid, 5);
if (arg(0) == 'node' && is_numeric(arg(1)) && !$nid) {
$nid = arg(1);
}
if ($nid != NULL) {
$cid = "{$vocid}:{$nid}";
if ($cache_lifetime) {
if ($cached = cache_get($cid, 'cache_similarterms')) {
return $cached->data;
}
}
if (variable_get('similarterms_override_options', 0)) {
$lists = similarterms_get_overrides($nid, $vocid);
if ($lists[$vocid]) {
foreach ($lists[$vocid] as $nid_list) {
if (is_numeric($nid_list)) {
if ($list_num <= $count) {
$list_num = $list_num + 1;
$nodes[] = node_load($nid_list);
}
}
}
}
}
$node_obj = node_load($nid);
if ($vocid == 0) {
$terms = array_keys(taxonomy_node_get_terms($node_obj));
}
else {
$terms = array_keys(taxonomy_node_get_terms_by_vocabulary($node_obj, $vocid));
}
// Filter out some terms
$terms_filter = variable_get('simterms_ignoreterms_' . $vocid, array());
foreach ($terms_filter as $v) {
$idx = array_search($v, $terms);
if ($idx >= 0) {
unset($terms[$idx]);
}
}
if (!empty($terms)) {
//past events
$pasts = array();
$sql = 'SELECT n.nid, n.title, COUNT(n.nid) AS ncount ';
$sql .= 'FROM {node} n ';
$sql .= 'INNER JOIN {term_node} tn ON n.vid = tn.vid ';
$sql .= 'WHERE tn.tid IN (';
$number_of_terms = count($terms);
foreach ($terms as $terms_items) {
$number_of_terms--;
if ($number_of_terms) {
$sql .= "'%s',";
}
else {
$sql .= "'%s'";
}
$args[] = $terms_items;
}
$sql .= ') ';
$types = variable_get('simterms_sametype_' . $vocid, FALSE);
if ($types !== FALSE && is_array($types) && count($types) > 0 && $types['0'] == NULL) {
if ($types[1]) {
$node_obj = node_load($nid);
$types[1] = $node_obj->type;
}
$sql .= 'AND n.type IN (';
$number_of_types = count($types);
foreach ($types as $types_items) {
$number_of_types--;
if ($number_of_types) {
$sql .= "'%s',";
}
else {
$sql .= "'%s'";
}
$args[] = $types_items;
}
$sql .= ') ';
}
//if showcurrentnode option is false (default state), create filter for query.
if (!variable_get('similarterms_showcurrentnode_' . $vocid, FALSE)) {
$sql .= 'AND n.nid != %d ';
$args[] = $nid;
}
$sql .= 'AND n.status = 1 ';
$sql .= 'AND n.moderate = 0 ';
$sql .= "AND (n.language = '' OR n.language = '%s') ";
$args[] = $node_obj->language;
$sql .= 'GROUP BY n.nid, n.title, n.created ';
if (variable_get('similarterms_ncount_options', 'default') == 'default') {
$sql .= 'ORDER BY ncount DESC, ';
}
else {
$sql .= 'ORDER BY ncount ASC, ';
}
$sql .= 'n.created DESC ';
$sql .= 'LIMIT %d';
$args[] = $count;
$sql = db_rewrite_sql($sql);
$result = db_query($sql, $args);
// watchdog('similarterms', $sql, NULL, WATCHDOG_INFO);
while ($r = db_fetch_object($result)) {
$nodes[] = node_load($r->nid);
}
// Allow modules to alter the list of nodes by implementing a hook.
// Design pattern from comment_invoke_comment().
foreach (module_implements('similarterms') as $name) {
$function = $name . '_similarterms';
$function($nodes, $node_obj);
}
if ($cache_lifetime) {
cache_set($cid, $nodes, 'cache_similarterms', time() + $cache_lifetime);
}
}
}
return $nodes;
}