<?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
*
* 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 Lof_HelpDesk
* @copyright Copyright (c) 2021 Landofcoder (https://landofcoder.com/)
* @license https://landofcoder.com/license-1-0
*/
namespace Lof\HelpDesk\Model\ResourceModel;
/**
* Abstract collection of CMS pages and blocks
*/
abstract class AbstractCollection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
{
/**
* Store manager
*
* @var \Magento\Store\Model\StoreManagerInterface
*/
protected $storeManager;
/**
* @param \Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory
* @param \Psr\Log\LoggerInterface $logger
* @param \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy
* @param \Magento\Framework\Event\ManagerInterface $eventManager
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param \Magento\Framework\DB\Adapter\AdapterInterface|null $connection
* @param \Magento\Framework\Model\ResourceModel\Db\AbstractDb|null $resource
*/
public function __construct(
\Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory,
\Psr\Log\LoggerInterface $logger,
\Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
\Magento\Framework\Event\ManagerInterface $eventManager,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
\Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null
)
{
parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $connection, $resource);
$this->storeManager = $storeManager;
}
/**
* [performAfterLoad description]
* @param [type] $tableName [description]
* @param [type] $columnName [description]
* @return [type] [description]
*/
protected function performAfterLoad($tableName, $columnName)
{
$items = $this->getColumnValues($columnName);
if (count($items)) {
$connection = $this->getConnection();
$select = $connection->select()->from(['_entity_store' => $this->getTable($tableName)])
->where('_entity_store.' . $columnName . ' IN (?)', $items);
$result = $connection->fetchPairs($select);
foreach ($this as $item) {
$entityId = $item->getData($columnName);
$result_store_id = 0;
if ($result) {
if (isset($result[$entityId]) && $result[$entityId] == 0) {
$stores = $this->storeManager->getStores(false, true);
$storeId = current($stores)->getId();
$storeCode = key($stores);
$result_store_id = $result[$entityId];
} elseif (isset($result[$item->getData($columnName)])) {
$storeId = $result[$item->getData($columnName)];
$storeCode = $this->storeManager->getStore($storeId)->getCode();
$result_store_id = $result[$entityId];
}
}
if (!isset($storeId)) {
$stores = $this->storeManager->getStores(false, true);
$storeId = current($stores)->getId();
$storeCode = key($stores);
}
$item->setData('_first_store_id', $storeId);
$item->setData('store_code', $storeCode);
$item->setData('store_id', [$result_store_id]);
}
}
}
protected function performAfterLoadUser($tableName, $columnName)
{
$items = $this->getColumnValues($columnName);
if (count($items)) {
$connection = $this->getConnection();
$select = $connection->select()->from(['lof_helpdesk_department_user' => $this->getTable($tableName)])
->where('lof_helpdesk_department_user.' . $columnName . ' IN (?)', $items);
$result = $connection->fetchAll($select);
if ($result) {
foreach ($this as $item) {
$entityId = $item->getData($columnName);
$data = [];
foreach ($result as $key => $_result) {
if ($_result['department_id'] == $entityId) {
$data[] = $_result['user_id'];
}
}
$item->setData('user_id', $data);
}
}
}
}
protected function performAfterLoadDepartment($tableName, $columnName)
{
$items = $this->getColumnValues($columnName);
if (count($items)) {
$connection = $this->getConnection();
$select = $connection->select()->from(['_permission_department' => $this->getTable($tableName)])
->where('_permission_department.' . $columnName . ' IN (?)', $items);
$result = $connection->fetchAll($select);
if ($result) {
foreach ($this as $item) {
$entityId = $item->getData($columnName);
$data = [];
foreach ($result as $key => $_result) {
if ($_result['permission_id'] == $entityId) {
$data[] = $_result['department_id'];
}
}
$item->setData('department_id', $data);
}
}
}
}
protected function performAfterLoadCategory($tableName, $columnName)
{
$items = $this->getColumnValues($columnName);
if (count($items)) {
$connection = $this->getConnection();
$select = $connection->select('category_id')->from(['_department_category' => $this->getTable($tableName)])
->where('_department_category.' . $columnName . ' IN (?)', $items);
$result = $connection->fetchAll($select);
if ($result) {
foreach ($this as $item) {
$entityId = $item->getData($columnName);
$data = [];
foreach ($result as $key => $_result) {
if ($_result['department_id'] == $entityId) {
$data[] = $_result['category_id'];
}
}
$item->setData('category_id', $data);
}
}
}
}
/**
* 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 field filter to collection
*
* @param array|string $field
* @param string|int|array|null $condition
* @return $this
*/
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|\Magento\Store\Model\Store $store
* @param bool $withAdmin
* @return $this
*/
abstract public function addStoreFilter($store, $withAdmin = true);
/**
* Perform adding filter by store
*
* @param int|array|\Magento\Store\Model\Store $store
* @param bool $withAdmin
* @return void
*/
protected function performAddStoreFilter($store, $withAdmin = true)
{
if ($store instanceof \Magento\Store\Model\Store) {
$store = [$store->getId()];
}
if (!is_array($store)) {
$store = [$store];
}
if ($withAdmin) {
$store[] = \Magento\Store\Model\Store::DEFAULT_STORE_ID;
}
$this->addFilter('store_id', ['in' => $store], 'public');
}
/**
* Join store relation table if there is store filter
*
* @param string $tableName
* @param string $columnName
* @return void
*/
protected function joinStoreRelationTable($tableName, $columnName)
{
if ($this->getFilter('store')) {
$this->getSelect()->join(
['store_table' => $this->getTable($tableName)],
'main_table.' . $columnName . ' = store_table.' . $columnName,
[]
)->group(
'main_table.' . $columnName
);
}
parent::_renderFiltersBefore();
}
}
|