JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
added stupid hack so I can have a default for a "pass by reference" parameter
[wfpl.git] / calendar.php
1 <?php
2
3 #  Copyright (C) 2006 Jason Woofenden
4 #
5 #  This file is part of wfpl.
6 #
7 #  wfpl is free software; you can redistribute it and/or modify it
8 #  under the terms of the GNU General Public License as published by
9 #  the Free Software Foundation; either version 2, or (at your option)
10 #  any later version.
11 #
12 #  wfpl is distributed in the hope that it will be useful, but
13 #  WITHOUT ANY WARRANTY; without even the implied warranty of
14 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 #  General Public License for more details.
16 #
17 #  You should have received a copy of the GNU General Public License
18 #  along with wfpl; see the file COPYING.  If not, write to the
19 #  Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20 #  MA 02111-1307, USA.
21
22
23 # This file generates a simple calendar which looks something like this:
24 #
25 #            December 2006
26 #     Sun Mon Tue Wed Thu Fri Sat
27 #      1   2
28 #      3   4   5   6   7   8   9
29 #     10  11  12  13  14  15  16
30 #     17  18  19  20  21  22  23
31 #     24  25  26  27  28  29  30
32 #     31
33 #
34 # The days with events have different CSS classes and can link to custom pages
35 # or javascript or whatever.
36 #
37 # The html and CSS is completely customizable without opening a php file.
38
39
40
41 require_once('code/wfpl/template.php');
42
43 function calendar_week(&$template) {
44         $template->sub('week');
45 }
46
47 function calendar_day($kind, &$template) {
48         $template->sub($kind);
49         $template->sub('day');
50 }
51
52 # php4 is broken, in that you cannot set a default value for a parameter that
53 # is passed by reference. So, this is set up to use the following screwy
54 # syntax:
55 #
56 # calendar('2006', '12', $events, ref($my_template))
57 function calendar($year, $month, $events = 0, $template = 0) {
58         if($template === 0) {
59                 $template = &$GLOBALS['wfpl_template'];
60         } else {
61                 $template = &$template->ref;
62         }
63
64         if(strlen($year) == 2) {
65                 $year = "20$year";
66         }
67
68         $start_timestamp = strtotime("$year-$month-01 00:00");
69         $cell = 0;
70
71         $template->set('month_year', strftime('%B', $start_timestamp) . " " . $year);
72
73         # number of non-day slots at the begining of the month
74         $pre_non_days = date( 'w', $start_timestamp );
75
76         # first display empty cells so the 1st can be in the right column
77         while($cell < $pre_non_days) {
78                 calendar_day('nonday', $template);
79                 $cell++;
80         }
81
82         # do the days in this month
83         $days_count = date( 't', $start_timestamp );
84         for($day = 1; $day <= $days_count; $day++ ) {
85                 $template->set('day_number', $day);
86                 if(($cell + 1) % 7 < 2) {
87                         $type = 'weekend';
88                 } else {
89                         $type = 'day';
90                 }
91                 if($events[$day]) {
92                         $template->set('day_page', $events[$day]);
93                         calendar_day("busy_$type", $template);
94                 } else {
95                         calendar_day("empty_$type", $template);
96                 }
97
98                 $cell++;
99                 if($cell % 7 == 0) {
100                         calendar_week($template);
101                 }
102         }
103
104         # fill the rest of the row with empty cells
105         if($cell % 7) {
106                 while($cell % 7) {
107                         calendar_day('nonday', $template);
108                         $cell++;
109                 }
110                 calendar_week($template);
111         }
112 }
113
114 ?>