]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/Calendar.php
gettext() changed to _()
[SourceForge/phpwiki.git] / lib / plugin / Calendar.php
1 <?php // -*-php-*-
2 rcs_id('$Id: Calendar.php,v 1.11 2002-01-24 06:52:12 carstenklapp 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         return array('prefix'           => '[pagename].',
33                      'date_format'      => '%Y-%m-%d',
34                      'year'             => '',
35                      'month'            => '',
36                      'month_offset'     => 0,
37
38                      'month_format'     => '%B, %Y',
39                      'wday_format'      => '%a',
40                      'start_wday'       => '0');
41     }
42
43     function __header($pagename, $time) {
44         $args = &$this->args;
45
46         $t = localtime($time - SECONDS_PER_DAY, 1);
47         $prev_url = WikiURL($pagename, array('month' => $t['tm_mon'] + 1,
48                                              'year'  => $t['tm_year'] + 1900));
49
50         $t = localtime($time + 32 * SECONDS_PER_DAY, 1);
51         $next_url = WikiURL($pagename, array('month' => $t['tm_mon'] + 1,
52                                              'year'  => $t['tm_year'] + 1900));
53
54         $prev = HTML::a(array('href'  => $prev_url,
55                               'class' => 'cal-arrow',
56                               'title' => _("Previous Month")),
57                         '<');
58         $next = HTML::a(array('href'  => $next_url,
59                               'class' => 'cal-arrow',
60                               'title' => _("Next Month")),
61                         '>');
62
63
64         $row = HTML::tr(HTML::td(array('align' => 'left'), $prev),
65                         HTML::td(array('align' => 'center'),
66                                  HTML::strong(array('class' => 'cal-header'),
67                                               strftime($args['month_format'], $time))),
68                         HTML::td(array('align' => 'right'), $next));
69         
70         return HTML::tr(HTML::td(array('colspan' => 7,
71                                        'align'   => 'center'),
72                                  HTML::table(array('width' => '100%'), $row)));
73     }
74
75
76     function __daynames($start_wday) {
77         $time  = mktime(12, 0, 0, 1, 1, 2001);
78         $t     = localtime($time, 1);
79         $time += (7 + $start_wday - $t['tm_wday']) * SECONDS_PER_DAY;
80
81         $t = localtime($time, 1);
82         assert($t['tm_wday'] == $start_wday);
83
84         $fs = $this->args['wday_format'];
85         $row = HTML::tr();
86         for ($i = 0; $i < 7; $i++) {
87             $row->pushContent(HTML::td(array('class' => 'cal-dayname',
88                                              'align' => 'center'),
89                                        strftime($fs, $time)));
90             $time += SECONDS_PER_DAY;
91         }
92         return $row;
93     }
94
95     function __date($dbi, $time) {
96         $args = &$this->args;
97
98         $page_for_date = $args['prefix'] . strftime($args['date_format'],
99                                                     $time);
100         $t = localtime($time, 1);
101
102         if ($dbi->isWikiPage($page_for_date)) {
103             $date = HTML::a(array('class' => 'cal-day',
104                                   'href'  => WikiURL($page_for_date),
105                                   'title' => $page_for_date),
106                             HTML::strong($t['tm_mday']));
107         }
108         else {
109             $date = HTML::a(array('class' => 'cal-hide',
110                                   'href'  => WikiURL($page_for_date,
111                                                      array('action' => 'edit')),
112                                   'title' => sprintf(_("Edit %s"), $page_for_date)),
113                             $t['tm_mday']);
114         }
115
116         return  HTML::td(array('align' => 'center'), NBSP . $date . NBSP);
117     }
118
119     function run($dbi, $argstr, $request) {
120         $this->args = $this->getArgs($argstr, $request);
121         $args       = &$this->args;
122
123         $now = localtime(time(), 1);
124         foreach ( array('month' => $now['tm_mon'] + 1,
125                         'year'  => $now['tm_year'] + 1900)
126                   as $param => $dflt ) {
127
128             if (!($args[$param] = intval($args[$param])))
129                 $args[$param]   = $dflt;
130
131         }
132
133         $time = mktime(12, 0, 0,                               // hh, mm, ss,
134                        $args['month'] + $args['month_offset'], // month (1-12)
135                        1,                                      // mday (1-31)
136                        $args['year']);
137
138         $cal = HTML::table(array('cellspacing' => 0,
139                                  'cellpadding' => 2,
140                                  'class'       => 'cal'),
141                            $this->__header($request->getArg('pagename'), $time),
142                            $this->__daynames($args['start_wday']));
143
144         $row = HTML::tr();
145         
146         $t = localtime($time, 1);
147         $col = (7 + $t['tm_wday'] - $args['start_wday']) % 7;
148         if ($col > 0)
149             $row->pushContent(HTML::td(array('colspan' => $col)));
150         $done = false;
151
152         while (!$done) {
153             $row->pushContent($this->__date($dbi, $time));
154
155             if (++$col % 7 == 0) {
156                 $cal->pushContent($row);
157                 $col = 0;
158                 $row = HTML::tr();
159             }
160
161             $time += SECONDS_PER_DAY;
162             $t     = localtime($time, 1);
163             $done  = $t['tm_mday'] == 1;
164         }
165
166         if ($row->getContent()) {
167             $row->pushContent(HTML::td(array('colspan' => (42 - $col) % 7)));
168             $cal->pushContent($row);
169         }
170         return $cal;
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 ?>