/[drupal]/contributions/modules/related_nodes/related_nodes.module
ViewVC logotype

Diff of /contributions/modules/related_nodes/related_nodes.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.5, Sun Dec 4 02:50:41 2005 UTC revision 1.6, Fri Jun 1 04:38:01 2007 UTC
# Line 1  Line 1 
1  <?php  <?php
2  // $Id: related_nodes.module,v 1.2 2005/12/03 20:05:48 eaton Exp $  // $Id$
3    
4  /**  /**
5   * @file   * @file
# Line 7  Line 7 
7   */   */
8    
9  /**  /**
  * Implementation of hook_help().  
  *  
  * @param $section  
  *   provides help for /admin/modules and /admin/help and admin/settings/related_nodes.  
  *  
  * @return  
  *   A brief message for administrators to explain what this module does  
  */  
 function related_nodes_help($section) {  
   switch ($section) {  
     case 'admin/modules#description':  
       return t('Displays a sidebar block of nodes related by taxonomy terms.');  
     case 'admin/settings/related_nodes':  
       return t('The related_nodes module maintains and displays lists of nodes that are related by taxonomy term.');  
   }  
 }  
   
 /**  
10   * Implementation of hook_block();   * Implementation of hook_block();
11   *   *
12   * @param $op   * @param $op
# Line 42  function related_nodes_block($op = 'list Line 24  function related_nodes_block($op = 'list
24        return $block;        return $block;
25    
26      case('configure'):      case('configure'):
27        $form = array();        $form = array();
       $form['related_nodes_block_title'] = array(  
         '#type' => 'textfield',  
         '#title' => t('The title used for the related nodes block'),  
         '#default_value' => variable_get('related_nodes_block_title', t('Related Nodes')),  
       );  
   
28        $form['related_nodes_block_num'] = array(        $form['related_nodes_block_num'] = array(
29          '#type' => 'select',          '#type' => 'select',
30          '#title' => t('Number of related nodes listed in sidebar'),          '#title' => t('Number of related nodes listed in sidebar'),
31          '#default_value' => variable_get('related_nodes_block_num', '5'),          '#default_value' => variable_get('related_nodes_block_num', '5'),
32          '#options' => drupal_map_assoc(array(5, 10, 15, 20, 25, 30)),          '#options' => drupal_map_assoc(array(5, 10, 15, 20, 25, 30)),
33        );        );
34    
35        $form['related_nodes_vocabularies'] = array(        $form['related_nodes_vocabularies'] = array(
36          '#type' => 'select',          '#type' => 'select',
37          '#title' => t('Vocabularies used to relate nodes (select none for unrestricted)'),          '#title' => t('Vocabularies used to relate nodes (select none for unrestricted)'),
# Line 64  function related_nodes_block($op = 'list Line 40  function related_nodes_block($op = 'list
40          '#return_value' => array(),          '#return_value' => array(),
41          '#options' => _related_nodes_get_vocabularies(),          '#options' => _related_nodes_get_vocabularies(),
42        );        );
43    
44          $types = node_get_types('names');
45    
46          $form['related_nodes_type'] = array(
47            '#type' => 'select',
48            '#title' => t('Node type used to relate nodes (select none for unrestricted)'),
49            '#multiple' => FALSE,
50            '#default_value' => variable_get('related_nodes_type', ''),
51            '#return_value' => '',
52            '#options' => array_merge(array('' => t('None')), $types),
53          );
54    
55        return $form;        return $form;
56      case('save'):      case('save'):
       variable_set('related_nodes_block_title', $edit['related_nodes_block_title']);  
57        variable_set('related_nodes_block_num', $edit['related_nodes_block_num']);        variable_set('related_nodes_block_num', $edit['related_nodes_block_num']);
58        variable_set('related_nodes_vocabularies', $edit['related_nodes_vocabularies']);        variable_set('related_nodes_vocabularies', $edit['related_nodes_vocabularies']);
59          variable_set('related_nodes_type', explode(',',$edit['related_nodes_type']));
60        break;        break;
61    
62      case('view'):      case('view'):
63        if (arg(0) == 'node' && arg(1) && user_access('access content')) {        if (arg(0) == 'node' && is_numeric(arg(1))) {
64          if (is_numeric(arg(1))) {          $nid = arg(1);
65            $nid = arg(1);          $count = variable_get('related_nodes_count', 5);
66            $content = '';          $vocabularies = variable_get('related_nodes_vocabularies', array());
67            $numTags = variable_get('related_nodes_count', 5);          $node_type = variable_get('related_nodes_type', '');
68    
69            $vocab_list = variable_get('related_nodes_vocabularies', array());          if (count($numTags)) {
70            if ($vocab_list) {            $nodes = related_nodes_get_nodes($nid, $count, $vocabularies, $node_type);
71              $vocabularies = implode(',',$vocab_list);  
72            }            if (count($nodes)) {
73                $blocks['subject'] = variable_get('related_nodes_block_title', t('Related Nodes'));
74            if (is_numeric($numTags) && $numTags >= 1) {              $blocks['content'] = theme('related_nodes_list', $nodes);
             $content .= '<ul>';  
             $haveOne = FALSE;  
             $result = related_nodes_get_nodes($nid, $numTags, $vocabularies, FALSE);  
            while ($rn = db_fetch_object($result)) {  
               $haveOne = TRUE;  
               $content .= '<li>' . l(t($rn->title), 'node/' . $rn->nid) . '</li>';  
             }  
             $content .= '</ul>';  
75            }            }
   
           if (!$haveOne)  
             return;  
   
           $blocks['subject'] = variable_get('related_nodes_block_title', t('Related Nodes'));  
           $blocks['content'] = $content;  
   
           return $blocks;  
76          }          }
77    
78            return $blocks;
79        }        }
80    }    }
81  }  }
82    
83  function related_nodes_get_nodes($nid, $numNodes = 5, $vocabularies = '', $usePager = FALSE, $from = 0) {  function related_nodes_get_nodes($nid, $numNodes = 5, $vocabularies = array(), $node_type = '', $reset = FALSE) {
84    if (!is_numeric($nid))    static $nodes;
85      return FALSE;    if (empty($nodes[$nid]) || $reset) {
86    if ($nid <= 0)      if (!$reset && (($cache = cache_get('related_nodes:nodelist:'. $nid)) && !empty($cache->data))) {
87      return FALSE;        $nodes[$nid] = unserialize($cache->data);
   if (!is_numeric($numNodes))  
     $numNodes= variable_get('related_nodes_count', 5);  
   
   if ($vocabularies) {  
     $join = 'INNER JOIN {term_data} td ON tn.tid = td.tid ';  
     $where = ' AND td.vid IN ( ' . $vocabularies . ') ';  
   }  
   
   $sql = 'SELECT n.nid AS nid, n.title, ' .  
       'COUNT(tn3.tid) c FROM {term_node} tn ' .  
       $join .  
       'INNER JOIN {term_node} tn2 ON tn.tid = tn2.tid ' .  
       'INNER JOIN {term_node} tn3 ON tn2.nid = tn3.nid ' .  
       'INNER JOIN {node} n ON tn3.nid = n.nid ' .  
       'WHERE n.status = 1 AND tn.nid = ' . $nid . ' AND tn2.nid <> ' . $nid .  
       $where .  
       ' AND tn.nid <> tn3.nid ' .  
       ' GROUP BY n.nid ORDER BY c DESC';  
   
   $sql = db_rewrite_sql($sql);  
   if ($usePager) {  
     $result = pager_query($sql, $numNodes);  
   }  
   else {  
     if ($numNodes > 0) {  
       $result = db_query_range($sql, $from, $numNodes);  
88      }      }
89      else {      else {
90        $result = db_query($sql);        if (count($vocabularies)) {
91            $join = 'INNER JOIN {term_data} td ON tn.tid = tn.tid ';
92            $where = ' AND td.vid IN ('. implode(',', $vocabularies) .') ';
93          }
94    
95          if (!empty($node_type)) {
96            $where .= " AND n.type = '". $node_type ."'";
97          }
98    
99          $sql = 'SELECT n.nid AS nid, n.title as title, COUNT(tn3.tid) c FROM {term_node} tn ';
100          $sql .=  $join;
101          $sql .= 'INNER JOIN {term_node} tn2 ON tn.tid = tn2.tid INNER JOIN {term_node} tn3 ON tn2.nid = tn3.nid INNER JOIN {node} n ON tn3.nid = n.nid ';
102          $sql .= 'WHERE n.status = 1 AND tn.nid = %d AND tn2.nid <> %d '. $where .' AND tn.nid <> tn3.nid ';
103          $sql .= ' GROUP BY n.nid ORDER BY c DESC';
104    
105          $results = db_query($sql, $nid, $nid);
106          $nodes = array();
107    
108          while ($node = db_fetch_object($results)) {
109            $nodes[$nid][$node->nid] = l($node->title, 'node/'. $node->nid);
110          }
111          cache_set('related_nodes:nodelist:'. $nid, 'cache', serialize($nodes[$nid]), time() + 600);
112      }      }
113    }    }
114    return $result;    return $nodes[$nid];
115  }  }
116    
117  function _related_nodes_get_vocabularies() {  function _related_nodes_get_vocabularies() {
# Line 153  function _related_nodes_get_vocabularies Line 122  function _related_nodes_get_vocabularies
122    return $vocabularies;    return $vocabularies;
123  }  }
124    
125    function theme_related_nodes_list($nodes = array()) {
126      return theme('item_list', $nodes);
127    }

Legend:
Removed from v.1.5  
changed lines
  Added in v.1.6

  ViewVC Help
Powered by ViewVC 1.1.2