]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/CalendarList.php
save edit-time links as requested in #946679
[SourceForge/phpwiki.git] / lib / plugin / CalendarList.php
1 <?php // -*-php-*-
2 rcs_id('$Id: CalendarList.php,v 1.5 2004-12-06 19:15:04 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     /**
49      * return links (static only as of action=edit) 
50      *
51      * @param string $argstr The plugin argument string.
52      * @param string $basepage The pagename the plugin is invoked from.
53      * @return array List of pagenames linked to (or false).
54      */
55     function getWikiPageLinks ($argstr, $basepage) {
56         if (isset($this->_links)) 
57             return $this->_links;
58         else {
59             global $request;    
60             $this->run($request->_dbi, $argstr, &$request, $basepage);
61             return $this->_links;
62         }
63     }
64
65     function __date($dbi, $time) {
66         $args = &$this->args;
67         $date_string = strftime($args['date_format'], $time);
68
69         $page_for_date = $args['prefix'] . SUBPAGE_SEPARATOR . $date_string;
70         $t = localtime($time, 1);
71
72         $td = HTML::td(array('align' => 'center'));
73
74         if ($dbi->isWikiPage($page_for_date)) {
75             // Extract the page contents for this date
76             $p = $dbi->getPage($page_for_date);
77             $r = $p->getCurrentRevision();
78             $c = $r->getContent();
79             include_once('lib/BlockParser.php');
80             $content = TransformText(implode("\n", $c), $r->get('markup'));
81             $link = HTML::a(array('class' => 'cal-hide',
82                                   'href'  => WikiURL($page_for_date,
83                                                      array('action' => 'edit')),
84                                   'title' => sprintf(_("Edit %s"), $page_for_date)),
85                             $date_string);
86             $this->_links[] = $page_for_date;
87             $a = array(HTML::dt($link), HTML::dd($content));
88         } else {
89             $a = array();
90         }
91         return $a;
92     }
93
94     function run($dbi, $argstr, &$request, $basepage) {
95         $this->args = $this->getArgs($argstr, $request);
96         $args       = &$this->args;
97         $this->_links = array();
98
99         $now = localtime(time() + 3600 * $request->getPref('timeOffset'), 1);
100         foreach ( array('month' => $now['tm_mon'] + 1,
101                         'year'  => $now['tm_year'] + 1900)
102                   as $param => $dflt ) {
103
104             if (!($args[$param] = intval($args[$param])))
105                 $args[$param]   = $dflt;
106         }
107         if ($args['last_n_days']) {
108             $time = mktime(0, 0, 0,                            // hh, mm, ss,
109                            $args['month'] + $args['month_offset'], // month (1-12)
110                            $now['tm_mday'] - $args['last_n_days'],
111                            $args['year']);
112         } elseif ($args['next_n_days'] or $args['next_n'] 
113                   or ($args['order'] == 'reverse')
114                  /* or $args['last_n']*/) {
115             $time = mktime(0, 0, 0,                            // hh, mm, ss,
116                            $args['month'] + $args['month_offset'], // month (1-12)
117                            $now['tm_mday'] ,                   // starting today
118                            $args['year']);
119         } else {
120             $time = mktime(12, 0, 0,                           // hh, mm, ss,
121                            $args['month'] + $args['month_offset'], // month (1-12)
122                            1,                                   // starting at monday
123                            $args['year']);
124         } 
125         $t = localtime($time, 1);
126
127         if ($now['tm_year'] == $t['tm_year'] && $now['tm_mon'] == $t['tm_mon'])
128             $this->_today = $now['tm_mday'];
129         else
130             $this->_today = false;
131
132         $cal = HTML::dl();
133
134         $done = false;
135         $n = 0; 
136         if ($args['order'] == "reverse")
137             $max = $time - (180 * SECONDS_PER_DAY);
138         else
139             $max = $time + (180 * SECONDS_PER_DAY);
140         while (!$done) {
141             $success = $cal->pushContent($this->__date($dbi, $time));
142             if ($args['order'] == "reverse") {
143                 $time -= SECONDS_PER_DAY;
144                 if ($time <= $max) return $cal;
145             } else {
146                 $time += SECONDS_PER_DAY;
147                 if ($time >= $max) return $cal;
148             }
149
150             $t     = localtime($time, 1);
151             if ($args['next_n_days']) {
152                 if ($n == $args['next_n_days']) return $cal;
153                 $n++;
154             } elseif ($args['next_n']) {
155                 if ($n == $args['next_n']) return $cal;
156                 if (!empty($success))
157                     $n++;
158             } elseif ($args['last_n_days']) {
159                 $done = ($t['tm_mday'] == $now['tm_mday']);
160             } else { // stop at next/prev month
161                 $done = ($t['tm_mon'] != $now['tm_mon']);
162             }
163         }
164         return $cal;
165     }
166 };
167
168
169 // $Log: not supported by cvs2svn $
170 // Revision 1.4  2004/12/06 18:32:39  rurban
171 // added order=reverse: feature request from #981109
172 //
173 // Revision 1.3  2004/09/22 13:36:45  rurban
174 // Support ranges, based on a simple patch by JoshWand
175 //   next_n_days, last_n_days, next_n
176 //   last_n not yet
177 //
178 // Revision 1.2  2004/02/17 12:11:36  rurban
179 // 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, ...)
180 //
181 // Revision 1.1  2003/11/18 19:06:03  carstenklapp
182 // New plugin to be used in conjunction with the Calendar plugin.
183 // Upgraded to use SUBPAGE_SEPARATOR for subpages. SF patch tracker
184 // submission 565369.
185 //
186
187
188 // For emacs users
189 // Local Variables:
190 // mode: php
191 // tab-width: 8
192 // c-basic-offset: 4
193 // c-hanging-comment-ender-p: nil
194 // indent-tabs-mode: nil
195 // End:
196 ?>