]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/Calendar.php
added SubPages support: see SUBPAGE_SEPERATOR in index.php
[SourceForge/phpwiki.git] / lib / plugin / Calendar.php
1 <?php // -*-php-*-
2 rcs_id('$Id: Calendar.php,v 1.23 2002-08-17 15:52:51 rurban Exp $');
3
4 if (!defined('SECONDS_PER_DAY'))
5 define('SECONDS_PER_DAY', 24 * 3600);
6
7 // FIXME: Still needs:
8 //
9 //   o Better way to navigate to distant months.
10 //     (Maybe a form with selectors for month and year)?
11 //
12 //   o Docs.  Write a pgsrc/CalendarPlugin.
13 //
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
16 // easier.)
17
18 /**
19  */
20 class WikiPlugin_Calendar
21 extends WikiPlugin
22 {
23     function getName () {
24         return _("Calendar");
25     }
26
27     function getDescription () {
28         return _("Calendar");
29     }
30
31     function getDefaultArguments() {
32         return array('prefix'           => '[pagename]' . SUBPAGE_SEPARATOR,
33                      'date_format'      => '%Y-%m-%d',
34                      'year'             => '',
35                      'month'            => '',
36                      'month_offset'     => 0,
37
38                      'month_format'     => '%B, %Y',
39                      'wday_format'      => '%a',
40                      'start_wday'       => '0');
41     }
42
43     function __header($pagename, $time) {
44         $args = &$this->args;
45
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));
49
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));
53
54         $prev = HTML::a(array('href'  => $prev_url,
55                               'class' => 'cal-arrow',
56                               'title' => _("Previous Month")),
57                         '<');
58         $next = HTML::a(array('href'  => $next_url,
59                               'class' => 'cal-arrow',
60                               'title' => _("Next Month")),
61                         '>');
62
63
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));
69
70         return HTML::tr(HTML::td(array('colspan' => 7,
71                                        'align'   => 'center'),
72                                  HTML::table(array('width' => '100%',
73                                                    'class' => 'cal-header'), $row)));
74     }
75
76
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;
81
82         $t = localtime($time, 1);
83         assert($t['tm_wday'] == $start_wday);
84
85         $fs = $this->args['wday_format'];
86         $row = HTML::tr();
87         $row->setattr('class', 'cal-dayname');
88         for ($i = 0; $i < 7; $i++) {
89             $row->pushContent(HTML::td(array('class' => 'cal-dayname',
90                                              'align' => 'center'),
91                                        strftime($fs, $time)));
92             $time += SECONDS_PER_DAY;
93         }
94         return $row;
95     }
96
97     function __date($dbi, $time) {
98         $args = &$this->args;
99
100         $page_for_date = $args['prefix'] . strftime($args['date_format'],
101                                                     $time);
102         $t = localtime($time, 1);
103
104         $td = HTML::td(array('align' => 'center'));
105
106         $mday = $t['tm_mday'];
107         if ($mday == $this->_today) {
108             $mday = HTML::strong($mday);
109             $td->setAttr('class', 'cal-today');
110         }
111         else if ($dbi->isWikiPage($page_for_date)) {
112             $td->setAttr('class', 'cal-day');
113         }
114
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),
119                             HTML::em($mday));
120         }
121         else {
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)),
126                             $mday);
127         }
128         $td->pushContent(NBSP, $date, NBSP);
129         return $td;
130     }
131
132     function run($dbi, $argstr, $request) {
133         $this->args = $this->getArgs($argstr, $request);
134         $args       = &$this->args;
135
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 ) {
140
141             if (!($args[$param] = intval($args[$param])))
142                 $args[$param]   = $dflt;
143         }
144
145         $time = mktime(12, 0, 0,                               // hh, mm, ss,
146                        $args['month'] + $args['month_offset'], // month (1-12)
147                        1,                                      // mday (1-31)
148                        $args['year']);
149
150         $cal = HTML::table(array('cellspacing' => 0,
151                                  'cellpadding' => 2,
152                                  'class'       => 'cal'),
153                            HTML::thead(
154                                        $this->__header($request->getArg('pagename'), $time),
155                                        $this->__daynames($args['start_wday'])));
156
157         $t = localtime($time, 1);
158
159         if ($now['tm_year'] == $t['tm_year'] && $now['tm_mon'] == $t['tm_mon'])
160             $this->_today = $now['tm_mday'];
161         else
162             $this->_today = false;
163
164         $tbody = HTML::tbody();
165         $row = HTML::tr();
166
167         $col = (7 + $t['tm_wday'] - $args['start_wday']) % 7;
168         if ($col > 0)
169             $row->pushContent(HTML::td(array('colspan' => $col)));
170         $done = false;
171
172         while (!$done) {
173             $row->pushContent($this->__date($dbi, $time));
174
175             if (++$col % 7 == 0) {
176                 $tbody->pushContent($row);
177                 $col = 0;
178                 $row = HTML::tr();
179             }
180
181             $time += SECONDS_PER_DAY;
182             $t     = localtime($time, 1);
183             $done  = $t['tm_mday'] == 1;
184         }
185
186         if ($row->getContent()) {
187             $row->pushContent(HTML::td(array('colspan' => (42 - $col) % 7)));
188             $tbody->pushContent($row);
189         }
190         $cal->pushContent($tbody);
191         return $cal;
192     }
193 };
194
195 // For emacs users
196 // Local Variables:
197 // mode: php
198 // tab-width: 8
199 // c-basic-offset: 4
200 // c-hanging-comment-ender-p: nil
201 // indent-tabs-mode: nil
202 // End:
203 ?>