load('feedData'))) { $feed = []; $feed['title'] = SITE_NAME; $feed['link'] = BASE_URL_LANGUAGE . '/' . CURRENT_PAGE; $feed['published'] = gmdate('D, d M Y H:i:s'); $feed['charset'] = 'utf-8'; $feed['language'] = 'en-us'; $maxItems = (int) $this->maxFeedItemCount; $list = $this->getList(['order' => 'date DESC', 'limit' => $maxItems]); foreach ($list as $item) { $feedItem = [ 'title' => $item['title'], 'link' => BASE_URL_LANGUAGE . '/' . CURRENT_PAGE_FINAL . '/' . ((empty($item['alias'])) ? $item['id'] : $item['alias']), 'lastUpdate' => strtotime($item['date']), ]; if (empty($item['excerpt'])) { $feedItem['description'] = (empty($item['password'])) ? $item['content'] : '[Content protected with password]'; } else { $feedItem['description'] = htmlspecialchars($item['excerpt']) . ' [Read more]'; } $feed['entries'][] = $feedItem; } $cache->save($feed, 'feedData', ['blog', 'feedData']); } return $feed; } public function getAllTags() { $cache = self::_getCacheObject(); if (!($tags = $cache->load('tags'))) { $countPosts = $this->_db->select(); $countPosts->from($this->_getPair('Post2Tag', 'p2t'), ['count' => 'COUNT(`p2t`.`id`)']); $countPosts->join($this->_getPair(null, 'p'), '`p`.`id` = `p2t`.`idPost` AND `p`.`enabled` = "y"', []); $countPosts->join( $this->_getPair('PostCategory', 'pc'), '`pc`.`id` = `p`.`categoryId` AND `pc`.`enabled` = "y"', [] ); $countPosts->where("t.id = p2t.idTag"); $select = $this->_db->select(); $select->from( $this->_getPair('PostTag', 't'), ['title' => 'value', 'count' => new \Zend_Db_Expr('(' . $countPosts . ')')] ); $select->having('`count` > 0'); $tags = $this->_db->fetchAll($select); $cache->save($tags, 'tags', ['blog', 'tags']); } return $tags; } public function getAllCategories() { $cache = self::_getCacheObject(); if (!($categories = $cache->load('categories'))) { $countPosts = $this->_db->select(); $countPosts->from($this->_getPair(), 'COUNT(id)'); $countPosts->where("`{$this->_tableAlias}`.`enabled` = 'y'"); $countPosts->where("`{$this->_tableAlias}`.`categoryId` = `c`.`id`"); $countPosts->where("`{$this->_tableAlias}`.`date` <= CURDATE()"); $select = $this->_db->select(); $select->from( ['c' => $this->_getTableName('PostCategory')], ['id', 'title', 'alias', 'count' => new \Zend_Db_Expr('(' . $countPosts . ')')] ); $select->where('`enabled` = "y"'); $select->having('`count` > 0'); $select->order('sorter'); $categories = $this->_db->fetchAll($select); $cache->save($categories, 'categories', ['blog', 'categories']); } return $categories; } public function getAllMonths() { $countPosts = $this->_db->select(); $countPosts->from(['p' => $this->_getTableName()], ['COUNT(id)']); $countPosts->where("`p`.`enabled` = 'y'"); $countPosts->where("`p`.`date` LIKE CONCAT(DATE_FORMAT(`Post`.`date`, '%Y-%m'), '%')"); $countPosts->where('`p`.`date` <= CURDATE()'); $select = $this->_db->select(); $columns = [ 'date', 'full' => "DATE_FORMAT(`date`, '%Y-%m')", 'year' => "YEAR(`date`)", 'month' => "MONTH(`date`)", 'count' => new \Zend_Db_Expr('(' . $countPosts . ')'), ]; $select->from($this->_getPair(), $columns); $select->join( $this->_getPair('PostCategory'), '`PostCategory`.`id` = `' . $this->_tableAlias . '`.`categoryId`', [] ); $select->where('`' . $this->_tableAlias . '`.`enabled` = "y"'); $select->where('`PostCategory`.`enabled` = "y"'); $select->where('`date` <= CURDATE()'); $select->group('full'); $select->order('date DESC'); $select->having('`count` > 0'); // $select->limit(50); return $this->_db->fetchAll($select); } public function buildMonthTree() { $cache = self::_getCacheObject(); if (!($tree = $cache->load('monthTree'))) { $data = $this->getAllMonths(); $tree = ['months' => [], 'counts' => []]; foreach ($data as $r) { $y = $r['year']; if (empty($tree['months'][$y])) { $tree['months'][$y] = []; $tree['counts'][$y] = 0; } $tree['months'][$y][] = $r; $tree['counts'][$y] += $r['count']; } $cache->save($tree, 'monthTree', ['blog', 'monthTree']); } if (empty($tree['months'])) { $tree = []; } return $tree; } public function getMonthPosts($year = null, $month = null) { if (null === $year) { if (!empty($this->_date)) { $dateParts = explode('-', $this->_date); $year = array_shift($dateParts); } else { $year = date('Y'); } } if (null === $month) { if (!empty($this->_date)) { $dateParts = explode('-', $this->_date); array_shift($dateParts); if (!empty($dateParts)) { $month = array_shift($dateParts); } else { $month = date('m'); } } else { $month = date('m'); } } $fmtDate = sprintf('%s-%02s', $year, $month); $select = $this->_db->select(); $select->from( $this->_getPair(), ['date' => "DATE_FORMAT(`" . $this->_tableAlias . "`.`date`, '%Y-%m-%d')", 'id'] ); $select->join( $this->_getPair('PostCategory'), '`PostCategory`.`id` = `' . $this->_tableAlias . '`.`categoryId`', [] ); $select->where('`' . $this->_tableAlias . '`.`enabled` = "y"'); $select->where('`PostCategory`.`enabled` = "y"'); $select->where('`' . $this->_tableAlias . '`.`date` LIKE ?', $fmtDate . '%'); $select->where('`' . $this->_tableAlias . '`.`date` <= CURDATE()'); $select->group($this->_tableAlias . '.date'); return $this->_db->fetchPairs($select); } public function getPostsDatesRange() { $select = $this->_db->select(); $select->from($this->_getPair(), ['min' => "MIN(`date`)", 'max' => "MAX(`date`)"]); $select->where('`date` <= CURDATE()'); $range = $this->_db->fetchRow($select); if (!$range['min']) { $range = []; } return $range; } public function setCategoryAlias($alias) { $this->_categoryAlias = $alias; return $this; } public function setDate($date) { $this->_date = $date; return $this; } public function getDate() { return $this->_date; } public function setTags($tags) { $this->_tags = (array) $tags; return $this; } public function getTags() { return $this->_tags; } public function getCategoryId() { return $this->_category ? (int) $this->getCategory('id') : null; } public function getCategoryTitle() { return $this->_category ? (int) $this->getCategory('title') : null; } public function getCategory($field = null) { if (null === $this->_category && $this->_categoryAlias) { $select = $this->_db->select(); $select->from($this->_getPair('PostCategory'), ['*']); $select->where('`alias` = ?', $this->_categoryAlias); $select->limit(1); $this->_category = $this->_db->fetchRow($select); } if ($this->_category) { if (null === $field) { return $this->_category; } return (array_key_exists($field, $this->_category)) ? $this->_category[$field] : null; } return null; } public function getListSelect() { if (null === $this->_select) { parent::getListSelect(); $this->_select->where('`' . $this->_tableAlias . '`.`enabled` = "y"'); $this->_select->where('`PostCategory`.`enabled` = "y"'); if ($this->_tags) { $this->_select->join( $this->_getPair('Post2Tag'), "`Post2Tag`.`idPost` = `{$this->_tableAlias}`.`id`", '' ); $this->_select->join($this->_getPair('PostTag'), '`PostTag`.`id` = `Post2Tag`.`idTag`', ''); } $this->_select->where("`{$this->_tableAlias}`.`date` <= ?", date('Y-m-d')); if ($this->_category) { $this->_select->where('`categoryId` = ?', $this->getCategoryId(), \Zend_Db::INT_TYPE); } if ($this->_date) { $this->_select->where('`date` LIKE ?', $this->_date . '%'); } if ($this->_tags) { $this->_select->where('`PostTag`.`value` IN (?)', $this->_tags); $this->_select->group("{$this->_tableAlias}.id"); } } return $this->_select; } protected function _getFromColumns() { $columns = parent::_getFromColumns(); $commentsCount = $this->_db->select(); $commentsCount->from($this->_getPair($this->_tableAlias . 'Comment'), ['COUNT(id)']); $commentsCount->where('`' . $this->_tableAlias . 'Comment`.`idPost` = `' . $this->_tableAlias . '`.`id`'); $commentsCount->where('`' . $this->_tableAlias . 'Comment`.`status` = "approved"'); $columns['countComments'] = new \Zend_Db_Expr('(' . $commentsCount . ')'); return $columns; } public function initFromForm(array $data) { if (!empty($this->_userInfo)) { $data = array_merge((array) $this->_userInfo, $data); } $data['status'] = 'pending'; return parent::initFromForm($data); } public function insert(array $data = null) { if (null === $data) { $data = $this->_data; } else { $this->_data = $data; } unset($data['id']); $this->_db->beginTransaction(); $this->_clearErrors(); try { $this->_primaryKey = $this->_getTable($this->_tableAlias . 'Comment')->insert($data); $this->_insertDependency(); $this->_db->commit(); } catch (\Exception $e) { $this->_deleteFiles($data); \Qs_Debug::log($e->getMessage(), 3); $this->_addError($e->getMessage()); $this->_db->rollBack(); return false; } $this->_handleFiles(); return $this->_primaryKey; } protected function _addDependenciesFromDb(array &$data) { $category = $this->_getPostCategory($data['categoryId']); $data['categoryTitle'] = $category['title']; $data['categoryAlias'] = $category['alias']; $data['categoryEnabled'] = $category['enabled']; $data['tags'] = $this->_getPostTags(); return $this; } /** * @param null|int $categoryId * @return array */ protected function _getPostCategory($categoryId = null) { $select = $this->_db->select(); $select->from($this->_getPair($this->_tableAlias . 'Category')); if (null !== $categoryId) { $select->where('`' . $this->_tableAlias . 'Category`.`id` = ?', $categoryId, \Zend_Db::INT_TYPE); } else { $select->join( $this->_getPair(), '`' . $this->_tableAlias . 'Category`.`id` = `' . $this->_tableAlias . '`.`categoryId`', '' ); $select->where('`' . $this->_tableAlias . '`.`id` = ?', $this->getPrimaryKey(), \Zend_Db::INT_TYPE); } return $this->_db->fetchRow($select); } protected function _getPostTags($postId = null) { if (null === $postId) { $postId = $this->getPrimaryKey(); } $select = $this->_db->select(); $select->from($this->_getPair($this->_tableAlias . '2Tag'), null); $select->where('`idPost` = ?', $postId, \Zend_Db::INT_TYPE); $select->joinLeft( $this->_getPair("PostTag"), '`' . $this->_tableAlias . '2Tag`.`idTag` = `PostTag`.`id`', ['id', 'value'] ); return $this->_db->fetchPairs($select); } protected function _getPostsTags(array $postsIds) { if (!is_array($postsIds) || empty($postsIds)) { return false; } $select = $this->_db->select(); $select->from($this->_getPair($this->_tableAlias . '2Tag'), ['idPost', 'idTag']); $select->where('`idPost` IN (?)', $postsIds, \Zend_Db::INT_TYPE); $select->join( $this->_getPair("PostTag"), '`' . $this->_tableAlias . '2Tag`.`idTag` = `PostTag`.`id`', ['value'] ); $list = $this->_db->fetchAll($select); $data = []; foreach ($list as $row) { $data[$row['idPost']][$row['idTag']] = $row['value']; } return $data; } protected function _prepareList(&$list) { parent::_prepareRow($list); if (!empty($list)) { $postsIds = \Qs_Array::fetchCol($list, 'id'); $tags = $this->_getPostsTags($postsIds); foreach ($list as &$row) { $row['tags'] = []; if (array_key_exists($row['id'], $tags)) { $row['tags'] = $tags[$row['id']]; } } unset($row); } return $this; } public function getListStatement4XmlSitemap($columns = null, $options = []) { if (null === $columns) { $aliasField = $this->_db->quoteIdentifier($this->_tableAlias . '.alias'); $idField = $this->_db->quoteIdentifier($this->_tableAlias . '.id'); $columns = ['alias' => "IFNULL({$aliasField}, {$idField})"]; } return parent::getListStatement4XmlSitemap($columns, $options); } }