function similarterms_list in Similar By Terms 7
Same name and namespace in other branches
- 5 similarterms.module \similarterms_list()
- 6 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 - Implements hook_block_view().
File
- ./
similarterms.module, line 159 - Similar By Terms module displays a block with similar content based on taxonomy terms.
Code
function similarterms_list($vocid = 'all', $nid = NULL) {
$lists = array();
$nodes = array();
$args = array();
$list_num = 0;
$sql = "";
$cache_lifetime = variable_get('similarterms_cache_options', 3600);
$count = variable_get('similarterms_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 == 'all') {
$terms = array_keys(similarterms_taxonomy_node_get_terms($node_obj));
}
else {
$terms = array_keys(similarterms_taxonomy_node_get_terms($node_obj, $vocid));
}
// Filter out some terms
$terms_filter = variable_get('similarterms_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();
$query = db_select('node', 'n');
$query
->addField('n', 'nid');
$query
->addExpression('COUNT(n.nid)', 'ncount');
$query
->innerJoin('taxonomy_index', 'ti', 'n.nid = ti.nid');
$query
->condition('ti.tid', $terms, 'IN');
$types = variable_get('similarterms_sametype_' . $vocid, FALSE);
if ($types !== FALSE && is_array($types) && count($types) > 0 && !isset($types['0'])) {
if (isset($types['1'])) {
unset($types['1']);
$types[$node_obj->type] = $node_obj->type;
}
$query
->condition('n.type', $types, 'IN');
}
//if showcurrentnode option is false (default state), create filter for query.
if (!variable_get('similarterms_showcurrentnode_' . $vocid, FALSE)) {
$query
->condition('n.nid', $nid, '<>');
}
$query
->condition('n.status', 1)
->condition(db_or()
->condition('n.language', 'und')
->condition('n.language', $node_obj->language))
->groupBy('n.nid')
->groupBy('n.title')
->groupBy('n.created');
if (variable_get('similarterms_ncount_options', 'default') == 'default') {
$query
->orderBy('ncount', 'DESC');
}
else {
$query
->orderBy('ncount', 'ASC');
}
$query
->orderBy('n.created', 'DESC');
$query
->range(0, $count);
$result = $query
->execute();
$nids = array();
foreach ($result as $record) {
$nids[] = $record->nid;
}
$nodes = node_load_multiple($nids);
// 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', REQUEST_TIME + $cache_lifetime);
}
}
}
return $nodes;
}