]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/CalendarList.php
added order=reverse: feature request from #981109
[SourceForge/phpwiki.git] / lib / plugin / CalendarList.php
1 <?php // -*-php-*-
2 rcs_id('$Id: CalendarList.php,v 1.4 2004-12-06 18:32:39 rurban Exp $');
3
4 if (!defined('SECONDS_PER_DAY'))
5 define('SECONDS_PER_DAY', 24 * 3600);
6
7 /**
8  * This is a list of calendar appointments. 
9  * Same arguments as Calendar, so no one is confused
10  * Uses <dl><dd>DATE<dt>page contents...
11  * Derived from Calendar.php by Martin Norbäck <martin@safelogic.se>
12  *
13  * Insert this plugin into your Calendar page, for example in:
14  *     WikiUser/Calendar
15  * Add the line: <?plugin CalendarList ?>
16  *
17  */
18 class WikiPlugin_CalendarList
19 extends WikiPlugin
20 {
21     function getName () {
22         return _("CalendarList");
23     }
24
25     function getDescription () {
26         return _("CalendarList");
27     }
28
29     function getDefaultArguments() {
30         return array('prefix'       => '[pagename]',
31                      'date_format'  => '%Y-%m-%d',
32                      'order'        => 'normal', // or reverse (counting backwards)
33                      'year'         => '',
34                      'month'        => '',
35                      'month_offset' => 0,
36                      //support ranges, based on a simple patch by JoshWand
37                      'next_n_days'  => '',
38                      'last_n_days'  => '',
39                      // next or last n entries:
40                      'next_n'  => '',
41                      //'last_n'  => '', // not yet
42
43                      'month_format' => '%B, %Y',
44                      'wday_format'  => '%a',
45                      'start_wday'   => '0');
46     }
47
48     function __date($dbi, $time) {
49         $args = &$this->args;
50         $date_string = strftime($args['date_format'], $time);
51
52         $page_for_date = $args['prefix'] . SUBPAGE_SEPARATOR . $date_string;
53         $t = localtime($time, 1);
54
55         $td = HTML::td(array('align' => 'center'));
56
57         if ($dbi->isWikiPage($page_for_date)) {
58             // Extract the page contents for this date
59             $p = $dbi->getPage($page_for_date);
60             $r = $p->getCurrentRevision();
61             $c = $r->getContent();
62             include_once('lib/BlockParser.php');
63             $content = TransformText(implode("\n", $c), $r->get('markup'));
64             $link = HTML::a(array('class' => 'cal-hide',
65                                   'href'  => WikiURL($page_for_date,
66                                                      array('action' => 'edit')),
67                                   'title' => sprintf(_("Edit %s"), $page_for_date)),
68                             $date_string);
69             $a = array(HTML::dt($link), HTML::dd($content));
70         } else {
71             $a = array();
72         }
73         return $a;
74     }
75
76     function run($dbi, $argstr, &$request, $basepage) {
77         $this->args = $this->getArgs($argstr, $request);
78         $args       = &$this->args;
79
80         $now = localtime(time() + 3600 * $request->getPref('timeOffset'), 1);
81         foreach ( array('month' => $now['tm_mon'] + 1,
82                         'year'  => $now['tm_year'] + 1900)
83                   as $param => $dflt ) {
84
85             if (!($args[$param] = intval($args[$param])))
86                 $args[$param]   = $dflt;
87         }
88         if ($args['last_n_days']) {
89             $time = mktime(0, 0, 0,                            // hh, mm, ss,
90                            $args['month'] + $args['month_offset'], // month (1-12)
91                            $now['tm_mday'] - $args['last_n_days'],
92                            $args['year']);
93         } elseif ($args['next_n_days'] or $args['next_n'] 
94                   or ($args['order'] == 'reverse')
95                  /* or $args['last_n']*/) {
96             $time = mktime(0, 0, 0,                            // hh, mm, ss,
97                            $args['month'] + $args['month_offset'], // month (1-12)
98                            $now['tm_mday'] ,                   // starting today
99                            $args['year']);
100         } else {
101             $time = mktime(12, 0, 0,                           // hh, mm, ss,
102                            $args['month'] + $args['month_offset'], // month (1-12)
103                            1,                                   // starting at monday
104                            $args['year']);
105         } 
106         $t = localtime($time, 1);
107
108         if ($now['tm_year'] == $t['tm_year'] && $now['tm_mon'] == $t['tm_mon'])
109             $this->_today = $now['tm_mday'];
110         else
111             $this->_today = false;
112
113         $cal = HTML::dl();
114
115         $done = false;
116         $n = 0; 
117         if ($args['order'] == "reverse")
118             $max = $time - (180 * SECONDS_PER_DAY);
119         else
120             $max = $time + (180 * SECONDS_PER_DAY);
121         while (!$done) {
122             $success = $cal->pushContent($this->__date($dbi, $time));
123             if ($args['order'] == "reverse") {
124                 $time -= SECONDS_PER_DAY;
125                 if ($time <= $max) return $cal;
126             } else {
127                 $time += SECONDS_PER_DAY;
128                 if ($time >= $max) return $cal;
129             }
130
131             $t     = localtime($time, 1);
132             if ($args['next_n_days']) {
133                 if ($n == $args['next_n_days']) return $cal;
134                 $n++;
135             } elseif ($args['next_n']) {
136                 if ($n == $args['next_n']) return $cal;
137                 if (!empty($success))
138                     $n++;
139             } elseif ($args['last_n_days']) {
140                 $done = ($t['tm_mday'] == $now['tm_mday']);
141             } else { // stop at next/prev month
142                 $done = ($t['tm_mon'] != $now['tm_mon']);
143             }
144         }
145         return $cal;
146     }
147 };
148
149
150 // $Log: not supported by cvs2svn $
151 // Revision 1.3  2004/09/22 13:36:45  rurban
152 // Support ranges, based on a simple patch by JoshWand
153 //   next_n_days, last_n_days, next_n
154 //   last_n not yet
155 //
156 // Revision 1.2  2004/02/17 12:11:36  rurban
157 // added missing 4th basepage arg at plugin->run() to almost all plugins. This caused no harm so far, because it was silently dropped on normal usage. However on plugin internal ->run invocations it failed. (InterWikiSearch, IncludeSiteMap, ...)
158 //
159 // Revision 1.1  2003/11/18 19:06:03  carstenklapp
160 // New plugin to be used in conjunction with the Calendar plugin.
161 // Upgraded to use SUBPAGE_SEPARATOR for subpages. SF patch tracker
162 // submission 565369.
163 //
164
165
166 // For emacs users
167 // Local Variables:
168 // mode: php
169 // tab-width: 8
170 // c-basic-offset: 4
171 // c-hanging-comment-ender-p: nil
172 // indent-tabs-mode: nil
173 // End:
174 ?>