]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/Calendar.php
Argh .. get rid of silly DOS \rs
[SourceForge/phpwiki.git] / lib / plugin / Calendar.php
1 <?php // -*-php-*-
2 rcs_id('$Id: Calendar.php,v 1.28 2004-05-08 14:06:13 rurban Exp $');
3 /**
4  Copyright 1999, 2000, 2001, 2002 $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.28 $");
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
60                      'month_format'     => '%B, %Y',
61                      'wday_format'      => '%a',
62                      'start_wday'       => '0');
63     }
64
65     /** Get wiki-pages linked to by plugin invocation.
66      *
67      * A plugin may override this method to add pages to the
68      * link database for the invoking page.
69      *
70      * For example, the IncludePage plugin should override this so
71      * that the including page shows up in the backlinks list for the
72      * included page.
73      *
74      * Not all plugins which generate links to wiki-pages need list
75      * those pages here.
76      *
77      * Note also that currently the links are calculated at page save
78      * time, so only static page links (e.g. those dependent on the PI
79      * args, not the rest of the wikidb state or any request query args)
80      * will work correctly here.
81      *
82      * @param string $argstr The plugin argument string.
83      * @param string $basepage The pagename the plugin is invoked from.
84      * @return array List of pagenames linked to (or false).
85      */
86     function getWikiPageLinks ($argstr, $basepage) {
87         if (isset($this->_links)) return $this->_links;
88         else return false;
89     }
90
91     function __header($pagename, $time) {
92         $args = &$this->args;
93
94         $t = localtime($time - SECONDS_PER_DAY, 1);
95         $prev_url = WikiURL($pagename, array('month' => $t['tm_mon'] + 1,
96                                              'year'  => $t['tm_year'] + 1900));
97
98         $t = localtime($time + 32 * SECONDS_PER_DAY, 1);
99         $next_url = WikiURL($pagename, array('month' => $t['tm_mon'] + 1,
100                                              'year'  => $t['tm_year'] + 1900));
101
102         $prev = HTML::a(array('href'  => $prev_url,
103                               'class' => 'cal-arrow',
104                               'title' => _("Previous Month")),
105                         '<');
106         $next = HTML::a(array('href'  => $next_url,
107                               'class' => 'cal-arrow',
108                               'title' => _("Next Month")),
109                         '>');
110
111
112         $row = HTML::tr(HTML::td(array('align' => 'left'), $prev),
113                         HTML::td(array('align' => 'center'),
114                                  HTML::strong(array('class' => 'cal-header'),
115                                               strftime($args['month_format'],
116                                                        $time))),
117                         HTML::td(array('align' => 'right'), $next));
118
119         return HTML::tr(HTML::td(array('colspan' => 7,
120                                        'align'   => 'center'),
121                                  HTML::table(array('width' => '100%',
122                                                    'class' => 'cal-header'),
123                                              $row)));
124     }
125
126
127     function __daynames($start_wday) {
128         $time  = mktime(12, 0, 0, 1, 1, 2001);
129         $t     = localtime($time, 1);
130         $time += (7 + $start_wday - $t['tm_wday']) * SECONDS_PER_DAY;
131
132         $t = localtime($time, 1);
133         assert($t['tm_wday'] == $start_wday);
134
135         $fs = $this->args['wday_format'];
136         $row = HTML::tr();
137         $row->setattr('class', 'cal-dayname');
138         for ($i = 0; $i < 7; $i++) {
139             $row->pushContent(HTML::td(array('class' => 'cal-dayname',
140                                              'align' => 'center'),
141                                        strftime($fs, $time)));
142             $time += SECONDS_PER_DAY;
143         }
144         return $row;
145     }
146
147     function __date($dbi, $time) {
148         $args = &$this->args;
149
150         $page_for_date = $args['prefix'] . strftime($args['date_format'],
151                                                     $time);
152         $t = localtime($time, 1);
153
154         $td = HTML::td(array('align' => 'center'));
155
156         $mday = $t['tm_mday'];
157         if ($mday == $this->_today) {
158             $mday = HTML::strong($mday);
159             $td->setAttr('class', 'cal-today');
160         }
161         else if ($dbi->isWikiPage($page_for_date)) {
162             $this->_links[] = $page_for_date;
163             $td->setAttr('class', 'cal-day');
164         }
165
166         if ($dbi->isWikiPage($page_for_date)) {
167             $this->_links[] = $page_for_date;
168             $date = HTML::a(array('class' => 'cal-day',
169                                   'href'  => WikiURL($page_for_date),
170                                   'title' => $page_for_date),
171                             HTML::em($mday));
172         }
173         else {
174             $date = HTML::a(array('class' => 'cal-hide',
175                                   'href'  => WikiURL($page_for_date,
176                                                      array('action' => 'edit')),
177                                   'title' => sprintf(_("Edit %s"),
178                                                      $page_for_date)),
179                             $mday);
180         }
181         $td->pushContent(HTML::raw('&nbsp;'), $date, HTML::raw('&nbsp;'));
182         return $td;
183     }
184
185     function run($dbi, $argstr, &$request, $basepage) {
186         $this->args = $this->getArgs($argstr, $request);
187         $args       = &$this->args;
188         $this->_links = array();
189
190         $now = localtime(time() + 3600 * $request->getPref('timeOffset'), 1);
191         foreach ( array('month' => $now['tm_mon'] + 1,
192                         'year'  => $now['tm_year'] + 1900)
193                   as $param => $dflt ) {
194
195             if (!($args[$param] = intval($args[$param])))
196                 $args[$param]   = $dflt;
197         }
198
199         $time = mktime(12, 0, 0,                               // hh, mm, ss,
200                        $args['month'] + $args['month_offset'], // month (1-12)
201                        1,                                      // mday (1-31)
202                        $args['year']);
203
204         $cal = HTML::table(array('cellspacing' => 0,
205                                  'cellpadding' => 2,
206                                  'class'       => 'cal'),
207                            HTML::thead(
208                                        $this->__header($request->getArg('pagename'),
209                                                        $time),
210                                        $this->__daynames($args['start_wday'])));
211
212         $t = localtime($time, 1);
213
214         if ($now['tm_year'] == $t['tm_year'] && $now['tm_mon'] == $t['tm_mon'])
215             $this->_today = $now['tm_mday'];
216         else
217             $this->_today = false;
218
219         $tbody = HTML::tbody();
220         $row = HTML::tr();
221
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
227         while (!$done) {
228             $row->pushContent($this->__date($dbi, $time));
229
230             if (++$col % 7 == 0) {
231                 $tbody->pushContent($row);
232                 $col = 0;
233                 $row = HTML::tr();
234             }
235
236             $time += SECONDS_PER_DAY;
237             $t     = localtime($time, 1);
238             $done  = $t['tm_mday'] == 1;
239         }
240
241         if ($row->getContent()) {
242             $row->pushContent(HTML::td(array('colspan' => (42 - $col) % 7)));
243             $tbody->pushContent($row);
244         }
245         $cal->pushContent($tbody);
246         return $cal;
247     }
248 };
249
250 // $Log: not supported by cvs2svn $
251 // Revision 1.27  2004/02/17 12:11:36  rurban
252 // 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, ...)
253 //
254 // Revision 1.26  2003/01/18 21:19:25  carstenklapp
255 // Code cleanup:
256 // Reformatting; added copyleft, getVersion, getDescription
257 //
258
259 // For emacs users
260 // Local Variables:
261 // mode: php
262 // tab-width: 8
263 // c-basic-offset: 4
264 // c-hanging-comment-ender-p: nil
265 // indent-tabs-mode: nil
266 // End:
267 ?>