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

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

    protected function getDataFromDb()
    {
        if ($this->auto) {
            $packs = '"' . pSQL(implode('","', $this->datatables['packs']['data'])) . '"';
            $packsType = $this->datatables['packs']['type'];
        } else {
            $packs = pSQL(Tools::getValue('packs_data'));
            $packs = '"' . implode('","', explode(',', $packs)) . '"';
            $packsType = Tools::getValue('packs_type');
        }
        
        $eIHelper = new EIAHelper($this->module);
        
        $this->sql = '
            SELECT SQL_CALC_FOUND_ROWS ';
        if (in_array($this->fileType, ['xml'])) {
            $fieldsInTree = $eIHelper->getFieldsInTree('packs');
            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_ . 'pack pack
            JOIN (SELECT product.id_product, product.reference, product_lang.name FROM ' . _DB_PREFIX_ . 'product product
                JOIN ' . _DB_PREFIX_ . 'product_lang product_lang ON product.id_product = product_lang.id_product
                    AND product_lang.id_shop = ' . $this->shopId . '
                    AND product_lang.id_lang = ' . $this->langId . ') pack_name ON pack.id_product_pack = pack_name.id_product
            JOIN ' . _DB_PREFIX_ . 'product product ON pack.id_product_item = product.id_product
            JOIN ' . _DB_PREFIX_ . 'product_lang product_lang ON product.id_product = product_lang.id_product
                AND product_lang.id_shop = ' . $this->shopId . '
                AND product_lang.id_lang = ' . $this->langId . '
            LEFT JOIN ' . _DB_PREFIX_ . 'product_attribute combination ON pack.id_product_attribute_item = combination.id_product_attribute
            LEFT JOIN ' . _DB_PREFIX_ . 'product_attribute_shop product_attribute_shop ON combination.id_product_attribute = product_attribute_shop.id_product_attribute
                AND product_attribute_shop.id_shop = ' . $this->shopId . '
            LEFT JOIN (
                SELECT                        
                    pac.id_product_attribute,
                    GROUP_CONCAT(CONCAT_WS(":", IFNULL(agl.name, ""), al.name) ORDER BY agl.name, al.name SEPARATOR ", ") name_values,
                    GROUP_CONCAT(CONCAT_WS(":", agl.name, agl.public_name, ag.group_type) ORDER BY agl.name, al.name SEPARATOR ", ") `groups`,
                    GROUP_CONCAT(CONCAT_WS(":", agl.name, al.name) ORDER BY agl.name, al.name SEPARATOR ", ") `values`
                FROM ' . _DB_PREFIX_ . 'product_attribute_combination pac
                JOIN ' . _DB_PREFIX_ . 'attribute a ON pac.id_attribute = a.id_attribute
                JOIN ' . _DB_PREFIX_ . 'attribute_shop ash ON a.id_attribute = ash.id_attribute AND ash.id_shop = ' . $this->shopId . '
                JOIN ' . _DB_PREFIX_ . 'attribute_lang al ON a.id_attribute = al.id_attribute AND al.id_lang = ' . $this->langId . '
                JOIN ' . _DB_PREFIX_ . 'attribute_group ag ON a.id_attribute_group = ag.id_attribute_group
                JOIN ' . _DB_PREFIX_ . 'attribute_group_shop agsh ON a.id_attribute_group = agsh.id_attribute_group AND agsh.id_shop = ' . $this->shopId . '
                JOIN ' . _DB_PREFIX_ . 'attribute_group_lang agl ON a.id_attribute_group = agl.id_attribute_group AND agl.id_lang = ' . $this->langId . '
                GROUP BY pac.id_product_attribute
            ) attributes ON combination.id_product_attribute = attributes.id_product_attribute
            ';

        $this->sql .= '
                WHERE 1
            ';
        
        // Filter By Pack
        $packCond = '';
        if ($packs) {
            if ($packsType === 'unselected') {
                $packCond = 'CONCAT_WS("-", pack.id_product_pack, pack.id_product_item, pack.id_product_attribute_item) NOT IN (' . $packs . ')';
            } else {
                $packCond = 'CONCAT_WS("-", pack.id_product_pack, pack.id_product_item, pack.id_product_attribute_item) IN (' . $packs . ')';
            }
        }
        if ($packCond) {
            $this->sql .= ' 
                        AND (' . $packCond . ') ';
        }

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

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

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