JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
release public domain / CC0
[wfpl.git] / calendar.php
1 <?php
2
3 # This program is in the public domain within the United States. Additionally,
4 # we waive copyright and related rights in the work worldwide through the CC0
5 # 1.0 Universal public domain dedication, which can be found at
6 # http://creativecommons.org/publicdomain/zero/1.0/
7
8
9
10 # This file generates a simple calendar which looks something like this:
11 #
12 #            December 2006
13 #     Sun Mon Tue Wed Thu Fri Sat
14 #                          1   2
15 #      3   4   5   6   7   8   9
16 #     10  11  12  13  14  15  16
17 #     17  18  19  20  21  22  23
18 #     24  25  26  27  28  29  30
19 #     31
20 #
21 # The days with events have different CSS classes and can link to custom pages
22 # or javascript or whatever.
23 #
24 # The html and CSS is completely customizable without opening a php file.
25
26
27
28 require_once(__DIR__.'/'.'template.php');
29
30 function calendar_week(&$template) {
31         $template->sub('week');
32 }
33
34 function calendar_day($kind, &$template) {
35         $template->sub($kind);
36         $template->sub('day');
37 }
38
39 # php4 is broken, in that you cannot set a default value for a parameter that
40 # is passed by reference. So, this is set up to use the following screwy
41 # syntax:
42 #
43 # calendar('2006', '12', $events, ref($my_template))
44 function calendar($year, $month, $events = 0, $template = 0) {
45         if($template === 0) {
46                 $template = &$GLOBALS['wfpl_template'];
47         } else {
48                 $template = &$template->ref;
49         }
50
51         if(strlen($year) == 2) {
52                 $year = "20$year";
53         }
54
55         $start_timestamp = strtotime("$year-$month-01 00:00");
56         $cell = 0;
57
58         $template->set('month_year', strftime('%B', $start_timestamp) . " " . $year);
59
60         # number of non-day slots at the begining of the month
61         $pre_non_days = date('w', $start_timestamp );
62
63         # first display empty cells so the 1st can be in the right column
64         while($cell < $pre_non_days) {
65                 calendar_day('nonday', $template);
66                 $cell++;
67         }
68
69         # do the days in this month
70         $days_count = date('t', $start_timestamp );
71         for($day = 1; $day <= $days_count; $day++ ) {
72                 $template->set('day_number', $day);
73                 if(($cell + 1) % 7 < 2) {
74                         $type = 'weekend';
75                 } else {
76                         $type = 'day';
77                 }
78                 if($events[$day]) {
79                         $template->set('day_page', $events[$day]);
80                         calendar_day("busy_$type", $template);
81                 } else {
82                         calendar_day("empty_$type", $template);
83                 }
84
85                 $cell++;
86                 if($cell % 7 == 0) {
87                         calendar_week($template);
88                 }
89         }
90
91         # fill the rest of the row with empty cells
92         if($cell % 7) {
93                 while($cell % 7) {
94                         calendar_day('nonday', $template);
95                         $cell++;
96                 }
97                 calendar_week($template);
98         }
99 }
100
101 ?>