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