![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/old/vendor/mageworx/module-seocrosslinks/Model/ResourceModel/Crosslink/ |
<?php /** * Copyright © 2016 MageWorx. All rights reserved. * See LICENSE.txt for license details. */ namespace MageWorx\SeoCrossLinks\Model\ResourceModel\Crosslink; use Magento\Framework\DB\Select; /** * Collection */ class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection { /** * Store manager * * @var \Magento\Store\Model\StoreManagerInterface */ protected $storeManager; /** * @var string */ protected $_idFieldName = 'crosslink_id'; /** * Load data for preview flag * * @var bool */ protected $_previewFlag; /** * @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; } /** * Define resource model * * @return void */ protected function _construct() { $this->_init('MageWorx\SeoCrossLinks\Model\Crosslink', 'MageWorx\SeoCrossLinks\Model\ResourceModel\Crosslink'); $this->_map['fields']['crosslink_id'] = 'main_table.crosslink_id'; $this->_map['fields']['store'] = 'store_table.store_id'; } /** * Perform operations after collection load * * @param string $tableName * @param string $columnName * @return void */ protected function performAfterLoad($tableName, $columnName) { $items = $this->getColumnValues($columnName); if (count($items)) { $connection = $this->getConnection(); $select = $connection->select()->from(['crosslink_entity_store' => $this->getTable($tableName)]) ->where('crosslink_entity_store.' . $columnName . ' IN (?)', $items); $result = $connection->fetchPairs($select); if ($result) { $stores = $this->storeManager->getStores(false, true); $storeCodes = []; foreach ($stores as $code => $store) { $storeCodes[$store->getId()] = $code; } foreach ($this as $item) { $entityId = $item->getData($columnName); if (!isset($result[$entityId])) { continue; } if ($result[$entityId] == 0) { $storeId = current($stores)->getId(); } else { $storeId = $result[$item->getData($columnName)]; } $item->setData('_first_store_id', $storeId); $item->setData('store_code', $storeCodes[$storeId]); $item->setData('store_id', [$result[$entityId]]); } } } } /** * 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); } /** * Set first store flag * * @param bool $flag * @return $this */ public function setFirstStoreFlag($flag = false) { $this->_previewFlag = $flag; return $this; } /** * 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); } return $this; } /** * Perform operations after collection load * * @return $this */ protected function _afterLoad() { $this->performAfterLoad('mageworx_seocrosslinks_crosslink_store', 'crosslink_id'); $this->_previewFlag = false; return parent::_afterLoad(); } /** * Perform operations before rendering filters * * @return void */ protected function _renderFiltersBefore() { $this->joinStoreRelationTable('mageworx_seocrosslinks_crosslink_store', 'crosslink_id'); } /** * 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', ['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(); } /** * Add type filter * * @return $this */ public function addEnabledFilter() { return $this->getSelect()->where('main_table.is_active = 1'); } /** * Add filter by some text * * @param string $content * @return $this */ public function addContentFilter($content) { return $this->getSelect()->where("(?) LIKE CONCAT('%', TRIM(BOTH '+' FROM `keyword`), '%')", $content); } /** * Add product destination filter * * @return $this */ public function addInProductFilter() { return $this->getSelect()->where('main_table.in_product = 1'); } /** * Add category destination filter * * @return $this */ public function addInCategoryFilter() { return $this->getSelect()->where('main_table.in_category = 1'); } /** * Add CMS Page destination filter * * @return $this */ public function addInCmsPageFilter() { return $this->getSelect()->where('main_table.in_cms_page = 1'); } /** * Add Landing Page destination filter * * @return $this */ public function addInLandingPageFilter() { return $this->getSelect()->where('main_table.in_landingpage = 1'); } /** * 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; } /** * @return array */ public function loadKeywordsOnly() { $select = clone $this->getSelect(); $select->columns(['crosslink_id', 'keyword']); return $this->getConnection()->fetchPairs($select); } }