*/ /** * patTemplate-function Calendar * * @version 0.3.3 * @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 * * Additional parameters for each decorator: usedecoratorDECORATOR = yes | no * * @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, 'itemdetail' => 0, 'withpast' => 'no', ), $params); switch ($params['withpast']) { case 'yes': case '1': $params['withpast'] = 1; break; default: case 'no': case '0': $params['withpast'] = 0; // implicit withpast if (0 < $params['monthpre']) { $params['withpast'] = 1; } break; } // 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'); if (patErrorManager::isError($this->loadTemplate($params['tmpl']))) { return ''; } $this->tmpl->addGlobalVar('showitemdetail', $params['itemdetail']); $this->tmpl->addGlobalVars($params, 'ATT_'); 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']) { $params['monthpre'] = min(7, $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']) { $params['monthpost'] = min(12, $params['monthpost']); for ($i = 1; $i <= $params['monthpost']; ++$i) { $this->cal->add('P' . $i . 'M'); $this->cal->getDays4Month($list); } } foreach ($list as &$l) { $this->dayItems2HTML($l); } // display day by day if ($this->tmpl->exists('day')) { return $this->displayDay($list); } // display week-wise if ($this->tmpl->exists('week')) { return $this->displayWeek($list); } return $this->displayMonth($list); } /** * Load Template * * Try to load exact template, if not fount, load more generic template. * * @return bool */ private function loadTemplate($tmpl) { // patErrorManager::pushExpect(PATTEMPLATE_READER_ERROR_NO_INPUT); patErrorManager::pushExpect(6000); $res = $this->tmpl->readTemplatesFromInput('Calendar/Function/' . $tmpl . '.tmpl'); if (patErrorManager::isError($res)) { if (false === strstr($tmpl, '-')) { return $res; } $tmpl = explode('-', $tmpl); array_pop($tmpl); $tmpl = implode('-', $tmpl); $res = $this->tmpl->readTemplatesFromInput('Calendar/Function/' . $tmpl . '.tmpl'); } else { patErrorManager::popExpect(); } return $res; } /** * Convert Item List to HTML * * @param array */ private function dayItems2HTML(&$week) { if (!$this->tmpl->exists('dayitem_entry')) { for ($dow = 1; $dow <= 7; ++$dow) { if (empty($week['day'. $dow. '_item'])) { continue; } $week['day'. $dow. '_item_count'] = 0; $week['day'. $dow. '_item_html'] = ''; } return; } for ($dow = 1; $dow <= 7; ++$dow) { if (empty($week['day'. $dow. '_item'])) { continue; } $this->tmpl->clearTemplate('dayitem'); $this->tmpl->clearTemplate('dayitem_entry'); $week['day'. $dow. '_item_count'] = count($week['day'. $dow. '_item']); $this->tmpl->addGlobalVar('dayitem_count', $week['day'. $dow. '_item_count']); $this->tmpl->addRows('dayitem_entry', $week['day'. $dow. '_item']); $week['day'. $dow. '_item_html'] = $this->tmpl->getParsedTemplate('dayitem'); } } /** * Start Decorator * * @param array * @param string */ 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; } // switch off by paramater $p = 'usedecorator' . strtolower($c); if (isset($params[$p]) && 'no' == strtolower($params[$p])) { continue; } /** @var WBDatasource_Calendar_Decorator */ $dec = WBClass::create('WBDatasource_Calendar_Decorator_' . $c); $dec->configure($params); $this->cal->attach($dec); } } /** * Display Days In Flat List * * @param array list of weeks */ private function displayDay($list) { $day = array(); foreach ($list as $l) { for ($dow = 1; $dow <= 7; ++$dow) { if (empty($l['day' . $dow])) { continue; } if (empty($l['day' . $dow . '_item'])) { continue; } $tmp = array( 'year' => $l['year'], 'month' => $l['month'], 'day' => $l['day' . $dow], 'month_name' => $l['month_name'], 'month_short' => $l['month_short'], 'week_no' => $l['week_no'], 'class' => $l['day' . $dow . '_class'], 'iso' => $l['day' . $dow . '_iso'], 'unix' => $l['day' . $dow . '_unix'], 'weekday_no' => $l['day' . $dow . '_weekday_no'], 'weekday' => $l['day' . $dow . '_weekday'], 'weekday_short' => $l['day' . $dow . '_weekday_short'], 'item' => $l['day' . $dow . '_item'], 'item_count' => $l['day' . $dow . '_item_count'], 'item_html' => $l['day' . $dow . '_item_html'] ); $day[] = $tmp; } } $this->tmpl->addRows('day', $day); $this->tmpl->addGlobalVar('day_count', count($day)); return $this->tmpl->getParsedTemplate('snippet'); } /** * 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(); } }