Current File : /var/www/vinorea/modules/ipexportimport/classes/export/EIAGroupsExport.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 EIAGroupsExport extends EIAExport
{

    protected $xmlMainTag = 'Group';
    protected $xmlMainTagPl = 'Groups';

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

    protected function getDataFromDb()
    {
        if ($this->auto) {
            $groups = pSQL(implode(',', $this->datatables['groups2']['data']));
            $groupsType = $this->datatables['groups2']['type'];
        } else {
            $groups = pSQL(Tools::getValue('groups2_data'));
            $groupsType = Tools::getValue('groups2_type');
        }
        
        $eIHelper = new EIAHelper($this->module);

        $this->sql = '
            SELECT SQL_CALC_FOUND_ROWS ';
        if (in_array($this->fileType, ['xml'])) {
            $fieldsInTree = $eIHelper->getFieldsInTree('groups');
            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_ . 'group groupp
                LEFT JOIN
            ' . _DB_PREFIX_ . 'group_shop group_shop ON groupp.id_group = group_shop.id_group
                    AND group_shop.id_shop = ' . $this->shopId . '
                LEFT JOIN
            ' . _DB_PREFIX_ . 'group_lang group_lang ON groupp.id_group = group_lang.id_group
                    AND group_lang.id_lang = ' . $this->langId;

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

        // Filter By Group
        $groupCond = '';
        if ($groups) {
            if ($groupsType === 'unselected') {
                $groupCond = 'groupp.id_group NOT IN (' . $groups . ')';
            } else {
                $groupCond = 'groupp.id_group IN (' . $groups . ')';
            }
        }
        if ($groupCond) {
            $this->sql .= ' 
                        AND (' . $groupCond . ') ';
        }

        // 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 !== 'groupp.id_group') {
            $this->sql .= ', groupp.id_group ASC';
        }
        
        $this->sql .= ' LIMIT ' . $this->offset . ', ' . $this->limit;

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