| 1 | <?php
|
|---|
| 2 | /* vim: set expandtab tabstop=4 shiftwidth=4: */
|
|---|
| 3 |
|
|---|
| 4 | /**
|
|---|
| 5 | * Contains the Calendar_Decorator_Wrapper class
|
|---|
| 6 | *
|
|---|
| 7 | * PHP versions 4 and 5
|
|---|
| 8 | *
|
|---|
| 9 | * LICENSE: Redistribution and use in source and binary forms, with or without
|
|---|
| 10 | * modification, are permitted provided that the following conditions are met:
|
|---|
| 11 | * 1. Redistributions of source code must retain the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 13 | * 2. Redistributions in binary form must reproduce the above copyright
|
|---|
| 14 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 15 | * documentation and/or other materials provided with the distribution.
|
|---|
| 16 | * 3. The name of the author may not be used to endorse or promote products
|
|---|
| 17 | * derived from this software without specific prior written permission.
|
|---|
| 18 | *
|
|---|
| 19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
|
|---|
| 20 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|---|
| 21 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 22 | * IN NO EVENT SHALL THE FREEBSD PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY
|
|---|
| 23 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|---|
| 24 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|---|
| 25 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|---|
| 26 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 28 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 29 | *
|
|---|
| 30 | * @category Date and Time
|
|---|
| 31 | * @package Calendar
|
|---|
| 32 | * @author Harry Fuecks <[email protected]>
|
|---|
| 33 | * @copyright 2003-2007 Harry Fuecks
|
|---|
| 34 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
|
|---|
| 35 | * @version CVS: $Id: Helper.php 246317 2007-11-16 20:05:32Z quipo $
|
|---|
| 36 | * @link http://pear.php.net/package/Calendar
|
|---|
| 37 | */
|
|---|
| 38 |
|
|---|
| 39 | /**
|
|---|
| 40 | * Used by Calendar_Month_Weekdays, Calendar_Month_Weeks and Calendar_Week to
|
|---|
| 41 | * help with building the calendar in tabular form
|
|---|
| 42 | *
|
|---|
| 43 | * @category Date and Time
|
|---|
| 44 | * @package Calendar
|
|---|
| 45 | * @author Harry Fuecks <[email protected]>
|
|---|
| 46 | * @copyright 2003-2007 Harry Fuecks
|
|---|
| 47 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
|
|---|
| 48 | * @link http://pear.php.net/package/Calendar
|
|---|
| 49 | * @access public
|
|---|
| 50 | */
|
|---|
| 51 | class Calendar_Table_Helper
|
|---|
| 52 | {
|
|---|
| 53 | /**
|
|---|
| 54 | * Instance of the Calendar object being helped.
|
|---|
| 55 | * @var object
|
|---|
| 56 | * @access private
|
|---|
| 57 | */
|
|---|
| 58 | var $calendar;
|
|---|
| 59 |
|
|---|
| 60 | /**
|
|---|
| 61 | * Instance of the Calendar_Engine
|
|---|
| 62 | * @var object
|
|---|
| 63 | * @access private
|
|---|
| 64 | */
|
|---|
| 65 | var $cE;
|
|---|
| 66 |
|
|---|
| 67 | /**
|
|---|
| 68 | * First day of the week
|
|---|
| 69 | * @access private
|
|---|
| 70 | * @var string
|
|---|
| 71 | */
|
|---|
| 72 | var $firstDay;
|
|---|
| 73 |
|
|---|
| 74 | /**
|
|---|
| 75 | * The seven days of the week named
|
|---|
| 76 | * @access private
|
|---|
| 77 | * @var array
|
|---|
| 78 | */
|
|---|
| 79 | var $weekDays;
|
|---|
| 80 |
|
|---|
| 81 | /**
|
|---|
| 82 | * Days of the week ordered with $firstDay at the beginning
|
|---|
| 83 | * @access private
|
|---|
| 84 | * @var array
|
|---|
| 85 | */
|
|---|
| 86 | var $daysOfWeek = array();
|
|---|
| 87 |
|
|---|
| 88 | /**
|
|---|
| 89 | * Days of the month built from days of the week
|
|---|
| 90 | * @access private
|
|---|
| 91 | * @var array
|
|---|
| 92 | */
|
|---|
| 93 | var $daysOfMonth = array();
|
|---|
| 94 |
|
|---|
| 95 | /**
|
|---|
| 96 | * Number of weeks in month
|
|---|
| 97 | * @var int
|
|---|
| 98 | * @access private
|
|---|
| 99 | */
|
|---|
| 100 | var $numWeeks = null;
|
|---|
| 101 |
|
|---|
| 102 | /**
|
|---|
| 103 | * Number of emtpy days before real days begin in month
|
|---|
| 104 | * @var int
|
|---|
| 105 | * @access private
|
|---|
| 106 | */
|
|---|
| 107 | var $emptyBefore = 0;
|
|---|
| 108 |
|
|---|
| 109 | /**
|
|---|
| 110 | * Constructs Calendar_Table_Helper
|
|---|
| 111 | *
|
|---|
| 112 | * @param object &$calendar Calendar_Month_Weekdays, Calendar_Month_Weeks, Calendar_Week
|
|---|
| 113 | * @param int $firstDay (optional) first day of the week e.g. 1 for Monday
|
|---|
| 114 | *
|
|---|
| 115 | * @access protected
|
|---|
| 116 | */
|
|---|
| 117 | function Calendar_Table_Helper(& $calendar, $firstDay=null)
|
|---|
| 118 | {
|
|---|
| 119 | $this->calendar = & $calendar;
|
|---|
| 120 | $this->cE = & $calendar->getEngine();
|
|---|
| 121 | if (is_null($firstDay)) {
|
|---|
| 122 | $firstDay = $this->cE->getFirstDayOfWeek(
|
|---|
| 123 | $this->calendar->thisYear(),
|
|---|
| 124 | $this->calendar->thisMonth(),
|
|---|
| 125 | $this->calendar->thisDay()
|
|---|
| 126 | );
|
|---|
| 127 | }
|
|---|
| 128 | $this->firstDay = $firstDay;
|
|---|
| 129 | $this->setFirstDay();
|
|---|
| 130 | $this->setDaysOfMonth();
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | /**
|
|---|
| 134 | * Constructs $this->daysOfWeek based on $this->firstDay
|
|---|
| 135 | *
|
|---|
| 136 | * @return void
|
|---|
| 137 | * @access private
|
|---|
| 138 | */
|
|---|
| 139 | function setFirstDay()
|
|---|
| 140 | {
|
|---|
| 141 | $weekDays = $this->cE->getWeekDays(
|
|---|
| 142 | $this->calendar->thisYear(),
|
|---|
| 143 | $this->calendar->thisMonth(),
|
|---|
| 144 | $this->calendar->thisDay()
|
|---|
| 145 | );
|
|---|
| 146 | $endDays = array();
|
|---|
| 147 | $tmpDays = array();
|
|---|
| 148 | $begin = false;
|
|---|
| 149 | foreach ($weekDays as $day) {
|
|---|
| 150 | if ($begin) {
|
|---|
| 151 | $endDays[] = $day;
|
|---|
| 152 | } else if ($day === $this->firstDay) {
|
|---|
| 153 | $begin = true;
|
|---|
| 154 | $endDays[] = $day;
|
|---|
| 155 | } else {
|
|---|
| 156 | $tmpDays[] = $day;
|
|---|
| 157 | }
|
|---|
| 158 | }
|
|---|
| 159 | $this->daysOfWeek = array_merge($endDays, $tmpDays);
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 | /**
|
|---|
| 163 | * Constructs $this->daysOfMonth
|
|---|
| 164 | *
|
|---|
| 165 | * @return void
|
|---|
| 166 | * @access private
|
|---|
| 167 | */
|
|---|
| 168 | function setDaysOfMonth()
|
|---|
| 169 | {
|
|---|
| 170 | $this->daysOfMonth = $this->daysOfWeek;
|
|---|
| 171 | $daysInMonth = $this->cE->getDaysInMonth(
|
|---|
| 172 | $this->calendar->thisYear(), $this->calendar->thisMonth());
|
|---|
| 173 | $firstDayInMonth = $this->cE->getFirstDayInMonth(
|
|---|
| 174 | $this->calendar->thisYear(), $this->calendar->thisMonth());
|
|---|
| 175 | $this->emptyBefore=0;
|
|---|
| 176 | foreach ($this->daysOfMonth as $dayOfWeek) {
|
|---|
| 177 | if ($firstDayInMonth == $dayOfWeek) {
|
|---|
| 178 | break;
|
|---|
| 179 | }
|
|---|
| 180 | $this->emptyBefore++;
|
|---|
| 181 | }
|
|---|
| 182 | $this->numWeeks = ceil(
|
|---|
| 183 | ($daysInMonth + $this->emptyBefore)
|
|---|
| 184 | /
|
|---|
| 185 | $this->cE->getDaysInWeek(
|
|---|
| 186 | $this->calendar->thisYear(),
|
|---|
| 187 | $this->calendar->thisMonth(),
|
|---|
| 188 | $this->calendar->thisDay()
|
|---|
| 189 | )
|
|---|
| 190 | );
|
|---|
| 191 | for ($i=1; $i < $this->numWeeks; $i++) {
|
|---|
| 192 | $this->daysOfMonth =
|
|---|
| 193 | array_merge($this->daysOfMonth, $this->daysOfWeek);
|
|---|
| 194 | }
|
|---|
| 195 | }
|
|---|
| 196 |
|
|---|
| 197 | /**
|
|---|
| 198 | * Returns the first day of the month
|
|---|
| 199 | *
|
|---|
| 200 | * @return int
|
|---|
| 201 | * @access protected
|
|---|
| 202 | * @see Calendar_Engine_Interface::getFirstDayOfWeek()
|
|---|
| 203 | */
|
|---|
| 204 | function getFirstDay()
|
|---|
| 205 | {
|
|---|
| 206 | return $this->firstDay;
|
|---|
| 207 | }
|
|---|
| 208 |
|
|---|
| 209 | /**
|
|---|
| 210 | * Returns the order array of days in a week
|
|---|
| 211 | *
|
|---|
| 212 | * @return int
|
|---|
| 213 | * @access protected
|
|---|
| 214 | */
|
|---|
| 215 | function getDaysOfWeek()
|
|---|
| 216 | {
|
|---|
| 217 | return $this->daysOfWeek;
|
|---|
| 218 | }
|
|---|
| 219 |
|
|---|
| 220 | /**
|
|---|
| 221 | * Returns the number of tabular weeks in a month
|
|---|
| 222 | *
|
|---|
| 223 | * @return int
|
|---|
| 224 | * @access protected
|
|---|
| 225 | */
|
|---|
| 226 | function getNumWeeks()
|
|---|
| 227 | {
|
|---|
| 228 | return $this->numWeeks;
|
|---|
| 229 | }
|
|---|
| 230 |
|
|---|
| 231 | /**
|
|---|
| 232 | * Returns the number of real days + empty days
|
|---|
| 233 | *
|
|---|
| 234 | * @return int
|
|---|
| 235 | * @access protected
|
|---|
| 236 | */
|
|---|
| 237 | function getNumTableDaysInMonth()
|
|---|
| 238 | {
|
|---|
| 239 | return count($this->daysOfMonth);
|
|---|
| 240 | }
|
|---|
| 241 |
|
|---|
| 242 | /**
|
|---|
| 243 | * Returns the number of empty days before the real days begin
|
|---|
| 244 | *
|
|---|
| 245 | * @return int
|
|---|
| 246 | * @access protected
|
|---|
| 247 | */
|
|---|
| 248 | function getEmptyDaysBefore()
|
|---|
| 249 | {
|
|---|
| 250 | return $this->emptyBefore;
|
|---|
| 251 | }
|
|---|
| 252 |
|
|---|
| 253 | /**
|
|---|
| 254 | * Returns the index of the last real day in the month
|
|---|
| 255 | *
|
|---|
| 256 | * @todo Potential performance optimization with static
|
|---|
| 257 | * @return int
|
|---|
| 258 | * @access protected
|
|---|
| 259 | */
|
|---|
| 260 | function getEmptyDaysAfter()
|
|---|
| 261 | {
|
|---|
| 262 | // Causes bug when displaying more than one month
|
|---|
| 263 | //static $index;
|
|---|
| 264 | //if (!isset($index)) {
|
|---|
| 265 | $index = $this->getEmptyDaysBefore() + $this->cE->getDaysInMonth(
|
|---|
| 266 | $this->calendar->thisYear(), $this->calendar->thisMonth());
|
|---|
| 267 | //}
|
|---|
| 268 | return $index;
|
|---|
| 269 | }
|
|---|
| 270 |
|
|---|
| 271 | /**
|
|---|
| 272 | * Returns the index of the last real day in the month, relative to the
|
|---|
| 273 | * beginning of the tabular week it is part of
|
|---|
| 274 | *
|
|---|
| 275 | * @return int
|
|---|
| 276 | * @access protected
|
|---|
| 277 | */
|
|---|
| 278 | function getEmptyDaysAfterOffset()
|
|---|
| 279 | {
|
|---|
| 280 | $eAfter = $this->getEmptyDaysAfter();
|
|---|
| 281 | return $eAfter - (
|
|---|
| 282 | $this->cE->getDaysInWeek(
|
|---|
| 283 | $this->calendar->thisYear(),
|
|---|
| 284 | $this->calendar->thisMonth(),
|
|---|
| 285 | $this->calendar->thisDay()
|
|---|
| 286 | ) * ($this->numWeeks-1));
|
|---|
| 287 | }
|
|---|
| 288 |
|
|---|
| 289 | /**
|
|---|
| 290 | * Returns the timestamp of the first day of the current week
|
|---|
| 291 | *
|
|---|
| 292 | * @param int $y year
|
|---|
| 293 | * @param int $m month
|
|---|
| 294 | * @param int $d day
|
|---|
| 295 | * @param int $firstDay first day of the week (default 1 = Monday)
|
|---|
| 296 | *
|
|---|
| 297 | * @return int timestamp
|
|---|
| 298 | */
|
|---|
| 299 | function getWeekStart($y, $m, $d, $firstDay=1)
|
|---|
| 300 | {
|
|---|
| 301 | $dow = $this->cE->getDayOfWeek($y, $m, $d);
|
|---|
| 302 | if ($dow > $firstDay) {
|
|---|
| 303 | $d -= ($dow - $firstDay);
|
|---|
| 304 | }
|
|---|
| 305 | if ($dow < $firstDay) {
|
|---|
| 306 | $d -= (
|
|---|
| 307 | $this->cE->getDaysInWeek(
|
|---|
| 308 | $this->calendar->thisYear(),
|
|---|
| 309 | $this->calendar->thisMonth(),
|
|---|
| 310 | $this->calendar->thisDay()
|
|---|
| 311 | ) - $firstDay + $dow);
|
|---|
| 312 | }
|
|---|
| 313 | return $this->cE->dateToStamp($y, $m, $d);
|
|---|
| 314 | }
|
|---|
| 315 | }
|
|---|
| 316 | ?> |
|---|