]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/Calendar.php
Set row attribute of day-name header for additional control over css formatting.
[SourceForge/phpwiki.git] / lib / plugin / Calendar.php
1 <?php // -*-php-*-
2 rcs_id('$Id: Calendar.php,v 1.20 2002-02-10 05:08:11 carstenklapp 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%',
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
112         if ($dbi->isWikiPage($page_for_date)) {
113             $date = HTML::a(array('class' => 'cal-day',
114                                   'href'  => WikiURL($page_for_date),
115                                   'title' => $page_for_date),
116                             HTML::em($mday));
117         }
118         else {
119             $date = HTML::a(array('class' => 'cal-hide',
120                                   'href'  => WikiURL($page_for_date,
121                                                      array('action' => 'edit')),
122                                   'title' => sprintf(_("Edit %s"), $page_for_date)),
123                             $mday);
124         }
125         $td->pushContent(NBSP, $date, NBSP);
126         return $td;
127     }
128
129     function run($dbi, $argstr, $request) {
130         $this->args = $this->getArgs($argstr, $request);
131         $args       = &$this->args;
132
133         $now = localtime(time() + 3600 * $request->getPref('timeOffset'), 1);
134         foreach ( array('month' => $now['tm_mon'] + 1,
135                         'year'  => $now['tm_year'] + 1900)
136                   as $param => $dflt ) {
137
138             if (!($args[$param] = intval($args[$param])))
139                 $args[$param]   = $dflt;
140         }
141
142         $time = mktime(12, 0, 0,                               // hh, mm, ss,
143                        $args['month'] + $args['month_offset'], // month (1-12)
144                        1,                                      // mday (1-31)
145                        $args['year']);
146
147         $cal = HTML::table(array('cellspacing' => 0,
148                                  'cellpadding' => 2,
149                                  'class'       => 'cal'),
150                            $this->__header($request->getArg('pagename'), $time),
151                            $this->__daynames($args['start_wday']));
152
153         $t = 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 ?>