]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/CalendarList.php
Merge OldTextFormattingRules into TextFormattingRules; rename underscore plugins
[SourceForge/phpwiki.git] / lib / plugin / CalendarList.php
1 <?php
2
3 /**
4  * * Copyright 1999-2002,2005-2007 $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 along
19  * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22
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', '');
34
35 /**
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>
40  *
41  * Insert this plugin into your Calendar page, for example in WikiUser/Calendar:
42  *   <<Calendar >>
43  *   <<CalendarList >>
44  *
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.
47  *
48  */
49 class WikiPlugin_CalendarList
50     extends WikiPlugin
51 {
52     function getDescription()
53     {
54         return _("CalendarList");
55     }
56
57     function getDefaultArguments()
58     {
59         return array('prefix' => '[pagename]',
60             'date_format' => '%Y-%m-%d',
61             'order' => PLUGIN_CALENDARLIST_ORDER, // normal or reverse (report sequence)
62             'year' => '',
63             'month' => '',
64             'month_offset' => 0,
65             //support ranges: next n days/events
66             'next_n_days' => PLUGIN_CALENDARLIST_NEXT_N_DAYS, // one or the other, not both
67             'next_n' => PLUGIN_CALENDARLIST_NEXT_N,
68             // last n days/entries:
69             'last_n_days' => PLUGIN_CALENDARLIST_LAST_N_DAYS, // one or the other, not both
70             'last_n' => PLUGIN_CALENDARLIST_LAST_N,
71
72             'month_format' => '%B %Y',
73             'wday_format' => '%a',
74             'start_wday' => '1');
75     }
76
77     /**
78      * return links (static only as of action=edit)
79      *
80      * @param  string $argstr   The plugin argument string.
81      * @param  string $basepage The pagename the plugin is invoked from.
82      * @return array  List of pagenames linked to (or false).
83      */
84     function getWikiPageLinks($argstr, $basepage)
85     {
86         if (isset($this->_links))
87             return $this->_links;
88         else {
89             global $request;
90             $this->run($request->_dbi, $argstr, $request, $basepage);
91             return $this->_links;
92         }
93     }
94
95     private function _count_events($dbi, $n = 7, $direction = 1)
96     {
97         //        This is used by the last_n/next_n options to determine the date that
98         //        accounts for the number of N events in the past/future.
99         //        RETURNS: date of N-th event or the last item found
100         $args = &$this->args; // gather the args array
101         $timeTMP = time(); // start with today's date
102         $t = $timeTMP; // init the control date variable to now
103
104         for ($i = 0; $i <= 180; $i++) { // loop thru 180 days, past or future
105             $date_string = strftime($args['date_format'], $t);
106             $page_for_date = $args['prefix'] . SUBPAGE_SEPARATOR . $date_string;
107             if ($dbi->isWikiPage($page_for_date)) { // if this date has any comments/events
108                 $timeTMP = $t; //  capture the date of this event for return
109                 if ($n-- <= 0) break; //  if we reached the limit, return the date
110             }
111             $t += 24 * 3600 * $direction; // advance one day back or forward
112         }
113
114         // return the date of the N-th or last, most past/future event in the range
115         return $timeTMP;
116     }
117
118     private function _date($dbi, $time)
119     {
120         $args = &$this->args;
121         $date_string = strftime($args['date_format'], $time);
122
123         $page_for_date = $args['prefix'] . SUBPAGE_SEPARATOR . $date_string;
124
125         if ($dbi->isWikiPage($page_for_date)) {
126             // Extract the page contents for this date
127             $p = $dbi->getPage($page_for_date);
128             $r = $p->getCurrentRevision();
129             $c = $r->getContent();
130             include_once 'lib/BlockParser.php';
131             $content = TransformText(implode("\n", $c));
132             $link = HTML::a(array('class' => 'cal-hide',
133                     'href' => WikiURL($page_for_date,
134                         array('action' => 'edit')),
135                     'title' => sprintf(_("Edit %s"), $page_for_date)),
136                 $date_string);
137             $this->_links[] = $page_for_date;
138             $a = array(HTML::dt($link), HTML::dd($content));
139         } else {
140             $a = array();
141         }
142         return $a;
143     }
144
145     function run($dbi, $argstr, &$request, $basepage)
146     {
147         $this->args = $this->getArgs($argstr, $request);
148         $args = &$this->args;
149         $this->_links = array();
150
151         // default to this month
152         $now = localtime(time() + 3600 * $request->getPref('timeOffset'), 1);
153         $args['mday'] = $now['tm_mday'];
154         foreach (array('month' => $now['tm_mon'] + 1,
155                      'year' => $now['tm_year'] + 1900,
156                      'mday' => $now['tm_mday'])
157                  as $param => $dflt) {
158             if (!($args[$param] = intval($args[$param])))
159                 $args[$param] = $dflt;
160         }
161         $base = mktime(0, 0, 0, // h, m, s
162             $args['month'], // month 1-12
163             $args['mday'],
164             $args['year']); // must have base 1900
165
166         // ***************************************************
167         // start of Plugin CalendarList display logic
168         // determine start date
169         if ($args['last_n_days']) { // back by month
170             // n days ago, affected by month or month_offset
171             $start = $base - ($args['last_n_days'] * 24 * 3600.0);
172         } elseif ($args['last_n']) {
173             // get date for last nth event
174             $start = $this->_count_events($dbi, $args['last_n'], -1);
175         } else {
176             // start of requested month
177             $start = mktime(0, 0, 0, // h, m, s
178                 $args['month'] + $args['month_offset'], // month (1-12)
179                 1, // days prior
180                 $args['year']);
181         }
182
183         // determine end date
184         if ($args['next_n_days']) {
185             // n days from now, affected by month and year
186             $end = $base + ($args['next_n_days'] * 24 * 3600.0);
187         } elseif ($args['last_n']) {
188             // get date for next nth event
189             $end = $this->_count_events($dbi, $args['next_n'], 1);
190         } else {
191             // trick to get last day of requested month
192             $end = mktime(0, 0, -1, // h, m, s
193                 $args['month'] + 1 + $args['month_offset'], // month (1-12)
194                 1, // days prior
195                 $args['year']);
196         }
197
198         // switch values for reverse order
199         $step = 24 * 3600;
200         if ($args['order'] == 'reverse') {
201             $time_tmp = $start;
202             $start = $end;
203             $end = $time_tmp;
204             $step *= -1;
205         }
206
207         // style tag on wiki description but not in here
208         $cal = HTML::dl();
209
210         // loop through dates and create list
211         for ($i = $start; ($step > 0) ? $i < $end : $i > $end; $i += $step) {
212             $cal->pushContent($this->_date($dbi, $i));
213         }
214         //        end of Plugin CalendarList display logic
215         // ***************************************************
216
217         return $cal;
218     }
219 }
220
221 // Local Variables:
222 // mode: php
223 // tab-width: 8
224 // c-basic-offset: 4
225 // c-hanging-comment-ender-p: nil
226 // indent-tabs-mode: nil
227 // End: