From 07118dbd48fc805c1cc2f98b82dddb74734eaea2 Mon Sep 17 00:00:00 2001
From: Dries Buytaert <dries@buytaert.net>
Date: Sat, 3 Jun 2000 11:39:07 +0000
Subject: [PATCH] * Improved calendar.class.php speed-wise: did some inline
 caching to make   rendering a calendar less expensive. * Added a function
 displayCalendar($theme, $active_date) to functions.inc. * Adjusted index.php
 so it would support URIs formated like   "?date=$unix_timestamp". *
 Integrated the calendar in my personal theme: themes/Dries/theme.class.   If
 you want to include the calendar in your theme, check the code in my  
 $theme->footer(): global $date and call displayCalendar($theme, $date).

Check the main page at http://beta.drop.org/ with theme 'Dries' to see it
yourself!
---
 calendar.class.php | 67 ++++++++++++++++++----------------------------
 functions.inc      |  5 ++++
 index.php          | 13 ++++-----
 3 files changed, 38 insertions(+), 47 deletions(-)

diff --git a/calendar.class.php b/calendar.class.php
index fe13e11af04a..182c4bf36ab8 100644
--- a/calendar.class.php
+++ b/calendar.class.php
@@ -13,6 +13,7 @@ function display() {
     ### Extract information from the given date:
     $month  = date("n", $this->date);
     $year = date("Y", $this->date);
+    $day = date("d", $this->date);
 
     ### Extract first day of the month:
     $first = date("w", mktime(0, 0, 0, $month, 1, $year));
@@ -21,70 +22,54 @@ function display() {
     $last = date("t", mktime(0, 0, 0, $month, 1, $year));
 
     ### Calculate previous and next months dates:
-    $prev = mktime(0, 0, 0, $month - 1, 1, $year);
-    $next = mktime(0, 0, 0, $month + 1, 1, $year);
+    $prev = mktime(0, 0, 0, $month - 1, $day, $year);
+    $next = mktime(0, 0, 0, $month + 1, $day, $year);
 
     ### Generate calendar header:
-    print "<TABLE WIDTH=\"150\" BORDER=\"1\" CELLSPACING=\"0\" CELLPADDING=\"2\">";
-    print " <TR><TH COLSPAN=\"7\"><A HREF=\"$PHP_SELF?date=$prev\">&lt;&lt;</A> &nbsp; ". date("F Y", $this->date) ." &nbsp; <A HREF=\"$PHP_SELF?date=$next\">&gt;&gt;</A></TH></TR>";
-    print " <TR><TH>S</TH><TH>M</TH><TH>T</TH><TH>W</TH><TH>T</TH><TH>F</TH><TH>S</TH></TR>\n";
+    $output .= "<TABLE WIDTH=\"150\" BORDER=\"1\" CELLSPACING=\"0\" CELLPADDING=\"2\">";
+    $output .= " <TR><TH COLSPAN=\"7\"><A HREF=\"$PHP_SELF?date=$prev\">&lt;&lt;</A> &nbsp; ". date("F Y", $this->date) ." &nbsp; <A HREF=\"$PHP_SELF?date=$next\">&gt;&gt;</A></TH></TR>";
+    $output .= " <TR><TH>S</TH><TH>M</TH><TH>T</TH><TH>W</TH><TH>T</TH><TH>F</TH><TH>S</TH></TR>\n";
  
     ### Initialize temporary variables:
-    $day = 1;
-    $weekday = $first;
+    $nday = 1;
+    $sday = $first;
    
     ### Loop through all the days of the month:
-    while ($day <= $last) {
+    while ($nday <= $last) {
       ### Set up blank days for first week of the month:
       if ($first) {
-        print "<TR><TD COLSPAN=\"$first\">&nbsp</TD>";
+        $output .= "<TR><TD COLSPAN=\"$first\">&nbsp</TD>";
         $first = 0;
       }
         
       ### Start every week on a new line:
-      if ($weekday == 0) print "<TR>";
+      if ($sday == 0) $output .=  "<TR>";
     
       ### Print one cell:
-      $date = mktime(0, 0, 0, $month, $day, $year);
-      if ($day == date("d", $this->date)) {
-        print "<TD ALIGN=\"center\"><B>$day</B></TD>";
-      }
-      else {
-        print "<TD ALIGN=\"center\"><A HREF=\"$PHP_SELF?date=$date\">$day</A></TD>";
-      }
+      $date = mktime(0, 0, 0, $month, $nday, $year);
+      if ($nday > $day) $output .= "<TD ALIGN=\"center\">$nday</TD>";
+      else if ($nday == $day) $output .= "<TD ALIGN=\"center\"><B>$nday</B></TD>";
+      else $output .= "<TD ALIGN=\"center\"><A HREF=\"$PHP_SELF?date=$date\">$nday</A></TD>";
      
       ### Start every week on a new line:
-      if ($weekday == 6) print "</TR>";
+      if ($sday == 6) $output .=  "</TR>";
         
       ### Update temporary variables:
-      $weekday++;
-      $weekday = $weekday % 7;
-      $day++;
+      $sday++;
+      $sday = $sday % 7;
+      $nday++;
     }
     
     ### End the calendar:
-    if ($weekday != 0) {
-      $end = 7 - $weekday;
-      print "<TD COLSPAN=\"$end\">&nbsp;</TD></TR>";
+    if ($sday != 0) {
+      $end = 7 - $sday;
+      $output .= "<TD COLSPAN=\"$end\">&nbsp;</TD></TR>";
     }
-    print "</TABLE>";
+    $output .= "</TABLE>";
+
+    ### Return calendar:
+    return $output;
   }
 }
 
-
-
-// -----------------------------------------------------------------------
-// ---------- TEMPORARY CODE - should be removed after testing -----------
-// -----------------------------------------------------------------------
-
-print "<H1>CALENDAR TEST</H1>";
-
-// Code to initialize and display a calendar:
-if (!$date) $date = time();
-$calendar = new calendar($date);
-$calendar->display();
-
-// Debug output:
-print "<P><B>Selected date:</B><BR>". date("l, F d, Y", $date) ."</P>";
-
 ?>
diff --git a/functions.inc b/functions.inc
index fa7a3d15cb5e..423067bac4b2 100644
--- a/functions.inc
+++ b/functions.inc
@@ -169,6 +169,11 @@ function displayAccount($theme) {
   }
 }
 
+function displayCalendar($theme, $date) {
+  include "calendar.class.php";
+  $calendar = new calendar($date);
+  $theme->box("Browse archives", $calendar->display());
+}
 
 function displayAccountSettings($theme) {
   global $user;
diff --git a/index.php b/index.php
index 0f7bd7bf477f..9fde6521aa98 100644
--- a/index.php
+++ b/index.php
@@ -7,18 +7,20 @@
   addRefer($url);
 }
 
-
 include "theme.inc";
 $theme->header();
 
 dbconnect();
 
-if (isset($user->storynum)) $number = $user->storynum; else $number = 10;
+### Initialize variables:
+$number = ($user->storynum) ? $user->storynum : 10;
+$date = ($date) ? $date : time();
 
-$result = mysql_query("SELECT * FROM stories ORDER BY sid DESC LIMIT $number");
+### Perform query:
+$result = mysql_query("SELECT * FROM stories WHERE time <= $date ORDER BY sid DESC LIMIT $number");
 
+### Display stories:
 while ($story = mysql_fetch_object($result)) {
-
   ### Compose more-link:
   $morelink = "[ ";
   if ($story->article) {
@@ -28,16 +30,15 @@
     $bytes = strlen($story->article);
     $morelink .= "\"><FONT COLOR=\"$theme->hlcolor2\"><B>read more</B></FONT></A> | $bytes bytes in body | "; 
   }
-
   $query = mysql_query("SELECT sid FROM comments WHERE sid = $story->sid");
   if (!$query) { $count = 0; } else { $count = mysql_num_rows($query); }
-
   $morelink .= "<A HREF=\"article.php?sid=$story->sid";
   if (isset($user->umode)) { $morelink .= "&mode=$user->umode"; } else { $morelink .= "&mode=threaded"; }
   if (isset($user->uorder)) { $morelink .= "&order=$user->uorder"; } else { $morelink .= "&order=0"; }
   if (isset($user->thold)) { $morelink .= "&thold=$user->thold"; } else { $morelink .= "&thold=0"; }
   $morelink .= "\"><FONT COLOR=\"$theme->hlcolor2\">$count comments</FONT></A> ]";
 
+  ### Display story:
   $theme->abstract($story->aid, $story->informant, $story->time, stripslashes($story->subject), stripslashes($story->abstract), stripslashes($story->comments), $story->category, $story->department, $morelink);
 }
 
-- 
GitLab