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