]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/CalendarList.php
Support ranges, based on a simple patch by JoshWand
[SourceForge/phpwiki.git] / lib / plugin / CalendarList.php
1 <?php // -*-php-*-
2 rcs_id('$Id: CalendarList.php,v 1.3 2004-09-22 13:36:45 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 appoinments. 
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                      'year'         => '',
33                      'month'        => '',
34                      'month_offset' => 0,
35                      //support ranges, based on a simple patch by JoshWand
36                      'next_n_days'  => '',
37                      'last_n_days'  => '',
38                      // next or last n entries:
39                      'next_n'  => '',
40                      //'last_n'  => '', // not yet
41
42                      'month_format' => '%B, %Y',
43                      'wday_format'  => '%a',
44                      'start_wday'   => '0');
45     }
46
47     function __date($dbi, $time) {
48         $args = &$this->args;
49         $date_string = strftime($args['date_format'], $time);
50
51         $page_for_date = $args['prefix'] . SUBPAGE_SEPARATOR . $date_string;
52         $t = localtime($time, 1);
53
54         $td = HTML::td(array('align' => 'center'));
55
56         if ($dbi->isWikiPage($page_for_date)) {
57             // Extract the page contents for this date
58             $p = $dbi->getPage($page_for_date);
59             $r = $p->getCurrentRevision();
60             $c = $r->getContent();
61             include_once('lib/BlockParser.php');
62             $content = TransformText(implode("\n", $c), $r->get('markup'));
63             $link = HTML::a(array('class' => 'cal-hide',
64                                   'href'  => WikiURL($page_for_date,
65                                                      array('action' => 'edit')),
66                                   'title' => sprintf(_("Edit %s"), $page_for_date)),
67                             $date_string);
68             $a = array(HTML::dt($link), HTML::dd($content));
69         } else {
70           $a = array();
71         }
72         return $a;
73     }
74
75     function run($dbi, $argstr, &$request, $basepage) {
76         $this->args = $this->getArgs($argstr, $request);
77         $args       = &$this->args;
78
79         $now = localtime(time() + 3600 * $request->getPref('timeOffset'), 1);
80         foreach ( array('month' => $now['tm_mon'] + 1,
81                         'year'  => $now['tm_year'] + 1900)
82                   as $param => $dflt ) {
83
84             if (!($args[$param] = intval($args[$param])))
85                 $args[$param]   = $dflt;
86         }
87         if ($args['last_n_days']) {
88             $time = mktime(0, 0, 0,                            // hh, mm, ss,
89                            $args['month'] + $args['month_offset'], // month (1-12)
90                            $now['tm_mday'] - $args['last_n_days'],
91                            $args['year']);
92         } elseif ($args['next_n_days'] || $args['next_n']/* || $args['last_n']*/) {
93             $time = mktime(0, 0, 0,                            // hh, mm, ss,
94                            $args['month'] + $args['month_offset'], // month (1-12)
95                            $now['tm_mday'] ,                   // starting today
96                            $args['year']);
97         } else {
98             $time = mktime(12, 0, 0,                           // hh, mm, ss,
99                            $args['month'] + $args['month_offset'], // month (1-12)
100                            1,                                   // starting at monday
101                            $args['year']);
102         } 
103         $t = localtime($time, 1);
104
105         if ($now['tm_year'] == $t['tm_year'] && $now['tm_mon'] == $t['tm_mon'])
106             $this->_today = $now['tm_mday'];
107         else
108             $this->_today = false;
109
110         $cal = HTML::dl();
111
112         $done = false;
113         $n = 0; $max = (180 * SECONDS_PER_DAY) + $time;
114         while (!$done) {
115             $success = $cal->pushContent($this->__date($dbi, $time));
116             $time += SECONDS_PER_DAY;
117             if ($time >= $max) return $cal;
118
119             $t     = localtime($time, 1);
120             if ($args['next_n_days']) {
121                 if ($n == $args['next_n_days']) return $cal;
122                 $n++;
123             } elseif ($args['next_n']) {
124                 if ($n == $args['next_n']) return $cal;
125                 if (!empty($success))
126                     $n++;
127             } elseif ($args['last_n_days']) {
128                 $done = ($t['tm_mday'] == $now['tm_mday']);
129             } else {
130                 $done = ($t['tm_mday'] == 1);
131             }
132         }
133         return $cal;
134     }
135 };
136
137
138 // $Log: not supported by cvs2svn $
139 // Revision 1.2  2004/02/17 12:11:36  rurban
140 // 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, ...)
141 //
142 // Revision 1.1  2003/11/18 19:06:03  carstenklapp
143 // New plugin to be used in conjunction with the Calendar plugin.
144 // Upgraded to use SUBPAGE_SEPARATOR for subpages. SF patch tracker
145 // submission 565369.
146 //
147
148
149 // For emacs users
150 // Local Variables:
151 // mode: php
152 // tab-width: 8
153 // c-basic-offset: 4
154 // c-hanging-comment-ender-p: nil
155 // indent-tabs-mode: nil
156 // End:
157 ?>