1 | <?php |
---|
2 | /** |
---|
3 | * Description: demonstrates a decorator to provide simple output formatting |
---|
4 | * on the month while still allowing the days to be accessed via the decorator |
---|
5 | * In practice you _wouldn't_ do this - each decorator comes with a performance |
---|
6 | * hit for extra method calls. For this example some simple functions could help |
---|
7 | * format the month while the days are accessed via the normal Month object |
---|
8 | */ |
---|
9 | if ( !@include 'Calendar/Calendar.php' ) { |
---|
10 | define('CALENDAR_ROOT','../../'); |
---|
11 | } |
---|
12 | require_once CALENDAR_ROOT.'Month/Weekdays.php'; |
---|
13 | require_once CALENDAR_ROOT.'Decorator.php'; |
---|
14 | |
---|
15 | // Decorate a Month with methods to improve formatting |
---|
16 | class MonthDecorator extends Calendar_Decorator { |
---|
17 | /** |
---|
18 | * @param Calendar_Month |
---|
19 | */ |
---|
20 | function MonthDecorator(& $Month) { |
---|
21 | parent::Calendar_Decorator($Month); |
---|
22 | } |
---|
23 | /** |
---|
24 | * Override the prevMonth method to format the output |
---|
25 | */ |
---|
26 | function prevMonth() { |
---|
27 | $prevStamp = parent::prevMonth(TRUE); |
---|
28 | // Build the URL for the previous month |
---|
29 | return $_SERVER['PHP_SELF'].'?y='.date('Y',$prevStamp). |
---|
30 | '&m='.date('n',$prevStamp).'&d='.date('j',$prevStamp); |
---|
31 | } |
---|
32 | /** |
---|
33 | * Override the thisMonth method to format the output |
---|
34 | */ |
---|
35 | function thisMonth() { |
---|
36 | $thisStamp = parent::thisMonth(TRUE); |
---|
37 | // A human readable string from this month |
---|
38 | return date('F Y',$thisStamp); |
---|
39 | } |
---|
40 | /** |
---|
41 | * Override the nextMonth method to format the output |
---|
42 | */ |
---|
43 | function nextMonth() { |
---|
44 | $nextStamp = parent::nextMonth(TRUE); |
---|
45 | // Build the URL for next month |
---|
46 | return $_SERVER['PHP_SELF'].'?y='.date('Y',$nextStamp). |
---|
47 | '&m='.date('n',$nextStamp).'&d='.date('j',$nextStamp); |
---|
48 | } |
---|
49 | } |
---|
50 | |
---|
51 | if (!isset($_GET['y'])) $_GET['y'] = date('Y'); |
---|
52 | if (!isset($_GET['m'])) $_GET['m'] = date('n'); |
---|
53 | |
---|
54 | // Creata a month as usual |
---|
55 | $Month = new Calendar_Month_Weekdays($_GET['y'],$_GET['m']); |
---|
56 | |
---|
57 | // Pass it to the decorator and use the decorator from now on... |
---|
58 | $MonthDecorator = new MonthDecorator($Month); |
---|
59 | $MonthDecorator->build(); |
---|
60 | ?> |
---|
61 | |
---|
62 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
---|
63 | <html> |
---|
64 | <head> |
---|
65 | <title> A Simple Decorator </title> |
---|
66 | </head> |
---|
67 | <body> |
---|
68 | <h1>A Simple Decorator</h1> |
---|
69 | <table> |
---|
70 | <caption><?php echo ( $MonthDecorator->thisMonth() ); ?></caption> |
---|
71 | <?php |
---|
72 | while ( $Day = $MonthDecorator->fetch() ) { |
---|
73 | if ( $Day->isFirst() ) { |
---|
74 | echo ( "\n<tr>\n" ); |
---|
75 | } |
---|
76 | if ( $Day->isEmpty() ) { |
---|
77 | echo ( "<td> </td>" ); |
---|
78 | } else { |
---|
79 | echo ( "<td>".$Day->thisDay()."</td>" ); |
---|
80 | } |
---|
81 | if ( $Day->isLast() ) { |
---|
82 | echo ( "\n</tr>\n" ); |
---|
83 | } |
---|
84 | } |
---|
85 | ?> |
---|
86 | <tr> |
---|
87 | <td><a href="<?php echo ($MonthDecorator->prevMonth()); ?>">Prev</a></td> |
---|
88 | <td colspan="5"> </td> |
---|
89 | <td><a href="<?php echo ($MonthDecorator->nextMonth()); ?>">Next</a></td> |
---|
90 | </tr> |
---|
91 | </table> |
---|
92 | </body> |
---|
93 | </html> |
---|