summaryrefslogtreecommitdiff
path: root/modules/archive/archive.module
blob: 71aec57f61717de2a6a64d12f4e3217206417bae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
// $Id$

function archive_system($field){
  $system["description"] = t("Displays a calendar to navigation old content.");
  return $system[$field];
}

function archive_calendar($original = 0) {

  // Extract today's date:
  $today = mktime(23, 59, 59, date("n", time()), date("d", time()), date("Y", time()));

  // Extract the requested date:
  if (arg(0) == "archive" && arg(3)) {
    $year = arg(1);
    $month = arg(2);
    $day = arg(3);

    $requested = mktime(23, 59, 59, $month, $day, $year);
  }
  else {
    $year = date("Y", time());
    $month  = date("n", time());
    $day = date("d", time());

    $requested = $today;
  }

  // Extract first day of the month:
  $first = date("w", mktime(0, 0, 0, $month, 1, $year));

  // Extract last day of the month:
  $last = date("t", mktime(0, 0, 0, $month, 1, $year));

  // 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);

  // Generate calendar header:
  $output .= "\n<!-- calendar -->\n";
  $output .= "<table width=\"100%\" border=\"1\" cellspacing=\"0\" cellpadding=\"1\">\n";
  $output .= " <tr><td align=\"center\" colspan=\"7\"><small>". l("&lt;", "archive/". date("Y/m/d", $prev)) ." ". date("F Y", $requested) ." ". ($nextmonth <= time() ? l("&gt;", "archive/". date("Y/m/d", $next)) : "&gt;") ."</small></td></tr>\n";

  // 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;

  // Generate the days of the week:
  $firstcolumn = mktime(0, 0, 0, 3, 20 + $weekstart, 1994);

  $output .= " <tr>";
  for ($i = 0; $i < 7; $i++) {
    $output .= "<td align=\"center\"><small>". substr(ucfirst(t(date("l", $firstcolumn + $i * 86400))), 0, 1) ."</small></td>";
  }
  $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:
    if ($first != $weekstart) {
      $blankdays = ($first - $weekstart + 7) % 7;
      $output .= " <tr><td colspan=\"$blankdays\">&nbsp;</td>\n";
      $first = $weekstart;
    }

    // Start every week on a new line:
    if ($sday == $weekstart) {
      $output .=  " <tr>\n";
    }

    // Print one cell:
    $date = mktime(0, 0, 0, $month, $nday, $year);

    if ($date == $requested) {
      $output .= "  <td align=\"center\"><small><b>$nday</b></small></td>\n";
    }
    else if ($date > $today) {
      $output .= "  <td align=\"center\"><small>$nday</small></td>\n";
    }
    else {
      $output .= "  <td align=\"center\"><small>". l($nday, "archive/$year/$month/$nday", array("style" => "text-decoration: none;")) ."</small></td>\n";
    }

    // Start every week on a new line:
    if ($sday == $lastday) {
      $output .=  " </tr>\n";
    }

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

  // Complete the calendar:
  if ($sday != $weekstart) {
    $end = (7 - $sday + $weekstart) % 7;
    $output .= "  <td colspan=\"$end\">&nbsp;</td>\n </tr>\n";
  }

  $output .= "</table>\n\n";

  return $output;
}

function archive_block($op = "list", $delta = 0) {
  global $date;
  if ($op == "list") {
    $blocks[0]["info"] = t("Calendar to browse archives");
    return $blocks;
  }
  else {
    switch ($delta) {
      case 0:
        $block["subject"] = t("Browse archives");
        $block["content"] = archive_calendar();
        return $block;
    }
  }
}

function archive_link($type) {

  $links = array();

  if ($type == "page" && user_access("access content")) {
    $links[] = l(t("archives"), "archive", array("title" => t("Read the older content in our archive.")));
  }

  return $links;
}

function archive_page() {
  global $date, $edit, $op, $month, $year, $meta;

  theme("header");

  if (user_access("access content")) {
    if ($op == t("Show")) {
      $year = $edit["year"];
      $month = $edit["month"];
      $day = $edit["day"];
    }
    else {
      $year = arg(1);
      $month = arg(2);
      $day = arg(3);
    }

    $date = mktime(0, 0, 0, $month, $day, $year);

    /*
    ** Prepare the values of the form fields:
    */

    $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;

    $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 = ereg_replace("<[/]?p>", "", $start);

    theme("box", t("Archives"), form($start));

    /*
    ** Fetch nodes for the selected date, or current date if none
    ** selected.
    */

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

      while ($nid = db_fetch_object($result)) {
        node_view(node_load(array("nid" => $nid->nid)), 1);
      }
    }
  }
  else {
    message_access();
  }

  theme("footer");
}

function archive_settings() {

  $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;
}

?>