home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / Calendar / Week.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  12.2 KB  |  394 lines

  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. //
  23. /**
  24.  * @package Calendar
  25.  * @version $Id: Week.php,v 1.7 2005/10/22 10:26:49 quipo Exp $
  26.  */
  27.  
  28. /**
  29.  * Allows Calendar include path to be redefined
  30.  * @ignore
  31.  */
  32. if (!defined('CALENDAR_ROOT')) {
  33.     define('CALENDAR_ROOT', 'Calendar'.DIRECTORY_SEPARATOR);
  34. }
  35.  
  36. /**
  37.  * Load Calendar base class
  38.  */
  39. require_once CALENDAR_ROOT.'Calendar.php';
  40.  
  41. /**
  42.  * Represents a Week and builds Days in tabular format<br>
  43.  * <code>
  44.  * require_once 'Calendar'.DIRECTORY_SEPARATOR.'Week.php';
  45.  * $Week = & new Calendar_Week(2003, 10, 1); Oct 2003, 1st tabular week
  46.  * echo '<tr>';
  47.  * while ($Day = & $Week->fetch()) {
  48.  *     if ($Day->isEmpty()) {
  49.  *         echo '<td> </td>';
  50.  *     } else {
  51.  *         echo '<td>'.$Day->thisDay().'</td>';
  52.  *      }
  53.  * }
  54.  * echo '</tr>';
  55.  * </code>
  56.  * @package Calendar
  57.  * @access public
  58.  */
  59. class Calendar_Week extends Calendar
  60. {
  61.     /**
  62.      * Instance of Calendar_Table_Helper
  63.      * @var Calendar_Table_Helper
  64.      * @access private
  65.      */
  66.     var $tableHelper;
  67.  
  68.     /**
  69.      * Stores the timestamp of the first day of this week
  70.      * @access private
  71.      * @var object
  72.      */
  73.     var $thisWeek;
  74.  
  75.     /**
  76.      * Stores the timestamp of first day of previous week
  77.      * @access private
  78.      * @var object
  79.      */
  80.     var $prevWeek;
  81.  
  82.     /**
  83.      * Stores the timestamp of first day of next week
  84.      * @access private
  85.      * @var object
  86.      */
  87.     var $nextWeek;
  88.  
  89.     /**
  90.      * Used by build() to set empty days
  91.      * @access private
  92.      * @var boolean
  93.      */
  94.     var $firstWeek = false;
  95.  
  96.     /**
  97.      * Used by build() to set empty days
  98.      * @access private
  99.      * @var boolean
  100.      */
  101.     var $lastWeek = false;
  102.  
  103.     /**
  104.      * First day of the week (0=sunday, 1=monday...)
  105.      * @access private
  106.      * @var boolean
  107.      */
  108.     var $firstDay = 1;
  109.  
  110.     /**
  111.      * 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);
  133.     }
  134.  
  135.     /**
  136.      * Defines the calendar by a timestamp (Unix or ISO-8601), replacing values
  137.      * passed to the constructor
  138.      * @param int|string Unix or ISO-8601 timestamp
  139.      * @return void
  140.      * @access public
  141.      */
  142.     function setTimestamp($ts)
  143.     {
  144.         parent::setTimestamp($ts);
  145.         $this->thisWeek = $this->tableHelper->getWeekStart(
  146.             $this->year, $this->month, $this->day, $this->firstDay
  147.         );
  148.         $this->prevWeek = $this->tableHelper->getWeekStart(
  149.             $this->year, $this->month, $this->day - $this->cE->getDaysInWeek(
  150.                 $this->thisYear(),
  151.                 $this->thisMonth(),
  152.                 $this->thisDay()), $this->firstDay
  153.         );
  154.         $this->nextWeek = $this->tableHelper->getWeekStart(
  155.             $this->year, $this->month, $this->day + $this->cE->getDaysInWeek(
  156.                 $this->thisYear(),
  157.                 $this->thisMonth(),
  158.                 $this->thisDay()), $this->firstDay
  159.         );
  160.     }
  161.  
  162.     /**
  163.      * Builds Calendar_Day objects for this Week
  164.      * @param array (optional) Calendar_Day objects representing selected dates
  165.      * @return boolean
  166.      * @access public
  167.      */
  168.     function build($sDates = array())
  169.     {
  170.         require_once CALENDAR_ROOT.'Day.php';
  171.         $year  = $this->cE->stampToYear($this->thisWeek);
  172.         $month = $this->cE->stampToMonth($this->thisWeek);
  173.         $day   = $this->cE->stampToDay($this->thisWeek);
  174.         $end   = $this->cE->getDaysInWeek(
  175.             $this->thisYear(),
  176.             $this->thisMonth(),
  177.             $this->thisDay()
  178.         );
  179.  
  180.         for ($i=1; $i <= $end; $i++) {
  181.             $stamp = $this->cE->dateToStamp($year, $month, $day++);
  182.             $this->children[$i] = new Calendar_Day(
  183.                                 $this->cE->stampToYear($stamp),
  184.                                 $this->cE->stampToMonth($stamp),
  185.                                 $this->cE->stampToDay($stamp));
  186.         }
  187.  
  188.         //set empty days (@see Calendar_Month_Weeks::build())
  189.         if ($this->firstWeek) {
  190.             $eBefore = $this->tableHelper->getEmptyDaysBefore();
  191.             for ($i=1; $i <= $eBefore; $i++) {
  192.                 $this->children[$i]->setEmpty();
  193.             }
  194.         }
  195.         if ($this->lastWeek) {
  196.             $eAfter = $this->tableHelper->getEmptyDaysAfterOffset();
  197.             for ($i = $eAfter+1; $i <= $end; $i++) {
  198.                 $this->children[$i]->setEmpty();
  199.             }
  200.         }
  201.  
  202.         if (count($sDates) > 0) {
  203.             $this->setSelection($sDates);
  204.         }
  205.         return true;
  206.     }
  207.  
  208.     /**
  209.      * @param boolean
  210.      * @return void
  211.      * @access private
  212.      */
  213.     function setFirst($state=true)
  214.     {
  215.         $this->firstWeek = $state;
  216.     }
  217.  
  218.     /**
  219.      * @param boolean
  220.      * @return void
  221.      * @access private
  222.      */
  223.     function setLast($state=true)
  224.     {
  225.         $this->lastWeek = $state;
  226.     }
  227.  
  228.     /**
  229.      * Called from build()
  230.      * @param array
  231.      * @return void
  232.      * @access private
  233.      */
  234.     function setSelection($sDates)
  235.     {
  236.         foreach ($sDates as $sDate) {
  237.             foreach ($this->children as $key => $child) {
  238.                 if ($child->thisDay() == $sDate->thisDay() &&
  239.                     $child->thisMonth() == $sDate->thisMonth() &&
  240.                     $child->thisYear() == $sDate->thisYear()
  241.                 ) {
  242.                     $this->children[$key] = $sDate;
  243.                     $this->children[$key]->setSelected();
  244.                 }
  245.             }
  246.         }
  247.         reset($this->children);
  248.     }
  249.  
  250.     /**
  251.      * Gets the value of the previous week, according to the requested format
  252.      *
  253.      * @param string $format ['timestamp' | 'n_in_month' | 'n_in_year' | 'array']
  254.      * @return mixed
  255.      * @access public
  256.      */
  257.     function prevWeek($format = 'n_in_month')
  258.     {
  259.         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;
  281.         }
  282.     }
  283.  
  284.     /**
  285.      * Gets the value of the current week, according to the requested format
  286.      *
  287.      * @param string $format ['timestamp' | 'n_in_month' | 'n_in_year' | 'array']
  288.      * @return mixed
  289.      * @access public
  290.      */
  291.     function thisWeek($format = 'n_in_month')
  292.     {
  293.         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(
  306.                     $this->thisYear(),
  307.                     $this->thisMonth(),
  308.                     $this->thisDay(),
  309.                     $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;
  328.         }
  329.     }
  330.  
  331.     /**
  332.      * Gets the value of the following week, according to the requested format
  333.      *
  334.      * @param string $format ['timestamp' | 'n_in_month' | 'n_in_year' | 'array']
  335.      * @return mixed
  336.      * @access public
  337.      */
  338.     function nextWeek($format = 'n_in_month')
  339.     {
  340.         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;
  362.         }
  363.     }
  364.  
  365.     /**
  366.      * Returns the instance of Calendar_Table_Helper.
  367.      * Called from Calendar_Validator::isValidWeek
  368.      * @return Calendar_Table_Helper
  369.      * @access protected
  370.      */
  371.     function & getHelper()
  372.     {
  373.         return $this->tableHelper;
  374.     }
  375.  
  376.     /**
  377.      * Makes sure theres a value for $this->day
  378.      * @return void
  379.      * @access private
  380.      */
  381.     function findFirstDay()
  382.     {
  383.         if (!count($this->children) > 0) {
  384.             $this->build();
  385.             foreach ($this->children as $Day) {
  386.                 if (!$Day->isEmpty()) {
  387.                     $this->day = $Day->thisDay();
  388.                     break;
  389.                 }
  390.             }
  391.         }
  392.     }
  393. }
  394. ?>