4 * * Copyright 1999-2002,2005-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 PhpWiki; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 // if not defined in config.ini
24 if (!defined('PLUGIN_CALENDARLIST_ORDER'))
25 define('PLUGIN_CALENDARLIST_ORDER', 'normal');
26 if (!defined('PLUGIN_CALENDARLIST_NEXT_N_DAYS'))
27 define('PLUGIN_CALENDARLIST_NEXT_N_DAYS', '');
28 if (!defined('PLUGIN_CALENDARLIST_NEXT_N'))
29 define('PLUGIN_CALENDARLIST_NEXT_N', '');
30 if (!defined('PLUGIN_CALENDARLIST_LAST_N_DAYS'))
31 define('PLUGIN_CALENDARLIST_LAST_N_DAYS', '');
32 if (!defined('PLUGIN_CALENDARLIST_LAST_N'))
33 define('PLUGIN_CALENDARLIST_LAST_N', '');
36 * This is a list of calendar appointments.
37 * Same arguments as Calendar, so no one is confused
38 * Uses <dl><dd>DATE<dt>page contents...
39 * Derived from Calendar.php by Martin Norbäck <martin@safelogic.se>
41 * Insert this plugin into your Calendar page, for example in WikiUser/Calendar:
45 * Honors now year + month args as start base - together with Calendar navigation.
46 * The behaviour before 2007 with last/next_n_days was to start now.
49 class WikiPlugin_CalendarList
54 return _("CalendarList");
57 function getDescription()
59 return _("CalendarList");
62 function getDefaultArguments()
64 return array('prefix' => '[pagename]',
65 'date_format' => '%Y-%m-%d',
66 'order' => PLUGIN_CALENDARLIST_ORDER, // normal or reverse (report sequence)
70 //support ranges: next n days/events
71 'next_n_days' => PLUGIN_CALENDARLIST_NEXT_N_DAYS, // one or the other, not both
72 'next_n' => PLUGIN_CALENDARLIST_NEXT_N,
73 // last n days/entries:
74 'last_n_days' => PLUGIN_CALENDARLIST_LAST_N_DAYS, // one or the other, not both
75 'last_n' => PLUGIN_CALENDARLIST_LAST_N,
77 'month_format' => '%B %Y',
78 'wday_format' => '%a',
83 * return links (static only as of action=edit)
85 * @param string $argstr The plugin argument string.
86 * @param string $basepage The pagename the plugin is invoked from.
87 * @return array List of pagenames linked to (or false).
89 function getWikiPageLinks($argstr, $basepage)
91 if (isset($this->_links))
95 $this->run($request->_dbi, $argstr, $request, $basepage);
100 private function _count_events($dbi, $n = 7, $direction = 1)
102 // This is used by the last_n/next_n options to determine the date that
103 // accounts for the number of N events in the past/future.
104 // RETURNS: date of N-th event or the last item found
105 $args = &$this->args; // gather the args array
106 $timeTMP = time(); // start with today's date
107 $t = $timeTMP; // init the control date variable to now
109 for ($i = 0; $i <= 180; $i++) { // loop thru 180 days, past or future
110 $date_string = strftime($args['date_format'], $t);
111 $page_for_date = $args['prefix'] . SUBPAGE_SEPARATOR . $date_string;
112 if ($dbi->isWikiPage($page_for_date)) { // if this date has any comments/events
113 $timeTMP = $t; // capture the date of this event for return
114 if ($n-- <= 0) break; // if we reached the limit, return the date
116 $t += 24 * 3600 * $direction; // advance one day back or forward
119 // return the date of the N-th or last, most past/future event in the range
123 private function _date($dbi, $time)
125 $args = &$this->args;
126 $date_string = strftime($args['date_format'], $time);
128 $page_for_date = $args['prefix'] . SUBPAGE_SEPARATOR . $date_string;
130 if ($dbi->isWikiPage($page_for_date)) {
131 // Extract the page contents for this date
132 $p = $dbi->getPage($page_for_date);
133 $r = $p->getCurrentRevision();
134 $c = $r->getContent();
135 include_once 'lib/BlockParser.php';
136 $content = TransformText(implode("\n", $c), $r->get('markup'));
137 $link = HTML::a(array('class' => 'cal-hide',
138 'href' => WikiURL($page_for_date,
139 array('action' => 'edit')),
140 'title' => sprintf(_("Edit %s"), $page_for_date)),
142 $this->_links[] = $page_for_date;
143 $a = array(HTML::dt($link), HTML::dd($content));
150 function run($dbi, $argstr, &$request, $basepage)
152 $this->args = $this->getArgs($argstr, $request);
153 $args = &$this->args;
154 $this->_links = array();
156 // default to this month
157 $now = localtime(time() + 3600 * $request->getPref('timeOffset'), 1);
158 $args['mday'] = $now['tm_mday'];
159 foreach (array('month' => $now['tm_mon'] + 1,
160 'year' => $now['tm_year'] + 1900,
161 'mday' => $now['tm_mday'])
162 as $param => $dflt) {
163 if (!($args[$param] = intval($args[$param])))
164 $args[$param] = $dflt;
166 $base = mktime(0, 0, 0, // h, m, s
167 $args['month'], // month 1-12
169 $args['year']); // must have base 1900
171 // ***************************************************
172 // start of Plugin CalendarList display logic
173 // determine start date
174 if ($args['last_n_days']) { // back by month
175 // n days ago, affected by month or month_offset
176 $start = $base - ($args['last_n_days'] * 24 * 3600.0);
177 } elseif ($args['last_n']) {
178 // get date for last nth event
179 $start = $this->_count_events($dbi, $args['last_n'], -1);
181 // start of requested month
182 $start = mktime(0, 0, 0, // h, m, s
183 $args['month'] + $args['month_offset'], // month (1-12)
188 // determine end date
189 if ($args['next_n_days']) {
190 // n days from now, affected by month and year
191 $end = $base + ($args['next_n_days'] * 24 * 3600.0);
192 } elseif ($args['last_n']) {
193 // get date for next nth event
194 $end = $this->_count_events($dbi, $args['next_n'], 1);
196 // trick to get last day of requested month
197 $end = mktime(0, 0, -1, // h, m, s
198 $args['month'] + 1 + $args['month_offset'], // month (1-12)
203 // switch values for reverse order
205 if ($args['order'] == 'reverse') {
212 // style tag on wiki description but not in here
215 // loop through dates and create list
216 for ($i = $start; ($step > 0) ? $i < $end : $i > $end; $i += $step) {
217 $cal->pushContent($this->_date($dbi, $i));
219 // end of Plugin CalendarList display logic
220 // ***************************************************
230 // c-hanging-comment-ender-p: nil
231 // indent-tabs-mode: nil