You are here

function similarterms_list in Similar By Terms 5

Same name and namespace in other branches
  1. 6 similarterms.module \similarterms_list()
  2. 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 in ./similarterms.module
Implementation of hook_block().

File

./similarterms.module, line 106
Similar By Terms module displays a block with similar content based on taxonomy terms.

Code

function similarterms_list($vocid = 0, $nid = NULL) {
  $nodes = array();
  $sql = "";
  $args = array();
  $cache_lifetime = variable_get('similarterms_cache_options', 3600);
  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 unserialize($cached->data);
      }
    }
    $node_obj = node_load($nid);
    if ($vocid == 0) {
      $terms = array_keys(taxonomy_node_get_terms($node_obj->nid));
    }
    else {
      $terms = array_keys(taxonomy_node_get_terms_by_vocabulary($node_obj->nid, $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]);
      }
    }
    $count = variable_get('simterms_count_' . $vocid, 5);
    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.nid = tn.nid ';
      $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, 'cache_similarterms', serialize($nodes), time() + $cache_lifetime);
      }
    }
  }
  return $nodes;
}