Changeset 23126 for branches/version-2_13_0/data/module/Calendar/Week.php
- Timestamp:
- 2013/08/26 15:52:37 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/version-2_13_0/data/module/Calendar/Week.php
r20119 r23126 1 1 <?php 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 // 2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ 3 23 4 /** 24 * @package Calendar 25 * @version $Id: Week.php,v 1.7 2005/10/22 10:26:49 quipo Exp $ 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 26 38 */ 27 39 … … 42 54 * Represents a Week and builds Days in tabular format<br> 43 55 * <code> 44 * require_once 'Calendar '.DIRECTORY_SEPARATOR.'Week.php';45 * $Week = &new Calendar_Week(2003, 10, 1); Oct 2003, 1st tabular week56 * require_once 'Calendar/Week.php'; 57 * $Week = new Calendar_Week(2003, 10, 1); Oct 2003, 1st tabular week 46 58 * echo '<tr>'; 47 59 * while ($Day = & $Week->fetch()) { … … 54 66 * echo '</tr>'; 55 67 * </code> 56 * @package Calendar 57 * @access public 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 58 76 */ 59 77 class Calendar_Week extends Calendar … … 110 128 /** 111 129 * Constructs Week 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); 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 ); 133 165 } 134 166 … … 136 168 * Defines the calendar by a timestamp (Unix or ISO-8601), replacing values 137 169 * passed to the constructor 138 * @param int|string Unix or ISO-8601 timestamp 170 * 171 * @param int|string $ts Unix or ISO-8601 timestamp 172 * 139 173 * @return void 140 174 * @access public … … 147 181 ); 148 182 $this->prevWeek = $this->tableHelper->getWeekStart( 149 $this->year, $this->month, $this->day - $this->cE->getDaysInWeek( 183 $this->year, 184 $this->month, 185 $this->day - $this->cE->getDaysInWeek( 150 186 $this->thisYear(), 151 187 $this->thisMonth(), 152 $this->thisDay()), $this->firstDay 188 $this->thisDay() 189 ), 190 $this->firstDay 153 191 ); 154 192 $this->nextWeek = $this->tableHelper->getWeekStart( 155 $this->year, $this->month, $this->day + $this->cE->getDaysInWeek( 193 $this->year, 194 $this->month, 195 $this->day + $this->cE->getDaysInWeek( 156 196 $this->thisYear(), 157 197 $this->thisMonth(), 158 $this->thisDay()), $this->firstDay 198 $this->thisDay() 199 ), 200 $this->firstDay 159 201 ); 160 202 } … … 162 204 /** 163 205 * Builds Calendar_Day objects for this Week 164 * @param array (optional) Calendar_Day objects representing selected dates 206 * 207 * @param array $sDates (optional) Calendar_Day objects representing selected dates 208 * 165 209 * @return boolean 166 210 * @access public … … 168 212 function build($sDates = array()) 169 213 { 170 require_once CALENDAR_ROOT.'Day.php';214 include_once CALENDAR_ROOT.'Day.php'; 171 215 $year = $this->cE->stampToYear($this->thisWeek); 172 216 $month = $this->cE->stampToMonth($this->thisWeek); … … 181 225 $stamp = $this->cE->dateToStamp($year, $month, $day++); 182 226 $this->children[$i] = new Calendar_Day( 183 $this->cE->stampToYear($stamp), 184 $this->cE->stampToMonth($stamp), 185 $this->cE->stampToDay($stamp)); 227 $this->cE->stampToYear($stamp), 228 $this->cE->stampToMonth($stamp), 229 $this->cE->stampToDay($stamp) 230 ); 186 231 } 187 232 … … 207 252 208 253 /** 209 * @param boolean 254 * Set as first week of the month 255 * 256 * @param boolean $state whether it's first or not 257 * 210 258 * @return void 211 259 * @access private 212 260 */ 213 function setFirst($state =true)261 function setFirst($state = true) 214 262 { 215 263 $this->firstWeek = $state; … … 217 265 218 266 /** 219 * @param boolean 267 * Set as last week of the month 268 * 269 * @param boolean $state whether it's lasst or not 270 * 220 271 * @return void 221 272 * @access private 222 273 */ 223 function setLast($state =true)274 function setLast($state = true) 224 275 { 225 276 $this->lastWeek = $state; … … 228 279 /** 229 280 * Called from build() 230 * @param array 281 * 282 * @param array $sDates Calendar_Day objects representing selected dates 283 * 231 284 * @return void 232 285 * @access private … … 249 302 250 303 /** 304 * Returns the value for this year 305 * 306 * When a on the first/last week of the year, the year of the week is 307 * calculated according to ISO-8601 308 * 309 * @param string $format return value format ['int' | 'timestamp' | 'object' | 'array'] 310 * 311 * @return int e.g. 2003 or timestamp 312 * @access public 313 */ 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 /** 251 337 * Gets the value of the previous week, according to the requested format 252 338 * 253 339 * @param string $format ['timestamp' | 'n_in_month' | 'n_in_year' | 'array'] 340 * 254 341 * @return mixed 255 342 * @access public … … 258 345 { 259 346 switch (strtolower($format)) { 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; 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; 281 363 } 282 364 } … … 286 368 * 287 369 * @param string $format ['timestamp' | 'n_in_month' | 'n_in_year' | 'array'] 370 * 288 371 * @return mixed 289 372 * @access public … … 292 375 { 293 376 switch (strtolower($format)) { 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( 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( 306 384 $this->thisYear(), 307 385 $this->thisMonth(), 308 $this->thisDay(),309 386 $this->firstDay); 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; 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; 328 406 } 329 407 } … … 333 411 * 334 412 * @param string $format ['timestamp' | 'n_in_month' | 'n_in_year' | 'array'] 413 * 335 414 * @return mixed 336 415 * @access public … … 339 418 { 340 419 switch (strtolower($format)) { 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; 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; 362 436 } 363 437 } … … 366 440 * Returns the instance of Calendar_Table_Helper. 367 441 * Called from Calendar_Validator::isValidWeek 442 * 368 443 * @return Calendar_Table_Helper 369 444 * @access protected … … 376 451 /** 377 452 * Makes sure theres a value for $this->day 453 * 378 454 * @return void 379 455 * @access private
Note: See TracChangeset
for help on using the changeset viewer.