JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
added calendar.php
[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 # ACKNOWLEDGMENTS
41 #
42 # Thank you Oscar Merida for figuring out the necessary date() and strftime()
43 # calls: http://freshmeat.net/projects/simplephpcalendar/
44
45
46
47 require_once('code/wfpl/template.php');
48
49 function calendar_week(&$template) {
50         $template->sub('week');
51 }
52
53 function calendar_day($kind, &$template) {
54         $template->sub($kind);
55         $template->sub('day');
56 }
57
58 function calendar($year, $month, $events = 0, &$template = 0) {
59         if($template == 0) {
60                 $template = $GLOBALS['wfpl_template'];
61         }
62
63         if(strlen($year) == 2) {
64                 $year = "20$year";
65         }
66
67         $start_timestamp = strtotime("$year-$month-01 00:00");
68         $cell = 0;
69
70         $template->set('month_year', strftime('%B', $start_timestamp) . " " . $year);
71
72         # number of non-day slots at the begining of the month
73         $pre_non_days = date( 'w', $start_timestamp );
74
75         # first display empty cells so the 1st can be in the right column
76         while($cell < $pre_non_days) {
77                 calendar_day('nonday', $template);
78                 $cell++;
79         }
80
81         # do the days in this month
82         $days_count = date( 't', $start_timestamp );
83         for($day = 1; $day <= $days_count; $day++ ) {
84                 $template->set('day_number', $day);
85                 if(($cell + 1) % 7 < 2) {
86                         $type = 'weekend';
87                 } else {
88                         $type = 'day';
89                 }
90                 if($events[$day]) {
91                         $template->set('day_page', $events[$day]);
92                         calendar_day("busy_$type", $template);
93                 } else {
94                         calendar_day("empty_$type", $template);
95                 }
96
97                 $cell++;
98                 if($cell % 7 == 0) {
99                         calendar_week($template);
100                 }
101         }
102
103         # fill the rest of the row with empty cells
104         if($cell % 7) {
105                 while($cell % 7) {
106                         calendar_day('nonday', $template);
107                         $cell++;
108                 }
109                 calendar_week($template);
110         }
111 }
112
113 ?>