archive.module 7.58 KB
Newer Older
Dries's avatar
 
Dries committed
1 2 3
<?php
// $Id$

4
function archive_system($field){
Dries's avatar
 
Dries committed
5
  $system["description"] = t("Displays a calendar to navigate old content.");
Dries's avatar
 
Dries committed
6
  $system["admin_help"] = t("Choose the starting \"day of the week\" for the displayed calendar block.");
7 8 9
  return $system[$field];
}

Dries's avatar
 
Dries committed
10
function archive_calendar($original = 0) {
Dries's avatar
 
Dries committed
11
  global $user;
Dries's avatar
 
Dries committed
12
  $edit = $_POST["edit"];
Dries's avatar
 
Dries committed
13

Dries's avatar
 
Dries committed
14
  // Extract today's date:
Dries's avatar
 
Dries committed
15 16 17
  $offset = time() + $user->timezone;
  $start_of_today = mktime(0, 0, 0, date("n", $offset), date("d", $offset), date("Y", $offset)) + $user->timezone;
  $end_of_today = mktime(23, 59, 59, date("n", $offset), date("d", $offset), date("Y", $offset)) + $user->timezone;
Dries's avatar
 
Dries committed
18

Dries's avatar
 
Dries committed
19
  // Extract the requested date:
Dries's avatar
 
Dries committed
20 21 22 23 24
  if ($edit["year"] && $edit["month"] && $edit["day"]) {
    $year = $edit["year"];
    $month = $edit["month"];
    $day = $edit["day"];

Dries's avatar
 
Dries committed
25
    $requested = mktime(0, 0, 0, $month, $day, $year) + $user->timezone;
Dries's avatar
 
Dries committed
26 27
  }
  else if (arg(0) == "archive" && arg(3)) {
Dries's avatar
 
Dries committed
28 29 30
    $year = arg(1);
    $month = arg(2);
    $day = arg(3);
Dries's avatar
 
Dries committed
31

Dries's avatar
 
Dries committed
32
    $requested = mktime(0, 0, 0, $month, $day, $year) + $user->timezone;
Dries's avatar
 
Dries committed
33 34 35 36 37
  }
  else {
    $year = date("Y", time());
    $month  = date("n", time());
    $day = date("d", time());
Dries's avatar
 
Dries committed
38

Dries's avatar
 
Dries committed
39
    $requested = $end_of_today + $user->timezone;
Dries's avatar
 
Dries committed
40
  }
Dries's avatar
 
Dries committed
41

Dries's avatar
 
Dries committed
42 43
  $start_of_month = mktime(0, 0, 0, $month, 1, $year);

Dries's avatar
 
Dries committed
44
  // Extract first day of the month:
Dries's avatar
 
Dries committed
45
  $first = date("w", $start_of_month);
Dries's avatar
 
Dries committed
46 47

  // Extract last day of the month:
Dries's avatar
 
Dries committed
48 49 50 51 52 53 54 55 56
  $last = date("t", $start_of_month);

  $end_of_month = mktime(23,59,59, $month, $last, $year);

  $cache = cache_get("archive:calendar:$start_of_month");

  if (!empty($cache)) {
    return $cache->data;
  }
Dries's avatar
 
Dries committed
57 58 59 60 61 62 63

  // Calculate previous and next months dates and check for shorter months (28/30 days)
  $prevmonth = mktime(23, 59, 59, $month - 1, 1, $year);
  $prev = mktime(23, 59, 59, $month - 1, min(date("t", $prevmonth), $day), $year);
  $nextmonth = mktime(23, 59, 59, $month + 1, 1, $year);
  $next = mktime(23, 59, 59, $month + 1, min(date("t", $nextmonth), $day), $year);

Dries's avatar
 
Dries committed
64

Dries's avatar
 
Dries committed
65
  $result = db_query("SELECT created FROM {node} WHERE status = 1 AND created > $start_of_month AND created < $end_of_month");
Dries's avatar
 
Dries committed
66 67 68

  $days_with_posts = array();
  while ($day_with_post = db_fetch_object($result)) {
Dries's avatar
 
Dries committed
69
    $days_with_posts[] = date("j", $day_with_post->created + $user->timezone);
Dries's avatar
 
Dries committed
70 71 72
  }
  $days_with_posts = array_unique($days_with_posts);

Dries's avatar
 
Dries committed
73 74
  // Generate calendar header:
  $output .= "\n<!-- calendar -->\n";
Dries's avatar
 
Dries committed
75
  $output .= "<div class=\"calendar\">";
Dries's avatar
 
Dries committed
76
  $output .= "<table>\n";
Dries's avatar
 
Dries committed
77
  $output .= " <tr><td colspan=\"7\" class=\"header-month\">". l("&laquo;", "archive/". date("Y/m/d", $prev)) ." ". date("F Y", $requested) ." ". ($nextmonth <= time() ? l("&raquo;", "archive/". date("Y/m/d", $next)) : "&nbsp;") ."</td></tr>\n";
Dries's avatar
 
Dries committed
78 79 80 81 82 83

  // First day of week (0 => Sunday, 1 => Monday, ...)
  $weekstart = variable_get("default_firstday", 0);

  // Last day of week
  ($weekstart - 1 == -1 ) ? $lastday = 6 : $lastday = $weekstart - 1;
Dries's avatar
 
Dries committed
84 85

  // Generate the days of the week:
Dries's avatar
 
Dries committed
86
  $firstcolumn = mktime(0, 0, 0, 3, 20 + $weekstart, 1994);
Dries's avatar
 
Dries committed
87

Dries's avatar
 
Dries committed
88
  $output .= " <tr class=\"header-week\">";
Dries's avatar
 
Dries committed
89
  for ($i = 0; $i < 7; $i++) {
90
    $output .= "<td>". t(substr(ucfirst(date("l", $firstcolumn + $i * 86400)), 0, 2)) ."</td>";
Dries's avatar
 
Dries committed
91 92 93 94 95 96 97 98 99 100
  }
  $output .= "</tr>\n";

  // Initialize temporary variables:
  $nday = 1;
  $sday = $first;

  // Loop through all the days of the month:
  while ($nday <= $last) {
    // Set up blank days for first week of the month:
Dries's avatar
 
Dries committed
101 102
    if ($first != $weekstart) {
      $blankdays = ($first - $weekstart + 7) % 7;
Dries's avatar
 
Dries committed
103
      $output .= " <tr class=\"row-week\"><td class=\"day-blank\" colspan=\"$blankdays\">&nbsp;</td>\n";
Dries's avatar
 
Dries committed
104
      $first = $weekstart;
Dries's avatar
 
Dries committed
105 106
    }
    // Start every week on a new line:
Dries's avatar
 
Dries committed
107
    if ($sday == $weekstart) {
Dries's avatar
 
Dries committed
108
      $output .= " <tr class=\"row-week\">\n";
Dries's avatar
 
Dries committed
109 110 111
    }

    // Print one cell:
Dries's avatar
 
Dries committed
112
    $date = mktime(0, 0, 0, $month, $nday, $year) + $user->timezone;
Dries's avatar
 
Dries committed
113 114
    if (in_array($nday, $days_with_posts)) {
      $daytext = l($nday, "archive/$year/$month/$nday");
Dries's avatar
 
Dries committed
115
      $dayclass = "day-link";
Dries's avatar
 
Dries committed
116 117
    }
    else {
Dries's avatar
 
Dries committed
118
      $daytext = "<div>$nday</div>";
Dries's avatar
 
Dries committed
119
      $dayclass = "day-normal";
Dries's avatar
 
Dries committed
120
    }
Dries's avatar
 
Dries committed
121
    if ($date == $requested) {
Dries's avatar
 
Dries committed
122
      $output .= "  <td class=\"day-selected\">$daytext</td>\n";
Dries's avatar
 
Dries committed
123 124
    }
    else if ($date == $start_of_today) {
Dries's avatar
 
Dries committed
125
      $output .= "  <td class=\"day-today\">$daytext</td>\n";
Dries's avatar
 
Dries committed
126
    }
Dries's avatar
 
Dries committed
127
    else if ($date > $end_of_today) {
Dries's avatar
 
Dries committed
128
      $output .= "  <td class=\"day-future\">$daytext</td>\n";
Dries's avatar
 
Dries committed
129 130
    }
    else {
Dries's avatar
 
Dries committed
131
      $output .= "  <td class=\"$dayclass\">$daytext</td>\n";
Dries's avatar
 
Dries committed
132 133 134
    }

    // Start every week on a new line:
Dries's avatar
 
Dries committed
135
    if ($sday == $lastday) {
Dries's avatar
 
Dries committed
136 137 138 139 140 141 142 143 144 145
      $output .=  " </tr>\n";
    }

    // Update temporary variables:
    $sday++;
    $sday = $sday % 7;
    $nday++;
  }

  // Complete the calendar:
Dries's avatar
 
Dries committed
146 147
  if ($sday != $weekstart) {
    $end = (7 - $sday + $weekstart) % 7;
Dries's avatar
 
Dries committed
148
    $output .= "  <td class=\"day-blank\" colspan=\"$end\">&nbsp;</td>\n </tr>\n";
Dries's avatar
 
Dries committed
149 150
  }

Dries's avatar
 
Dries committed
151
  $output .= "</table></div>\n\n";
Dries's avatar
 
Dries committed
152

Dries's avatar
 
Dries committed
153
  cache_set("archive:calendar:$start_of_month", $output, time() + variable_get("cache_clear", 120));
Dries's avatar
 
Dries committed
154

Dries's avatar
 
Dries committed
155 156 157
  return $output;
}

Dries's avatar
 
Dries committed
158
function archive_block($op = "list", $delta = 0) {
Dries's avatar
 
Dries committed
159
  global $date;
Dries's avatar
 
Dries committed
160 161 162 163
  if ($op == "list") {
    $blocks[0]["info"] = t("Calendar to browse archives");
    return $blocks;
  }
Dries's avatar
 
Dries committed
164
  else if (user_access("access content")) {
Dries's avatar
 
Dries committed
165 166 167
    switch ($delta) {
      case 0:
        $block["subject"] = t("Browse archives");
Dries's avatar
 
Dries committed
168
        $block["content"] = archive_calendar();
Dries's avatar
 
Dries committed
169 170 171
        return $block;
    }
  }
Dries's avatar
 
Dries committed
172 173 174
}

function archive_link($type) {
Dries's avatar
 
Dries committed
175 176 177

  $links = array();

Dries's avatar
 
Dries committed
178
  if ($type == "page" && user_access("access content")) {
Dries's avatar
 
Dries committed
179
    $links[] = l(t("archives"), "archive", array("title" => t("Read the older content in our archive.")));
Dries's avatar
 
Dries committed
180 181
  }

Dries's avatar
 
Dries committed
182
  return $links;
Dries's avatar
 
Dries committed
183 184 185
}

function archive_page() {
Dries's avatar
 
Dries committed
186
  global $date, $month, $year, $meta, $user;
Dries's avatar
 
Dries committed
187 188 189

  $op = $_POST["op"];
  $edit = $_POST["edit"];
Dries's avatar
 
Dries committed
190

Dries's avatar
 
Dries committed
191
  theme("header");
Dries's avatar
 
Dries committed
192 193

  if (user_access("access content")) {
Dries's avatar
 
Dries committed
194 195 196 197 198 199 200 201 202 203
    if ($op == t("Show")) {
      $year = $edit["year"];
      $month = $edit["month"];
      $day = $edit["day"];
    }
    else {
      $year = arg(1);
      $month = arg(2);
      $day = arg(3);
    }
Dries's avatar
 
Dries committed
204

Dries's avatar
 
Dries committed
205
    $date = mktime(0, 0, 0, $month, $day, $year) - $user->timezone;
Dries's avatar
 
Dries committed
206

Dries's avatar
 
Dries committed
207 208 209
    /*
    ** Prepare the values of the form fields:
    */
Dries's avatar
 
Dries committed
210

Dries's avatar
 
Dries committed
211 212 213
    $years = array(2000 => "2000", 2001 => "2001", 2002 => "2002", 2003 => "2003", 2004 => "2004", 2005 => "2005");
    $months = array(1 => t("January"), 2 => t("February"), 3 => t("March"), 4 => t("April"), 5 => t("May"), 6 => t("June"), 7 => t("July"), 8 => t("August"), 9 => t("September"), 10 => t("October"), 11 => t("November"), 12 => t("December"));
    for ($i = 1; $i <= 31; $i++) $days[$i] = $i;
Dries's avatar
 
Dries committed
214

Dries's avatar
 
Dries committed
215
    $start = "<div class=\"container-inline\">";
Dries's avatar
 
Dries committed
216 217
    $start .= form_select("", "year", ($year ? $year : date("Y")), $years). form_select("", "month", ($month ? $month : date("m")), $months) . form_select("", "day", ($day ? $day : date("d")), $days) . form_submit(t("Show"));
    $start .= "</div>";
Dries's avatar
 
Dries committed
218
    theme("box", t("Archives"), form($start));
Dries's avatar
 
Dries committed
219

Dries's avatar
 
Dries committed
220 221 222 223
    /*
    ** Fetch nodes for the selected date, or current date if none
    ** selected.
    */
Dries's avatar
 
Dries committed
224

Dries's avatar
 
Dries committed
225
    if ($year && $month && $day) {
Dries's avatar
 
Dries committed
226
      $result = db_query_range("SELECT nid FROM {node} WHERE status = '1' AND created > %d ORDER BY created", $date, 0, 20);
Dries's avatar
 
Dries committed
227

Dries's avatar
 
Dries committed
228 229
      while ($nid = db_fetch_object($result)) {
        node_view(node_load(array("nid" => $nid->nid)), 1);
Dries's avatar
 
Dries committed
230
      }
Dries's avatar
 
Dries committed
231
    }
Dries's avatar
 
Dries committed
232 233
  }
  else {
Dries's avatar
 
Dries committed
234
    message_access();
Dries's avatar
 
Dries committed
235 236
  }

Dries's avatar
 
Dries committed
237
  theme("footer");
Dries's avatar
 
Dries committed
238 239
}

240
function archive_settings() {
Dries's avatar
 
Dries committed
241 242 243 244 245 246 247

  $output .= form_select( t("First day of week"), "default_firstday", variable_get("default_firstday", 0), array(0 => t("Sunday"), 1 => t("Monday"), 2 => t("Tuesday"), 3 => t("Wednesday"), 4 => t("Thursday"), 5 => t("Friday"), 6 => t("Saturday")), t("The first day of the week.  By changing this value you choose how the calendar block is rendered."));

  return $output;
}

?>