<?php
/**
* Landofcoder
*
* NOTICE OF LICENSE
*
* This source file is subject to the Landofcoder.com license that is
* available through the world-wide-web at this URL:
* https://landofcoder.com/license-agreement.html
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade this extension to newer
* version in the future.
*
* @category Landofcoder
* @package Lofmp_Blog
* @copyright Copyright (c) 2016 Landofcoder (https://landofcoder.com/)
* @license https://landofcoder.com/LICENSE-1.0.html
*/
namespace Lofmp\Blog\Model\ResourceModel\Tag;
use \Lofmp\Blog\Model\ResourceModel\AbstractCollection;
class Collection extends AbstractCollection
{
/**
* @var int
*/
protected $_storeId;
/**
* Constructor
* Configures collection
*
* @return void
*/
protected function _construct()
{
parent::_construct();
$this->_init(\Lofmp\Blog\Model\Tag::class, \Lofmp\Blog\Model\ResourceModel\Tag::class);
$this->_map['fields']['tag_id'] = 'main_table.tag_id';
$this->_map['fields']['store'] = 'store_table.store_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 filter by store
*
* @param int|array|\Magento\Store\Model\Store $store
* @param bool $withAdmin
* @return $this
*/
public function addStoreFilter($store, $withAdmin = true)
{
if (!$this->getFlag('store_filter_added')) {
$this->performAddStoreFilter($store, $withAdmin);
$this->setFlag('store_filter_added', 1);
}
return $this;
}
/**
* 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;
}
/**
* Add active filter to collection
* @return self
*/
public function addActiveFilter()
{
return $this
->addFieldToFilter('main_table.is_active', \Lofmp\Blog\Model\Tag::STATUS_ENABLED);
}
/**
* Perform operations after collection load
*
* @return $this
*/
protected function _afterLoad()
{
$items = $this->getColumnValues('tag_id');
if (count($items)) {
$connection = $this->getConnection();
$tableName = $this->getTable('lofmp_blog_tag_store');
$select = $connection->select()
->from(['cps' => $tableName])
->where('cps.tag_id IN (?)', $items);
$result = [];
foreach ($connection->fetchAll($select) as $item) {
if (!isset($result[$item['tag_id']])) {
$result[$item['tag_id']] = [];
}
$result[$item['tag_id']][] = $item['store_id'];
}
if ($result) {
foreach ($this as $item) {
$tagId = $item->getData('tag_id');
if (!isset($result[$tagId])) {
continue;
}
if ($result[$tagId] == 0) {
$stores = $this->_storeManager->getStores(false, true);
$storeId = current($stores)->getId();
} else {
$storeId = $result[$item->getData('tag_id')];
}
$item->setData('_first_store_id', $storeId);
$item->setData('store_ids', $result[$tagId]);
}
}
if ($this->_storeId) {
foreach ($this as $item) {
$item->setStoreId($this->_storeId);
}
}
}
return parent::_afterLoad();
}
/**
* Join store relation table if there is store filter
*
* @return void
*/
protected function _renderFiltersBefore()
{
if ($this->getFilter('store')) {
$this->getSelect()->join(
['store_table' => $this->getTable('lofmp_blog_tag_store')],
'main_table.tag_id = store_table.tag_id',
[]
)->group(
'main_table.tag_id'
);
}
parent::_renderFiltersBefore();
}
}
|