CalendarMonthWeekly.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2006-2007 by Anton E. Lebedevich                        *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Lesser General Public License as        *
00007  *   published by the Free Software Foundation; either version 3 of the    *
00008  *   License, or (at your option) any later version.                       *
00009  *                                                                         *
00010  ***************************************************************************/
00011 
00017     final class CalendarMonthWeekly
00018     {
00019         private $monthRange = null;
00020         private $fullRange  = null;
00021         private $fullLength = null;
00022         
00023         private $weeks      = array();
00024         private $days       = array();
00025         
00026         public function __construct(
00027             Date $base, $weekStart = Timestamp::WEEKDAY_MONDAY
00028         )
00029         {
00030             $firstDayOfMonth = Date::create(
00031                 $base->getYear().'-'.$base->getMonth().'-01'
00032             );
00033             
00034             $lastDayOfMonth = Date::create(
00035                 $base->getYear().'-'.$base->getMonth().'-'
00036                 .date('t', $base->toStamp()));
00037             
00038             $start = $firstDayOfMonth->getFirstDayOfWeek($weekStart);
00039             
00040             $end = $lastDayOfMonth->getLastDayOfWeek($weekStart);
00041             
00042             $this->monthRange = DateRange::create()->lazySet(
00043                 $firstDayOfMonth, $lastDayOfMonth
00044             );
00045             
00046             $this->fullRange = DateRange::create()->lazySet(
00047                 $start, $end
00048             );
00049             
00050             $rawDays = $this->fullRange->split();
00051             $this->fullLength = 0;
00052             
00053             foreach ($rawDays as $rawDay) {
00054                 $day = CalendarDay::create($rawDay->toStamp());
00055                 
00056                 if ($this->monthRange->contains($day))
00057                     $day->setOutside(false);
00058                 else
00059                     $day->setOutside(true);
00060                     
00061                 $this->days[$day->toDate()] = $day;
00062                 
00063                 $weekNumber = floor($this->fullLength/7);
00064                 
00065                 if (!isset($this->weeks[$weekNumber]))
00066                     $this->weeks[$weekNumber] = CalendarWeek::create();
00067                 
00068                 $this->weeks[$weekNumber]->addDay($day);
00069                 ++$this->fullLength;
00070             }
00071             
00072             ++$this->fullLength;
00073         }
00074         
00078         public static function create(
00079             Date $base, $weekStart = Timestamp::WEEKDAY_MONDAY
00080         )
00081         {
00082             return new self($base, $weekStart);
00083         }
00084         
00085         public function getWeeks()
00086         {
00087             return $this->weeks;
00088         }
00089         
00090         public function getDays()
00091         {
00092             return $this->days;
00093         }
00094         
00098         public function getFullRange()
00099         {
00100             return $this->fullRange;
00101         }
00102         
00103         public function getFullLength()
00104         {
00105             return $this->fullLength;
00106         }
00107         
00111         public function getMonthRange()
00112         {
00113             return $this->monthRange;
00114         }
00115         
00120         public function setSelected(Date $day)
00121         {
00122             if (!isset($this->days[$day->toDate()]))
00123                 throw new WrongArgumentException($day->toDate().' not in calendar');
00124             
00125             $this->days[$day->toDate()]->setSelected(true);
00126             
00127             return $this;
00128         }
00129         
00133         public function getNextMonthBase()
00134         {
00135             return $this->monthRange->getEnd()->spawn('+1 day');
00136         }
00137         
00141         public function getPrevMonthBase()
00142         {
00143             return $this->monthRange->getStart()->spawn('-1 day');
00144         }
00145         
00149         public function getBase()
00150         {
00151             return $this->monthRange->getStart();
00152         }
00153     }
00154 ?>