Ignore:
Timestamp:
2013/08/28 13:55:43 (7 years ago)
Author:
m_uehara
Message:

#2348 r23140 をマージ

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/version-2_13_0/data/module/Calendar/Week.php

    r23126 r23143  
    11<?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// 
    423/** 
    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 $ 
    3826 */ 
    3927 
     
    5442 * Represents a Week and builds Days in tabular format<br> 
    5543 * <code> 
    56  * require_once 'Calendar/Week.php'; 
    57  * $Week = new Calendar_Week(2003, 10, 1); Oct 2003, 1st tabular week 
     44 * require_once 'Calendar'.DIRECTORY_SEPARATOR.'Week.php'; 
     45 * $Week = & new Calendar_Week(2003, 10, 1); Oct 2003, 1st tabular week 
    5846 * echo '<tr>'; 
    5947 * while ($Day = & $Week->fetch()) { 
     
    6654 * echo '</tr>'; 
    6755 * </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 
    7658 */ 
    7759class Calendar_Week extends Calendar 
     
    128110    /** 
    129111     * 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); 
    165133    } 
    166134 
     
    168136     * Defines the calendar by a timestamp (Unix or ISO-8601), replacing values 
    169137     * 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 
    173139     * @return void 
    174140     * @access public 
     
    181147        ); 
    182148        $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( 
    186150                $this->thisYear(), 
    187151                $this->thisMonth(), 
    188                 $this->thisDay() 
    189             ),  
    190             $this->firstDay 
     152                $this->thisDay()), $this->firstDay 
    191153        ); 
    192154        $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( 
    196156                $this->thisYear(), 
    197157                $this->thisMonth(), 
    198                 $this->thisDay() 
    199             ),  
    200             $this->firstDay 
     158                $this->thisDay()), $this->firstDay 
    201159        ); 
    202160    } 
     
    204162    /** 
    205163     * 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 
    209165     * @return boolean 
    210166     * @access public 
     
    212168    function build($sDates = array()) 
    213169    { 
    214         include_once CALENDAR_ROOT.'Day.php'; 
     170        require_once CALENDAR_ROOT.'Day.php'; 
    215171        $year  = $this->cE->stampToYear($this->thisWeek); 
    216172        $month = $this->cE->stampToMonth($this->thisWeek); 
     
    225181            $stamp = $this->cE->dateToStamp($year, $month, $day++); 
    226182            $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)); 
    231186        } 
    232187 
     
    252207 
    253208    /** 
    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) 
    262214    { 
    263215        $this->firstWeek = $state; 
     
    265217 
    266218    /** 
    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) 
    275224    { 
    276225        $this->lastWeek = $state; 
     
    279228    /** 
    280229     * Called from build() 
    281      * 
    282      * @param array $sDates Calendar_Day objects representing selected dates 
    283      * 
     230     * @param array 
    284231     * @return void 
    285232     * @access private 
     
    302249 
    303250    /** 
    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     /** 
    337251     * Gets the value of the previous week, according to the requested format 
    338252     * 
    339253     * @param string $format ['timestamp' | 'n_in_month' | 'n_in_year' | 'array'] 
    340      * 
    341254     * @return mixed 
    342255     * @access public 
     
    345258    { 
    346259        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; 
    363281        } 
    364282    } 
     
    368286     * 
    369287     * @param string $format ['timestamp' | 'n_in_month' | 'n_in_year' | 'array'] 
    370      * 
    371288     * @return mixed 
    372289     * @access public 
     
    375292    { 
    376293        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( 
    384306                    $this->thisYear(), 
    385307                    $this->thisMonth(), 
     308                    $this->thisDay(), 
    386309                    $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; 
    406328        } 
    407329    } 
     
    411333     * 
    412334     * @param string $format ['timestamp' | 'n_in_month' | 'n_in_year' | 'array'] 
    413      * 
    414335     * @return mixed 
    415336     * @access public 
     
    418339    { 
    419340        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; 
    436362        } 
    437363    } 
     
    440366     * Returns the instance of Calendar_Table_Helper. 
    441367     * Called from Calendar_Validator::isValidWeek 
    442      * 
    443368     * @return Calendar_Table_Helper 
    444369     * @access protected 
     
    451376    /** 
    452377     * Makes sure theres a value for $this->day 
    453      * 
    454378     * @return void 
    455379     * @access private 
Note: See TracChangeset for help on using the changeset viewer.