archive.module 8.31 KB
Newer Older
1 2 3
<?php
// $Id$

4 5 6
/**
 * Implementation of hook_help().
 */
7 8
function archive_help($section) {
  switch ($section) {
9
    case 'admin/system/modules#description':
10
      return t('Displays a calendar to navigate old content.');
11
    case 'admin/system/modules/archive':
12
      return t('Choose the starting "day of the week" for the displayed calendar block.');
13 14
  }
}
15

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

Dries's avatar
Dries committed
23
  // Extract today's date:
24
  $offset = time() + $user->timezone;
25 26
  $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;
27

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

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

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

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

51 52
  $start_of_month = mktime(0, 0, 0, $month, 1, $year);

53
  // Extract first day of the month:
54
  $first = date('w', $start_of_month);
55 56

  // Extract last day of the month:
57
  $last = date('t', $start_of_month);
58

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

61
  $cache = cache_get("archive:calendar:$day-$month-$year");
62 63 64 65

  if (!empty($cache)) {
    return $cache->data;
  }
66 67 68

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

73

74
  $result = db_query("SELECT created FROM {node} WHERE status = 1 AND created > $start_of_month AND created < $end_of_month");
75 76 77

  $days_with_posts = array();
  while ($day_with_post = db_fetch_object($result)) {
78
    $days_with_posts[] = date('j', $day_with_post->created + $user->timezone);
79 80 81
  }
  $days_with_posts = array_unique($days_with_posts);

82 83
  // Generate calendar header:
  $output .= "\n<!-- calendar -->\n";
84 85 86
  $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";
87 88

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

  // Last day of week
92
  ($weekstart - 1 == -1) ? $lastday = 6 : $lastday = $weekstart - 1;
93 94

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

97 98 99 100 101
  $output .= " <tr class=\"header-week\">\n";
  $days = array(t('Su') => t('Sunday'), t('Mo') => t('Monday'), t('Tu') => t('Tuesday'), t('We') => t('Wednesday'), t('Th') => t('Thursday'), t('Fr') => t('Friday'), t('Sa') => t('Saturday'));
  if ($weekstart) {
    $days = array_merge(array_slice($days, $weekstart), array_slice($days, 0, $weekstart));
  }
102

103
  foreach ($days as $name => $fullname) {
104
    $output .= ' <th abbr="'. $fullname .'">'. $name . "</th>\n";
105 106 107 108 109 110 111 112 113 114
  }
  $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:
115 116
    if ($first != $weekstart) {
      $blankdays = ($first - $weekstart + 7) % 7;
117
      $output .= " <tr class=\"row-week\"><td class=\"day-blank\" colspan=\"$blankdays\">&nbsp;</td>\n";
118
      $first = $weekstart;
119 120
    }
    // Start every week on a new line:
121
    if ($sday == $weekstart) {
122
      $output .= " <tr class=\"row-week\">\n";
123 124 125
    }

    // Print one cell:
126
    $date = mktime(0, 0, 0, $month, $nday, $year) + $user->timezone;
127 128
    if (in_array($nday, $days_with_posts)) {
      $daytext = l($nday, "archive/$year/$month/$nday");
129
      $dayclass = 'day-link';
130 131
    }
    else {
132
      $daytext = $nday;
133
      $dayclass = 'day-normal';
134
    }
Dries's avatar
Dries committed
135
    if ($date == $requested) {
136
      $output .= "  <td class=\"day-selected\">$daytext</td>\n";
137 138
    }
    else if ($date == $start_of_today) {
139
      $output .= "  <td class=\"day-today\">$daytext</td>\n";
140
    }
141
    else if ($date > $end_of_today) {
142
      $output .= "  <td class=\"day-future\">$daytext</td>\n";
143 144
    }
    else {
145
      $output .= "  <td class=\"$dayclass\">$daytext</td>\n";
146 147 148
    }

    // Start every week on a new line:
149
    if ($sday == $lastday) {
150 151 152 153 154 155 156 157 158 159
      $output .=  " </tr>\n";
    }

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

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

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

167
  cache_set("archive:calendar:$day-$month-$year", $output, 1);
168

169 170 171
  return $output;
}

172 173 174 175 176 177 178 179 180
/**
 * 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');
181 182
    return $blocks;
  }
183
  else if (user_access('access content')) {
184 185
    switch ($delta) {
      case 0:
186 187
        $block['subject'] = t('Browse archives');
        $block['content'] = archive_calendar();
188 189 190
        return $block;
    }
  }
191 192
}

193 194 195
/**
 * Implementation of hook_link().
 */
196
function archive_link($type) {
Dries's avatar
Dries committed
197 198
  $links = array();

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

203 204
  if ($type == 'system') {
    menu('archive', t('archives'), user_access('access content') ? 'archive_page' : MENU_DENIED, 0, MENU_HIDE);
Dries's avatar
Dries committed
205 206
  }

Dries's avatar
Dries committed
207
  return $links;
208 209
}

210 211 212 213 214
/**
 * Menu callback. Lists all nodes posted on a given date.
 */
function archive_page($year = 0, $month = 0, $day = 0) {
  global $user;
215

216 217 218
  $output = '';
  $op = $_POST['op'];
  $edit = $_POST['edit'];
219

220 221 222 223
  if ($op == t('Show')) {
    $year = $edit['year'];
    $month = $edit['month'];
    $day = $edit['day'];
224
  }
225

226 227
  $date = mktime(0, 0, 0, $month, $day, $year) - $user->timezone;
  $date_end = mktime(0, 0, 0, $month, $day + 1, $year) - $user->timezone;
228

229
  // Prepare the values of the form fields.
230
  $years = drupal_map_assoc(range(2000, 2005));
231
  $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'));
232
  $days = drupal_map_assoc(range(0, 31));
233

234 235 236
  $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>';
237
  $output .= form($start);
238

239
  if ($year && $month && $day) {
240 241
    // Fetch nodes for the selected date, if one was specified.
    $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);
242

243
    while ($nid = db_fetch_object($result)) {
244
      $output .= node_view(node_load(array('nid' => $nid->nid)), 1);
Dries's avatar
Dries committed
245
    }
246
  }
247
  print theme('page', $output);
248 249
}

250 251 252
/**
 * Implementation of hook_settings().
 */
253
function archive_settings() {
254
  $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.'));
255 256 257 258 259

  return $output;
}

?>