Current File : /var/www/vinorea/modules/ipexportimport/classes/export/EIAFeaturesExport.php
<?php
/**
 *
 * NOTICE OF LICENSE
 *
 *  @author    SmartPresta <tehran.alishov@gmail.com>
 *  @copyright 2024 SmartPresta
 *  @license   http://opensource.org/licenses/afl-3.0.php Commercial License!
 */

if (!defined('_PS_VERSION_')) {
    exit;
}

class EIAFeaturesExport extends EIAExport
{
    protected $xmlMainTag = 'Feature';
    protected $xmlMainTagPl = 'Features';

    public function __construct($module)
    {
        parent::__construct($module);
        $this->entityNamePl = $module->l('Features', 'EIAFeaturesExport');
    }

    protected function getDataFromDb()
    {
        if ($this->auto) {
            $features = pSQL(implode(',', $this->datatables['featuresForFeatures']['data']));
            $featuresType = $this->datatables['featuresForFeatures']['type'];
            
            $featureValues = pSQL(implode(',', $this->datatables['featureValues']['data']));
            $featureValuesType = $this->datatables['featureValues']['type'];
        } else {
            $features = pSQL(Tools::getValue('featuresForFeatures_data'));
            $featuresType = Tools::getValue('featuresForFeatures_type');
            
            $featureValues = pSQL(Tools::getValue('featureValues_data'));
            $featureValuesType = Tools::getValue('featureValues_type');
        }
        
        $eIHelper = new EIAHelper($this->module);

        $this->sql = '
            SELECT SQL_CALC_FOUND_ROWS ';
        if (in_array($this->fileType, ['xml'])) {
            $fieldsInTree = $eIHelper->getFieldsInTree('features');
            foreach ($this->selectedColumns as $k => $col) {
                $this->sql .= "
                    $k AS `{$fieldsInTree[$k]}`, ";
            }
        } else {
            foreach ($this->selectedColumns as $k => $col) {
                $this->sql .= "
                    $k `$col`, ";
            }
        }

        $this->sql = rtrim($this->sql, ', ');

        $this->sql .= '
            FROM 
            ' . _DB_PREFIX_ . 'feature feature
                LEFT JOIN
            ' . _DB_PREFIX_ . 'feature_shop feature_shop ON feature.id_feature = feature_shop.id_feature
                    AND feature_shop.id_shop = ' . $this->shopId . '
                LEFT JOIN
            ' . _DB_PREFIX_ . 'feature_lang feature_lang ON feature.id_feature = feature_lang.id_feature
                    AND feature_lang.id_lang = ' . $this->langId . '
                LEFT JOIN ' . _DB_PREFIX_ . 'feature_value feature_value ON feature.id_feature = feature_value.id_feature
                LEFT JOIN ' . _DB_PREFIX_ . 'feature_value_lang feature_value_lang ON feature_value.id_feature_value = feature_value_lang.id_feature_value 
                    AND feature_value_lang.id_lang = ' . $this->langId . '
                ';

        $this->sql .= '
                WHERE 1
            ';

        // Filter By Feature
        $featureCond = '';
        if ($features) {
            if ($featuresType === 'unselected') {
                $featureCond = 'feature.id_feature NOT IN (' . $features . ')';
            } else {
                $featureCond = 'feature.id_feature IN (' . $features . ')';
            }
        }
        if ($featureCond) {
            $this->sql .= ' 
                        AND (' . $featureCond . ') ';
        }

        // Filter By Feature Value
        $featureValueCond = '';
        if ($featureValues) {
            if ($featureValuesType === 'unselected') {
                $featureValueCond = 'feature_value.id_feature_value NOT IN (' . $featureValues . ')';
            } else {
                $featureValueCond = 'feature_value.id_feature_value IN (' . $featureValues . ')';
            }
        }
        if ($featureValueCond) {
            $this->sql .= ' 
                        AND (' . $featureValueCond . ') ';
        }

        // Filter by fields data
        $this->sql .= $eIHelper->getFieldsFilterString($this->auto, $this->inputs);

        // Sort By ...
        $this->sql .= ' ORDER BY ' . $this->sort . $this->sortWay;
        if ($this->sort !== 'feature.id_feature') {
            $this->sql .= ', feature.id_feature ASC';
        }

        $this->sql .= ' LIMIT ' . $this->offset . ', ' . $this->limit;

//        d($this->sql);
        return Db::getInstance()->executeS($this->sql);
    }
}