2 rcs_id('$Id: Calendar.php,v 1.23 2002-08-17 15:52:51 rurban Exp $');
4 if (!defined('SECONDS_PER_DAY'))
5 define('SECONDS_PER_DAY', 24 * 3600);
9 // o Better way to navigate to distant months.
10 // (Maybe a form with selectors for month and year)?
12 // o Docs. Write a pgsrc/CalendarPlugin.
14 // It would be nice to have some way to get from the individual date
15 // pages back to the calendar page. (Subpage support might make this
20 class WikiPlugin_Calendar
27 function getDescription () {
31 function getDefaultArguments() {
32 return array('prefix' => '[pagename]' . SUBPAGE_SEPARATOR,
33 'date_format' => '%Y-%m-%d',
38 'month_format' => '%B, %Y',
39 'wday_format' => '%a',
43 function __header($pagename, $time) {
46 $t = localtime($time - SECONDS_PER_DAY, 1);
47 $prev_url = WikiURL($pagename, array('month' => $t['tm_mon'] + 1,
48 'year' => $t['tm_year'] + 1900));
50 $t = localtime($time + 32 * SECONDS_PER_DAY, 1);
51 $next_url = WikiURL($pagename, array('month' => $t['tm_mon'] + 1,
52 'year' => $t['tm_year'] + 1900));
54 $prev = HTML::a(array('href' => $prev_url,
55 'class' => 'cal-arrow',
56 'title' => _("Previous Month")),
58 $next = HTML::a(array('href' => $next_url,
59 'class' => 'cal-arrow',
60 'title' => _("Next Month")),
64 $row = HTML::tr(HTML::td(array('align' => 'left'), $prev),
65 HTML::td(array('align' => 'center'),
66 HTML::strong(array('class' => 'cal-header'),
67 strftime($args['month_format'], $time))),
68 HTML::td(array('align' => 'right'), $next));
70 return HTML::tr(HTML::td(array('colspan' => 7,
72 HTML::table(array('width' => '100%',
73 'class' => 'cal-header'), $row)));
77 function __daynames($start_wday) {
78 $time = mktime(12, 0, 0, 1, 1, 2001);
79 $t = localtime($time, 1);
80 $time += (7 + $start_wday - $t['tm_wday']) * SECONDS_PER_DAY;
82 $t = localtime($time, 1);
83 assert($t['tm_wday'] == $start_wday);
85 $fs = $this->args['wday_format'];
87 $row->setattr('class', 'cal-dayname');
88 for ($i = 0; $i < 7; $i++) {
89 $row->pushContent(HTML::td(array('class' => 'cal-dayname',
91 strftime($fs, $time)));
92 $time += SECONDS_PER_DAY;
97 function __date($dbi, $time) {
100 $page_for_date = $args['prefix'] . strftime($args['date_format'],
102 $t = localtime($time, 1);
104 $td = HTML::td(array('align' => 'center'));
106 $mday = $t['tm_mday'];
107 if ($mday == $this->_today) {
108 $mday = HTML::strong($mday);
109 $td->setAttr('class', 'cal-today');
111 else if ($dbi->isWikiPage($page_for_date)) {
112 $td->setAttr('class', 'cal-day');
115 if ($dbi->isWikiPage($page_for_date)) {
116 $date = HTML::a(array('class' => 'cal-day',
117 'href' => WikiURL($page_for_date),
118 'title' => $page_for_date),
122 $date = HTML::a(array('class' => 'cal-hide',
123 'href' => WikiURL($page_for_date,
124 array('action' => 'edit')),
125 'title' => sprintf(_("Edit %s"), $page_for_date)),
128 $td->pushContent(NBSP, $date, NBSP);
132 function run($dbi, $argstr, $request) {
133 $this->args = $this->getArgs($argstr, $request);
134 $args = &$this->args;
136 $now = localtime(time() + 3600 * $request->getPref('timeOffset'), 1);
137 foreach ( array('month' => $now['tm_mon'] + 1,
138 'year' => $now['tm_year'] + 1900)
139 as $param => $dflt ) {
141 if (!($args[$param] = intval($args[$param])))
142 $args[$param] = $dflt;
145 $time = mktime(12, 0, 0, // hh, mm, ss,
146 $args['month'] + $args['month_offset'], // month (1-12)
150 $cal = HTML::table(array('cellspacing' => 0,
154 $this->__header($request->getArg('pagename'), $time),
155 $this->__daynames($args['start_wday'])));
157 $t = localtime($time, 1);
159 if ($now['tm_year'] == $t['tm_year'] && $now['tm_mon'] == $t['tm_mon'])
160 $this->_today = $now['tm_mday'];
162 $this->_today = false;
164 $tbody = HTML::tbody();
167 $col = (7 + $t['tm_wday'] - $args['start_wday']) % 7;
169 $row->pushContent(HTML::td(array('colspan' => $col)));
173 $row->pushContent($this->__date($dbi, $time));
175 if (++$col % 7 == 0) {
176 $tbody->pushContent($row);
181 $time += SECONDS_PER_DAY;
182 $t = localtime($time, 1);
183 $done = $t['tm_mday'] == 1;
186 if ($row->getContent()) {
187 $row->pushContent(HTML::td(array('colspan' => (42 - $col) % 7)));
188 $tbody->pushContent($row);
190 $cal->pushContent($tbody);
200 // c-hanging-comment-ender-p: nil
201 // indent-tabs-mode: nil