]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/Calendar.php
No comma in default
[SourceForge/phpwiki.git] / lib / plugin / Calendar.php
1 <?php // -*-php-*-
2 rcs_id('$Id: Calendar.php,v 1.35 2008-08-17 07:45:15 vargenau Exp $');
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
19  along with PhpWiki; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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 getVersion() {
49         return preg_replace("/[Revision: $]/", '',
50                             "\$Revision: 1.35 $");
51     }
52
53     function getDefaultArguments() {
54         return array('prefix'           => '[pagename]' . SUBPAGE_SEPARATOR,
55                      'date_format'      => '%Y-%m-%d',
56                      'year'             => '',
57                      'month'            => '',
58                      'month_offset'     => 0,
59                      'month_format'     => '%B %Y',
60                      'wday_format'      => '%a',
61                      'start_wday'       => '1', // start now with Monday
62                      'display_weeknum'  => 0);
63     }
64
65     /**
66      * return links (static only as of action=edit) 
67      *
68      * @param string $argstr The plugin argument string.
69      * @param string $basepage The pagename the plugin is invoked from.
70      * @return array List of pagenames linked to (or false).
71      */
72     function getWikiPageLinks ($argstr, $basepage) {
73         if (isset($this->_links)) 
74             return $this->_links;
75         else {
76             global $request;    
77             $this->run($request->_dbi, $argstr, $request, $basepage);
78             return $this->_links;
79         }
80     }
81
82     function __header($pagename, $time) {
83         $args = &$this->args;
84
85         $t = localtime($time - SECONDS_PER_DAY, 1);
86         $prev_url = WikiURL($pagename, array('month' => $t['tm_mon'] + 1,
87                                              'year'  => $t['tm_year'] + 1900));
88
89         $t = localtime($time + 32 * SECONDS_PER_DAY, 1);
90         $next_url = WikiURL($pagename, array('month' => $t['tm_mon'] + 1,
91                                              'year'  => $t['tm_year'] + 1900));
92
93         $prev = HTML::a(array('href'  => $prev_url,
94                               'class' => 'cal-arrow',
95                               'title' => _("Previous Month")),
96                         '<');
97         $next = HTML::a(array('href'  => $next_url,
98                               'class' => 'cal-arrow',
99                               'title' => _("Next Month")),
100                         '>');
101
102
103         $row = HTML::tr(HTML::td(array('align' => 'left'), $prev),
104                         HTML::td(array('align' => 'center'),
105                                  HTML::strong(array('class' => 'cal-header'),
106                                               strftime($args['month_format'],
107                                                        $time))),
108                         HTML::td(array('align' => 'right'), $next));
109
110         return HTML::tr(HTML::td(array('colspan' => $args['display_weeknum'] ? 8 : 7,
111                                        'align'   => 'center'),
112                                  HTML::table(array('width' => '100%',
113                                                    'class' => 'cal-header'),
114                                              $row)));
115     }
116
117
118     function __daynames($start_wday) {
119         $time  = mktime(12, 0, 0, 1, 1, 2001);
120         $t     = localtime($time, 1);
121         $time += (7 + $start_wday - $t['tm_wday']) * SECONDS_PER_DAY;
122
123         $t = localtime($time, 1);
124         assert($t['tm_wday'] == $start_wday);
125
126         $fs = $this->args['wday_format'];
127         $row = HTML::tr();
128         $row->setattr('class', 'cal-dayname');
129         if ($this->args['display_weeknum'])
130             $row->pushContent(HTML::td(array('class' => 'cal-dayname',
131                                              'align' => 'center'),
132                                        _("Wk")));
133         for ($i = 0; $i < 7; $i++) {
134             $row->pushContent(HTML::td(array('class' => 'cal-dayname',
135                                              'align' => 'center'),
136                                        strftime($fs, $time)));
137             $time += SECONDS_PER_DAY;
138         }
139         return $row;
140     }
141
142     function __date($dbi, $time) {
143         $args = &$this->args;
144
145         $page_for_date = $args['prefix'] . strftime($args['date_format'],
146                                                     $time);
147         $t = localtime($time, 1);
148
149         $td = HTML::td(array('align' => 'center'));
150
151         $mday = $t['tm_mday'];
152         if ($mday == $this->_today) {
153             $mday = HTML::strong($mday);
154             $td->setAttr('class', 'cal-today');
155         }
156         else if ($dbi->isWikiPage($page_for_date)) {
157             $this->_links[] = $page_for_date;
158             $td->setAttr('class', 'cal-day');
159         }
160
161         if ($dbi->isWikiPage($page_for_date)) {
162             $this->_links[] = $page_for_date;
163             $date = HTML::a(array('class' => 'cal-day',
164                                   'href'  => WikiURL($page_for_date),
165                                   'title' => $page_for_date),
166                             HTML::em($mday));
167         }
168         else {
169             $date = HTML::a(array('class' => 'cal-hide',
170                                   'rel'   => 'nofollow',
171                                   'href'  => WikiURL($page_for_date,
172                                                      array('action' => 'edit')),
173                                   'title' => sprintf(_("Edit %s"),
174                                                      $page_for_date)),
175                             $mday);
176         }
177         $td->pushContent(HTML::raw('&nbsp;'), $date, HTML::raw('&nbsp;'));
178         return $td;
179     }
180
181     function run($dbi, $argstr, &$request, $basepage) {
182         $this->args = $this->getArgs($argstr, $request);
183         $args       = &$this->args;
184         $this->_links = array();
185
186         $now = localtime(time() + 3600 * $request->getPref('timeOffset'), 1);
187         foreach ( array('month' => $now['tm_mon'] + 1,
188                         'year'  => $now['tm_year'] + 1900)
189                   as $param => $dflt ) {
190
191             if (!($args[$param] = intval($args[$param])))
192                 $args[$param]   = $dflt;
193         }
194
195         $time = mktime(12, 0, 0,                               // hh, mm, ss,
196                        $args['month'] + $args['month_offset'], // month (1-12)
197                        1,                                      // mday (1-31)
198                        $args['year']);
199
200         $colnum = $args['display_weeknum'] ? 8 : 7;
201         $cal = HTML::table(array('cellspacing' => 0,
202                                  'cellpadding' => 2,
203                                  'class'       => 'cal'),
204                            HTML::thead(
205                                        $this->__header($request->getArg('pagename'),
206                                                        $time),
207                                        $this->__daynames($args['start_wday'])));
208
209         $t = localtime($time, 1);
210
211         if ($now['tm_year'] == $t['tm_year'] && $now['tm_mon'] == $t['tm_mon'])
212             $this->_today = $now['tm_mday'];
213         else
214             $this->_today = false;
215
216         $tbody = HTML::tbody();
217         $row = HTML::tr();
218
219         if ($args['display_weeknum'])
220             $row->pushContent(HTML::td(array('class' => 'cal-weeknum'),
221                                        ((int)strftime("%U", $time))+1)); // %U problem. starts with 0
222         $col = (7 + $t['tm_wday'] - $args['start_wday']) % 7;
223         if ($col > 0)
224             $row->pushContent(HTML::td(array('colspan' => $col)));
225         $done = false;
226         while (!$done) {
227             $row->pushContent($this->__date($dbi, $time));
228
229             if (++$col % 7 == 0) {
230                 $tbody->pushContent($row);
231                 $col = 0;
232                 $row = HTML::tr();
233             }
234
235             $time += SECONDS_PER_DAY;
236             $t     = localtime($time, 1);
237             $done  = $t['tm_mday'] == 1;
238             if (!$col and !$done and $args['display_weeknum'])
239                 $row->pushContent(HTML::td(array('class' => 'cal-weeknum'),
240                                            ((int)strftime("%U", $time))+1)); // starts with 0
241         }
242
243         if ($row->getContent()) {
244             $row->pushContent(HTML::td(array('colspan' => (42 - $col) % 7)));
245             $tbody->pushContent($row);
246         }
247         $cal->pushContent($tbody);
248         return $cal;
249     }
250 };
251
252 // $Log: not supported by cvs2svn $
253 // Revision 1.34  2007/08/25 18:52:21  rurban
254 // Calendar weekday: change from Sunday to start now with Monday
255 //
256 // Revision 1.33  2007/01/22 23:48:54  rurban
257 // Fix Calendar %U: weeknum starting with 1
258 //
259 // Revision 1.32  2007/01/03 21:23:24  rurban
260 // add display_weeknum support.
261 //
262 // Revision 1.31  2006/03/19 14:26:29  rurban
263 // sf.net patch by Matt Brown: Add rel=nofollow to more actions
264 //
265 // Revision 1.30  2005/04/02 03:05:44  uckelman
266 // Removed & from vars passed by reference (not needed, causes PHP to complain).
267 //
268 // Revision 1.29  2004/12/06 19:15:04  rurban
269 // save edit-time links as requested in #946679
270 //
271 // Revision 1.28  2004/05/08 14:06:13  rurban
272 // new support for inlined image attributes: [image.jpg size=50x30 align=right]
273 // minor stability and portability fixes
274 //
275 // Revision 1.27  2004/02/17 12:11:36  rurban
276 // added missing 4th basepage arg at plugin->run() to almost all plugins. This caused no harm so far, because it was silently dropped on normal usage. However on plugin internal ->run invocations it failed. (InterWikiSearch, IncludeSiteMap, ...)
277 //
278 // Revision 1.26  2003/01/18 21:19:25  carstenklapp
279 // Code cleanup:
280 // Reformatting; added copyleft, getVersion, getDescription
281 //
282
283 // For emacs users
284 // Local Variables:
285 // mode: php
286 // tab-width: 8
287 // c-basic-offset: 4
288 // c-hanging-comment-ender-p: nil
289 // indent-tabs-mode: nil
290 // End:
291 ?>