| 1 | <?php |
|---|
| 2 | /** |
|---|
| 3 | * Description: demonstrates a decorator used to "attach a payload" to a selection |
|---|
| 4 | * to make it available when iterating over calendar children |
|---|
| 5 | */ |
|---|
| 6 | |
|---|
| 7 | //if you use ISO-8601 dates, switch to PearDate engine |
|---|
| 8 | define('CALENDAR_ENGINE', 'PearDate'); |
|---|
| 9 | |
|---|
| 10 | if ( !@include 'Calendar/Calendar.php' ) { |
|---|
| 11 | define('CALENDAR_ROOT','../../'); |
|---|
| 12 | } |
|---|
| 13 | |
|---|
| 14 | require_once CALENDAR_ROOT . 'Month/Weekdays.php'; |
|---|
| 15 | require_once CALENDAR_ROOT . 'Day.php'; |
|---|
| 16 | require_once CALENDAR_ROOT . 'Decorator.php'; |
|---|
| 17 | |
|---|
| 18 | // accepts multiple entries |
|---|
| 19 | class DiaryEvent extends Calendar_Decorator |
|---|
| 20 | { |
|---|
| 21 | var $entries = array(); |
|---|
| 22 | |
|---|
| 23 | function DiaryEvent($calendar) { |
|---|
| 24 | Calendar_Decorator::Calendar_Decorator($calendar); |
|---|
| 25 | } |
|---|
| 26 | |
|---|
| 27 | function addEntry($entry) { |
|---|
| 28 | $this->entries[] = $entry; |
|---|
| 29 | } |
|---|
| 30 | |
|---|
| 31 | function getEntry() { |
|---|
| 32 | $entry = each($this->entries); |
|---|
| 33 | if ($entry) { |
|---|
| 34 | return $entry['value']; |
|---|
| 35 | } else { |
|---|
| 36 | reset($this->entries); |
|---|
| 37 | return false; |
|---|
| 38 | } |
|---|
| 39 | } |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | class MonthPayload_Decorator extends Calendar_Decorator |
|---|
| 43 | { |
|---|
| 44 | //Calendar engine |
|---|
| 45 | var $cE; |
|---|
| 46 | var $tableHelper; |
|---|
| 47 | |
|---|
| 48 | var $year; |
|---|
| 49 | var $month; |
|---|
| 50 | var $firstDay = false; |
|---|
| 51 | |
|---|
| 52 | function build($events=array()) |
|---|
| 53 | { |
|---|
| 54 | require_once CALENDAR_ROOT . 'Day.php'; |
|---|
| 55 | require_once CALENDAR_ROOT . 'Table/Helper.php'; |
|---|
| 56 | |
|---|
| 57 | $this->tableHelper = & new Calendar_Table_Helper($this, $this->firstDay); |
|---|
| 58 | $this->cE = & $this->getEngine(); |
|---|
| 59 | $this->year = $this->thisYear(); |
|---|
| 60 | $this->month = $this->thisMonth(); |
|---|
| 61 | |
|---|
| 62 | $daysInMonth = $this->cE->getDaysInMonth($this->year, $this->month); |
|---|
| 63 | for ($i=1; $i<=$daysInMonth; $i++) { |
|---|
| 64 | $Day = new Calendar_Day(2000,1,1); // Create Day with dummy values |
|---|
| 65 | $Day->setTimeStamp($this->cE->dateToStamp($this->year, $this->month, $i)); |
|---|
| 66 | $this->children[$i] = new DiaryEvent($Day); |
|---|
| 67 | } |
|---|
| 68 | if (count($events) > 0) { |
|---|
| 69 | $this->setSelection($events); |
|---|
| 70 | } |
|---|
| 71 | Calendar_Month_Weekdays::buildEmptyDaysBefore(); |
|---|
| 72 | Calendar_Month_Weekdays::shiftDays(); |
|---|
| 73 | Calendar_Month_Weekdays::buildEmptyDaysAfter(); |
|---|
| 74 | Calendar_Month_Weekdays::setWeekMarkers(); |
|---|
| 75 | return true; |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | function setSelection($events) |
|---|
| 79 | { |
|---|
| 80 | $daysInMonth = $this->cE->getDaysInMonth($this->year, $this->month); |
|---|
| 81 | for ($i=1; $i<=$daysInMonth; $i++) { |
|---|
| 82 | $stamp1 = $this->cE->dateToStamp($this->year, $this->month, $i); |
|---|
| 83 | $stamp2 = $this->cE->dateToStamp($this->year, $this->month, $i+1); |
|---|
| 84 | foreach ($events as $event) { |
|---|
| 85 | if (($stamp1 >= $event['start'] && $stamp1 < $event['end']) || |
|---|
| 86 | ($stamp2 >= $event['start'] && $stamp2 < $event['end']) || |
|---|
| 87 | ($stamp1 <= $event['start'] && $stamp2 > $event['end']) |
|---|
| 88 | ) { |
|---|
| 89 | $this->children[$i]->addEntry($event); |
|---|
| 90 | $this->children[$i]->setSelected(); |
|---|
| 91 | } |
|---|
| 92 | } |
|---|
| 93 | } |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | function fetch() |
|---|
| 97 | { |
|---|
| 98 | $child = each($this->children); |
|---|
| 99 | if ($child) { |
|---|
| 100 | return $child['value']; |
|---|
| 101 | } else { |
|---|
| 102 | reset($this->children); |
|---|
| 103 | return false; |
|---|
| 104 | } |
|---|
| 105 | } |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | // Calendar instance used to get the dates in the preferred format: |
|---|
| 109 | // you can switch Calendar Engine and the example still works |
|---|
| 110 | $cal = new Calendar; |
|---|
| 111 | |
|---|
| 112 | $events = array(); |
|---|
| 113 | //add some events |
|---|
| 114 | $events[] = array( |
|---|
| 115 | 'start' => $cal->cE->dateToStamp(2004, 6, 1, 10), |
|---|
| 116 | 'end' => $cal->cE->dateToStamp(2004, 6, 1, 12), |
|---|
| 117 | 'desc' => 'Important meeting' |
|---|
| 118 | ); |
|---|
| 119 | $events[] = array( |
|---|
| 120 | 'start' => $cal->cE->dateToStamp(2004, 6, 1, 21), |
|---|
| 121 | 'end' => $cal->cE->dateToStamp(2004, 6, 1, 23, 59), |
|---|
| 122 | 'desc' => 'Dinner with the boss' |
|---|
| 123 | ); |
|---|
| 124 | $events[] = array( |
|---|
| 125 | 'start' => $cal->cE->dateToStamp(2004, 6, 5), |
|---|
| 126 | 'end' => $cal->cE->dateToStamp(2004, 6, 10, 23, 59), |
|---|
| 127 | 'desc' => 'Holidays!' |
|---|
| 128 | ); |
|---|
| 129 | |
|---|
| 130 | |
|---|
| 131 | |
|---|
| 132 | $Month = & new Calendar_Month_Weekdays(2004, 6); |
|---|
| 133 | $MonthDecorator = new MonthPayload_Decorator($Month); |
|---|
| 134 | $MonthDecorator->build($events); |
|---|
| 135 | |
|---|
| 136 | ?> |
|---|
| 137 | <!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN"> |
|---|
| 138 | <html> |
|---|
| 139 | <head> |
|---|
| 140 | <title> Calendar </title> |
|---|
| 141 | <style text="text/css"> |
|---|
| 142 | table { |
|---|
| 143 | border-collapse: collapse; |
|---|
| 144 | } |
|---|
| 145 | caption { |
|---|
| 146 | font-family: verdana; |
|---|
| 147 | font-size: 14pt; |
|---|
| 148 | padding-bottom: 4pt; |
|---|
| 149 | } |
|---|
| 150 | th { |
|---|
| 151 | font-family: verdana; |
|---|
| 152 | font-size: 11px; |
|---|
| 153 | text-align: center; |
|---|
| 154 | background-color: #e7e3e7; |
|---|
| 155 | padding: 5pt; |
|---|
| 156 | line-height: 150%; |
|---|
| 157 | border: 1px solid #ccc; |
|---|
| 158 | } |
|---|
| 159 | td { |
|---|
| 160 | font-family: verdana; |
|---|
| 161 | font-size: 11px; |
|---|
| 162 | text-align: left; |
|---|
| 163 | vertical-align: top; |
|---|
| 164 | } |
|---|
| 165 | td.calCell { |
|---|
| 166 | border: 1px solid #b5bece; |
|---|
| 167 | padding: 3px; |
|---|
| 168 | } |
|---|
| 169 | td.calCellEmpty { |
|---|
| 170 | background-color: #f3f3f7; |
|---|
| 171 | } |
|---|
| 172 | td.calCellBusy { |
|---|
| 173 | background-color: #efeffa; |
|---|
| 174 | } |
|---|
| 175 | div.dayNumber { |
|---|
| 176 | text-align: right; |
|---|
| 177 | background-color: #f8f8f8; |
|---|
| 178 | border-bottom: 1px solid #ccc; |
|---|
| 179 | } |
|---|
| 180 | ul { |
|---|
| 181 | margin-left: 0; |
|---|
| 182 | margin-top: 5pt; |
|---|
| 183 | padding: 0 10pt 0 12pt; |
|---|
| 184 | list-style-type: square; |
|---|
| 185 | } |
|---|
| 186 | </style> |
|---|
| 187 | </head> |
|---|
| 188 | |
|---|
| 189 | <body> |
|---|
| 190 | |
|---|
| 191 | <h2>Sample Calendar Payload Decorator (using <?php echo CALENDAR_ENGINE; ?> engine)</h2> |
|---|
| 192 | <table class="calendar" width="98%" cellspacing="0" cellpadding="0"> |
|---|
| 193 | <caption> |
|---|
| 194 | <?php echo $MonthDecorator->thisMonth().' / '.$MonthDecorator->thisYear(); ?> |
|---|
| 195 | </caption> |
|---|
| 196 | <tr> |
|---|
| 197 | <th>Monday</th> |
|---|
| 198 | <th>Tuesday</th> |
|---|
| 199 | <th>Wednesday</th> |
|---|
| 200 | <th>Thursday</th> |
|---|
| 201 | <th>Friday</th> |
|---|
| 202 | <th>Saturday</th> |
|---|
| 203 | <th>Sunday</th> |
|---|
| 204 | </tr> |
|---|
| 205 | <?php |
|---|
| 206 | while ($Day = $MonthDecorator->fetch()) { |
|---|
| 207 | |
|---|
| 208 | if ($Day->isFirst()) { |
|---|
| 209 | echo "<tr>\n"; |
|---|
| 210 | } |
|---|
| 211 | |
|---|
| 212 | echo '<td class="calCell'; |
|---|
| 213 | if ($Day->isSelected()) { |
|---|
| 214 | echo ' calCellBusy'; |
|---|
| 215 | } elseif ($Day->isEmpty()) { |
|---|
| 216 | echo ' calCellEmpty'; |
|---|
| 217 | } |
|---|
| 218 | echo '">'; |
|---|
| 219 | echo '<div class="dayNumber">'.$Day->thisDay().'</div>'; |
|---|
| 220 | |
|---|
| 221 | if ($Day->isEmpty()) { |
|---|
| 222 | echo ' '; |
|---|
| 223 | } else { |
|---|
| 224 | echo '<div class="dayContents"><ul>'; |
|---|
| 225 | while ($entry = $Day->getEntry()) { |
|---|
| 226 | echo '<li>'.$entry['desc'].'</li>'; |
|---|
| 227 | //you can print the time range as well |
|---|
| 228 | } |
|---|
| 229 | echo '</ul></div>'; |
|---|
| 230 | } |
|---|
| 231 | echo '</td>'; |
|---|
| 232 | |
|---|
| 233 | if ($Day->isLast()) { |
|---|
| 234 | echo "</tr>\n"; |
|---|
| 235 | } |
|---|
| 236 | } |
|---|
| 237 | ?> |
|---|
| 238 | </table> |
|---|
| 239 | </body> |
|---|
| 240 | </html> |
|---|