*/ // WBClass::load('WBConfig'); /** * patTemplate-function Calendar * * @version 0.2.0 * @package Wombat * @subpackage patTemplate */ class patTemplate_Function_Calendar extends patTemplate_Function { /** * Name of the function * @access protected * @var string */ public $_name = 'calendar'; /** * tell that this function must not be executed during runtime * @var string */ public $_type = PATTEMPLATE_FUNCTION_RUNTIME; /** * @var patTemplate */ private $tmpl; /** * @var WBDatasource_Calendar */ private $cal; /** * Call the function * * @param array $params parameters of the function (= attributes of the tag) * @param string $content inside tag * @return string content to insert into the template */ public function call($params, $content) { $params = array_merge(array( 'tmpl' => 'month', 'date' => 'now', 'monthpre' => 0, 'monthpost' => 0 ), $params); // make sure we are on start of month, to get complete month $date = strtotime($params['date']); $params['date'] = strftime('%Y-%m-01', $date); $this->tmpl = WBClass::create('patTemplate'); $this->tmpl->readTemplatesFromInput('Calendar/' . $params['tmpl'] . '.tmpl'); WBClass::load('WBDatasource_Calendar'); $this->cal = WBClass::create('WBDatasource_Calendar', $params); $this->startDecorator($params, $content); $list = array(); // month' before current one if (0 < $params['monthpre']) { for ($i = $params['monthpre']; $i > 0; --$i) { $this->cal->sub('P' . $i . 'M'); $this->cal->getDays4Month($list); } $this->cal->sub('P0M'); } // current month $this->cal->getDays4Month($list); // month' after current one if (0 < $params['monthpost']) { for ($i = 1; $i <= $params['monthpost']; ++$i) { $this->cal->add('P' . $i . 'M'); $this->cal->getDays4Month($list); } } // display week-wise if ($this->tmpl->exists('week')) { return $this->displayWeek($list); } return $this->displayMonth($list); } private function startDecorator($params, $content) { $content = trim($content); if (empty($content)) { return; } $content = explode("\n", $content); foreach ($content as $c) { $c = trim($c); if (empty($c)) { continue; } /** @var WBDatasource_Calendar_Decorator */ $dec = WBClass::create('WBDatasource_Calendar_Decorator_' . $c); $dec->configure($params); $this->cal->attach($dec); } } /** * Display Days In Week List * * @param array list of weeks */ private function displayWeek($list) { $this->tmpl->addRows('week', $list); return $this->tmpl->getParsedTemplate('snippet'); } /** * Display Days In Month List * * @param array list of weeks */ private function displayMonth($list) { $days = array(); $day = 0; $dc = 0; $wc = 0; $m = 0; foreach ($list as $l) { if ($m != $l['month']) { // display 37 columns for each month if (0 < $dc) { for ($i = $dc; $i < 37; ++$i) { $days[] = array( 'month' => $m ); } } $m = $l['month']; $wc = 0; $dc = 0; } ++$wc; for ($i = 1; $i <= 7; ++$i) { // 6th week ends on Tuesday for 31st of month (in case 1st is as Sunday) if (5 < $wc && 2 < $i) { continue; } ++$dc; $days[] = array( 'month_name' => $l['month_name'], 'month_short' => $l['month_short'], 'month' => $l['month'], 'year' => $l['year'], 'week_no' => $l['week_no'], 'day' => $l['day' . $i], 'day_iso' => $l['day' . $i . '_iso'], 'day_unix' => $l['day' . $i . '_unix'], 'weekday' => $l['day' . $i . '_weekday'], 'weekday_no' => $l['day' . $i . '_weekday_no'], 'weekday_short' => $l['day' . $i . '_weekday_short'], ); } } $this->tmpl->addRows('day', $days); return $this->tmpl->getParsedTemplate(); } }