]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/Calendar.php
Add private keyword for functions
[SourceForge/phpwiki.git] / lib / plugin / Calendar.php
1 <?php
2
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 class WikiPlugin_Calendar
36     extends WikiPlugin
37 {
38     function getName()
39     {
40         return _("Calendar");
41     }
42
43     function getDescription()
44     {
45         return _("Calendar");
46     }
47
48     function getDefaultArguments()
49     {
50         return array('prefix' => '[pagename]' . SUBPAGE_SEPARATOR,
51             'date_format' => '%Y-%m-%d',
52             'year' => '',
53             'month' => '',
54             'month_offset' => 0,
55             'month_format' => '%B %Y',
56             'wday_format' => '%a',
57             'start_wday' => '1', // start now with Monday
58             'display_weeknum' => 0);
59     }
60
61     /**
62      * return links (static only as of action=edit)
63      *
64      * @param  string $argstr   The plugin argument string.
65      * @param  string $basepage The pagename the plugin is invoked from.
66      * @return array  List of pagenames linked to (or false).
67      */
68     function getWikiPageLinks($argstr, $basepage)
69     {
70         if (isset($this->_links))
71             return $this->_links;
72         else {
73             global $request;
74             $this->run($request->_dbi, $argstr, $request, $basepage);
75             return $this->_links;
76         }
77     }
78
79     private function __header($pagename, $time)
80     {
81         $args = &$this->args;
82
83         $t = localtime($time - SECONDS_PER_DAY, 1);
84         $prev_url = WikiURL($pagename, array('month' => $t['tm_mon'] + 1,
85             'year' => $t['tm_year'] + 1900));
86
87         $t = localtime($time + 32 * SECONDS_PER_DAY, 1);
88         $next_url = WikiURL($pagename, array('month' => $t['tm_mon'] + 1,
89             'year' => $t['tm_year'] + 1900));
90
91         $prev = HTML::a(array('href' => $prev_url,
92                 'class' => 'cal-arrow',
93                 'title' => _("Previous Month")),
94             '<');
95         $next = HTML::a(array('href' => $next_url,
96                 'class' => 'cal-arrow',
97                 'title' => _("Next Month")),
98             '>');
99
100         $row = HTML::tr(HTML::td(array('align' => 'left'), $prev),
101             HTML::td(array('align' => 'center'),
102                 HTML::strong(array('class' => 'cal-header'),
103                     strftime($args['month_format'],
104                         $time))),
105             HTML::td(array('align' => 'right'), $next));
106
107         return HTML::tr(HTML::td(array('colspan' => $args['display_weeknum'] ? 8 : 7,
108                 'align' => 'center'),
109             HTML::table(array('width' => '100%',
110                     'class' => 'cal-header'),
111                 $row)));
112     }
113
114     private function __daynames($start_wday)
115     {
116         $time = mktime(12, 0, 0, 1, 1, 2001);
117         $t = localtime($time, 1);
118         $time += (7 + $start_wday - $t['tm_wday']) * SECONDS_PER_DAY;
119
120         $t = localtime($time, 1);
121         assert($t['tm_wday'] == $start_wday);
122
123         $fs = $this->args['wday_format'];
124         $row = HTML::tr();
125         $row->setattr('class', 'cal-dayname');
126         if ($this->args['display_weeknum'])
127             $row->pushContent(HTML::td(array('class' => 'cal-dayname',
128                     'align' => 'center'),
129                 _("Wk")));
130         for ($i = 0; $i < 7; $i++) {
131             $row->pushContent(HTML::td(array('class' => 'cal-dayname',
132                     'align' => 'center'),
133                 strftime($fs, $time)));
134             $time += SECONDS_PER_DAY;
135         }
136         return $row;
137     }
138
139     private function __date($dbi, $time)
140     {
141         $args = &$this->args;
142
143         $page_for_date = $args['prefix'] . strftime($args['date_format'],
144             $time);
145         $t = localtime($time, 1);
146
147         $td = HTML::td(array('align' => 'center'));
148
149         $mday = $t['tm_mday'];
150         if ($mday == $this->_today) {
151             $mday = HTML::strong($mday);
152             $td->setAttr('class', 'cal-today');
153         } elseif ($dbi->isWikiPage($page_for_date)) {
154             $this->_links[] = $page_for_date;
155             $td->setAttr('class', 'cal-day');
156         }
157
158         if ($dbi->isWikiPage($page_for_date)) {
159             $this->_links[] = $page_for_date;
160             $date = HTML::a(array('class' => 'cal-day',
161                     'href' => WikiURL($page_for_date),
162                     'title' => $page_for_date),
163                 HTML::em($mday));
164         } else {
165             $date = HTML::a(array('class' => 'cal-hide',
166                     'rel' => 'nofollow',
167                     'href' => WikiURL($page_for_date,
168                         array('action' => 'edit')),
169                     'title' => sprintf(_("Edit %s"),
170                         $page_for_date)),
171                 $mday);
172         }
173         $td->pushContent(HTML::raw('&nbsp;'), $date, HTML::raw('&nbsp;'));
174         return $td;
175     }
176
177     function run($dbi, $argstr, &$request, $basepage)
178     {
179         $this->args = $this->getArgs($argstr, $request);
180         $args = &$this->args;
181         $this->_links = array();
182
183         $now = localtime(time() + 3600 * $request->getPref('timeOffset'), 1);
184         foreach (array('month' => $now['tm_mon'] + 1,
185                      'year' => $now['tm_year'] + 1900)
186                  as $param => $dflt) {
187
188             if (!($args[$param] = intval($args[$param])))
189                 $args[$param] = $dflt;
190         }
191
192         $time = mktime(12, 0, 0, // hh, mm, ss,
193             $args['month'] + $args['month_offset'], // month (1-12)
194             1, // mday (1-31)
195             $args['year']);
196
197         $cal = HTML::table(array('cellspacing' => 0,
198                 'cellpadding' => 2,
199                 'class' => 'cal'),
200             HTML::thead(
201                 $this->__header($request->getArg('pagename'),
202                     $time),
203                 $this->__daynames($args['start_wday'])));
204
205         $t = localtime($time, 1);
206
207         if ($now['tm_year'] == $t['tm_year'] && $now['tm_mon'] == $t['tm_mon'])
208             $this->_today = $now['tm_mday'];
209         else
210             $this->_today = false;
211
212         $tbody = HTML::tbody();
213         $row = HTML::tr();
214
215         if ($args['display_weeknum'])
216             $row->pushContent(HTML::td(array('class' => 'cal-weeknum'),
217                 ((int)strftime("%U", $time)) + 1)); // %U problem. starts with 0
218         $col = (7 + $t['tm_wday'] - $args['start_wday']) % 7;
219         if ($col > 0)
220             $row->pushContent(HTML::td(array('colspan' => $col)));
221         $done = false;
222         while (!$done) {
223             $row->pushContent($this->__date($dbi, $time));
224
225             if (++$col % 7 == 0) {
226                 $tbody->pushContent($row);
227                 $col = 0;
228                 $row = HTML::tr();
229             }
230
231             $time += SECONDS_PER_DAY;
232             $t = localtime($time, 1);
233             $done = $t['tm_mday'] == 1;
234             if (!$col and !$done and $args['display_weeknum'])
235                 $row->pushContent(HTML::td(array('class' => 'cal-weeknum'),
236                     ((int)strftime("%U", $time)) + 1)); // starts with 0
237         }
238
239         if ($row->getContent()) {
240             $row->pushContent(HTML::td(array('colspan' => (42 - $col) % 7)));
241             $tbody->pushContent($row);
242         }
243         $cal->pushContent($tbody);
244         return $cal;
245     }
246 }
247
248 // Local Variables:
249 // mode: php
250 // tab-width: 8
251 // c-basic-offset: 4
252 // c-hanging-comment-ender-p: nil
253 // indent-tabs-mode: nil
254 // End: