2002-01-31 20:28:39 +00:00
<?php
// $Id$
2003-08-05 18:33:39 +00:00
function archive_help($section) {
$output = "";
switch ($section) {
2003-10-07 18:16:41 +00:00
case 'admin/system/modules#description':
2003-10-03 14:55:27 +00:00
$output = t("Displays a calendar to navigate old content.");
2003-08-05 18:33:39 +00:00
break;
case 'admin/system/modules/archive':
2003-10-03 14:55:27 +00:00
$output = t("Choose the starting \"day of the week\" for the displayed calendar block.");
2003-08-05 18:33:39 +00:00
break;
}
2003-10-03 14:55:27 +00:00
return $output;
2003-08-05 18:33:39 +00:00
}
2002-06-01 21:57:29 +00:00
2003-01-06 19:51:01 +00:00
function archive_calendar($original = 0) {
2003-06-22 08:46:34 +00:00
global $user;
2003-06-07 06:08:52 +00:00
$edit = $_POST["edit"];
2002-01-31 20:28:39 +00:00
2003-01-06 19:51:01 +00:00
// Extract today's date:
2003-06-22 08:46:34 +00:00
$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;
2002-01-31 20:28:39 +00:00
2003-01-06 19:51:01 +00:00
// Extract the requested date:
2003-06-07 06:08:52 +00:00
if ($edit["year"] && $edit["month"] && $edit["day"]) {
$year = $edit["year"];
$month = $edit["month"];
$day = $edit["day"];
2003-06-22 08:46:34 +00:00
$requested = mktime(0, 0, 0, $month, $day, $year) + $user->timezone;
2003-06-07 06:08:52 +00:00
}
else if (arg(0) == "archive" && arg(3)) {
2003-01-06 19:51:01 +00:00
$year = arg(1);
$month = arg(2);
$day = arg(3);
2002-01-31 20:28:39 +00:00
2003-06-22 08:46:34 +00:00
$requested = mktime(0, 0, 0, $month, $day, $year) + $user->timezone;
2003-01-06 19:51:01 +00:00
}
else {
$year = date("Y", time());
$month = date("n", time());
$day = date("d", time());
2002-01-31 20:28:39 +00:00
2003-06-22 08:46:34 +00:00
$requested = $end_of_today + $user->timezone;
2003-01-06 19:51:01 +00:00
}
2002-01-31 20:28:39 +00:00
2003-06-05 21:34:56 +00:00
$start_of_month = mktime(0, 0, 0, $month, 1, $year);
2002-01-31 20:28:39 +00:00
// Extract first day of the month:
2003-06-05 21:34:56 +00:00
$first = date("w", $start_of_month);
2002-01-31 20:28:39 +00:00
// Extract last day of the month:
2003-06-05 21:34:56 +00:00
$last = date("t", $start_of_month);
2003-10-07 18:16:41 +00:00
$end_of_month = mktime(23, 59, 59, $month, $last, $year);
2003-06-05 21:34:56 +00:00
$cache = cache_get("archive:calendar:$start_of_month");
if (!empty($cache)) {
return $cache->data;
}
2002-01-31 20:28:39 +00:00
// 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);
2003-06-05 21:34:56 +00:00
2003-07-10 17:46:44 +00:00
$result = db_query("SELECT created FROM {node} WHERE status = 1 AND created > $start_of_month AND created < $end_of_month");
2003-06-05 21:34:56 +00:00
$days_with_posts = array();
while ($day_with_post = db_fetch_object($result)) {
2003-06-22 08:46:34 +00:00
$days_with_posts[] = date("j", $day_with_post->created + $user->timezone);
2003-06-05 21:34:56 +00:00
}
$days_with_posts = array_unique($days_with_posts);
2002-01-31 20:28:39 +00:00
// Generate calendar header:
$output .= "\n<!-- calendar -->\n";
2003-05-31 12:42:02 +00:00
$output .= "<div class=\"calendar\">";
2003-06-08 19:26:26 +00:00
$output .= "<table>\n";
2003-11-09 01:22:40 +00:00
$output .= " <tr><td colspan=\"7\" class=\"header-month\">". l("«", "archive/". date("Y/m/d", $prev)) ." ". t(date("F", $requested)) . date(" Y", $requested) ." ". ($nextmonth <= time() ? l("»", "archive/". date("Y/m/d", $next)) : " ") ."</td></tr>\n";
2003-01-21 22:50:03 +00:00
// First day of week (0 => Sunday, 1 => Monday, ...)
$weekstart = variable_get("default_firstday", 0);
// Last day of week
2003-10-07 18:16:41 +00:00
($weekstart - 1 == -1) ? $lastday = 6 : $lastday = $weekstart - 1;
2002-01-31 20:28:39 +00:00
// Generate the days of the week:
2003-01-21 22:50:03 +00:00
$firstcolumn = mktime(0, 0, 0, 3, 20 + $weekstart, 1994);
2002-01-31 20:28:39 +00:00
2003-05-31 12:42:02 +00:00
$output .= " <tr class=\"header-week\">";
2002-01-31 20:28:39 +00:00
for ($i = 0; $i < 7; $i++) {
2003-06-27 17:48:20 +00:00
$output .= "<td>". t(substr(ucfirst(date("l", $firstcolumn + $i * 86400)), 0, 2)) ."</td>";
2002-01-31 20:28:39 +00:00
}
$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:
2003-01-21 22:50:03 +00:00
if ($first != $weekstart) {
$blankdays = ($first - $weekstart + 7) % 7;
2003-05-31 12:42:02 +00:00
$output .= " <tr class=\"row-week\"><td class=\"day-blank\" colspan=\"$blankdays\"> </td>\n";
2003-01-21 22:50:03 +00:00
$first = $weekstart;
2002-01-31 20:28:39 +00:00
}
// Start every week on a new line:
2003-01-21 22:50:03 +00:00
if ($sday == $weekstart) {
2003-06-08 19:26:26 +00:00
$output .= " <tr class=\"row-week\">\n";
2002-01-31 20:28:39 +00:00
}
// Print one cell:
2003-06-22 08:46:34 +00:00
$date = mktime(0, 0, 0, $month, $nday, $year) + $user->timezone;
2003-06-05 21:34:56 +00:00
if (in_array($nday, $days_with_posts)) {
$daytext = l($nday, "archive/$year/$month/$nday");
2003-06-16 17:17:51 +00:00
$dayclass = "day-link";
2003-06-05 21:34:56 +00:00
}
else {
2003-06-08 19:26:26 +00:00
$daytext = "<div>$nday</div>";
2003-06-16 17:17:51 +00:00
$dayclass = "day-normal";
2003-06-05 21:34:56 +00:00
}
2003-01-06 19:51:01 +00:00
if ($date == $requested) {
2003-06-05 21:34:56 +00:00
$output .= " <td class=\"day-selected\">$daytext</td>\n";
2003-05-31 12:42:02 +00:00
}
else if ($date == $start_of_today) {
2003-06-05 21:34:56 +00:00
$output .= " <td class=\"day-today\">$daytext</td>\n";
2002-01-31 20:28:39 +00:00
}
2003-05-31 12:42:02 +00:00
else if ($date > $end_of_today) {
2003-06-05 21:34:56 +00:00
$output .= " <td class=\"day-future\">$daytext</td>\n";
2002-01-31 20:28:39 +00:00
}
else {
2003-06-16 17:17:51 +00:00
$output .= " <td class=\"$dayclass\">$daytext</td>\n";
2002-01-31 20:28:39 +00:00
}
// Start every week on a new line:
2003-01-21 22:50:03 +00:00
if ($sday == $lastday) {
2002-01-31 20:28:39 +00:00
$output .= " </tr>\n";
}
// Update temporary variables:
$sday++;
$sday = $sday % 7;
$nday++;
}
// Complete the calendar:
2003-01-21 22:50:03 +00:00
if ($sday != $weekstart) {
$end = (7 - $sday + $weekstart) % 7;
2003-05-31 12:42:02 +00:00
$output .= " <td class=\"day-blank\" colspan=\"$end\"> </td>\n </tr>\n";
2002-01-31 20:28:39 +00:00
}
2003-05-31 12:42:02 +00:00
$output .= "</table></div>\n\n";
2002-01-31 20:28:39 +00:00
2003-07-23 18:33:12 +00:00
cache_set("archive:calendar:$start_of_month", $output, 1);
2003-06-05 21:34:56 +00:00
2002-01-31 20:28:39 +00:00
return $output;
}
2002-10-26 15:17:26 +00:00
function archive_block($op = "list", $delta = 0) {
2002-01-31 20:28:39 +00:00
global $date;
2002-10-26 15:17:26 +00:00
if ($op == "list") {
$blocks[0]["info"] = t("Calendar to browse archives");
return $blocks;
}
2003-07-01 21:45:25 +00:00
else if (user_access("access content")) {
2002-10-26 15:17:26 +00:00
switch ($delta) {
case 0:
$block["subject"] = t("Browse archives");
2003-01-06 19:51:01 +00:00
$block["content"] = archive_calendar();
2002-10-26 15:17:26 +00:00
return $block;
}
}
2002-01-31 20:28:39 +00:00
}
function archive_link($type) {
2003-04-21 14:55:03 +00:00
$links = array();
2002-01-31 20:28:39 +00:00
if ($type == "page" && user_access("access content")) {
2003-01-06 19:51:01 +00:00
$links[] = l(t("archives"), "archive", array("title" => t("Read the older content in our archive.")));
2002-01-31 20:28:39 +00:00
}
2003-11-20 21:51:23 +00:00
if ($type == "system") {
if (user_access("access content")) {
2003-12-17 22:27:23 +00:00
menu("archive", t("archives"), "archive_page", 0, MENU_HIDE);
2003-11-20 21:51:23 +00:00
}
}
2003-04-21 14:55:03 +00:00
return $links;
2002-01-31 20:28:39 +00:00
}
function archive_page() {
2003-06-22 08:46:34 +00:00
global $date, $month, $year, $meta, $user;
2003-05-13 18:36:38 +00:00
$op = $_POST["op"];
$edit = $_POST["edit"];
2002-01-31 20:28:39 +00:00
if (user_access("access content")) {
2003-01-06 19:51:01 +00:00
if ($op == t("Show")) {
$year = $edit["year"];
$month = $edit["month"];
$day = $edit["day"];
}
else {
$year = arg(1);
$month = arg(2);
$day = arg(3);
}
2002-01-31 20:28:39 +00:00
2003-06-22 08:46:34 +00:00
$date = mktime(0, 0, 0, $month, $day, $year) - $user->timezone;
2003-12-29 12:41:58 +00:00
$date_end = mktime(0, 0, 0, $month, $day + 1, $year) - $user->timezone;
2002-01-31 20:28:39 +00:00
2003-01-06 19:51:01 +00:00
/*
** Prepare the values of the form fields:
*/
2002-01-31 20:28:39 +00:00
2003-01-06 19:51:01 +00:00
$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;
2002-01-31 20:28:39 +00:00
2003-05-29 15:36:17 +00:00
$start = "<div class=\"container-inline\">";
2003-05-29 10:18:38 +00:00
$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>";
2003-11-25 19:26:21 +00:00
$output .= form($start);
2002-01-31 20:28:39 +00:00
2003-01-06 19:51:01 +00:00
/*
** Fetch nodes for the selected date, or current date if none
** selected.
*/
2002-01-31 20:28:39 +00:00
2003-01-06 19:51:01 +00:00
if ($year && $month && $day) {
2003-12-29 12:41:58 +00:00
$result = db_query_range("SELECT nid FROM {node} WHERE status = '1' AND created > %d AND created < %d ORDER BY created", $date, $date_end, 0, 20);
2002-01-31 20:28:39 +00:00
2003-01-06 19:51:01 +00:00
while ($nid = db_fetch_object($result)) {
2003-11-25 19:26:21 +00:00
$output .= node_view(node_load(array("nid" => $nid->nid)), 1);
2002-01-31 20:28:39 +00:00
}
2003-01-06 19:51:01 +00:00
}
2003-11-25 19:26:21 +00:00
print theme("page", $output);
2002-01-31 20:28:39 +00:00
}
else {
2003-11-25 19:26:21 +00:00
print theme("page", message_access());
2002-01-31 20:28:39 +00:00
}
}
2003-02-11 20:01:17 +00:00
function archive_settings() {
2003-01-21 22:50:03 +00:00
2003-12-28 10:47:33 +00:00
$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."));
2003-01-21 22:50:03 +00:00
return $output;
}
?>