/[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.1, Sat Dec 3 20:00:39 2005 UTC revision 1.2, Sat Dec 3 20:05:48 2005 UTC
# Line 25  function related_nodes_help($section) { Line 25  function related_nodes_help($section) {
25  }  }
26    
27  /**  /**
  * Implementation of hook_settings()  
  *  
  * @return  
  *   The settings controls  
  */  
 function related_nodes_settings() {  
   $form = array();  
   $form['related_nodes']['related_nodes_title'] = array(  
     '#type' => 'textfield',  
     '#title' => t('The title used for the related nodes block'),  
     '#default_value' => variable_get('related_nodes_title', 'Related nodes'),  
   );  
   
   $form['related_nodes']['related_nodes_block_num'] = array(  
     '#type' => 'select',  
     '#title' => t('Number of related nodes listed in sidebar'),  
     '#default_value' => variable_get('related_nodes_count', '5'),  
     '#options' => drupal_map_assoc(array(5, 10, 15, 20, 25, 30)),  
   );  
   
   $form['related_nodes']['related_nodes_vocabularies'] = array(  
     '#type' => 'select',  
     '#title' => t('Vocabularies used to relate nodes (select none for unrestricted)'),  
     '#multiple' => TRUE,  
     '#default_value' => variable_get('related_nodes_vocabularies', array()),  
     '#options' => taxonomy_get_vocabularies(),  
   );  
   return $form;  
 }  
   
   
 /**  
28   * Implementation of hook_block();   * Implementation of hook_block();
29   *   *
30   * @param $op   * @param $op
# Line 66  function related_nodes_settings() { Line 34  function related_nodes_settings() {
34   *   *
35   * @return a block containing the related nodes   * @return a block containing the related nodes
36   */   */
37  function related_nodes_block($op = 'list', $delta = 0) {  function related_nodes_block($op = 'list', $delta = 0, $edit = array()) {
38    global $user;    global $user;
39    switch($op) {    switch($op) {
40      case ('list'):      case ('list'):
41        $block[0]['info'] = t('Related nodes');        $block[0]['info'] = t('Related nodes');
42        return $block;        return $block;
43    
44        case('configure'):
45          $form = array();
46          $form['related_nodes_block_title'] = array(
47            '#type' => 'textfield',
48            '#title' => t('The title used for the related nodes block'),
49            '#default_value' => variable_get('related_nodes_block_title', t('Related Nodes')),
50          );
51    
52          $form['related_nodes_block_num'] = array(
53            '#type' => 'select',
54            '#title' => t('Number of related nodes listed in sidebar'),
55            '#default_value' => variable_get('related_nodes_count', '5'),
56            '#options' => drupal_map_assoc(array(5, 10, 15, 20, 25, 30)),
57          );
58          return $form;
59        case('save'):
60          variable_set('related_nodes_block_title', $edit['related_nodes_block_title']);
61          variable_set('related_nodes_block_num', $edit['related_nodes_block_num']);
62          break;
63    
64      case('view'):      case('view'):
65        if (arg(0) == 'node' && arg(1) && user_access('access content')) {        if (arg(0) == 'node' && arg(1) && user_access('access content')) {
66          if (is_numeric(arg(1))) {          if (is_numeric(arg(1))) {
67              $nid = arg(1);
68            $content = '';            $content = '';
69            $numTags = variable_get('related_nodes_count', 5);            $numTags = variable_get('related_nodes_count', 5);
70    
           $vlist = variable_get('related_nodes_vocabularies', array());  
           $vocabularies = implode(',',$vlist);  
   
71            if (is_numeric($numTags) && $numTags >= 1) {            if (is_numeric($numTags) && $numTags >= 1) {
72              $content .= '<ul>';              $content .= '<ul>';
73              $haveOne = FALSE;              $haveOne = FALSE;
74              $result = related_nodes_get_nodes($nid, $numTags, $vocabularies, FALSE);              $result = related_nodes_get_nodes($nid, $numTags, FALSE);
75              while ($rn = db_fetch_object($result)) {             while ($rn = db_fetch_object($result)) {
76                $haveOne = TRUE;                $haveOne = TRUE;
77                $content .= '<li>' . l(t($rn->title), 'node/' . $rn->nid) . '</li>';                $content .= '<li>' . l(t($rn->title), 'node/' . $rn->nid) . '</li>';
78              }              }
# Line 95  function related_nodes_block($op = 'list Line 82  function related_nodes_block($op = 'list
82            if (!$haveOne)            if (!$haveOne)
83              return;              return;
84    
85            $blocks['subject'] = t('Related Nodes');            $blocks['subject'] = variable_get('related_nodes_block_title', t('Related Nodes'));
86            $blocks['content'] = $content;            $blocks['content'] = $content;
87    
88            return $blocks;            return $blocks;
89          }          }
90        }        }
91      }
92  }  }
93    
94  function related_nodes_get_nodes($nid, $numNodes = 5, $vocabularies = '', $usePager = FALSE, $from = 0) {  function related_nodes_get_nodes($nid, $numNodes = 5, $usePager = FALSE, $from = 0) {
95    if (!is_numeric($nid))    if (!is_numeric($nid))
96      return FALSE;      return FALSE;
97    if ($nid <= 0)    if ($nid <= 0)
# Line 111  function related_nodes_get_nodes($nid, $ Line 99  function related_nodes_get_nodes($nid, $
99    if (!is_numeric($numNodes))    if (!is_numeric($numNodes))
100      $numNodes= variable_get('related_nodes_count', 5);      $numNodes= variable_get('related_nodes_count', 5);
101    
   if ($vocabularies) {  
     $vjoin = 'INNER JOIN {term_data} td ON tn.tid = td.tid ';  
     $vwhere = ' AND td.vid IN (' . $vocabularies . ') ';  
   }  
   
102    $sql = 'SELECT n.nid AS nid, n.title, ' .    $sql = 'SELECT n.nid AS nid, n.title, ' .
103        'COUNT(tn3.tid) c FROM {term_node} tn ' .        'COUNT(tn3.tid) c FROM {term_node} tn ' .
       $vjoin .  
104        'INNER JOIN {term_node} tn2 ON tn.tid = tn2.tid ' .        'INNER JOIN {term_node} tn2 ON tn.tid = tn2.tid ' .
105        'INNER JOIN {term_node} tn3 ON tn2.nid = tn3.nid ' .        'INNER JOIN {term_node} tn3 ON tn2.nid = tn3.nid ' .
106        'INNER JOIN {node} n ON tn3.nid = n.nid ' .        'INNER JOIN {node} n ON tn3.nid = n.nid ' .
107        'WHERE n.status = 1 AND tn.nid = ' . $nid . ' AND tn2.nid <> ' . $nid .        'WHERE n.status = 1 AND tn.nid = ' . $nid . ' AND tn2.nid <> ' . $nid .
       $vwhere .  
108        ' AND tn.nid <> tn3.nid ' .        ' AND tn.nid <> tn3.nid ' .
109        ' GROUP BY n.nid ORDER BY c DESC';        ' GROUP BY n.nid ORDER BY c DESC';
110    
111    $sql = db_rewrite_sql($sql);    $sql = db_rewrite_sql($sql);
112    if ($usePager)    if ($usePager) {
113      $result = pager_query($sql, $numNodes);      $result = pager_query($sql, $numNodes);
114      }
115    else {    else {
116      if ($numNodes > 0)      if ($numNodes > 0) {
117        $result = db_query_range($sql, $from, $numNodes);        $result = db_query_range($sql, $from, $numNodes);
118      else      }
119        else {
120        $result = db_query($sql);        $result = db_query($sql);
121        }
122    }    }
123    return $result;    return $result;
124  }  }
125    

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.2

  ViewVC Help
Powered by ViewVC 1.1.2