Model/ResourceModel/Pdfgenerator/AbstractCollection.php
<?php
/**
* Copyright © EAdesign by Eco Active S.R.L.,All rights reserved.
* See LICENSE for license details.
*/
namespace Eadesigndev\Pdfgenerator\Model\ResourceModel\Pdfgenerator;
use Magento\Framework\Data\Collection\Db\FetchStrategyInterface;
use Magento\Framework\Data\Collection\EntityFactoryInterface;
use Magento\Framework\DB\Adapter\AdapterInterface;
use Magento\Framework\DB\Select;
use Magento\Framework\EntityManager\MetadataPool;
use Magento\Framework\Event\ManagerInterface;
use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection as MagentoAbstractCollection;
use Magento\Store\Model\Store;
use Magento\Store\Model\StoreManagerInterface;
use Psr\Log\LoggerInterface;
/**
* Abstract collection of PDF templates
* @SuppressWarnings("CouplingBetweenObjects")
*/
abstract class AbstractCollection extends MagentoAbstractCollection
{
/**
* Store manager
*
* @var StoreManagerInterface
*/
private $storeManager;
/**
* @var MetadataPool
*/
private $metadataPool;
/**
* @param EntityFactoryInterface $entityFactory
* @param LoggerInterface $logger
* @param FetchStrategyInterface $fetchStrategy
* @param ManagerInterface $eventManager
* @param StoreManagerInterface $storeManager
* @param MetadataPool $metadataPool
* @param AdapterInterface|null $connection
* @param AbstractDb|null $resource
*/
public function __construct(
EntityFactoryInterface $entityFactory,
LoggerInterface $logger,
FetchStrategyInterface $fetchStrategy,
ManagerInterface $eventManager,
StoreManagerInterface $storeManager,
MetadataPool $metadataPool,
AdapterInterface $connection = null,
AbstractDb $resource = null
) {
$this->storeManager = $storeManager;
$this->metadataPool = $metadataPool;
parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $connection, $resource);
}
/**
* Perform operations after collection load
*
* @param string $tableName
* @param string|null $linkField
* @return void
* @SuppressWarnings("Performance.InefficientMethods")
*/
public function performAfterLoad($tableName, $linkField)
{
$linkedIds = $this->getColumnValues($linkField);
if (!empty($linkedIds)) {
$connection = $this->getConnection();
$select = $connection->select()->from(['eadesign_pdf_store' => $this->getTable($tableName)])
->where('eadesign_pdf_store.' . $linkField . ' IN (?)', $linkedIds);
//@codingStandardsIgnoreStart
$result = $connection->fetchAll($select);
//@codingStandardsIgnoreEnd
if ($result) {
$storesData = [];
foreach ($result as $storeData) {
$storesData[$storeData[$linkField]][] = $storeData['store_id'];
}
foreach ($this as $item) {
$linkedId = $item->getData($linkField);
if (!isset($storesData[$linkedId])) {
continue;
}
$storeIdKey = array_search(Store::DEFAULT_STORE_ID, $storesData[$linkedId], true);
if ($storeIdKey !== false) {
$stores = $this->storeManager->getStores(false, true);
$storeId = current($stores)->getId();
$storeCode = key($stores);
} else {
$storeId = current($storesData[$linkedId]);
$storeCode = $this->storeManager->getStore($storeId)->getCode();
}
$item->setData('_first_store_id', $storeId);
$item->setData('store_code', $storeCode);
$item->setData('store_id', $storesData[$linkedId]);
}
}
}
}
/**
* Add field filter to collection
*
* @param array|string $field
* @param string|int|array|null $condition
* @return \Magento\Framework\Data\Collection\AbstractDb
*/
public function addFieldToFilter($field, $condition = null)
{
if ($field === 'store_id') {
return $this->addStoreFilter($condition, false);
}
return parent::addFieldToFilter($field, $condition);
}
/**
* Add filter by store
*
* @param int|array|Store $store
* @param bool $withAdmin
* @return $this
*/
abstract public function addStoreFilter($store, $withAdmin = true);
/**
* Perform adding filter by store
*
* @param int|array|Store $store
* @param bool $withAdmin
* @return void
*/
public function performAddStoreFilter($store, $withAdmin = true)
{
if ($store instanceof Store) {
$store = [$store->getId()];
}
if (!is_array($store)) {
$store = [$store];
}
if ($withAdmin) {
$store[] = Store::DEFAULT_STORE_ID;
}
$this->addFilter('store', ['in' => $store], 'public');
}
/**
* Join store relation table if there is store filter
*
* @param string $tableName
* @param string|null $linkField
* @return void
* @SuppressWarnings("MEQP1.SlowQuery")
*/
public function joinStoreRelationTable($tableName, $linkField)
{
if ($this->getFilter('store')) {
$this->getSelect()->join(
['store_table' => $this->getTable($tableName)],
'main_table.' . $linkField . ' = store_table.' . $linkField,
[]
)
//@codingStandardsIgnoreStart
->group(
'main_table.' . $linkField
);
//@codingStandardsIgnoreEnd
}
parent::_renderFiltersBefore();
}
/**
* Get SQL for get record count
*
* Extra GROUP BY strip added.
*
* @return Select
*/
public function getSelectCountSql()
{
$countSelect = parent::getSelectCountSql();
$countSelect->reset(Select::GROUP);
return $countSelect;
}
}