]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/Calendar.php
Bugfix for urls in Calendar <http://sourceforge.net/forum/message.php?msg_id=1479658>
[SourceForge/phpwiki.git] / lib / plugin / Calendar.php
1 <?php // -*-php-*-
2 rcs_id('$Id: Calendar.php,v 1.17 2002-02-06 02:41:25 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  * A class representing a clickable day "button".
20  */
21 class DayButton extends HtmlElement {
22     /** Constructor
23      *
24      */
25     function DayButton ($page_for_date, $mday, $isWikiPage, $istoday) {
26
27         $this->HtmlElement('td', array('align' => 'center'));
28
29         if ($isWikiPage) {
30             $class = 'cal-day';
31             $title = $page_for_date;
32             $mday = HTML::em($mday);
33         }
34         else {
35             $class = 'cal-hide';
36             $title = sprintf(_("Edit %s"), $page_for_date);
37         }
38         if ($istoday) {
39             $mday = HTML::strong($mday);
40             $this->setAttr('class', 'cal-today');
41         }
42         $url = WikiURL($page_for_date, array('action' => 'edit'));
43         $this->pushContent(HTML::a(array('href'  => $url,
44                                          'class' => $class,
45                                          'title' => $title), $mday));
46     }
47
48 };
49
50 /**
51  */
52 class WikiPlugin_Calendar
53 extends WikiPlugin
54 {
55     function getName () {
56         return _("Calendar");
57     }
58
59     function getDescription () {
60         return _("Calendar");
61     }
62
63     function getDefaultArguments() {
64         return array('prefix'       => '[pagename].',
65                      'date_format'  => '%Y-%m-%d',
66                      'year'         => '',
67                      'month'        => '',
68                      'month_offset' => 0,
69
70                      'month_format' => '%B, %Y',
71                      'wday_format'  => '%a',
72                      'start_wday'   => '0');
73     }
74
75     function __header($pagename, $time) {
76         $args = &$this->args;
77
78         $t = localtime($time - SECONDS_PER_DAY, 1);
79         $prev_url = WikiURL($pagename, array('month' => $t['tm_mon'] + 1,
80                                              'year'  => $t['tm_year'] + 1900));
81
82         $t = localtime($time + 32 * SECONDS_PER_DAY, 1);
83         $next_url = WikiURL($pagename, array('month' => $t['tm_mon'] + 1,
84                                              'year'  => $t['tm_year'] + 1900));
85
86         $prev = HTML::a(array('href'  => $prev_url,
87                               'class' => 'cal-arrow',
88                               'title' => _("Previous Month")),
89                         '<');
90         $next = HTML::a(array('href'  => $next_url,
91                               'class' => 'cal-arrow',
92                               'title' => _("Next Month")),
93                         '>');
94
95
96         $row = HTML::tr(HTML::td(array('align' => 'left'), $prev),
97                         HTML::td(array('align' => 'center'),
98                                  HTML::strong(array('class' => 'cal-header'),
99                                               strftime($args['month_format'], $time))),
100                         HTML::td(array('align' => 'right'), $next));
101
102         return HTML::tr(HTML::td(array('colspan' => 7,
103                                        'align'   => 'center'),
104                                  HTML::table(array('width' => '100%',
105                                                    'class' => 'cal-header'), $row)));
106     }
107
108
109     function __daynames($start_wday) {
110         $time  = mktime(12, 0, 0, 1, 1, 2001);
111         $t     = localtime($time, 1);
112         $time += (7 + $start_wday - $t['tm_wday']) * SECONDS_PER_DAY;
113
114         $t = localtime($time, 1);
115         assert($t['tm_wday'] == $start_wday);
116
117         $fs = $this->args['wday_format'];
118         $row = HTML::tr();
119         for ($i = 0; $i < 7; $i++) {
120             $row->pushContent(HTML::td(array('class' => 'cal-dayname',
121                                              'align' => 'center'),
122                                        strftime($fs, $time)));
123             $time += SECONDS_PER_DAY;
124         }
125         return $row;
126     }
127
128     function __date($dbi, $time) {
129         $args = &$this->args;
130
131         $page_for_date = $args['prefix'] . strftime($args['date_format'],
132                                                     $time);
133         $t = localtime($time, 1);
134         $mday = $t['tm_mday'];
135
136         $isWikiPage = $dbi->isWikiPage($page_for_date);
137         $istoday = $mday == $this->_today;
138
139         return new DayButton($page_for_date, $mday, $isWikiPage, $istoday);
140     }
141
142     function run($dbi, $argstr, $request) {
143         $this->args = $this->getArgs($argstr, $request);
144         $args       = &$this->args;
145
146         $now = localtime(time(), 1);
147         foreach ( array('month' => $now['tm_mon'] + 1,
148                         'year'  => $now['tm_year'] + 1900)
149                   as $param => $dflt ) {
150
151             if (!($args[$param] = intval($args[$param])))
152                 $args[$param]   = $dflt;
153
154         }
155
156         $time = mktime(12, 0, 0,                               // hh, mm, ss,
157                        $args['month'] + $args['month_offset'], // month (1-12)
158                        1,                                      // mday (1-31)
159                        $args['year']);
160
161         $cal = HTML::table(array('cellspacing' => 0,
162                                  'cellpadding' => 2,
163                                  'class'       => 'cal'),
164                            $this->__header($request->getArg('pagename'), $time),
165                            $this->__daynames($args['start_wday']));
166
167         $t = localtime($time, 1);
168         $now = localtime(time(), 1);
169
170         if ($now['tm_year'] == $t['tm_year'] && $now['tm_mon'] == $t['tm_mon'])
171             $this->_today = $now['tm_mday'];
172         else
173             $this->_today = false;
174
175
176         $row = HTML::tr();
177
178         $col = (7 + $t['tm_wday'] - $args['start_wday']) % 7;
179         if ($col > 0)
180             $row->pushContent(HTML::td(array('colspan' => $col)));
181         $done = false;
182
183         while (!$done) {
184             $row->pushContent($this->__date($dbi, $time));
185
186             if (++$col % 7 == 0) {
187                 $cal->pushContent($row);
188                 $col = 0;
189                 $row = HTML::tr();
190             }
191
192             $time += SECONDS_PER_DAY;
193             $t     = localtime($time, 1);
194             $done  = $t['tm_mday'] == 1;
195         }
196
197         if ($row->getContent()) {
198             $row->pushContent(HTML::td(array('colspan' => (42 - $col) % 7)));
199             $cal->pushContent($row);
200         }
201         return $cal;
202     }
203 };
204
205 // For emacs users
206 // Local Variables:
207 // mode: php
208 // tab-width: 8
209 // c-basic-offset: 4
210 // c-hanging-comment-ender-p: nil
211 // indent-tabs-mode: nil
212 // End:
213 ?>