* @package WB */ // WBClass::load(); /** * Populate form element's values with entity list * * Use table data to populate values * * @version 0.3.0 * @package WB */ class WBDatasource_Calendar extends WBStdClass { /** * @var DateTime */ private $date; /** * Basic Time String * @var string */ private $baseTimeString; /** * @var array of WBDatasource_Calendar_Decorator */ private $decorator = array(); /** * Constructor * * @param array */ public function __construct($params = array()) { $params = array_merge(array( 'date' => 'now' ), $params); $this->setBaseDate($params['date']); } /** * Set Date * * @param string timestring, like 'now' */ public function setBaseDate($date = 'now') { $this->baseTimeString = $date; $this->date = new DateTime($date); } /** * Attach Decorator * * @param WBDatasource_Calendar_Decorator */ public function attach($dec) { $this->decorator[] = $dec; } /** * Add Date Interval * * @see sub() * @param string date Interval */ public function add($di, $offset = false) { if (!$offset) { $this->date = new DateTime($this->baseTimeString); } $this->date->add(new DateInterval($di)); } /** * Substract Date Interval * * @param string date Interval */ public function sub($di, $offset = false) { if (!$offset) { $this->date = new DateTime($this->baseTimeString); } $this->date->sub(new DateInterval($di)); } /** * Get Days 4 Month * * Return number of days, but also fill list array with days. * * @param array list for days * @return int number of days in month */ public function getDays4Month(&$list = null) { $month = array( 'year' => $this->date->format('Y'), 'month' => $this->date->format('m') ); $date = new DateTime(); $date->setDate($month['year'], $month['month'], 1); $days = $date->format('t'); if (!is_array($list)) { return $days; } $week = array( 'month_name' => $date->format('F'), 'month_short' => $date->format('M'), 'month' => $month['month'], 'year' => $month['year'], 'week_no' => $date->format('W'), 'day1' => '', 'day2' => '', 'day3' => '', 'day4' => '', 'day5' => '', 'day6' => '', 'day7' => '' ); for ($i = 1; $i <= $days; ++$i) { if ($date->format('W') != $week['week_no']) { $this->injectInfo4Week($date, $week); $list[] = $week; $week = array( 'month_name' => $date->format('F'), 'month_short' => $date->format('M'), 'month' => $month['month'], 'year' => $month['year'], 'week_no' => $date->format('W'), 'day1' => '', 'day2' => '', 'day3' => '', 'day4' => '', 'day5' => '', 'day6' => '', 'day7' => '' ); } $this->injectInfo4Day($date, $week); $date->add(new DateInterval('P1D')); } if (empty($week['day1'])) { return $days; } $this->injectInfo4Week($date, $week); $list[] = $week; return $days; } /** * Inject Information for Each Day * * @param DateTime * @param array week data */ private function injectInfo4Day($date, &$week) { $dow = $date->format('N'); $week['day' . $dow] = $date->format('j'); $week['day' . $dow . '_class'] = array(); $week['day' . $dow . '_title'] = array(); $week['day' . $dow . '_iso'] = $date->format('Y-m-d'); $week['day' . $dow . '_unix'] = $date->format('U'); $week['day' . $dow . '_weekday_no'] = $date->format('N'); $week['day' . $dow . '_weekday'] = $date->format('l'); $week['day' . $dow . '_weekday_short'] = $date->format('D'); // use modules foreach ($this->decorator as $dec) { /** @var WBDatasource_Calendar_Decorator */ $dec->decorateDay($date, $week, $dow); } $week['day' . $dow . '_class'] = implode(' ', $week['day' . $dow . '_class']); $week['day' . $dow . '_title'] = implode(' ', $week['day' . $dow . '_title']); return; } /** * Inject Information for Each Week * * @param DateTime * @param array week data */ private function injectInfo4Week($date, &$week) { foreach ($this->decorator as $dec) { /** @var WBDatasource_Calendar_Decorator */ $dec->decorateWeek($date, $week); } // use modules } }