5 * * Copyright 1999-2002,2005-2007 $ThePhpWikiProgrammingTeam
7 * This file is part of PhpWiki.
9 * PhpWiki is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * PhpWiki is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with PhpWiki; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 // if not defined in config.ini
25 if (!defined('PLUGIN_CALENDARLIST_ORDER'))
26 define('PLUGIN_CALENDARLIST_ORDER', 'normal');
27 if (!defined('PLUGIN_CALENDARLIST_NEXT_N_DAYS'))
28 define('PLUGIN_CALENDARLIST_NEXT_N_DAYS','');
29 if (!defined('PLUGIN_CALENDARLIST_NEXT_N'))
30 define('PLUGIN_CALENDARLIST_NEXT_N', '');
31 if (!defined('PLUGIN_CALENDARLIST_LAST_N_DAYS'))
32 define('PLUGIN_CALENDARLIST_LAST_N_DAYS','');
33 if (!defined('PLUGIN_CALENDARLIST_LAST_N'))
34 define('PLUGIN_CALENDARLIST_LAST_N', '');
37 * This is a list of calendar appointments.
38 * Same arguments as Calendar, so no one is confused
39 * Uses <dl><dd>DATE<dt>page contents...
40 * Derived from Calendar.php by Martin Norbäck <martin@safelogic.se>
42 * Insert this plugin into your Calendar page, for example in WikiUser/Calendar:
43 * <?plugin Calendar ?>
44 * <?plugin CalendarList ?>
46 * Honors now year + month args as start base - together with Calendar navigation.
47 * The behaviour before 2007 with last/next_n_days was to start now.
50 class WikiPlugin_CalendarList
54 return _("CalendarList");
57 function getDescription () {
58 return _("CalendarList");
61 function getVersion() {
62 return preg_replace("/[Revision: $]/", '',
66 function getDefaultArguments() {
67 return array('prefix' => '[pagename]',
68 'date_format' => '%Y-%m-%d',
69 'order' => PLUGIN_CALENDARLIST_ORDER, // normal or reverse (report sequence)
73 //support ranges: next n days/events
74 'next_n_days' => PLUGIN_CALENDARLIST_NEXT_N_DAYS, // one or the other, not both
75 'next_n' => PLUGIN_CALENDARLIST_NEXT_N,
76 // last n days/entries:
77 'last_n_days' => PLUGIN_CALENDARLIST_LAST_N_DAYS, // one or the other, not both
78 'last_n' => PLUGIN_CALENDARLIST_LAST_N,
80 'month_format' => '%B %Y',
81 'wday_format' => '%a',
86 * return links (static only as of action=edit)
88 * @param string $argstr The plugin argument string.
89 * @param string $basepage The pagename the plugin is invoked from.
90 * @return array List of pagenames linked to (or false).
92 function getWikiPageLinks ($argstr, $basepage) {
93 if (isset($this->_links))
97 $this->run($request->_dbi, $argstr, $request, $basepage);
102 function _count_events($dbi, $n = 7, $direction = 1) {
103 // This is used by the last_n/next_n options to determine the date that
104 // accounts for the number of N events in the past/future.
105 // RETURNS: date of N-th event or the last item found
106 $args = &$this->args; // gather the args array
107 $timeTMP = time(); // start with today's date
108 $t = $timeTMP; // init the control date variable to now
110 for ($i=0; $i<=180; $i++) { // loop thru 180 days, past or future
111 $date_string = strftime($args['date_format'], $t);
112 $page_for_date = $args['prefix'] . SUBPAGE_SEPARATOR . $date_string;
113 if ($dbi->isWikiPage($page_for_date)) { // if this date has any comments/events
114 $timeTMP = $t; // capture the date of this event for return
115 if ($n-- <= 0) break; // if we reached the limit, return the date
117 $t += 24 * 3600 * $direction; // advance one day back or forward
120 // return the date of the N-th or last, most past/future event in the range
124 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;
129 $t = localtime($time, 1);
131 $td = HTML::td(array('align' => 'center'));
133 if ($dbi->isWikiPage($page_for_date)) {
134 // Extract the page contents for this date
135 $p = $dbi->getPage($page_for_date);
136 $r = $p->getCurrentRevision();
137 $c = $r->getContent();
138 include_once('lib/BlockParser.php');
139 $content = TransformText(implode("\n", $c), $r->get('markup'));
140 $link = HTML::a(array('class' => 'cal-hide',
141 'href' => WikiURL($page_for_date,
142 array('action' => 'edit')),
143 'title' => sprintf(_("Edit %s"), $page_for_date)),
145 $this->_links[] = $page_for_date;
146 $a = array(HTML::dt($link), HTML::dd($content));
153 function run($dbi, $argstr, &$request, $basepage) {
154 $this->args = $this->getArgs($argstr, $request);
155 $args = &$this->args;
156 $this->_links = array();
158 // default to this month
159 $now = localtime(time() + 3600 * $request->getPref('timeOffset'), 1);
160 $args['mday'] = $now['tm_mday'];
161 foreach ( array('month' => $now['tm_mon'] + 1,
162 'year' => $now['tm_year'] + 1900,
163 'mday' => $now['tm_mday'])
166 if (! ($args[$param] = intval($args[$param])))
167 $args[$param] = $dflt;
169 $base = mktime(0, 0, 0, // h, m, s
170 $args['month'], // month 1-12
172 $args['year']); // must have base 1900
174 // ***************************************************
175 // start of Plugin CalendarList display logic
176 // determine start date
177 if ($args['last_n_days']) { // back by month
178 // n days ago, affected by month or month_offset
179 $start = $base - ($args['last_n_days'] * 24 * 3600.0);
181 elseif ($args['last_n']) {
182 // get date for last nth event
183 $start = $this->_count_events($dbi, $args['last_n'], -1);
186 // start of requested month
187 $start = mktime(0, 0, 0, // h, m, s
188 $args['month'] + $args['month_offset'], // month (1-12)
193 // determine end date
194 if ($args['next_n_days']) {
195 // n days from now, affected by month and year
196 $end = $base + ($args['next_n_days'] * 24 * 3600.0);
198 elseif ($args['last_n']) {
199 // get date for next nth event
200 $end = $this->_count_events($dbi, $args['next_n'], 1);
203 // trick to get last day of requested month
204 $end = mktime(0, 0, -1, // h, m, s
205 $args['month'] + 1 + $args['month_offset'], // month (1-12)
210 // switch values for reverse order
212 if ($args['order'] == 'reverse') {
219 // style tag on wiki description but not in here
222 // loop through dates and create list
223 for ($i = $start; ($step > 0) ? $i < $end : $i > $end; $i += $step) {
224 $cal->pushContent($this->_date($dbi, $i));
226 // end of Plugin CalendarList display logic
227 // ***************************************************
238 // c-hanging-comment-ender-p: nil
239 // indent-tabs-mode: nil