Current File : /var/www/vinorea/modules/ipexportimport/classes/export/EIAAttributesExport.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 EIAAttributesExport extends EIAExport
{
    protected $xmlMainTag = 'Attribute';
    protected $xmlMainTagPl = 'Attributes';

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

    protected function getDataFromDb()
    {
        if ($this->auto) {
            $attributeGroups = pSQL(implode(',', $this->datatables['attributeGroups']['data']));
            $attributeGroupsType = $this->datatables['attributeGroups']['type'];
            
            $attributeValues = pSQL(implode(',', $this->datatables['attributeValues']['data']));
            $attributeValuesType = $this->datatables['attributeValues']['type'];
        } else {
            $attributeGroups = pSQL(Tools::getValue('attributeGroups_data'));
            $attributeGroupsType = Tools::getValue('attributeGroups_type');
            
            $attributeValues = pSQL(Tools::getValue('attributeValues_data'));
            $attributeValuesType = Tools::getValue('attributeValues_type');
        }
        
        $eIHelper = new EIAHelper($this->module);

        $this->sql = '
            SELECT SQL_CALC_FOUND_ROWS ';
        if (in_array($this->fileType, ['xml'])) {
            $fieldsInTree = $eIHelper->getFieldsInTree('attributes');
            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_ . 'attribute_group attribute_group
                LEFT JOIN
            ' . _DB_PREFIX_ . 'attribute_group_shop attribute_group_shop ON attribute_group.id_attribute_group = attribute_group_shop.id_attribute_group
                    AND attribute_group_shop.id_shop = ' . $this->shopId . '
                LEFT JOIN
            ' . _DB_PREFIX_ . 'attribute_group_lang attribute_group_lang ON attribute_group.id_attribute_group = attribute_group_lang.id_attribute_group
                    AND attribute_group_lang.id_lang = ' . $this->langId . '
                LEFT JOIN
            ' . _DB_PREFIX_ . 'attribute attribute ON attribute_group.id_attribute_group = attribute.id_attribute_group
                LEFT JOIN
            ' . _DB_PREFIX_ . 'attribute_lang attribute_lang ON attribute.id_attribute = attribute_lang.id_attribute
                    AND attribute_lang.id_lang = ' . $this->langId . '
                ';

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

        // Filter By Attribute Group
        $attributeGroupCond = '';
        if ($attributeGroups) {
            if ($attributeGroupsType === 'unselected') {
                $attributeGroupCond = 'attribute_group.id_attribute_group NOT IN (' . $attributeGroups . ')';
            } else {
                $attributeGroupCond = 'attribute_group.id_attribute_group IN (' . $attributeGroups . ')';
            }
        }
        if ($attributeGroupCond) {
            $this->sql .= ' 
                        AND (' . $attributeGroupCond . ') ';
        }

        // Filter By Attribute
        $attributeCond = '';
        if ($attributeValues) {
            if ($attributeValuesType === 'unselected') {
                $attributeCond = 'attribute.id_attribute NOT IN (' . $attributeValues . ')';
            } else {
                $attributeCond = 'attribute.id_attribute IN (' . $attributeValues . ')';
            }
        }
        if ($attributeCond) {
            $this->sql .= ' 
                        AND (' . $attributeCond . ') ';
        }

        // 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 !== 'attribute_group.id_attribute_group') {
            $this->sql .= ', attribute_group.id_attribute_group ASC';
        }

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

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