00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00018 final class CalendarMonthWeekly
00019 {
00020 private $monthRange = null;
00021 private $fullRange = null;
00022 private $fullLength = null;
00023
00024 private $weeks = array();
00025 private $days = array();
00026
00027 public function __construct(
00028 Date $base, $weekStart = Timestamp::WEEKDAY_MONDAY
00029 )
00030 {
00031 $firstDayOfMonth = Date::create(
00032 $base->getYear().'-'.$base->getMonth().'-01'
00033 );
00034
00035 $lastDayOfMonth = Date::create(
00036 $base->getYear().'-'.$base->getMonth().'-'
00037 .date('t', $base->toStamp()));
00038
00039 $start = $firstDayOfMonth->getFirstDayOfWeek($weekStart);
00040
00041 $end = $lastDayOfMonth->getLastDayOfWeek($weekStart);
00042
00043 $this->monthRange = DateRange::create()->lazySet(
00044 $firstDayOfMonth, $lastDayOfMonth
00045 );
00046
00047 $this->fullRange = DateRange::create()->lazySet(
00048 $start, $end
00049 );
00050
00051 $rawDays = $this->fullRange->split();
00052 $this->fullLength = 0;
00053
00054 foreach ($rawDays as $rawDay) {
00055 $day = CalendarDay::create($rawDay->toStamp());
00056
00057 if ($this->monthRange->contains($day))
00058 $day->setOutside(false);
00059 else
00060 $day->setOutside(true);
00061
00062 $this->days[$day->toDate()] = $day;
00063
00064 $weekNumber = floor($this->fullLength/7);
00065
00066 if (!isset($this->weeks[$weekNumber]))
00067 $this->weeks[$weekNumber] = CalendarWeek::create();
00068
00069 $this->weeks[$weekNumber]->addDay($day);
00070 ++$this->fullLength;
00071 }
00072
00073 ++$this->fullLength;
00074 }
00075
00079 public static function create(
00080 Date $base, $weekStart = Timestamp::WEEKDAY_MONDAY
00081 )
00082 {
00083 return new self($base, $weekStart);
00084 }
00085
00086 public function getWeeks()
00087 {
00088 return $this->weeks;
00089 }
00090
00091 public function getDays()
00092 {
00093 return $this->days;
00094 }
00095
00099 public function getFullRange()
00100 {
00101 return $this->fullRange;
00102 }
00103
00104 public function getFullLength()
00105 {
00106 return $this->fullLength;
00107 }
00108
00112 public function getMonthRange()
00113 {
00114 return $this->monthRange;
00115 }
00116
00121 public function setSelected(Date $day)
00122 {
00123 if (!isset($this->days[$day->toDate()]))
00124 throw new WrongArgumentException($day->toDate().' not in calendar');
00125
00126 $this->days[$day->toDate()]->setSelected(true);
00127
00128 return $this;
00129 }
00130
00134 public function getNextMonthBase()
00135 {
00136 return $this->monthRange->getEnd()->spawn('+1 day');
00137 }
00138
00142 public function getPrevMonthBase()
00143 {
00144 return $this->monthRange->getStart()->spawn('-1 day');
00145 }
00146
00150 public function getBase()
00151 {
00152 return $this->monthRange->getStart();
00153 }
00154 }
00155 ?>