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

Dries's avatar
 
Dries committed
4 5 6 7 8
/**
 * @file
 * Displays a calendar to navigate old content.
 */

Dries's avatar
 
Dries committed
9 10 11
/**
 * Implementation of hook_help().
 */
Dries's avatar
 
Dries committed
12 13
function archive_help($section) {
  switch ($section) {
Dries's avatar
 
Dries committed
14
    case 'admin/modules#description':
Dries's avatar
 
Dries committed
15
      return t('Displays a calendar to navigate old content.');
Dries's avatar
 
Dries committed
16 17
  }
}
18

Dries's avatar
 
Dries committed
19
/**
Dries's avatar
 
Dries committed
20
 * Generates a monthly calendar, for display in the archive block.
Dries's avatar
 
Dries committed
21
 */
Dries's avatar
 
Dries committed
22
function archive_calendar($original = 0) {
Dries's avatar
 
Dries committed
23
  global $user;
Dries's avatar
 
Dries committed
24
  $edit = $_POST['edit'];
Dries's avatar
 
Dries committed
25

Dries's avatar
 
Dries committed
26
  // Extract today's date:
Dries's avatar
 
Dries committed
27 28
  $start_of_today = mktime(0, 0, 0, date('n', time()), date('d', time()), date('Y', time())) + $user->timezone;
  $end_of_today = mktime(23, 59, 59, date('n', time()), date('d', time()), date('Y', time())) + $user->timezone;
Dries's avatar
 
Dries committed
29

Dries's avatar
 
Dries committed
30
  // Extract the requested date:
Dries's avatar
 
Dries committed
31 32 33 34
  if ($edit['year'] && $edit['month'] && $edit['day']) {
    $year = $edit['year'];
    $month = $edit['month'];
    $day = $edit['day'];
Dries's avatar
 
Dries committed
35

Dries's avatar
 
Dries committed
36
    $requested = mktime(0, 0, 0, $month, $day, $year) + $user->timezone;
Dries's avatar
 
Dries committed
37
  }
Dries's avatar
 
Dries committed
38
  else if (arg(0) == 'archive' && arg(3)) {
Dries's avatar
 
Dries committed
39 40 41
    $year = arg(1);
    $month = arg(2);
    $day = arg(3);
Dries's avatar
 
Dries committed
42

Dries's avatar
 
Dries committed
43
    $requested = mktime(0, 0, 0, $month, $day, $year) + $user->timezone;
Dries's avatar
 
Dries committed
44 45
  }
  else {
Dries's avatar
 
Dries committed
46 47 48
    $year = date('Y', time());
    $month  = date('n', time());
    $day = date('d', time());
Dries's avatar
 
Dries committed
49

Dries's avatar
 
Dries committed
50
    $requested = $end_of_today + $user->timezone;
Dries's avatar
 
Dries committed
51
  }
Dries's avatar
 
Dries committed
52

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

Dries's avatar
 
Dries committed
55
  // Extract first day of the month:
Dries's avatar
 
Dries committed
56
  $first = date('w', $start_of_month);
Dries's avatar
 
Dries committed
57 58

  // Extract last day of the month:
Dries's avatar
 
Dries committed
59
  $last = date('t', $start_of_month);
Dries's avatar
 
Dries committed
60

Dries's avatar
 
Dries committed
61
  $end_of_month = mktime(23, 59, 59, $month, $last, $year);
Dries's avatar
 
Dries committed
62

Dries's avatar
 
Dries committed
63
  $cache = cache_get("archive:calendar:$day-$month-$year");
Dries's avatar
 
Dries committed
64 65 66 67

  if (!empty($cache)) {
    return $cache->data;
  }
Dries's avatar
 
Dries committed
68 69 70

  // Calculate previous and next months dates and check for shorter months (28/30 days)
  $prevmonth = mktime(23, 59, 59, $month - 1, 1, $year);
Dries's avatar
 
Dries committed
71
  $prev = mktime(23, 59, 59, $month - 1, min(date('t', $prevmonth), $day), $year);
Dries's avatar
 
Dries committed
72
  $nextmonth = mktime(23, 59, 59, $month + 1, 1, $year);
Dries's avatar
 
Dries committed
73
  $next = mktime(23, 59, 59, $month + 1, min(date('t', $nextmonth), $day), $year);
Dries's avatar
 
Dries committed
74

Dries's avatar
 
Dries committed
75 76 77
  $sql = 'SELECT n.nid, n.created FROM {node} n WHERE n.status = 1 AND n.created > %d AND n.created < %d ORDER BY n.created';
  $sql = node_rewrite_sql($sql);
  $result = db_query($sql, $start_of_month, $end_of_month);
Dries's avatar
 
Dries committed
78 79 80

  $days_with_posts = array();
  while ($day_with_post = db_fetch_object($result)) {
81 82 83 84 85 86 87
    $daynum = date('j', $day_with_post->created + $user->timezone);
    if (isset($days_with_posts[$daynum])) {
      $days_with_posts[$daynum]++;
    }
    else {
      $days_with_posts[$daynum] = 1;
    }
Dries's avatar
 
Dries committed
88 89
  }

Dries's avatar
 
Dries committed
90 91
  // Generate calendar header:
  $output .= "\n<!-- calendar -->\n";
Dries's avatar
 
Dries committed
92 93 94
  $output .= '<div class="calendar">';
  $output .= '<table summary="'. t('A calendar to browse the archives') .".\">\n";
  $output .= ' <caption>'. l('&laquo;', 'archive/'. date('Y/m/d', $prev), array('title' => t('Previous month'))) .' '. format_date($requested, 'custom', 'F') . date(' Y', $requested) .' '. ($nextmonth <= time() ? l('&raquo;', 'archive/'. date('Y/m/d', $next), array('title' => t('Next month'))) : '&nbsp;') ."</caption>\n";
Dries's avatar
 
Dries committed
95 96

  // First day of week (0 => Sunday, 1 => Monday, ...)
97
  $weekstart = variable_get('date_first_day', 0);
Dries's avatar
 
Dries committed
98 99

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

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

Dries's avatar
 
Dries committed
105
  $output .= " <tr class=\"header-week\">\n";
106
  $days = array(t('Sunday') => t('Su'), t('Monday') => t('Mo'), t('Tuesday') => t('Tu'), t('Wednesday') => t('We'), t('Thursday') => t('Th'), t('Friday') => t('Fr'), t('Saturday') => t('Sa'));
Dries's avatar
 
Dries committed
107 108 109
  if ($weekstart) {
    $days = array_merge(array_slice($days, $weekstart), array_slice($days, 0, $weekstart));
  }
Dries's avatar
 
Dries committed
110

111
  foreach ($days as $fullname => $name) {
Dries's avatar
 
Dries committed
112
    $output .= ' <th abbr="'. $fullname .'">'. $name . "</th>\n";
Dries's avatar
 
Dries committed
113 114 115 116 117 118 119 120 121
  }
  $output .= "</tr>\n";

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

  // Loop through all the days of the month:
  while ($nday <= $last) {
Dries's avatar
 
Dries committed
122
    // Set up blank days for first week of the month (allowing individual blank day styling):
Dries's avatar
 
Dries committed
123 124
    if ($first != $weekstart) {
      $blankdays = ($first - $weekstart + 7) % 7;
Dries's avatar
 
Dries committed
125
      $output .= " <tr class=\"row-week\">" . str_repeat("<td class=\"day-blank\">&nbsp;</td>\n", $blankdays);
Dries's avatar
 
Dries committed
126
      $first = $weekstart;
Dries's avatar
 
Dries committed
127 128
    }
    // Start every week on a new line:
Dries's avatar
 
Dries committed
129
    if ($sday == $weekstart) {
Dries's avatar
 
Dries committed
130
      $output .= " <tr class=\"row-week\">\n";
Dries's avatar
 
Dries committed
131 132 133
    }

    // Print one cell:
Dries's avatar
 
Dries committed
134
    $date = mktime(0, 0, 0, $month, $nday, $year) + $user->timezone;
135 136
    if (isset($days_with_posts[$nday])) {
      $daytext = l($nday, "archive/$year/$month/$nday", array("title" => format_plural($days_with_posts[$nday], "1 post", "%count posts")));
Dries's avatar
 
Dries committed
137
      $dayclass = 'day-link';
Dries's avatar
 
Dries committed
138 139
    }
    else {
Dries's avatar
 
Dries committed
140
      $daytext = $nday;
Dries's avatar
 
Dries committed
141
      $dayclass = 'day-normal';
Dries's avatar
 
Dries committed
142
    }
Dries's avatar
 
Dries committed
143
    if ($date == $requested) {
Dries's avatar
 
Dries committed
144
      $output .= "  <td class=\"day-selected\">$daytext</td>\n";
Dries's avatar
 
Dries committed
145 146
    }
    else if ($date == $start_of_today) {
Dries's avatar
 
Dries committed
147
      $output .= "  <td class=\"day-today\">$daytext</td>\n";
Dries's avatar
 
Dries committed
148
    }
Dries's avatar
 
Dries committed
149
    else if ($date > $end_of_today) {
Dries's avatar
 
Dries committed
150
      $output .= "  <td class=\"day-future\">$daytext</td>\n";
Dries's avatar
 
Dries committed
151 152
    }
    else {
Dries's avatar
 
Dries committed
153
      $output .= "  <td class=\"$dayclass\">$daytext</td>\n";
Dries's avatar
 
Dries committed
154 155 156
    }

    // Start every week on a new line:
Dries's avatar
 
Dries committed
157
    if ($sday == $lastday) {
Dries's avatar
 
Dries committed
158 159 160 161 162 163 164 165 166
      $output .=  " </tr>\n";
    }

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

Dries's avatar
 
Dries committed
167
  // Complete the calendar (allowing individual blank day styling):
Dries's avatar
 
Dries committed
168 169
  if ($sday != $weekstart) {
    $end = (7 - $sday + $weekstart) % 7;
Dries's avatar
 
Dries committed
170
    $output .= str_repeat("<td class=\"day-blank\">&nbsp;</td>\n", $end) . "</tr>\n";
Dries's avatar
 
Dries committed
171 172
  }

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

175
  cache_set("archive:calendar:$day-$month-$year", $output, CACHE_TEMPORARY);
Dries's avatar
 
Dries committed
176

Dries's avatar
 
Dries committed
177 178 179
  return $output;
}

Dries's avatar
 
Dries committed
180 181 182 183 184 185 186 187 188
/**
 * Implementation of hook_block().
 *
 * Generates a calendar for the current month, with links to the archives
 * for each day.
 */
function archive_block($op = 'list', $delta = 0) {
  if ($op == 'list') {
    $blocks[0]['info'] = t('Calendar to browse archives');
Dries's avatar
 
Dries committed
189 190
    return $blocks;
  }
191
  else if ($op == 'view' && user_access('access content')) {
192 193 194
    $block['subject'] = t('Browse archives');
    $block['content'] = archive_calendar();
    return $block;
Dries's avatar
 
Dries committed
195
  }
Dries's avatar
 
Dries committed
196 197
}

Dries's avatar
 
Dries committed
198 199 200
/**
 * Implementation of hook_menu().
 */
Dries's avatar
 
Dries committed
201
function archive_menu($may_cache) {
Dries's avatar
 
Dries committed
202
  $items = array();
Dries's avatar
 
Dries committed
203 204 205 206 207 208 209

  if ($may_cache) {
    $items[] = array('path' => 'archive', 'title' => t('archives'),
      'access' => user_access('access content'),
      'callback' => 'archive_page',
      'type' => MENU_SUGGESTED_ITEM);
  }
Dries's avatar
 
Dries committed
210 211 212
  return $items;
}

Dries's avatar
 
Dries committed
213
/**
Dries's avatar
Dries committed
214
 * Menu callback; lists all nodes posted on a given date.
Dries's avatar
 
Dries committed
215 216 217
 */
function archive_page($year = 0, $month = 0, $day = 0) {
  global $user;
Dries's avatar
 
Dries committed
218

Dries's avatar
 
Dries committed
219 220 221
  $output = '';
  $op = $_POST['op'];
  $edit = $_POST['edit'];
Dries's avatar
 
Dries committed
222

Dries's avatar
 
Dries committed
223 224 225 226
  if ($op == t('Show')) {
    $year = $edit['year'];
    $month = $edit['month'];
    $day = $edit['day'];
Dries's avatar
 
Dries committed
227
  }
Dries's avatar
 
Dries committed
228

Dries's avatar
 
Dries committed
229 230
  $date = mktime(0, 0, 0, $month, $day, $year) - $user->timezone;
  $date_end = mktime(0, 0, 0, $month, $day + 1, $year) - $user->timezone;
Dries's avatar
 
Dries committed
231

Dries's avatar
 
Dries committed
232
  // Prepare the values of the form fields.
Dries's avatar
 
Dries committed
233
  $years = drupal_map_assoc(range(2000, 2005));
Dries's avatar
 
Dries committed
234
  $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'));
Dries's avatar
 
Dries committed
235
  $days = drupal_map_assoc(range(0, 31));
Dries's avatar
 
Dries committed
236

Dries's avatar
 
Dries committed
237 238 239
  $start = '<div class="container-inline">';
  $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
240
  $output .= form($start);
Dries's avatar
 
Dries committed
241

Dries's avatar
 
Dries committed
242
  if ($year && $month && $day) {
Dries's avatar
 
Dries committed
243
    // Fetch nodes for the selected date, if one was specified.
Steven Wittens's avatar
Steven Wittens committed
244
    $sql = 'SELECT n.nid, n.created FROM {node} n WHERE n.status = 1 AND n.created > %d AND n.created < %d ORDER BY n.created';
Dries's avatar
 
Dries committed
245 246
    $sql = node_rewrite_sql($sql);
    $result = db_query_range($sql, $date, $date_end, 0, 20);
Dries's avatar
 
Dries committed
247

Dries's avatar
 
Dries committed
248
    while ($nid = db_fetch_object($result)) {
Dries's avatar
 
Dries committed
249
      $output .= node_view(node_load(array('nid' => $nid->nid)), 1);
Dries's avatar
 
Dries committed
250
    }
Dries's avatar
 
Dries committed
251
  }
Dries's avatar
 
Dries committed
252
  print theme('page', $output);
Dries's avatar
 
Dries committed
253 254
}

Dries's avatar
 
Dries committed
255
?>