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