]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/Calendar.php
Wups. Put  s back in...
[SourceForge/phpwiki.git] / lib / plugin / Calendar.php
1 <?php // -*-php-*-
2 rcs_id('$Id: Calendar.php,v 1.14 2002-01-26 04:43:39 dairiki 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].',
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%'), $row)));
73     }
74
75
76     function __daynames($start_wday) {
77         $time  = mktime(12, 0, 0, 1, 1, 2001);
78         $t     = localtime($time, 1);
79         $time += (7 + $start_wday - $t['tm_wday']) * SECONDS_PER_DAY;
80
81         $t = localtime($time, 1);
82         assert($t['tm_wday'] == $start_wday);
83
84         $fs = $this->args['wday_format'];
85         $row = HTML::tr();
86         for ($i = 0; $i < 7; $i++) {
87             $row->pushContent(HTML::td(array('class' => 'cal-dayname',
88                                              'align' => 'center'),
89                                        strftime($fs, $time)));
90             $time += SECONDS_PER_DAY;
91         }
92         return $row;
93     }
94
95     function __date($dbi, $time) {
96         $args = &$this->args;
97
98         $page_for_date = $args['prefix'] . strftime($args['date_format'],
99                                                     $time);
100         $t = localtime($time, 1);
101
102         $td = HTML::td(array('align' => 'center'));
103
104         $mday = $t['tm_mday'];
105         if ($mday == $this->_today) {
106             $mday = HTML::strong($mday);
107             $td->setAttr('class', 'cal-today');
108         }
109
110         if ($dbi->isWikiPage($page_for_date)) {
111             $date = HTML::a(array('class' => 'cal-day',
112                                   'href'  => WikiURL($page_for_date),
113                                   'title' => $page_for_date),
114                             HTML::em($mday));
115         }
116         else {
117             $date = HTML::a(array('class' => 'cal-hide',
118                                   'href'  => WikiURL($page_for_date,
119                                                      array('action' => 'edit')),
120                                   'title' => sprintf(_("Edit %s"), $page_for_date)),
121                             $mday);
122         }
123         $td->pushContent(NBSP, $date, NBSP);
124         return $td;
125     }
126
127     function run($dbi, $argstr, $request) {
128         $this->args = $this->getArgs($argstr, $request);
129         $args       = &$this->args;
130
131         $now = localtime(time(), 1);
132         foreach ( array('month' => $now['tm_mon'] + 1,
133                         'year'  => $now['tm_year'] + 1900)
134                   as $param => $dflt ) {
135
136             if (!($args[$param] = intval($args[$param])))
137                 $args[$param]   = $dflt;
138
139         }
140
141         $time = mktime(12, 0, 0,                               // hh, mm, ss,
142                        $args['month'] + $args['month_offset'], // month (1-12)
143                        1,                                      // mday (1-31)
144                        $args['year']);
145
146         $cal = HTML::table(array('cellspacing' => 0,
147                                  'cellpadding' => 2,
148                                  'class'       => 'cal'),
149                            $this->__header($request->getArg('pagename'), $time),
150                            $this->__daynames($args['start_wday']));
151
152         $t = localtime($time, 1);
153         $now = localtime(time(), 1);
154
155         if ($now['tm_year'] == $t['tm_year'] && $now['tm_mon'] == $t['tm_mon'])
156             $this->_today = $now['tm_mday'];
157         else
158             $this->_today = false;
159         
160         
161         $row = HTML::tr();
162         
163         $col = (7 + $t['tm_wday'] - $args['start_wday']) % 7;
164         if ($col > 0)
165             $row->pushContent(HTML::td(array('colspan' => $col)));
166         $done = false;
167
168         while (!$done) {
169             $row->pushContent($this->__date($dbi, $time));
170
171             if (++$col % 7 == 0) {
172                 $cal->pushContent($row);
173                 $col = 0;
174                 $row = HTML::tr();
175             }
176
177             $time += SECONDS_PER_DAY;
178             $t     = localtime($time, 1);
179             $done  = $t['tm_mday'] == 1;
180         }
181
182         if ($row->getContent()) {
183             $row->pushContent(HTML::td(array('colspan' => (42 - $col) % 7)));
184             $cal->pushContent($row);
185         }
186         return $cal;
187     }
188 };
189
190 // For emacs users
191 // Local Variables:
192 // mode: php
193 // tab-width: 8
194 // c-basic-offset: 4
195 // c-hanging-comment-ender-p: nil
196 // indent-tabs-mode: nil
197 // End:
198 ?>