_parseParams(); if ($this->_defaultAction == 'view' || in_array($this->_defaultAction, $this->_specialParams)) { foreach ($items as $i => $item) { if ($item['type'] == 'HtmlBlock_') { unset($items[$i]); break; } } } return $this; } public function exec() { $this->_parseParams(); parent::exec(); $this->_addSubBlocks(); return $this; } protected function _parseParams() { if ($this->_isParamsParsed) { return $this; } $params = $this->getRestParams(); if (count($params) >= 1) { if (($param = strtolower($params[0])) && in_array($param, $this->_specialParams)) { $this->_defaultAction = $param; } else if (($id = $this->_getDataObj()->getIdByAlias($params[0]))) { if (1 == count($params)) { $this->_getDataObj()->setPrimaryKey($id); $this->_defaultAction = 'view'; } else { $this->_doc->display404(); } } else { $this->_doc->display404(); } } $this->_isParamsParsed = true; return $this; } protected function _getListOptions() { $options = parent::_getListOptions(); $options['viewAlias'] = $this->_getViewAlias(); return $options; } protected function _getLog() { if (null === $this->_log) { parent::_getLog(); $message = 'Viewed "%filterTitle%" %itemsName% list'; $this->_log->setAction('category', $message); $this->_log->setAction('date', $message); $this->_log->setAction('tag', $message); $this->_log->setAction('author', $message); } return $this->_log; } protected function _log() { if ($this->_hasLog) { $this->_getLog()->write($this->_action, $this->_getObjectInfo()); } return $this; } protected function _getObjectInfo() { $info = $this->_getDataObj()->getObjectInfo(); if (in_array($this->_action, $this->_specialParams)) { $info['filterTitle'] = $this->_filterHeader; $info['itemsName'] = $this->getConfig('itemsName'); } return $info; } protected function _doView() { $this->_addFeedLink(); $item = $this->_getDataObj()->getData(); if (empty($item) || 'y' != $item['enabled'] || 'y' != $item['categoryEnabled']) { $this->_doc->display404(); } $item['backUrl'] = $this->_getBackUrl(CURRENT_PAGE_FINAL, CURRENT_PAGE_FINAL); $item['viewAlias'] = $this->_getViewAlias(); if (!empty($item['password']) && !$this->_checkUserAuth($item['id'])) { if ($this->_getCommentsItemId() == Qs_Request::getRequestValue('__idItem') && Qs_Request::getRequestValue('action') ) { $data = array( 'isValid' => false, 'formErrors' => array('Your session has expired. Please refresh this page to try again.') ); $this->_displayJson($data); } return $this->_showLoginForm(); } $this->_initPageSeo($item); $item['config'] = $this->getConfig()->toArray(); $item['tpl'] = $this->getTemplate('view.tpl'); $this->_addItem($item); if ($item['allowComment'] == 'y') { $this->_addComments($item['id']); } $alias = (empty($item['alias'])) ? $item['id'] : $item['alias']; Qs_Navigation::append(array('title' => $item['title'], 'url' => $item['backUrl'] . '/' . $alias)); $this->_postView(); return $this; } protected function _getCommentsItemId() { $itemId = $this->getIdItem(); $itemId = -$itemId; return $itemId; } protected function _addComments($idPost) { $commentView = new Comment\View(['groupName' => $this->getOption('groupName')]); $commentView->setIdItem($this->_getCommentsItemId()); $commentView->setDoc($this->_doc); $commentView->setIdPost($idPost); $commentView->exec(); return $this; } protected function _doFeed() { $feed = Zend_Feed::importArray($this->_getDataObj()->getFeedData(), 'rss'); $feed->send(); exit(); } protected function _doTag() { $tags = array_slice($this->getRestParams(), 1); if (empty($tags)) { $this->_doc->display404(); } $tagsTitle = implode(', ', $tags); $this->_filterHeader = Qs_Translate::getPlural('Tag', count($tags)) . ': ' . $tagsTitle; $this->_addTitleItem($this->_filterHeader); $this->_doc->appendTitle($tagsTitle); Qs_Navigation::append(array( 'title' => $this->_filterHeader, 'url' => $this->_getViewAlias() . '/tag/' . implode('/', $tags)) ); $this->_getDataObj()->setTags($tags); $this->_doList(); return $this; } protected function _doDate() { $params = $this->getRestParams(); array_shift($params); if (false == ($date = $this->_parseDateParams($params))) { $this->_doc->display404(); } switch (count($params)) { case 1: $dateTitle = $date; break; case 2: $dateTitle = date('F Y', strtotime($date . '-01')); break; default: $dateTitle = date('F d, Y', strtotime($date)); } $this->_filterHeader = 'Date: ' . $dateTitle; $this->_addTitleItem($this->_filterHeader); $this->_doc->appendTitle($dateTitle); Qs_Navigation::append(array( 'title' => $this->_filterHeader, 'url' => $this->_getViewAlias() . '/date/' . str_replace('-', '/', $date) )); $this->_getDataObj()->setDate($date); $this->_doList(); return $this; } protected function _parseDateParams($params) { $date = array(); $date[0] = (isset($params[0]) && 1900 <= $params[0] && $params[0] <= 2500) ? sprintf("%04d", $params[0]) : null; $date[1] = (isset($params[1]) && 01 <= $params[1] && $params[1] <= 12) ? sprintf("%02d", $params[1]) : null; $date[2] = (isset($params[2]) && 01 <= $params[2] && $params[2] <= 31) ? sprintf("%02d", $params[2]) : null; $dateStr = trim(implode('-', $date), '-'); $paramsStr = implode('-', $params); if (empty($date[0]) || (empty($params[1]) && !empty($params[2])) || (!empty($params[1]) && $date[1] != $params[1]) || (!empty($params[2]) && $date[2] != $params[2]) || ($dateStr != $paramsStr) ) { return false; } return $dateStr; } protected function _doCategory() { $params = $this->getRestParams(); $alias = trim(Qs_Array::get($params, 1)); $this->_getDataObj()->setCategoryAlias($alias); $category = $this->_getDataObj()->getCategory(); if (!$category || count($params) > 2) { $this->_doc->display404(); return $this; } $this->_filterHeader = 'Category: ' . $category['title']; $this->_addTitleItem($this->_filterHeader); $this->_initPageSeo($category); Qs_Navigation::append(array( 'title' => $this->_filterHeader, 'url' => $this->_getViewAlias() . '/category/' . $alias )); $this->_doList(); return $this; } protected function _doCalendar() { return $this->addCalendarBlock(); } protected function _doCalendarAjax() { $year = Qs_Request::getRequestValue('year', null); $month = Qs_Request::getRequestValue('month', null); $response = array( 'status' => 'success', 'dates' => $this->_getDataObj()->getMonthPosts($year, $month) ); $this->_displayJson($response); } protected function _doAuthor() { $params = $this->getRestParams(); $paramsCount = count($params); if (2 !== $paramsCount) { $this->_doc->display404(); return $this; } $author = $params[1]; $this->_filterHeader = 'Author: ' . $author; $this->_addTitleItem($this->_filterHeader); Qs_Navigation::append(array( 'title' => $this->_filterHeader, 'url' => $this->_getViewAlias() . '/author/' . $author )); $this->_getDataObj()->addFilter(array('author' => $author)); $this->_doList(); return $this; } public function writeXmlSitemap(\App_Sitemap_Xml_Writer $xml) { if ('default' != $this->getConfig('type', 'default')) { return $this; } $pageUrl = ('y' == $this->_doc->getOption('isSecure')) ? BASE_URL_HTTPS : BASE_URL_HTTP; $pageUrl .= '/' . $this->_doc->getOption('fullAlias'); $stmt = $this->_getDataObj()->getListStatement4XmlSitemap(); while ($item = $stmt->fetch()) { $xml->writeUrl($pageUrl . '/' . $item['alias']); } return $this; } protected function _addFeedLink() { $this->_doc->addStyleSheet ( BASE_URL_LANGUAGE . '/' . $this->_getViewAlias() . '/feed', array ( 'type' => 'application/rss+xml', 'rel' => 'alternate', 'title' => SITE_NAME ) ); return $this; } protected function _getLoginForm() { $cancelUrl = $this->_getBackUrl(CURRENT_PAGE_FINAL, BASE_URL_LANGUAGE . '/' . CURRENT_PAGE_FINAL); $options = array('cancelUrl' => $cancelUrl, 'id' => 'blog-form-login'); return $this->_getFormInstance('login', $options); } protected function _showLoginForm($form = null) { if (null === $form) { $form = $this->_getLoginForm(); $form->setDefaults(); } $postTitle = $item = $this->_getDataObj()->getData('title'); Qs_Navigation::append(array('title' => $postTitle)); $this->_addTitleItem($postTitle); $this->_addFormItem($form); return $this; } protected function _addTitleItem($title) { $item = array( 'tpl' => $this->getTemplate('title-item.tpl'), 'title' => $title, 'backUrl' => $this->_getBackUrl(CURRENT_PAGE_FINAL, CURRENT_PAGE_FINAL) ); $this->_addItem($item); return $this; } protected function _doLogin() { $form = $this->_getLoginForm(); if ($form->validate()) { $values = $form->getValues(); $data = $this->_getDataObj()->getData(); if ($values['password'] == $data['password']) { $this->_setUserAuth4Post($data['id']); Qs_Http::redirect($this->url()); } else { $form->getElement('password')->addError('Invalid password'); } } $this->_showLoginForm($form); return $this; } protected function _setUserAuth4Post($idPost) { $name = 'post_' . $idPost; $session = $this->_getSession(); $session->{$name} = true; $session->setExpirationSeconds(3600, $name); return $this; } protected function _checkUserAuth($idPost) { $name = 'post_' . $idPost; $session = $this->_getSession(); return (isset($session->{$name}) && true === $session->{$name}); } protected function _addSubBlocks() { $oldGroupName = $this->_doc->getGroupName(); $this->_doc->setGroupName('RIGHT_ITEMS'); $blocks = $this->getConfig('blocks', array()); foreach ($blocks as $name) { $method = 'add' . $name . 'Block'; $this->{$method}(); } $this->_doc->setGroupName($oldGroupName); return $this; } public function addCategoryBlock() { $item = array(); $item['list'] = $this->_getDataObj()->getAllCategories(); if (!empty($item['list'])) { $item['viewAlias'] = $this->_getViewAlias() . '/category'; $item['title'] = 'Categories'; $item['currentId'] = $this->_getDataObj()->getCategoryId(); $item['tpl'] = $this->getTemplate('categories-block.tpl'); $this->_addItem($item); } return $this; } public function addTagBlock() { $item = array(); $item['list'] = $this->_getDataObj()->getAllTags(); if (!empty($item['list'])) { $item['viewAlias'] = $this->_getViewAlias() . '/tag'; $item['title'] = 'Tags'; $item['currentTags'] = $this->_getDataObj()->getTags(); $item['tpl'] = $this->getTemplate('tags-block.tpl'); $this->_addItem($item); } return $this; } public function addMonthBlock() { $item = array(); $item['list'] = $this->_getDataObj()->buildMonthTree(); if (!empty($item['list'])) { $item['currentYear'] = ''; $item['currentFull'] = ''; if ($this->_action == 'date') { $params = $this->getRestParams(); array_shift($params); switch (count($params)) { case 1: $item['currentYear'] = $params[0]; break; case 2: $item['currentYear'] = $params[0]; $item['currentFull'] = $params[0] . '-' . $params[1]; break; } } $item['viewAlias'] = $this->_getViewAlias(); $item['tpl'] = $this->getTemplate('month.tpl'); $this->_addItem($item); } return $this; } public function addCalendarBlock() { $range = $this->_getDataObj()->getPostsDatesRange(); if (!empty($range)) { $item = array( 'viewAlias' => $this->_getViewAlias(), 'tpl' => $this->getTemplate('calendar.tpl'), ); $this->_addItem($item); $params = array( 'dates' => $this->_getDataObj()->getMonthPosts(), 'viewAlias' => $this->_getViewAlias() . '/date' ); $params['minDate'] = strtotime($range['min']) * 1000; // timestamp in milliseconds $params['maxDate'] = time() * 1000; // timestamp in milliseconds $date = $this->_getDataObj()->getDate(); if ($date) { $timestamp = strtotime($date); $params['defaultDate'] = date('Y/m/d', $timestamp); } $this->_doc->addScript('js/jquery-ui.js') ->addScript('js/app/blog.js') ->addStylesheet('css/thirdpart/jquery-ui.css') ->addInitFunction('Blog.initCalendar', array($params)); } return $this; } public function getSideBlockItem(array $blockData = array()) { $this->_getDataObj()->setFilter(array('featured' => 'y')); $ipp = $this->getConfig('featuredBlockIpp'); $ipp = ($ipp) ? $ipp : 3; $this->_getList()->setIsDisabledLimit()->setIpp($ipp); $item = $this->_getListItem(); if (!empty($item['list'])) { $item['viewAlias'] = $this->_getViewAlias(); $item['tpl'] = $this->getTemplate('featured-list.tpl'); return $item; } return null; } }