/[drupal]/contributions/modules/onthisday/onthisday.module
ViewVC logotype

Contents of /contributions/modules/onthisday/onthisday.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.1 - (show annotations) (download) (as text)
Fri Jan 18 18:08:50 2008 UTC (22 months ago) by richard
Branch: MAIN
CVS Tags: DRUPAL-5--1-0, HEAD
Branch point for: DRUPAL-5
File MIME type: text/x-php
adding the On This Day .module and info files
1 <?php
2
3 function onthisday_menu () {
4 $items[] = array('path' => 'onthisday/feed', 'callback' => 'onthisday_feed', 'access' => user_access('access onthisday feed'));
5 $items[] = array('path' => 'onthisday', 'callback' => 'onthisday_page', 'access' => 'access onthisday page');
6 return $items;
7 }
8
9 function onthisday_perm() {
10 return array('access onthisday feed', 'access onthisday page');
11 }
12
13 function _onthisday_list($offset, $month = NULL, $day = NULL) {
14 global $base_url;
15 $year = date('Y');
16 if (empty($month)) {
17 $month = date('n');
18 $day = date('j');
19 }
20 while ($offset > 0) {
21 $year_ago = mktime(0, 0, 0, $month, $day, $year - $offset);
22 $year_ago_plus = $year_ago + 86400;
23 $result = db_query("SELECT nid, title FROM {node} WHERE status = 1 AND created > %d AND created < %d ORDER BY created ASC;", $year_ago, $year_ago_plus);
24 while ($node = db_fetch_object($result)) {
25 $items[$year-$offset][] = $node->nid;
26 }
27 $offset--;
28 }
29 return $items;
30 }
31
32 function _onthisday_block($offset, $month = NULL, $day = NULL) {
33 if ($offset == -1) {
34 $offset = _onthisday_maxyears();
35 }
36 foreach(_onthisday_list($offset, $month, $day) as $year => $nids) {
37 $output .= '<h4>' . $year . '</h4>';
38 $items = array();
39 foreach($nids as $nid) {
40 $n = node_load(array('nid' => $nid));
41 $items[] = l($n->title, 'node/' . $n->nid, array('rel' => 'bookmark'), NULL, NULL, TRUE);
42 }
43 $output .= theme('item_list', $items);
44 }
45 return $output;
46 }
47
48 function _onthisday_maxyears() {
49 $lowest_year = date('Y', db_result(db_query("SELECT MIN(created) from {node} WHERE status = 1;")));
50 return date('Y') - $lowest_year;
51 }
52
53 function onthisday_block($op = 'list', $delta = 0, $edit = array()) {
54 if ($op == 'list') {
55 $blocks[0]['info'] = t('On This Day');
56 $blocks[1]['info'] = t("Also Posted This Day in History");
57 return $blocks;
58 }
59 else if ($op == 'configure') {
60 $lowest_year = date('Y', db_result(db_query("SELECT MIN(created) from {node} WHERE status = 1;")));
61 $highest_year = date('Y');
62 $offsets = array(-1 => t('all previous years'));
63 for ($x = $lowest_year; $x < $highest_year; $x++) {
64 $offsets[$highest_year - $x] = $highest_year - $x;
65 }
66 $form['onthisday_years'] = array(
67 '#type' => 'select',
68 '#title' => t('Number of years ago'),
69 '#default_value' => variable_get('onthisday_years', 1),
70 '#options' => $offsets
71 );
72 $form['onthisday_more_link'] = array(
73 '#type' => 'checkbox',
74 '#title' => t('Add "more" link at bottom of block'),
75 '#default_value' => variable_get('onthisday_more_link', 0),
76 );
77 return $form;
78 }
79 else if ($op == 'save') {
80 variable_set('onthisday_years', $edit['onthisday_years']);
81 variable_set('onthisday_more_link', $edit['onthisday_more_link']);
82 }
83 else if ($op == 'view') {
84 $offset = variable_get('onthisday_years', 1);
85 if ($offset > 1) {
86 $plural = t('s');
87 }
88 switch ($delta) {
89 case 0:
90 $block['subject'] = t("On This Day");
91 $block['content'] = _onthisday_block($offset);
92 $block['content'] .= variable_get('onthisday_more_link', 0) == 1 ? '<div class="more-link">' . l(t('more'), 'onthisday' . date('/m/d')) . '</div>' : '';
93 return $block;
94 break;
95 case 1:
96 if (arg(0) == 'node' && is_numeric(arg(1)) && !arg(2)) {
97 $node = node_load(arg(1));
98 }
99 if ($node->created && date('Y', $node->created) == date('Y')) {
100 $month = date('n', $node->created);
101 $day = date('j', $node->created);
102 $block['subject'] = t("Also Posted This Day in History");
103 $block['content'] = _onthisday_block($offset, $month, $day);
104 $block['content'] .= variable_get('onthisday_more_link', 0) == 1 ? '<div class="onthisday-more-link">' . l(t('more'), 'onthisday') . '</div>' : '';
105 return $block;
106 }
107 break;
108 }
109 }
110 }
111
112 function _onthisday_page($offset) {
113 if ($offset == -1) {
114 $offset = _onthisday_maxyears();
115 }
116 if (is_numeric(arg(1)) && is_numeric(arg(2))) {
117 $nodes = _onthisday_list($offset, arg(1), arg(2));
118 $thisday = ' - ' . date('F jS', mktime(0, 0, 0, arg(1), arg(2)));
119 }
120 else {
121 $nodes = _onthisday_list($offset);
122 }
123 foreach ($nodes as $nids) {
124 foreach ($nids as $nid) {
125 $output .= node_view(node_load(array('nid' => $nid)), 1);
126 }
127 }
128 drupal_set_title(t('On This Day' . $thisday));
129 if (user_access('access onthisday feed')) {
130 drupal_add_link(array('rel' => 'alternate',
131 'type' => 'application/rss+xml',
132 'title' => t('RSS'),
133 'href' => url('onthisday/feed')));
134 $output .= theme('xml_icon', url('onthisday/feed'));
135 }
136 print theme('page', $output);
137 }
138
139 function onthisday_page() {
140 return _onthisday_page(variable_get('onthisday_years', 1), 'page');
141 }
142
143 function onthisday_feed() {
144 global $base_url;
145 $output = _onthisday_block(variable_get('onthisday_years', 1));
146 drupal_set_header('Content-Type: text/xml; charset=utf-8');
147 print '<';
148 print '?xml version="1.0"?>';
149 print '<rss version="2.0">';
150 $item = '<item><title>' . t('On This Day') . t(' for ') . date('F jS, Y') . '</title>' .
151 '<link>' . $base_url . '/' . url('onthisday') . '/' . date('Y') . '/' . date('m') . '/' . date('d') . '</link>' .
152 '<description>' . htmlspecialchars($output) . '</description></item>';
153 print format_rss_channel(t('On This Day') . ' at ' . variable_get('site_name', drupal), $base_url . '/' . url('onthisday'), '', $item);
154 print "</rss>";
155 }
156
157 function onthisday_views_handler($op) {
158 switch $op {
159 case 'filter':
160 break;
161 }
162 }
163
164 function onthisday_views_arguments($op, &$query, $a1, $a2 = null) {
165 $arguments['onthisday_month'] = array('name' => 'On This Day: Month', 'help' => '', 'handler' => 'onthisday_views_handler_month');
166 $arguments['onthisday_day'] = array('name' => 'On This Day: Day', 'help' => '', 'handler' => 'onthisday_views_hander_day');
167 return $arguments;
168 }
169
170 ?>

  ViewVC Help
Powered by ViewVC 1.1.2