]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/Calendar.php
Refactor $WikiPlugin::name and $WikiPlugin::description stuff.
[SourceForge/phpwiki.git] / lib / plugin / Calendar.php
1 <?php // -*-php-*-
2 rcs_id('$Id: Calendar.php,v 1.4 2001-12-16 18:33:25 dairiki 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
15 // date pages back to the calendar page.  (Subpage support might
16 // make this 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'           => '[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'], $time);
102         $t = localtime($time, 1);
103         
104         if ($dbi->isWikiPage($page_for_date)) {
105             $date = Element('a', array('class' => 'cal-day',
106                                        'href'  => WikiURL($page_for_date),
107                                        'title' => $page_for_date),
108                             QElement('b', $t['tm_mday']));
109         }
110         else {
111             $date = QElement('a', array('class' => 'cal-hide',
112                                         'href'  => WikiURL($page_for_date,
113                                                            array('action' => 'edit')),
114                                         'title' => sprintf(_("Edit %s"), $page_for_date)),
115                              $t['tm_mday']);
116         }
117
118         return  Element('td', array('align' => 'center'),
119                         "&nbsp;${date}&nbsp;");
120     }
121             
122     function run($dbi, $argstr, $request) {
123         $this->args = $this->getArgs($argstr, $request);
124         $args = &$this->args;
125
126         $now = localtime(time(), 1);
127         foreach ( array('month' => $now['tm_mon'] + 1,
128                         'year' => $now['tm_year'] + 1900)
129                   as $param => $dflt) {
130             if (!($args[$param] = intval($args[$param])))
131                 $args[$param] = $dflt;
132             
133         }
134
135         $time = mktime(12, 0, 0,  // hh, mm, ss,
136                        $args['month'] + $args['month_offset'], // month (1-12)
137                        1, // mday (1-31)
138                        $args['year']);
139         
140         $rows[] = $this->__header($request->getArg('pagename'), $time);
141         $rows[] = $this->__daynames($args['start_wday']);
142
143         $t = localtime($time, 1);
144         $col = (7 + $t['tm_wday'] - $args['start_wday']) % 7;
145         $row = $col > 0 ? Element('td', array('colspan' => $col)) : '';
146         $done = false;
147         
148         while (!$done) {
149             $row .= $this->__date($dbi, $time);
150
151             if (++$col % 7 == 0) {
152                 $rows[] = Element('tr', $row);
153                 $col = 0;
154                 $row = '';
155             }
156             
157             $time += SECONDS_PER_DAY;
158             $t = localtime($time, 1);
159             $done = $t['tm_mday'] == 1;
160         }
161
162         if ($row) {
163             $row .= Element('td', array('colspan' => (42 - $col) % 7));
164             $rows[] = Element('tr', $row);
165         }
166         
167         return Element('table', array('cellspacing' => 0,
168                                       'cellpadding' => 2,
169                                       'class' => 'cal'),
170                        "\n" . join("\n", $rows) . "\n");
171     }
172 };
173
174 // For emacs users
175 // Local Variables:
176 // mode: php
177 // tab-width: 8
178 // c-basic-offset: 4
179 // c-hanging-comment-ender-p: nil
180 // indent-tabs-mode: nil
181 // End:
182 ?>