2 // $Id: Calendar.php 7955 2011-03-03 16:41:35Z vargenau $
4 * Copyright 1999,2000,2001,2002,2007 $ThePhpWikiProgrammingTeam
6 * This file is part of PhpWiki.
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.
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.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 if (!defined('SECONDS_PER_DAY'))
24 define('SECONDS_PER_DAY', 24 * 3600);
26 // FIXME: Still needs:
28 // o Better way to navigate to distant months.
29 // (Maybe a form with selectors for month and year)?
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
37 class WikiPlugin_Calendar
44 function getDescription () {
48 function getDefaultArguments() {
49 return array('prefix' => '[pagename]' . SUBPAGE_SEPARATOR,
50 'date_format' => '%Y-%m-%d',
54 'month_format' => '%B %Y',
55 'wday_format' => '%a',
56 'start_wday' => '1', // start now with Monday
57 'display_weeknum' => 0);
61 * return links (static only as of action=edit)
63 * @param string $argstr The plugin argument string.
64 * @param string $basepage The pagename the plugin is invoked from.
65 * @return array List of pagenames linked to (or false).
67 function getWikiPageLinks ($argstr, $basepage) {
68 if (isset($this->_links))
72 $this->run($request->_dbi, $argstr, $request, $basepage);
77 function __header($pagename, $time) {
80 $t = localtime($time - SECONDS_PER_DAY, 1);
81 $prev_url = WikiURL($pagename, array('month' => $t['tm_mon'] + 1,
82 'year' => $t['tm_year'] + 1900));
84 $t = localtime($time + 32 * SECONDS_PER_DAY, 1);
85 $next_url = WikiURL($pagename, array('month' => $t['tm_mon'] + 1,
86 'year' => $t['tm_year'] + 1900));
88 $prev = HTML::a(array('href' => $prev_url,
89 'class' => 'cal-arrow',
90 'title' => _("Previous Month")),
92 $next = HTML::a(array('href' => $next_url,
93 'class' => 'cal-arrow',
94 'title' => _("Next Month")),
98 $row = HTML::tr(HTML::td(array('align' => 'left'), $prev),
99 HTML::td(array('align' => 'center'),
100 HTML::strong(array('class' => 'cal-header'),
101 strftime($args['month_format'],
103 HTML::td(array('align' => 'right'), $next));
105 return HTML::tr(HTML::td(array('colspan' => $args['display_weeknum'] ? 8 : 7,
106 'align' => 'center'),
107 HTML::table(array('width' => '100%',
108 'class' => 'cal-header'),
113 function __daynames($start_wday) {
114 $time = mktime(12, 0, 0, 1, 1, 2001);
115 $t = localtime($time, 1);
116 $time += (7 + $start_wday - $t['tm_wday']) * SECONDS_PER_DAY;
118 $t = localtime($time, 1);
119 assert($t['tm_wday'] == $start_wday);
121 $fs = $this->args['wday_format'];
123 $row->setattr('class', 'cal-dayname');
124 if ($this->args['display_weeknum'])
125 $row->pushContent(HTML::td(array('class' => 'cal-dayname',
126 'align' => 'center'),
128 for ($i = 0; $i < 7; $i++) {
129 $row->pushContent(HTML::td(array('class' => 'cal-dayname',
130 'align' => 'center'),
131 strftime($fs, $time)));
132 $time += SECONDS_PER_DAY;
137 function __date($dbi, $time) {
138 $args = &$this->args;
140 $page_for_date = $args['prefix'] . strftime($args['date_format'],
142 $t = localtime($time, 1);
144 $td = HTML::td(array('align' => 'center'));
146 $mday = $t['tm_mday'];
147 if ($mday == $this->_today) {
148 $mday = HTML::strong($mday);
149 $td->setAttr('class', 'cal-today');
151 else if ($dbi->isWikiPage($page_for_date)) {
152 $this->_links[] = $page_for_date;
153 $td->setAttr('class', 'cal-day');
156 if ($dbi->isWikiPage($page_for_date)) {
157 $this->_links[] = $page_for_date;
158 $date = HTML::a(array('class' => 'cal-day',
159 'href' => WikiURL($page_for_date),
160 'title' => $page_for_date),
164 $date = HTML::a(array('class' => 'cal-hide',
166 'href' => WikiURL($page_for_date,
167 array('action' => 'edit')),
168 'title' => sprintf(_("Edit %s"),
172 $td->pushContent(HTML::raw(' '), $date, HTML::raw(' '));
176 function run($dbi, $argstr, &$request, $basepage) {
177 $this->args = $this->getArgs($argstr, $request);
178 $args = &$this->args;
179 $this->_links = array();
181 $now = localtime(time() + 3600 * $request->getPref('timeOffset'), 1);
182 foreach ( array('month' => $now['tm_mon'] + 1,
183 'year' => $now['tm_year'] + 1900)
184 as $param => $dflt ) {
186 if (!($args[$param] = intval($args[$param])))
187 $args[$param] = $dflt;
190 $time = mktime(12, 0, 0, // hh, mm, ss,
191 $args['month'] + $args['month_offset'], // month (1-12)
195 $colnum = $args['display_weeknum'] ? 8 : 7;
196 $cal = HTML::table(array('cellspacing' => 0,
200 $this->__header($request->getArg('pagename'),
202 $this->__daynames($args['start_wday'])));
204 $t = localtime($time, 1);
206 if ($now['tm_year'] == $t['tm_year'] && $now['tm_mon'] == $t['tm_mon'])
207 $this->_today = $now['tm_mday'];
209 $this->_today = false;
211 $tbody = HTML::tbody();
214 if ($args['display_weeknum'])
215 $row->pushContent(HTML::td(array('class' => 'cal-weeknum'),
216 ((int)strftime("%U", $time))+1)); // %U problem. starts with 0
217 $col = (7 + $t['tm_wday'] - $args['start_wday']) % 7;
219 $row->pushContent(HTML::td(array('colspan' => $col)));
222 $row->pushContent($this->__date($dbi, $time));
224 if (++$col % 7 == 0) {
225 $tbody->pushContent($row);
230 $time += SECONDS_PER_DAY;
231 $t = localtime($time, 1);
232 $done = $t['tm_mday'] == 1;
233 if (!$col and !$done and $args['display_weeknum'])
234 $row->pushContent(HTML::td(array('class' => 'cal-weeknum'),
235 ((int)strftime("%U", $time))+1)); // starts with 0
238 if ($row->getContent()) {
239 $row->pushContent(HTML::td(array('colspan' => (42 - $col) % 7)));
240 $tbody->pushContent($row);
242 $cal->pushContent($tbody);
251 // c-hanging-comment-ender-p: nil
252 // indent-tabs-mode: nil