_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 (is_numeric($params[0])) { $id = intval($params[0]); if (($alias = $this->_getDataObj()->getAliasById($id))) { \Qs_Http::redirect(BASE_URL_LANGUAGE . '/' . CURRENT_PAGE_FINAL . '/' . $alias, 301); } else { $this->_doc->display404(); } } 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(); } } elseif ('y' == $this->_doc->getOption('isRoot')) { $blocks = $this->getConfigArray('blocks'); if (is_array($blocks) && 1 == count($blocks) && in_array('featured', $blocks) ) { $this->_defaultAction = 'featured'; } } $this->_isParamsParsed = true; return $this; } protected function _getListOptions() { $options = parent::_getListOptions(); $options['viewAlias'] = BASE_URL_LANGUAGE . '/' . $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 && Qs_ViewController_Log::getEnabled()) { $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'] = BASE_URL_LANGUAGE . '/' . $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 _doFeatured() { $this->_getDataObj()->setFilter(array('featured' => 'y')); $ipp = $this->getConfig('featuredSmallBlockIpp'); $this->_getList()->setIpp($ipp); $item = $this->_getListItem(); if ($item['list']) { $isEnabledPage = \Qs_SiteMap::findFirst(null, ['type' => 'Blog\\'], null, 'enabled'); if ($isEnabledPage == 'y') { if (!empty($item['list'])) { $item['blogUrl'] = \Qs_SiteMap::findFirst(['xMainBlog' => 'y'], ['type' => 'Blog\\'], null, 'url'); $item['tpl'] = $this->getTemplate('featured-list-small.tpl'); $item['config'] = $this->getConfigArray(); $this->_addItem($item); return $this; } } } 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(); } $this->_filterHeader = $this->translate('Tags: ') . implode(', ', $tags); $this->_addTitleItem($this->_filterHeader); \Qs_Navigation::append(array( 'title' => $this->_filterHeader, 'url' => BASE_URL_LANGUAGE . '/' . $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: $this->_filterHeader = $this->translate('Date: ') . $date; break; case 2: $this->_filterHeader = $this->translate('Date: ') . Qs_Translate::translate(date('F', strtotime($date . '-01')), 'common') . ' ' . date('Y', strtotime($date . '-01')); break; default: $this->_filterHeader = $this->translate('Date: ') . Qs_Translate::translate(date('F', strtotime($date)), 'common') . ' ' . date('d, Y', strtotime($date)); } $this->_addTitleItem($this->_filterHeader); \Qs_Navigation::append(array( 'title' => $this->_filterHeader, 'url' => BASE_URL_LANGUAGE . '/' . $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 || 'y' != $category['enabled'] || count($params) > 2) { $this->_doc->display404(); return $this; } $this->_filterHeader = $this->translate('Category: ') . $category['title']; $this->_addTitleItem($this->_filterHeader); $this->_initPageSeo($category); \Qs_Navigation::append(array( 'title' => $this->_filterHeader, 'url' => BASE_URL_LANGUAGE . '/' . $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() { $authorTypes = $this->_getDataObj()->getAuthorTypes(); $params = $this->getRestParams(); $paramsCount = count($params); if (3 == $paramsCount && is_numeric($params[2]) && in_array($params[1], $authorTypes)) { $type = $params[1]; // admin $id = $params[2]; } elseif (2 == $paramsCount && is_numeric($params[1])) { $type = 'user'; $id = $params[1]; } else { $this->_doc->display404(); return $this; } if (null == ($author = $this->_getDataObj()->getAuthorName($id, $type))) { $this->_doc->display404(); return $this; } $this->_filterHeader = 'Author: ' . $author; $this->_addTitleItem($this->_filterHeader); \Qs_Navigation::append(array( 'title' => $this->_filterHeader, 'url' => BASE_URL_LANGUAGE . '/' . $this->_getViewAlias() . '/author/' . (($type == 'admin') ? 'admin' : '') . $id )); $this->_getDataObj()->addFilter(array('authorType' => $type)); $this->_getDataObj()->addFilter(array('authorId' => $id)); $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->addLink( 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($this->translate('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' . ucfirst($name) . 'Block'; if (method_exists($this, $method)) { $this->{$method}(); } } $this->_doc->setGroupName($oldGroupName); return $this; } public function addCategoryBlock() { $item = array(); $item['list'] = $this->_getDataObj()->getAllCategories(); if (!empty($item['list'])) { $item['viewAlias'] = BASE_URL_LANGUAGE . '/' . $this->_getViewAlias() . '/category'; $item['title'] = 'Categories'; $item['currentId'] = $this->_getDataObj()->getCategoryId(); $item['tpl'] = $this->getTemplate('categories-block.tpl'); $this->_addItem($item, $this->_doc->getGroupName()); } return $this; } public function addTagBlock() { $item = array(); $item['list'] = $this->_getDataObj()->getAllTags(); if (!empty($item['list'])) { $item['viewAlias'] = BASE_URL_LANGUAGE . '/' . $this->_getViewAlias() . '/tag'; $item['title'] = 'Tags'; $item['currentTags'] = $this->_getDataObj()->getTags(); $item['tpl'] = $this->getTemplate('tags-block.tpl'); $this->_addItem($item, $this->_doc->getGroupName()); } 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'] = BASE_URL_LANGUAGE . '/' . $this->_getViewAlias(); $item['tpl'] = $this->getTemplate('month.tpl'); $this->_addItem($item, $this->_doc->getGroupName()); } return $this; } public function addCalendarBlock() { $range = $this->_getDataObj()->getPostsDatesRange(); if (!empty($range)) { $item = array( 'viewAlias' => BASE_URL_LANGUAGE . '/' . $this->_getViewAlias(), 'tpl' => $this->getTemplate('calendar.tpl'), ); $this->_addItem($item, $this->_doc->getGroupName()); $params = array( 'dates' => $this->_getDataObj()->getMonthPosts(), 'viewAlias' => BASE_URL_LANGUAGE . '/' . $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') ->addCriticalStylesheet('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(); $isEnabledPage = \Qs_SiteMap::findFirst(null, ['type' => 'Blog\\'], null,'enabled'); if ($isEnabledPage == 'y') { if (!empty($item['list'])) { $item['viewAlias'] = BASE_URL_LANGUAGE . '/' . $this->_getViewAlias(); $item['tpl'] = $this->getTemplate('featured-list.tpl'); return $item; } } return null; } }