Spamworldpro Mini Shell
Spamworldpro


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/app/code/Soon/Faq/Model/ResourceModel/Question/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home/corals/old/app/code/Soon/Faq/Model/ResourceModel/Question/Collection.php
<?php
namespace Soon\Faq\Model\ResourceModel\Question;

use Magento\Framework\Data\Collection\Db\FetchStrategyInterface;
use Magento\Framework\Data\Collection\EntityFactoryInterface;
use Magento\Framework\DB\Adapter\AdapterInterface;
use Magento\Framework\Event\ManagerInterface;
use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
use Magento\Store\Model\StoreManagerInterface;
use Psr\Log\LoggerInterface;
use Soon\Faq\Model\ResourceModel\Category\Collection as CategoryCollection;
use Soon\Faq\Model\ResourceModel\Category\CollectionFactory as CategoryCollectionFactory;

class Collection extends AbstractCollection
{
    const ADMIN_STORE_ID = 0;

    /**
     * @var CategoryCollectionFactory
     */
    private $categoryCollectionFactory;
    /**
     * @var  bool
     */
    private $hasCategoryData = false;
    /**
     * @var StoreManagerInterface
     */
    private $storeManager;

    public function __construct(
        CategoryCollectionFactory $categoryCollectionFactory,
        StoreManagerInterface $storeManager,
        EntityFactoryInterface $entityFactory,
        LoggerInterface $logger,
        FetchStrategyInterface $fetchStrategy,
        ManagerInterface $eventManager,
        AdapterInterface $connection = null,
        AbstractDb $resource  = null
    )
    {
        parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $connection, $resource);
        $this->categoryCollectionFactory = $categoryCollectionFactory;
        $this->storeManager = $storeManager;
    }

    protected function _construct()
    {
        $this->_init('Soon\Faq\Model\Question', 'Soon\Faq\Model\ResourceModel\Question');
    }

    /**
     * @return Collection
     */
    public function appendCategoryData()
    {
        if (!$this->hasCategoryData) {
            /** @var CategoryCollection $categoryCollection */
            $categoryCollection = $this->categoryCollectionFactory->create();
            $this->getSelect()
                ->joinLeft(
                    ['category_table' => $categoryCollection->getMainTable()],
                    'main_table.category_id = category_table.id',
                    [
                        'category_name' => 'category_table.name'
                    ]
                );

            $this->hasCategoryData = true;
        }

        return $this;
    }

    /**
     * @return Collection
     */
    public function addActiveFilter()
    {
        $this->getSelect()->where('main_table.is_active = ?', 1);

        return $this;
    }

    /**
     * @return Collection
     */
    public function addCurrentStoreFilter()
    {
        $this->appendCategoryData();
        $this->makeJoinOnStoresTable();
        $this->addStatementForAdminOrCurrentStore();
        $this->groupByInstanceId();

        return $this;
    }

    /**
     * Join stores
     */
    private function makeJoinOnStoresTable()
    {
        $this->getSelect()
            ->joinLeft(
                ['category_store_table' => $this->getTable('soon_faq_category_store')],
                'category_table.id = category_store_table.category_id',
                [
                    'store_id' => 'category_store_table.store_id'
                ]
            );
    }

    /**
     * Select only questions which categories are linked to admin or current store
     */
    private function addStatementForAdminOrCurrentStore()
    {
        $this->getSelect()
            ->where('store_id IN (?)', [self::ADMIN_STORE_ID, (int)$this->storeManager->getStore()->getId()]);
    }

    /**
     * Group results by ID field
     */
    private function groupByInstanceId()
    {
        $this->getSelect()->group('id');
    }

    /**
     * @param int $limit
     * @return Collection
     */
    public function setLimit($limit)
    {
        $this->getSelect()->limit($limit);

        return $this;
    }

    /**
     * @return Collection
     */
    public function addFrequentFilter()
    {
        $this->getSelect()
            ->where('main_table.is_most_frequently_asked = ?', 1);

        return $this;
    }

    /**
     * Sort collection by questions' sort_order field
     */
    public function sortBySortOrder()
    {
        $this->setOrder('sort_order', self::SORT_ORDER_ASC);
    }
}

Spamworldpro Mini