JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
comments
[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 function calendar($year, $month, $events = 0, &$template = 0) {
53         if($template == 0) {
54                 $template = $GLOBALS['wfpl_template'];
55         }
56
57         if(strlen($year) == 2) {
58                 $year = "20$year";
59         }
60
61         $start_timestamp = strtotime("$year-$month-01 00:00");
62         $cell = 0;
63
64         $template->set('month_year', strftime('%B', $start_timestamp) . " " . $year);
65
66         # number of non-day slots at the begining of the month
67         $pre_non_days = date( 'w', $start_timestamp );
68
69         # first display empty cells so the 1st can be in the right column
70         while($cell < $pre_non_days) {
71                 calendar_day('nonday', $template);
72                 $cell++;
73         }
74
75         # do the days in this month
76         $days_count = date( 't', $start_timestamp );
77         for($day = 1; $day <= $days_count; $day++ ) {
78                 $template->set('day_number', $day);
79                 if(($cell + 1) % 7 < 2) {
80                         $type = 'weekend';
81                 } else {
82                         $type = 'day';
83                 }
84                 if($events[$day]) {
85                         $template->set('day_page', $events[$day]);
86                         calendar_day("busy_$type", $template);
87                 } else {
88                         calendar_day("empty_$type", $template);
89                 }
90
91                 $cell++;
92                 if($cell % 7 == 0) {
93                         calendar_week($template);
94                 }
95         }
96
97         # fill the rest of the row with empty cells
98         if($cell % 7) {
99                 while($cell % 7) {
100                         calendar_day('nonday', $template);
101                         $cell++;
102                 }
103                 calendar_week($template);
104         }
105 }
106
107 ?>