]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/Calendar.php
Mysql fix for subpage seperator with USE_PATH_INFO: "." instead of ":"
[SourceForge/phpwiki.git] / lib / plugin / Calendar.php
1 <?php // -*-php-*-
2 rcs_id('$Id: Calendar.php,v 1.6 2002-01-10 19:54:09 rurban Exp $');
3
4 if (!defined('SECONDS_PER_DAY'))
5     define('SECONDS_PER_DAY', 24 * 3600);
6
7 // FIXME: Still needs:
8 //
9 //   o Better way to navigate to distant months.
10 //     (Maybe a form with selectors for month and year)?
11 //
12 //   o Docs.  Write a pgsrc/CalendarPlugin.
13 //
14 // It would be nice to have some way to get from the individual date
15 // pages back to the calendar page. (Subpage support might make this
16 // easier.)
17
18 /**
19  */
20 class WikiPlugin_Calendar
21 extends WikiPlugin
22 {
23     function getName () {
24         return _("Calendar");
25     }
26
27     function getDescription () {
28         return _("Calendar");
29     }
30
31     function getDefaultArguments() {
32         // FIXME: how to exclude multiple pages?
33         return array('prefix'           => (USE_PATH_INFO) ? '[pagename].' : '[pagename]:',
34                      'date_format'      => '%Y-%m-%d',
35                      'year'             => '',
36                      'month'            => '',
37                      'month_offset'     => 0,
38
39                      'month_format'     => '%B, %Y',
40                      'wday_format'      => '%a',
41                      'start_wday'       => '0');
42     }
43
44     function __header($pagename, $time) {
45         $args = &$this->args;
46
47         $t = localtime($time - SECONDS_PER_DAY, 1);
48         $prev_url = WikiURL($pagename, array('month' => $t['tm_mon'] + 1,
49                                              'year'  => $t['tm_year'] + 1900));
50
51         $t = localtime($time + 32 * SECONDS_PER_DAY, 1);
52         $next_url = WikiURL($pagename, array('month' => $t['tm_mon'] + 1,
53                                              'year'  => $t['tm_year'] + 1900));
54
55         $prev = QElement('a', array('href'  => $prev_url,
56                                     'class' => 'cal-arrow',
57                                     'title' => gettext("Previous Month")),
58                          '<');
59         $next = QElement('a', array('href'  => $next_url,
60                                     'class' => 'cal-arrow',
61                                     'title' => gettext("Next Month")),
62                          '>');
63
64
65         $row =  Element('td', array('align' => 'left'), $prev);
66         $row .= Element('td', array('align' => 'center'),
67                         QElement('b', array('class' => 'cal-header'),
68                                  strftime($args['month_format'], $time)));
69         $row .= Element('td', array('align' => 'right'), $next);
70
71         $row =  Element('table', array('width' => '100%'),
72                         Element('tr', $row));
73
74         return Element('tr', Element('td', array('colspan' => 7,
75                                                  'align'   => 'center'),
76                                      $row));
77     }
78
79
80     function __daynames($start_wday) {
81         $time  = mktime(12, 0, 0, 1, 1, 2001);
82         $t     = localtime($time, 1);
83         $time += (7 + $start_wday - $t['tm_wday']) * SECONDS_PER_DAY;
84
85         $t = localtime($time, 1);
86         assert($t['tm_wday'] == $start_wday);
87
88         $fs = $this->args['wday_format'];
89         for ($i = 0; $i < 7; $i++) {
90             $days[$i] = QElement('td', array('class' => 'cal-dayname',
91                                              'align' => 'center'),
92                                  strftime($fs, $time));
93             $time += SECONDS_PER_DAY;
94         }
95         return Element('tr', join('', $days));
96     }
97
98     function __date($dbi, $time) {
99         $args = &$this->args;
100
101         $page_for_date = $args['prefix'] . strftime($args['date_format'],
102                                                     $time);
103         $t = localtime($time, 1);
104
105         if ($dbi->isWikiPage($page_for_date)) {
106             $date = Element('a', array('class' => 'cal-day',
107                                        'href'  => WikiURL($page_for_date),
108                                        'title' => $page_for_date),
109                             QElement('b', $t['tm_mday']));
110         }
111         else {
112             $date = QElement('a',
113                              array('class' => 'cal-hide',
114                                    'href'  => WikiURL($page_for_date,
115                                                       array('action' => 'edit')),
116                                    'title' => sprintf(_("Edit %s"), $page_for_date)),
117                              $t['tm_mday']);
118         }
119
120         return  Element('td', array('align' => 'center'),
121                         "&nbsp;${date}&nbsp;");
122     }
123
124     function run($dbi, $argstr, $request) {
125         $this->args = $this->getArgs($argstr, $request);
126         $args       = &$this->args;
127
128         $now = localtime(time(), 1);
129         foreach ( array('month' => $now['tm_mon'] + 1,
130                         'year'  => $now['tm_year'] + 1900)
131                   as $param => $dflt ) {
132
133             if (!($args[$param] = intval($args[$param])))
134                 $args[$param]   = $dflt;
135
136         }
137
138         $time = mktime(12, 0, 0,                               // hh, mm, ss,
139                        $args['month'] + $args['month_offset'], // month (1-12)
140                        1,                                      // mday (1-31)
141                        $args['year']);
142
143         $rows[] = $this->__header($request->getArg('pagename'), $time);
144         $rows[] = $this->__daynames($args['start_wday']);
145
146         $t = localtime($time, 1);
147         $col = (7 + $t['tm_wday'] - $args['start_wday']) % 7;
148         $row = $col > 0 ? Element('td', array('colspan' => $col)) : '';
149         $done = false;
150
151         while (!$done) {
152             $row .= $this->__date($dbi, $time);
153
154             if (++$col % 7 == 0) {
155                 $rows[] = Element('tr', $row);
156                 $col    = 0;
157                 $row    = '';
158             }
159
160             $time += SECONDS_PER_DAY;
161             $t     = localtime($time, 1);
162             $done  = $t['tm_mday'] == 1;
163         }
164
165         if ($row) {
166             $row   .= Element('td', array('colspan' => (42 - $col) % 7));
167             $rows[] = Element('tr', $row);
168         }
169
170         return Element('table', array('cellspacing' => 0,
171                                       'cellpadding' => 2,
172                                       'class'       => 'cal'),
173                        "\n" . join("\n", $rows) . "\n");
174     }
175 };
176
177 // For emacs users
178 // Local Variables:
179 // mode: php
180 // tab-width: 8
181 // c-basic-offset: 4
182 // c-hanging-comment-ender-p: nil
183 // indent-tabs-mode: nil
184 // End:
185 ?>