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/Ecombricks/Inventory/Framework/Model/ResourceModel/Db/Collection/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : //home/corals/Ecombricks/Inventory/Framework/Model/ResourceModel/Db/Collection/SourceTrait.php
<?php
/**
 * Copyright © eComBricks. All rights reserved.
 * See LICENSE.txt for license details.
 */
namespace Ecombricks\Inventory\Framework\Model\ResourceModel\Db\Collection;

/**
 * Collection source trait
 */
trait SourceTrait
{
    
    /**
     * Source code loaded
     *
     * @var boolean
     */
    protected $sourceCodeLoaded = false;
    
    /**
     * Add source field
     * 
     * @return $this
     */
    public function addSourceField()
    {
        if (empty($this->sourceTable)) {
            return $this;
        }
        $resource = $this->getResource();
        $connection = $this->getConnection();
        $select = $this->getSelect();
        $fromPart = $select->getPart(\Magento\Framework\DB\Select::FROM);
        if (!isset($fromPart['source_table'])) {
            $idFieldName = $resource->getIdFieldName();
            $idFieldNameFull = 'main_table.'.$idFieldName;
            $idFieldNameQuoted = $connection->quoteIdentifier($idFieldName);
            $idFieldNameFullQuoted = $connection->quoteIdentifier($idFieldNameFull);
            $select->join(
                ['source_table' => $this->getTable($this->sourceTable)],
                $idFieldNameFullQuoted.' = '.$connection->quoteIdentifier('source_table.'.$idFieldName),
                ['source_code']
            );
            $wherePart = $select->getPart(\Magento\Framework\DB\Select::WHERE);
            if (count($wherePart)) {
                foreach ($wherePart as &$condition) {
                    if ((strpos($condition, $idFieldNameQuoted) !== false) && (strpos($condition, $idFieldNameFullQuoted) === false)) {
                        $condition = str_replace($idFieldNameQuoted, $idFieldNameFullQuoted, $condition);
                    }
                }
                $select->setPart(\Magento\Framework\DB\Select::WHERE, $wherePart);
            }
            $this->addFilterToMap($idFieldName, $idFieldNameFull);
            $this->sourceCodeLoaded = true;
        }
        return $this;
    }
    
    /**
     * Add source filter
     * 
     * @param string $sourceCode
     * @return $this
     */
    public function addSourceFilter($sourceCode)
    {
        $this->addSourceField();
        $connection = $this->getConnection();
        $select = $this->getSelect();
        $sourceCodeField = $connection->quoteIdentifier((!empty($this->sourceTable) ? 'source_table' : 'main_table').'.source_code');
        $conditionPrefix = $sourceCodeField.' = ';
        $wherePart = $select->getPart(\Magento\Framework\DB\Select::WHERE);
        $isFilterAdded = false;
        foreach ($wherePart as $condition) {
            if (strpos($condition, $conditionPrefix) !== false) {
                $isFilterAdded = true;
                break;
            }
        }
        if (!$isFilterAdded) {
            $select->where($conditionPrefix.'?', $sourceCode);
        }
        return $this;
    }
    
    /**
     * Add sources filter
     * 
     * @param array $sourceCodes
     * @return $this
     */
    public function addSourcesFilter($sourceCodes)
    {
        $this->addSourceField();
        $connection = $this->getConnection();
        $select = $this->getSelect();
        $sourceCodeField = $connection->quoteIdentifier((!empty($this->sourceTable) ? 'source_table' : 'main_table').'.source_code');
        $conditionPrefix = $sourceCodeField.' IN ';
        $wherePart = $select->getPart(\Magento\Framework\DB\Select::WHERE);
        $isFilterAdded = false;
        foreach ($wherePart as $condition) {
            if (strpos($condition, $conditionPrefix) !== false) {
                $isFilterAdded = true;
                break;
            }
        }
        if (!$isFilterAdded) {
            $select->where($conditionPrefix.'(?)', $sourceCodes);
        }
        return $this;
    }
    
    /**
     * After set vendor ID
     * 
     * @param string $sourceCode
     * @return $this
     */
    public function afterSetSourceCode($sourceCode)
    {
        if (empty($sourceCode)) {
            return $this;
        }
        $this->addSourceFilter($sourceCode);
        return $this;
    }
    
    /**
     * Get source code data
     * 
     * @return array
     */
    public function getSourceCodeData()
    {
        $sourceCode = [];
        $modelIds = [];
        foreach ($this->_items as $model) {
            $modelIds[] = $model->getId();
        }
        if (empty($modelIds)) {
            return $sourceCode;
        }
        $connection = $this->getConnection();
        $idFieldName = $this->getResource()->getIdFieldName();
        $select = $connection->select()
            ->from($this->getTable($this->sourceTable))
            ->where($idFieldName.' IN (?)', $modelIds);
        $rows = $connection->fetchAll($select);
        if (!empty($rows)) {
            foreach ($rows as $row) {
                $id = $row[$idFieldName];
                $sourceCode[$id] = $row['source_code'];
            }
        }
        return $sourceCode;
    }
    
    /**
     * Load source code data
     * 
     * @return $this
     */
    public function loadSourceCodeData()
    {
        if (empty($this->sourceTable)) {
            return $this;
        }
        if ($this->sourceCodeLoaded) {
            return $this;
        }
        $sourceCode = $this->getSourceCodeData();
        foreach ($this->_items as $model) {
            $modelId = $model->getId();
            $model->setSourceCode(!empty($sourceCode[$modelId]) ? $sourceCode[$modelId] : '');
        }
        $this->sourceCodeLoaded = true;
        return $this;
    }
    
    /**
     * After load
     * 
     * @return $this
     */
    public function inventoryAfterLoad()
    {
        $this->loadSourceCodeData();
        return $this;
    }
    
}

Spamworldpro Mini