All pastes #19558 Raw Edit

Unnamed

public text v1 · immutable
#19558 ·published 2005-08-08 18:34 UTC
rendered paste body
<?php
// $Id: node_neighbors.module,v 1.6 2005/05/18 18:42:09 morbus Exp $

/**
 * @file
 * Enables a block and page showing related stories.
 */
 
/**
 * Implementation of hook_help().
 */
function node_neighbors_help($section = 'admin/help#node_neighbors') {
  switch ($section) {
    case 'admin/modules#description':
      return t('Enables a block and page showing related stories.');
  }
}

/**
 * Implementation of hook_menu().
 */
function node_neighbors_menu($may_cache) {
  $items = array();

  if ($may_cache) {
    $items[] = array('title'    => t('node neighbors'),
                     'path'     => 'node_neighbors',
                     'callback' => 'node_neighbors_overview',
                     'access'   => user_access('access content'),
                     'type'     => MENU_CALLBACK);
  }

  return $items;
}

/**
 * Display the last ten nodes of terms assigned to $nnid.
 */
function node_neighbors_overview($nnid) {
  drupal_set_title('Related Stories');
  $node    = node_load(array('nid' => $nnid));
  $output  = '<br />'.theme('node', $node);

  // get terms assigned to this node. we had to write this manually because
  // we wanted alphabetical terms, regardless of the weight applied to vids.
  $terms = node_neighbors_taxonomy_node_get_terms($nnid);
  foreach ($terms as $term) { // bah. kludgey.

    // skip NHPR Programs vocabulary.
    if ($term->vid == 3) { continue; } $cells = NULL;

    // we don't use nhpr_taxonomy_get_term_nodes here because that auto
    // loads the ENTIRE node. that's a huge amount of overhead for a big
    // list of roughly 50 nodes on one page. we'll do everything manually.
    $sql = "SELECT fd.nid FROM {flexinode_data} fd INNER JOIN {term_node} tn ".
           "ON tn.nid = fd.nid WHERE fd.field_id = 1 AND fd.numeric_data < ".
           "UNIX_TIMESTAMP(NOW()) AND tn.tid = %d ORDER BY fd.numeric_data DESC LIMIT %d";
    $result = db_query($sql, $term->tid, 10); // check how many results there are too.
    $result_count = mysql_num_rows($result);  // used later to determine a "read more..."
    $xml_icon = theme('xml_icon', url("taxonomy/term/$term->tid/0/feed"));
    $term_url = url("taxonomy/term/$term->tid"); $term_uc = ucwords($term->name);
    while ($nid = db_fetch_object($result)) {
      if ($nnid == $nid->nid) { continue; } // skip ourselves.
      $node = db_fetch_object(db_query('SELECT n.nid, n.title, numeric_data AS flexinode_1 FROM node n LEFT '.
                 'JOIN flexinode_data fd ON fd.nid = n.nid WHERE n.nid = %d AND fd.field_id = 1', $nid->nid));
      $nurl    = url("node/$node->nid"); $date = format_date($node->flexinode_1, 'custom', 'l, F j, Y');
      $cells  .= "<tr><td class=\"node_list_date\">$date</td>"; // no times, no times!
      $cells  .= "<td class=\"node_list_title\"><a href=\"$nurl\">$node->title</a></td></tr>";
    } if ($cells) { $output .= "<h3 class=\"term_matches_header\">$xml_icon$term_uc</h3><table class=\"node_list\">$cells</table>"; }
    if ($result_count == 10) { $output .= "<span class=\"term_matches_more\"><a href=\"$term_url\">see all matches for $term_uc &gt;&gt;</a></span>"; }
  }
  
  return $output;
}

/**
 * Displays inline vertical or horizontal box on node content.
 */
function node_neighbors_box($nid, $vertical = 0) {
  $output  = NULL; // start the header for our vertical or horizontal box thing.
  $output .= $vertical ? '<div class="extras_vertical">' : '<table class="extras_horizontal"><tr><td class="left">';

  $terms = node_neighbors_taxonomy_node_get_terms($nid);
  $where = array(); foreach ($terms as $term) {
    if ($term->vid == 3) { continue; } // create a SQL WHERE clause
    $where[] = "tn1.tid = '$term->tid'"; // for this batch of terms.
  } $where = $where ? "(" . implode(' OR ', $where) . ")" : NULL;
  $except = " AND n.nid != '$nid' ";
  if (!$where) { return NULL; }

  // first up is three related news item, where the returned nodes
  // have to have "NHPR News" as one of their assigned terms. 
  $output .= "<h3 class\"term_matches_header\">Related news:</h3>";
  $result = db_query("SELECT DISTINCT(n.nid), n.title, numeric_data AS flexinode_1 ".
                     "FROM {node} n LEFT JOIN {flexinode_data} fd ON n.nid = fd.nid ". 
                     "LEFT JOIN {term_node} tn1 ON tn1.nid = n.nid LEFT JOIN {term_node} ".
                     "tn2 ON tn1.nid = tn2.nid AND tn2.tid = 15002 WHERE $where $except AND ".
                     "tn2.nid IS NOT NULL AND fd.field_id = 1 ORDER BY flexinode_1 DESC LIMIT 3");
  while ($node = db_fetch_object($result)) {
    $url    = url("node/$node->nid"); $date = format_date($node->flexinode_1, 'custom', 'l, F j, Y');
    $output .= "<p><span class=\"extras_date\">$date</span><br />";
    $output .= "<span class=\"extras_title\"><a href=\"$url\">$node->title</a></span></p>";
  }

  // any division for our horizontal box?
  $output .= $vertical ? NULL : '</td><td class="right">';

  // next are nodes that DO NOT have "NHPR News" as one of their terms.
  $output .= "<h3 class\"term_matches_header\">Related shows:</h3>";
  $result = db_query("SELECT DISTINCT(n.nid), n.title, numeric_data AS flexinode_1 ".
                     "FROM {node} n LEFT JOIN {flexinode_data} fd ON n.nid = fd.nid ". 
                     "LEFT JOIN {term_node} tn1 ON tn1.nid = n.nid LEFT JOIN {term_node} ".
                     "tn2 ON tn1.nid = tn2.nid AND tn2.tid = 15002 WHERE $where $except AND ".
                     "tn2.nid IS NULL AND fd.field_id = 1 ORDER BY flexinode_1 DESC LIMIT 3");
  while ($node = db_fetch_object($result)) {
    $url    = url("node/$node->nid"); $date = format_date($node->flexinode_1, 'custom', 'l, F j, Y');
    $output .= "<p><span class=\"extras_date\">$date</span><br />";
    $output .= "<span class=\"extras_title\"><a href=\"$url\">$node->title</a></span></p>";
  }
  
  $see_more_url = url("node_neighbors/$nid"); // oh, but there's plenty more!
  $see_more .= "<div style=\"text-align:right\"><a href=\"$see_more_url\">see more &gt;&gt;</a></div>";
  $output .= $vertical ? "$see_more</div>" : "</tr><tr><td colspan=\"2\">$see_more</td></tr></table>";
  return $output;
}

/**
 * Same as taxonomy_node_get_terms, only without weights.
 */
function node_neighbors_taxonomy_node_get_terms($nid, $key = 'tid') {
  static $terms;

  if (!isset($terms[$nid])) {
    $result = db_query('SELECT t.* FROM {term_node} r INNER JOIN {term_data} t '.
                       'ON r.tid = t.tid INNER JOIN {vocabulary} v ON t.vid = '.
                       'v.vid WHERE r.nid = %d ORDER BY t.name', $nid);
    $terms[$nid] = array();
    while ($term = db_fetch_object($result)) {
      $terms[$nid][$term->$key] = $term;
    }
  }
  return $terms[$nid];
} 

?>