]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/Calendar.php
extra_empty_lines
[SourceForge/phpwiki.git] / lib / plugin / Calendar.php
1 <?php // -*-php-*-
2 // $Id$
3 /**
4  * Copyright 1999,2000,2001,2002,2007 $ThePhpWikiProgrammingTeam
5  *
6  * This file is part of PhpWiki.
7  *
8  * PhpWiki is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * PhpWiki is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22
23 if (!defined('SECONDS_PER_DAY'))
24     define('SECONDS_PER_DAY', 24 * 3600);
25
26 // FIXME: Still needs:
27 //
28 //   o Better way to navigate to distant months.
29 //     (Maybe a form with selectors for month and year)?
30 //
31 // It would be nice to have some way to get from the individual date
32 // pages back to the calendar page. (Subpage support might make this
33 // easier.)
34
35 /**
36  */
37 class WikiPlugin_Calendar
38 extends WikiPlugin
39 {
40     function getName () {
41         return _("Calendar");
42     }
43
44     function getDescription () {
45         return _("Calendar");
46     }
47
48     function getDefaultArguments() {
49         return array('prefix'           => '[pagename]' . SUBPAGE_SEPARATOR,
50                      'date_format'      => '%Y-%m-%d',
51                      'year'             => '',
52                      'month'            => '',
53                      'month_offset'     => 0,
54                      'month_format'     => '%B %Y',
55                      'wday_format'      => '%a',
56                      'start_wday'       => '1', // start now with Monday
57                      'display_weeknum'  => 0);
58     }
59
60     /**
61      * return links (static only as of action=edit)
62      *
63      * @param  string $argstr   The plugin argument string.
64      * @param  string $basepage The pagename the plugin is invoked from.
65      * @return array  List of pagenames linked to (or false).
66      */
67     function getWikiPageLinks ($argstr, $basepage) {
68         if (isset($this->_links))
69             return $this->_links;
70         else {
71             global $request;
72             $this->run($request->_dbi, $argstr, $request, $basepage);
73             return $this->_links;
74         }
75     }
76
77     function __header($pagename, $time) {
78         $args = &$this->args;
79
80         $t = localtime($time - SECONDS_PER_DAY, 1);
81         $prev_url = WikiURL($pagename, array('month' => $t['tm_mon'] + 1,
82                                              'year'  => $t['tm_year'] + 1900));
83
84         $t = localtime($time + 32 * SECONDS_PER_DAY, 1);
85         $next_url = WikiURL($pagename, array('month' => $t['tm_mon'] + 1,
86                                              'year'  => $t['tm_year'] + 1900));
87
88         $prev = HTML::a(array('href'  => $prev_url,
89                               'class' => 'cal-arrow',
90                               'title' => _("Previous Month")),
91                         '<');
92         $next = HTML::a(array('href'  => $next_url,
93                               'class' => 'cal-arrow',
94                               'title' => _("Next Month")),
95                         '>');
96
97         $row = HTML::tr(HTML::td(array('align' => 'left'), $prev),
98                         HTML::td(array('align' => 'center'),
99                                  HTML::strong(array('class' => 'cal-header'),
100                                               strftime($args['month_format'],
101                                                        $time))),
102                         HTML::td(array('align' => 'right'), $next));
103
104         return HTML::tr(HTML::td(array('colspan' => $args['display_weeknum'] ? 8 : 7,
105                                        'align'   => 'center'),
106                                  HTML::table(array('width' => '100%',
107                                                    'class' => 'cal-header'),
108                                              $row)));
109     }
110
111     function __daynames($start_wday) {
112         $time  = mktime(12, 0, 0, 1, 1, 2001);
113         $t     = localtime($time, 1);
114         $time += (7 + $start_wday - $t['tm_wday']) * SECONDS_PER_DAY;
115
116         $t = localtime($time, 1);
117         assert($t['tm_wday'] == $start_wday);
118
119         $fs = $this->args['wday_format'];
120         $row = HTML::tr();
121         $row->setattr('class', 'cal-dayname');
122         if ($this->args['display_weeknum'])
123             $row->pushContent(HTML::td(array('class' => 'cal-dayname',
124                                              'align' => 'center'),
125                                        _("Wk")));
126         for ($i = 0; $i < 7; $i++) {
127             $row->pushContent(HTML::td(array('class' => 'cal-dayname',
128                                              'align' => 'center'),
129                                        strftime($fs, $time)));
130             $time += SECONDS_PER_DAY;
131         }
132         return $row;
133     }
134
135     function __date($dbi, $time) {
136         $args = &$this->args;
137
138         $page_for_date = $args['prefix'] . strftime($args['date_format'],
139                                                     $time);
140         $t = localtime($time, 1);
141
142         $td = HTML::td(array('align' => 'center'));
143
144         $mday = $t['tm_mday'];
145         if ($mday == $this->_today) {
146             $mday = HTML::strong($mday);
147             $td->setAttr('class', 'cal-today');
148         }
149         else if ($dbi->isWikiPage($page_for_date)) {
150             $this->_links[] = $page_for_date;
151             $td->setAttr('class', 'cal-day');
152         }
153
154         if ($dbi->isWikiPage($page_for_date)) {
155             $this->_links[] = $page_for_date;
156             $date = HTML::a(array('class' => 'cal-day',
157                                   'href'  => WikiURL($page_for_date),
158                                   'title' => $page_for_date),
159                             HTML::em($mday));
160         }
161         else {
162             $date = HTML::a(array('class' => 'cal-hide',
163                                   'rel'   => 'nofollow',
164                                   'href'  => WikiURL($page_for_date,
165                                                      array('action' => 'edit')),
166                                   'title' => sprintf(_("Edit %s"),
167                                                      $page_for_date)),
168                             $mday);
169         }
170         $td->pushContent(HTML::raw('&nbsp;'), $date, HTML::raw('&nbsp;'));
171         return $td;
172     }
173
174     function run($dbi, $argstr, &$request, $basepage) {
175         $this->args = $this->getArgs($argstr, $request);
176         $args       = &$this->args;
177         $this->_links = array();
178
179         $now = localtime(time() + 3600 * $request->getPref('timeOffset'), 1);
180         foreach ( array('month' => $now['tm_mon'] + 1,
181                         'year'  => $now['tm_year'] + 1900)
182                   as $param => $dflt ) {
183
184             if (!($args[$param] = intval($args[$param])))
185                 $args[$param]   = $dflt;
186         }
187
188         $time = mktime(12, 0, 0,                               // hh, mm, ss,
189                        $args['month'] + $args['month_offset'], // month (1-12)
190                        1,                                      // mday (1-31)
191                        $args['year']);
192
193         $colnum = $args['display_weeknum'] ? 8 : 7;
194         $cal = HTML::table(array('cellspacing' => 0,
195                                  'cellpadding' => 2,
196                                  'class'       => 'cal'),
197                            HTML::thead(
198                                        $this->__header($request->getArg('pagename'),
199                                                        $time),
200                                        $this->__daynames($args['start_wday'])));
201
202         $t = localtime($time, 1);
203
204         if ($now['tm_year'] == $t['tm_year'] && $now['tm_mon'] == $t['tm_mon'])
205             $this->_today = $now['tm_mday'];
206         else
207             $this->_today = false;
208
209         $tbody = HTML::tbody();
210         $row = HTML::tr();
211
212         if ($args['display_weeknum'])
213             $row->pushContent(HTML::td(array('class' => 'cal-weeknum'),
214                                        ((int)strftime("%U", $time))+1)); // %U problem. starts with 0
215         $col = (7 + $t['tm_wday'] - $args['start_wday']) % 7;
216         if ($col > 0)
217             $row->pushContent(HTML::td(array('colspan' => $col)));
218         $done = false;
219         while (!$done) {
220             $row->pushContent($this->__date($dbi, $time));
221
222             if (++$col % 7 == 0) {
223                 $tbody->pushContent($row);
224                 $col = 0;
225                 $row = HTML::tr();
226             }
227
228             $time += SECONDS_PER_DAY;
229             $t     = localtime($time, 1);
230             $done  = $t['tm_mday'] == 1;
231             if (!$col and !$done and $args['display_weeknum'])
232                 $row->pushContent(HTML::td(array('class' => 'cal-weeknum'),
233                                            ((int)strftime("%U", $time))+1)); // starts with 0
234         }
235
236         if ($row->getContent()) {
237             $row->pushContent(HTML::td(array('colspan' => (42 - $col) % 7)));
238             $tbody->pushContent($row);
239         }
240         $cal->pushContent($tbody);
241         return $cal;
242     }
243 };
244
245 // Local Variables:
246 // mode: php
247 // tab-width: 8
248 // c-basic-offset: 4
249 // c-hanging-comment-ender-p: nil
250 // indent-tabs-mode: nil
251 // End:
252 ?>