123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357 |
- <?php
- /**
- * Copyright © 2015 Ihor Vansach (ihor@magefan.com). All rights reserved.
- * See LICENSE.txt for license details (http://opensource.org/licenses/osl-3.0.php).
- *
- * Glory to Ukraine! Glory to the heroes!
- */
- namespace Magefan\Blog\Model\ResourceModel\Post;
- /**
- * Blog post collection
- */
- class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
- {
- /**
- * @var \Magento\Store\Model\StoreManagerInterface
- */
- protected $_storeManager;
- /**
- * @var \Magento\Framework\Stdlib\DateTime\DateTime
- */
- protected $_date;
- /**
- * @var int
- */
- protected $_storeId;
- /**
- * @var \Magefan\Blog\Model\Category
- */
- protected $category;
- /**
- * @param \Magento\Framework\Data\Collection\EntityFactory $entityFactory
- * @param \Psr\Log\LoggerInterface $logger
- * @param \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy
- * @param \Magento\Framework\Event\ManagerInterface $eventManager
- * @param \Magento\Framework\Stdlib\DateTime\DateTime $date
- * @param Magento\Store\Model\StoreManagerInterface $storeManager
- * @param null|\Zend_Db_Adapter_Abstract $connection
- * @param \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource
- */
- public function __construct(
- \Magento\Framework\Data\Collection\EntityFactory $entityFactory,
- \Psr\Log\LoggerInterface $logger,
- \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
- \Magento\Framework\Event\ManagerInterface $eventManager,
- \Magento\Framework\Stdlib\DateTime\DateTime $date,
- \Magento\Store\Model\StoreManagerInterface $storeManager,
- $connection = null,
- \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null
- ) {
- parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $connection, $resource);
- $this->_date = $date;
- $this->_storeManager = $storeManager;
- }
- /**
- * Constructor
- * Configures collection
- *
- * @return void
- */
- protected function _construct()
- {
- parent::_construct();
- $this->_init('Magefan\Blog\Model\Post', 'Magefan\Blog\Model\ResourceModel\Post');
- $this->_map['fields']['post_id'] = 'main_table.post_id';
- $this->_map['fields']['store'] = 'store_table.store_id';
- $this->_map['fields']['category'] = 'category_table.category_id';
- $this->_map['fields']['tag'] = 'tag_table.tag_id';
- }
- /**
- * Add field filter to collection
- *
- * @param string|array $field
- * @param null|string|array $condition
- * @return $this
- */
- public function addFieldToFilter($field, $condition = null)
- {
- if ($field === 'store_id' || $field === 'store_ids') {
- return $this->addStoreFilter($condition);
- }
- return parent::addFieldToFilter($field, $condition);
- }
- /**
- * Add store filter to collection
- * @param array|int|\Magento\Store\Model\Store $store
- * @param boolean $withAdmin
- * @return $this
- */
- public function addStoreFilter($store, $withAdmin = true)
- {
- if ($store === null) {
- return $this;
- }
- if (!$this->getFlag('store_filter_added')) {
- if ($store instanceof \Magento\Store\Model\Store) {
- $this->_storeId = $store->getId();
- $store = [$store->getId()];
- }
- if (!is_array($store)) {
- $this->_storeId = $store;
- $store = [$store];
- }
- if (in_array(\Magento\Store\Model\Store::DEFAULT_STORE_ID, $store)) {
- return $this;
- }
- if ($withAdmin) {
- $store[] = \Magento\Store\Model\Store::DEFAULT_STORE_ID;
- }
- $this->addFilter('store', ['in' => $store], 'public');
- }
- return $this;
- }
- /**
- * Add category filter to collection
- * @param array|int|\Magefan\Blog\Model\Category $category
- * @return $this
- */
- public function addCategoryFilter($category)
- {
- if (!$this->getFlag('category_filter_added')) {
- if ($category instanceof \Magefan\Blog\Model\Category) {
- $this->category = $category;
- $categories = $category->getChildrenIds();
- $categories[] = $category->getId();
- } else {
- $categories = $category;
- if (!is_array($categories)) {
- $categories = [$categories];
- }
- }
- $this->addFilter('category', ['in' => $categories], 'public');
- }
- return $this;
- }
- /**
- * Add archive filter to collection
- * @param int $year
- * @param int $month
- * @return $this
- */
- public function addArchiveFilter($year, $month)
- {
- $this->getSelect()
- ->where('YEAR(publish_time) = ?', $year)
- ->where('MONTH(publish_time) = ?', $month);
- return $this;
- }
- /**
- * Add search filter to collection
- * @param string $term
- * @return $this
- */
- public function addSearchFilter($term)
- {
- $this->addFieldToFilter(
- ['title', 'content_heading', 'content'],
- [
- ['like' => '%' . $term . '%'],
- ['like' => '%' . $term . '%'],
- ['like' => '% ' . $term . ' %']
- ]
- );
- return $this;
- }
- /**
- * Add tag filter to collection
- * @param array|int|\Magefan\Blog\Model\Tag $tag
- * @return $this
- */
- public function addTagFilter($tag)
- {
- if (!$this->getFlag('tag_filter_added')) {
- if ($tag instanceof \Magefan\Blog\Model\Tag) {
- $tag = [$tag->getId()];
- }
- if (!is_array($tag)) {
- $tag = [$tag];
- }
- $this->addFilter('tag', ['in' => $tag], 'public');
- }
- return $this;
- }
- /**
- * Add author filter to collection
- * @param array|int|\Magefan\Blog\Model\Author $author
- * @return $this
- */
- public function addAuthorFilter($author)
- {
- if (!$this->getFlag('author_filter_added')) {
- if ($author instanceof \Magefan\Blog\Model\Author) {
- $author = [$author->getId()];
- }
- if (!is_array($author)) {
- $author = [$author];
- }
- $this->addFilter('author_id', ['in' => $author], 'public');
- }
- return $this;
- }
- /**
- * Add is_active filter to collection
- * @return $this
- */
- public function addActiveFilter()
- {
- return $this
- ->addFieldToFilter('is_active', 1)
- ->addFieldToFilter('publish_time', ['lteq' => $this->_date->gmtDate()]);
- }
- /**
- * Get SQL for get record count
- *
- * Extra GROUP BY strip added.
- *
- * @return \Magento\Framework\DB\Select
- */
- public function getSelectCountSql()
- {
- $countSelect = parent::getSelectCountSql();
- $countSelect->reset(\Magento\Framework\DB\Select::GROUP);
- return $countSelect;
- }
- /**
- * Perform operations after collection load
- *
- * @return $this
- */
- protected function _afterLoad()
- {
- $items = $this->getColumnValues('post_id');
- if (count($items)) {
- $connection = $this->getConnection();
- $tableName = $this->getTable('magefan_blog_post_store');
- $select = $connection->select()
- ->from(['cps' => $tableName])
- ->where('cps.post_id IN (?)', $items);
- $result = [];
- foreach ($connection->fetchAll($select) as $item) {
- if (!isset($result[$item['post_id']])) {
- $result[$item['post_id']] = [];
- }
- $result[$item['post_id']][] = $item['store_id'];
- }
- if ($result) {
- foreach ($this as $item) {
- $postId = $item->getData('post_id');
- if (!isset($result[$postId])) {
- continue;
- }
- if ($result[$postId] == 0) {
- $stores = $this->_storeManager->getStores(false, true);
- $storeId = current($stores)->getId();
- } else {
- $storeId = $result[$item->getData('post_id')];
- }
- $item->setData('_first_store_id', $storeId);
- $item->setData('store_ids', $result[$postId]);
- }
- }
- foreach ($this as $item) {
- if ($this->_storeId) {
- $item->setStoreId($this->_storeId);
- }
- if ($this->category) {
- $item->setData('parent_category', $this->category);
- }
- }
- $map = [
- 'category' => 'categories',
- 'tag' => 'tags',
- ];
- foreach ($map as $key => $property) {
- $tableName = $this->getTable('magefan_blog_post_' . $key);
- $select = $connection->select()
- ->from(['cps' => $tableName])
- ->where('cps.post_id IN (?)', $items);
- $result = $connection->fetchAll($select);
- if ($result) {
- $data = [];
- foreach($result as $item) {
- $data[$item['post_id']][] = $item[$key . '_id'];
- }
- foreach ($this as $item) {
- $postId = $item->getData('post_id');
- if (isset($data[$postId])) {
- $item->setData($property, $data[$postId]);
- }
- }
- }
- }
- }
- $this->_previewFlag = false;
- return parent::_afterLoad();
- }
- /**
- * Join store relation table if there is store filter
- *
- * @return void
- */
- protected function _renderFiltersBefore()
- {
- foreach(['store', 'category', 'tag'] as $key) {
- if ($this->getFilter($key)) {
- $this->getSelect()->join(
- [$key.'_table' => $this->getTable('magefan_blog_post_'.$key)],
- 'main_table.post_id = '.$key.'_table.post_id',
- []
- )->group(
- 'main_table.post_id'
- );
- }
- }
- parent::_renderFiltersBefore();
- }
- }
|