Changeset 23141 for branches/version-2_13-dev/data/module/Calendar/Week.php
- Timestamp:
- 2013/08/28 13:26:44 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/version-2_13-dev/data/module/Calendar/Week.php
r23125 r23141 1 1 <?php 2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ 3 2 /* vim: set expandtab tabstop=4 shiftwidth=4: */ 3 // 4 // +----------------------------------------------------------------------+ 5 // | PHP Version 4 | 6 // +----------------------------------------------------------------------+ 7 // | Copyright (c) 1997-2002 The PHP Group | 8 // +----------------------------------------------------------------------+ 9 // | This source file is subject to version 2.02 of the PHP license, | 10 // | that is bundled with this package in the file LICENSE, and is | 11 // | available at through the world-wide-web at | 12 // | http://www.php.net/license/3_0.txt. | 13 // | If you did not receive a copy of the PHP license and are unable to | 14 // | obtain it through the world-wide-web, please send a note to | 15 // | license@php.net so we can mail you a copy immediately. | 16 // +----------------------------------------------------------------------+ 17 // | Authors: Harry Fuecks <hfuecks@phppatterns.com> | 18 // | Lorenzo Alberton <l dot alberton at quipo dot it> | 19 // +----------------------------------------------------------------------+ 20 // 21 // $Id: Week.php,v 1.7 2005/10/22 10:26:49 quipo Exp $ 22 // 4 23 /** 5 * Contains the Calendar_Week 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 <hfuecks@phppatterns.com> 33 * @author Lorenzo Alberton <l.alberton@quipo.it> 34 * @copyright 2003-2007 Harry Fuecks, Lorenzo Alberton 35 * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 36 * @version CVS: $Id: Week.php 300729 2010-06-24 12:05:53Z quipo $ 37 * @link http://pear.php.net/package/Calendar 24 * @package Calendar 25 * @version $Id: Week.php,v 1.7 2005/10/22 10:26:49 quipo Exp $ 38 26 */ 39 27 … … 54 42 * Represents a Week and builds Days in tabular format<br> 55 43 * <code> 56 * require_once 'Calendar /Week.php';57 * $Week = new Calendar_Week(2003, 10, 1); Oct 2003, 1st tabular week44 * require_once 'Calendar'.DIRECTORY_SEPARATOR.'Week.php'; 45 * $Week = & new Calendar_Week(2003, 10, 1); Oct 2003, 1st tabular week 58 46 * echo '<tr>'; 59 47 * while ($Day = & $Week->fetch()) { … … 66 54 * echo '</tr>'; 67 55 * </code> 68 * 69 * @category Date and Time 70 * @package Calendar 71 * @author Harry Fuecks <hfuecks@phppatterns.com> 72 * @author Lorenzo Alberton <l.alberton@quipo.it> 73 * @copyright 2003-2007 Harry Fuecks, Lorenzo Alberton 74 * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) 75 * @link http://pear.php.net/package/Calendar 56 * @package Calendar 57 * @access public 76 58 */ 77 59 class Calendar_Week extends Calendar … … 128 110 /** 129 111 * Constructs Week 130 * 131 * @param int $y year e.g. 2003 132 * @param int $m month e.g. 5 133 * @param int $d a day of the desired week 134 * @param int $firstDay (optional) first day of week (e.g. 0 for Sunday, 2 for Tuesday etc.) 135 * 136 * @access public 137 */ 138 function Calendar_Week($y, $m, $d, $firstDay = null) 139 { 140 include_once CALENDAR_ROOT.'Table/Helper.php'; 141 parent::Calendar($y, $m, $d); 142 $this->firstDay = $this->defineFirstDayOfWeek($firstDay); 143 $this->tableHelper = new Calendar_Table_Helper($this, $this->firstDay); 144 $this->thisWeek = $this->tableHelper->getWeekStart($y, $m, $d, $this->firstDay); 145 $this->prevWeek = $this->tableHelper->getWeekStart( 146 $y, 147 $m, 148 $d - $this->cE->getDaysInWeek( 149 $this->thisYear(), 150 $this->thisMonth(), 151 $this->thisDay() 152 ), 153 $this->firstDay 154 ); 155 $this->nextWeek = $this->tableHelper->getWeekStart( 156 $y, 157 $m, 158 $d + $this->cE->getDaysInWeek( 159 $this->thisYear(), 160 $this->thisMonth(), 161 $this->thisDay() 162 ), 163 $this->firstDay 164 ); 112 * @param int year e.g. 2003 113 * @param int month e.g. 5 114 * @param int a day of the desired week 115 * @param int (optional) first day of week (e.g. 0 for Sunday, 2 for Tuesday etc.) 116 * @access public 117 */ 118 function Calendar_Week($y, $m, $d, $firstDay=null) 119 { 120 require_once CALENDAR_ROOT.'Table/Helper.php'; 121 Calendar::Calendar($y, $m, $d); 122 $this->firstDay = $this->defineFirstDayOfWeek($firstDay); 123 $this->tableHelper = & new Calendar_Table_Helper($this, $this->firstDay); 124 $this->thisWeek = $this->tableHelper->getWeekStart($y, $m, $d, $this->firstDay); 125 $this->prevWeek = $this->tableHelper->getWeekStart($y, $m, $d - $this->cE->getDaysInWeek( 126 $this->thisYear(), 127 $this->thisMonth(), 128 $this->thisDay()), $this->firstDay); 129 $this->nextWeek = $this->tableHelper->getWeekStart($y, $m, $d + $this->cE->getDaysInWeek( 130 $this->thisYear(), 131 $this->thisMonth(), 132 $this->thisDay()), $this->firstDay); 165 133 } 166 134 … … 168 136 * Defines the calendar by a timestamp (Unix or ISO-8601), replacing values 169 137 * passed to the constructor 170 * 171 * @param int|string $ts Unix or ISO-8601 timestamp 172 * 138 * @param int|string Unix or ISO-8601 timestamp 173 139 * @return void 174 140 * @access public … … 181 147 ); 182 148 $this->prevWeek = $this->tableHelper->getWeekStart( 183 $this->year, 184 $this->month, 185 $this->day - $this->cE->getDaysInWeek( 149 $this->year, $this->month, $this->day - $this->cE->getDaysInWeek( 186 150 $this->thisYear(), 187 151 $this->thisMonth(), 188 $this->thisDay() 189 ), 190 $this->firstDay 152 $this->thisDay()), $this->firstDay 191 153 ); 192 154 $this->nextWeek = $this->tableHelper->getWeekStart( 193 $this->year, 194 $this->month, 195 $this->day + $this->cE->getDaysInWeek( 155 $this->year, $this->month, $this->day + $this->cE->getDaysInWeek( 196 156 $this->thisYear(), 197 157 $this->thisMonth(), 198 $this->thisDay() 199 ), 200 $this->firstDay 158 $this->thisDay()), $this->firstDay 201 159 ); 202 160 } … … 204 162 /** 205 163 * Builds Calendar_Day objects for this Week 206 * 207 * @param array $sDates (optional) Calendar_Day objects representing selected dates 208 * 164 * @param array (optional) Calendar_Day objects representing selected dates 209 165 * @return boolean 210 166 * @access public … … 212 168 function build($sDates = array()) 213 169 { 214 include_once CALENDAR_ROOT.'Day.php';170 require_once CALENDAR_ROOT.'Day.php'; 215 171 $year = $this->cE->stampToYear($this->thisWeek); 216 172 $month = $this->cE->stampToMonth($this->thisWeek); … … 225 181 $stamp = $this->cE->dateToStamp($year, $month, $day++); 226 182 $this->children[$i] = new Calendar_Day( 227 $this->cE->stampToYear($stamp), 228 $this->cE->stampToMonth($stamp), 229 $this->cE->stampToDay($stamp) 230 ); 183 $this->cE->stampToYear($stamp), 184 $this->cE->stampToMonth($stamp), 185 $this->cE->stampToDay($stamp)); 231 186 } 232 187 … … 252 207 253 208 /** 254 * Set as first week of the month 255 * 256 * @param boolean $state whether it's first or not 257 * 258 * @return void 259 * @access private 260 */ 261 function setFirst($state = true) 209 * @param boolean 210 * @return void 211 * @access private 212 */ 213 function setFirst($state=true) 262 214 { 263 215 $this->firstWeek = $state; … … 265 217 266 218 /** 267 * Set as last week of the month 268 * 269 * @param boolean $state whether it's lasst or not 270 * 271 * @return void 272 * @access private 273 */ 274 function setLast($state = true) 219 * @param boolean 220 * @return void 221 * @access private 222 */ 223 function setLast($state=true) 275 224 { 276 225 $this->lastWeek = $state; … … 279 228 /** 280 229 * Called from build() 281 * 282 * @param array $sDates Calendar_Day objects representing selected dates 283 * 230 * @param array 284 231 * @return void 285 232 * @access private … … 302 249 303 250 /** 304 * Returns the value for this year305 *306 * When a on the first/last week of the year, the year of the week is307 * calculated according to ISO-8601308 *309 * @param string $format return value format ['int' | 'timestamp' | 'object' | 'array']310 *311 * @return int e.g. 2003 or timestamp312 * @access public313 */314 function thisYear($format = 'int')315 {316 if (null !== $this->thisWeek) {317 $tmp_cal = new Calendar();318 $tmp_cal->setTimestamp($this->thisWeek);319 $first_dow = $tmp_cal->thisDay('array');320 $days_in_week = $tmp_cal->cE->getDaysInWeek($tmp_cal->year, $tmp_cal->month, $tmp_cal->day);321 $tmp_cal->day += $days_in_week;322 $last_dow = $tmp_cal->thisDay('array');323 324 if ($first_dow['year'] == $last_dow['year']) {325 return $first_dow['year'];326 }327 328 if ($last_dow['day'] > floor($days_in_week / 2)) {329 return $last_dow['year'];330 }331 return $first_dow['year'];332 }333 return parent::thisYear();334 }335 336 /**337 251 * Gets the value of the previous week, according to the requested format 338 252 * 339 253 * @param string $format ['timestamp' | 'n_in_month' | 'n_in_year' | 'array'] 340 *341 254 * @return mixed 342 255 * @access public … … 345 258 { 346 259 switch (strtolower($format)) { 347 case 'int': 348 case 'n_in_month': 349 return ($this->firstWeek) ? null : $this->thisWeek('n_in_month') -1; 350 case 'n_in_year': 351 return $this->cE->getWeekNInYear( 352 $this->cE->stampToYear($this->prevWeek), 353 $this->cE->stampToMonth($this->prevWeek), 354 $this->cE->stampToDay($this->prevWeek)); 355 case 'array': 356 return $this->toArray($this->prevWeek); 357 case 'object': 358 include_once CALENDAR_ROOT.'Factory.php'; 359 return Calendar_Factory::createByTimestamp('Week', $this->prevWeek); 360 case 'timestamp': 361 default: 362 return $this->prevWeek; 260 case 'int': 261 case 'n_in_month': 262 return ($this->firstWeek) ? null : $this->thisWeek('n_in_month') -1; 263 break; 264 case 'n_in_year': 265 return $this->cE->getWeekNInYear( 266 $this->cE->stampToYear($this->prevWeek), 267 $this->cE->stampToMonth($this->prevWeek), 268 $this->cE->stampToDay($this->prevWeek)); 269 break; 270 case 'array': 271 return $this->toArray($this->prevWeek); 272 break; 273 case 'object': 274 require_once CALENDAR_ROOT.'Factory.php'; 275 return Calendar_Factory::createByTimestamp('Week', $this->prevWeek); 276 break; 277 case 'timestamp': 278 default: 279 return $this->prevWeek; 280 break; 363 281 } 364 282 } … … 368 286 * 369 287 * @param string $format ['timestamp' | 'n_in_month' | 'n_in_year' | 'array'] 370 *371 288 * @return mixed 372 289 * @access public … … 375 292 { 376 293 switch (strtolower($format)) { 377 case 'int': 378 case 'n_in_month': 379 if ($this->firstWeek) { 380 return 1; 381 } 382 if ($this->lastWeek) { 383 return $this->cE->getWeeksInMonth( 294 case 'int': 295 case 'n_in_month': 296 if ($this->firstWeek) { 297 return 1; 298 } 299 if ($this->lastWeek) { 300 return $this->cE->getWeeksInMonth( 301 $this->thisYear(), 302 $this->thisMonth(), 303 $this->firstDay); 304 } 305 return $this->cE->getWeekNInMonth( 384 306 $this->thisYear(), 385 307 $this->thisMonth(), 308 $this->thisDay(), 386 309 $this->firstDay); 387 } 388 return $this->cE->getWeekNInMonth( 389 $this->thisYear(), 390 $this->thisMonth(), 391 $this->thisDay(), 392 $this->firstDay); 393 case 'n_in_year': 394 return $this->cE->getWeekNInYear( 395 $this->cE->stampToYear($this->thisWeek), 396 $this->cE->stampToMonth($this->thisWeek), 397 $this->cE->stampToDay($this->thisWeek)); 398 case 'array': 399 return $this->toArray($this->thisWeek); 400 case 'object': 401 include_once CALENDAR_ROOT.'Factory.php'; 402 return Calendar_Factory::createByTimestamp('Week', $this->thisWeek); 403 case 'timestamp': 404 default: 405 return $this->thisWeek; 310 break; 311 case 'n_in_year': 312 return $this->cE->getWeekNInYear( 313 $this->cE->stampToYear($this->thisWeek), 314 $this->cE->stampToMonth($this->thisWeek), 315 $this->cE->stampToDay($this->thisWeek)); 316 break; 317 case 'array': 318 return $this->toArray($this->thisWeek); 319 break; 320 case 'object': 321 require_once CALENDAR_ROOT.'Factory.php'; 322 return Calendar_Factory::createByTimestamp('Week', $this->thisWeek); 323 break; 324 case 'timestamp': 325 default: 326 return $this->thisWeek; 327 break; 406 328 } 407 329 } … … 411 333 * 412 334 * @param string $format ['timestamp' | 'n_in_month' | 'n_in_year' | 'array'] 413 *414 335 * @return mixed 415 336 * @access public … … 418 339 { 419 340 switch (strtolower($format)) { 420 case 'int': 421 case 'n_in_month': 422 return ($this->lastWeek) ? null : $this->thisWeek('n_in_month') +1; 423 case 'n_in_year': 424 return $this->cE->getWeekNInYear( 425 $this->cE->stampToYear($this->nextWeek), 426 $this->cE->stampToMonth($this->nextWeek), 427 $this->cE->stampToDay($this->nextWeek)); 428 case 'array': 429 return $this->toArray($this->nextWeek); 430 case 'object': 431 include_once CALENDAR_ROOT.'Factory.php'; 432 return Calendar_Factory::createByTimestamp('Week', $this->nextWeek); 433 case 'timestamp': 434 default: 435 return $this->nextWeek; 341 case 'int': 342 case 'n_in_month': 343 return ($this->lastWeek) ? null : $this->thisWeek('n_in_month') +1; 344 break; 345 case 'n_in_year': 346 return $this->cE->getWeekNInYear( 347 $this->cE->stampToYear($this->nextWeek), 348 $this->cE->stampToMonth($this->nextWeek), 349 $this->cE->stampToDay($this->nextWeek)); 350 break; 351 case 'array': 352 return $this->toArray($this->nextWeek); 353 break; 354 case 'object': 355 require_once CALENDAR_ROOT.'Factory.php'; 356 return Calendar_Factory::createByTimestamp('Week', $this->nextWeek); 357 break; 358 case 'timestamp': 359 default: 360 return $this->nextWeek; 361 break; 436 362 } 437 363 } … … 440 366 * Returns the instance of Calendar_Table_Helper. 441 367 * Called from Calendar_Validator::isValidWeek 442 *443 368 * @return Calendar_Table_Helper 444 369 * @access protected … … 451 376 /** 452 377 * Makes sure theres a value for $this->day 453 *454 378 * @return void 455 379 * @access private
Note: See TracChangeset
for help on using the changeset viewer.