]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/Calendar.php
don't display the SQL dsn connection password
[SourceForge/phpwiki.git] / lib / plugin / Calendar.php
1 <?php // -*-php-*-
2 rcs_id('$Id: Calendar.php,v 1.26 2003-01-18 21:19:25 carstenklapp 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.26 $");
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     function __header($pagename, $time) {
66         $args = &$this->args;
67
68         $t = localtime($time - SECONDS_PER_DAY, 1);
69         $prev_url = WikiURL($pagename, array('month' => $t['tm_mon'] + 1,
70                                              'year'  => $t['tm_year'] + 1900));
71
72         $t = localtime($time + 32 * SECONDS_PER_DAY, 1);
73         $next_url = WikiURL($pagename, array('month' => $t['tm_mon'] + 1,
74                                              'year'  => $t['tm_year'] + 1900));
75
76         $prev = HTML::a(array('href'  => $prev_url,
77                               'class' => 'cal-arrow',
78                               'title' => _("Previous Month")),
79                         '<');
80         $next = HTML::a(array('href'  => $next_url,
81                               'class' => 'cal-arrow',
82                               'title' => _("Next Month")),
83                         '>');
84
85
86         $row = HTML::tr(HTML::td(array('align' => 'left'), $prev),
87                         HTML::td(array('align' => 'center'),
88                                  HTML::strong(array('class' => 'cal-header'),
89                                               strftime($args['month_format'],
90                                                        $time))),
91                         HTML::td(array('align' => 'right'), $next));
92
93         return HTML::tr(HTML::td(array('colspan' => 7,
94                                        'align'   => 'center'),
95                                  HTML::table(array('width' => '100%',
96                                                    'class' => 'cal-header'),
97                                              $row)));
98     }
99
100
101     function __daynames($start_wday) {
102         $time  = mktime(12, 0, 0, 1, 1, 2001);
103         $t     = localtime($time, 1);
104         $time += (7 + $start_wday - $t['tm_wday']) * SECONDS_PER_DAY;
105
106         $t = localtime($time, 1);
107         assert($t['tm_wday'] == $start_wday);
108
109         $fs = $this->args['wday_format'];
110         $row = HTML::tr();
111         $row->setattr('class', 'cal-dayname');
112         for ($i = 0; $i < 7; $i++) {
113             $row->pushContent(HTML::td(array('class' => 'cal-dayname',
114                                              'align' => 'center'),
115                                        strftime($fs, $time)));
116             $time += SECONDS_PER_DAY;
117         }
118         return $row;
119     }
120
121     function __date($dbi, $time) {
122         $args = &$this->args;
123
124         $page_for_date = $args['prefix'] . strftime($args['date_format'],
125                                                     $time);
126         $t = localtime($time, 1);
127
128         $td = HTML::td(array('align' => 'center'));
129
130         $mday = $t['tm_mday'];
131         if ($mday == $this->_today) {
132             $mday = HTML::strong($mday);
133             $td->setAttr('class', 'cal-today');
134         }
135         else if ($dbi->isWikiPage($page_for_date)) {
136             $td->setAttr('class', 'cal-day');
137         }
138
139         if ($dbi->isWikiPage($page_for_date)) {
140             $date = HTML::a(array('class' => 'cal-day',
141                                   'href'  => WikiURL($page_for_date),
142                                   'title' => $page_for_date),
143                             HTML::em($mday));
144         }
145         else {
146             $date = HTML::a(array('class' => 'cal-hide',
147                                   'href'  => WikiURL($page_for_date,
148                                                      array('action' => 'edit')),
149                                   'title' => sprintf(_("Edit %s"),
150                                                      $page_for_date)),
151                             $mday);
152         }
153         $td->pushContent(HTML::raw('&nbsp;'), $date, HTML::raw('&nbsp;'));
154         return $td;
155     }
156
157     function run($dbi, $argstr, $request) {
158         $this->args = $this->getArgs($argstr, $request);
159         $args       = &$this->args;
160
161         $now = localtime(time() + 3600 * $request->getPref('timeOffset'), 1);
162         foreach ( array('month' => $now['tm_mon'] + 1,
163                         'year'  => $now['tm_year'] + 1900)
164                   as $param => $dflt ) {
165
166             if (!($args[$param] = intval($args[$param])))
167                 $args[$param]   = $dflt;
168         }
169
170         $time = mktime(12, 0, 0,                               // hh, mm, ss,
171                        $args['month'] + $args['month_offset'], // month (1-12)
172                        1,                                      // mday (1-31)
173                        $args['year']);
174
175         $cal = HTML::table(array('cellspacing' => 0,
176                                  'cellpadding' => 2,
177                                  'class'       => 'cal'),
178                            HTML::thead(
179                                        $this->__header($request->getArg('pagename'),
180                                                        $time),
181                                        $this->__daynames($args['start_wday'])));
182
183         $t = localtime($time, 1);
184
185         if ($now['tm_year'] == $t['tm_year'] && $now['tm_mon'] == $t['tm_mon'])
186             $this->_today = $now['tm_mday'];
187         else
188             $this->_today = false;
189
190         $tbody = HTML::tbody();
191         $row = HTML::tr();
192
193         $col = (7 + $t['tm_wday'] - $args['start_wday']) % 7;
194         if ($col > 0)
195             $row->pushContent(HTML::td(array('colspan' => $col)));
196         $done = false;
197
198         while (!$done) {
199             $row->pushContent($this->__date($dbi, $time));
200
201             if (++$col % 7 == 0) {
202                 $tbody->pushContent($row);
203                 $col = 0;
204                 $row = HTML::tr();
205             }
206
207             $time += SECONDS_PER_DAY;
208             $t     = localtime($time, 1);
209             $done  = $t['tm_mday'] == 1;
210         }
211
212         if ($row->getContent()) {
213             $row->pushContent(HTML::td(array('colspan' => (42 - $col) % 7)));
214             $tbody->pushContent($row);
215         }
216         $cal->pushContent($tbody);
217         return $cal;
218     }
219 };
220
221 // $Log: not supported by cvs2svn $
222
223 // For emacs users
224 // Local Variables:
225 // mode: php
226 // tab-width: 8
227 // c-basic-offset: 4
228 // c-hanging-comment-ender-p: nil
229 // indent-tabs-mode: nil
230 // End:
231 ?>