<?php
class ConfiguratorStorage extends ObjectModel
{
    /**
     * Product with configuration
     * @param int $id_product
     * @return boolean
     */
    public static function hasConfiguratorOpt($id_product)
    {
        $result = Db::getInstance()->getValue('
    		SELECT count(*) AS nb
            FROM `'._DB_PREFIX_.'product_configurator_opt_group` pcog,
                `'._DB_PREFIX_.'configurator_opt_group` cog
    		WHERE pcog.`id_product` = '.(int)$id_product.'
                AND cog.`id_configurator_opt_group` = pcog.`id_configurator_opt_group`');
        
        return ( $result > 0 ? true : false );
    }
    
    /**
     * Retourne les groupes options
     * @param int $id_product
     * @return array|false|NULL|mysqli_result|PDOStatement|resource
     */
    public static function getConfiguratorOptGroup($id_product)
    {
        $context = Context::getContext();
        $result = Db::getInstance()->executeS('
    		SELECT pcog.*, col.`name`, cog.`type`, cog.`reference`
            FROM `'._DB_PREFIX_.'product_configurator_opt_group` pcog,
                `'._DB_PREFIX_.'configurator_opt_group` cog,
                `'._DB_PREFIX_.'configurator_opt_group_lang` col
    		WHERE pcog.`id_product` = '.(int)$id_product.'
                AND cog.`id_configurator_opt_group` = col.`id_configurator_opt_group`
                AND pcog.`id_configurator_opt_group` = col.`id_configurator_opt_group`
                AND col.`id_lang` = '.(int)$context->language->id.'
    		ORDER BY pcog.`position` ASC');
        
        return $result;
    }
    
    /**
     * Retourne les impacts
     * @param int $id_product
     * @return array|false|NULL|mysqli_result|PDOStatement|resource
     */
    public static function getConfiguratorOptImpact($id_product)
    {
        $context = Context::getContext();
        $result = Db::getInstance()->executeS('
            SELECT pcoi.`id_product_configurator_opt_impact`, pcoi.`id_product_configurator_opt_group`, pcoi.`id_configurator_opt`, col.`name`, pcoi.`price`, co.`position`
            FROM `'._DB_PREFIX_.'product_configurator_opt_impact` pcoi
            LEFT JOIN `'._DB_PREFIX_.'configurator_opt` co ON co.`id_configurator_opt` = pcoi.`id_configurator_opt`
            LEFT JOIN `'._DB_PREFIX_.'configurator_opt_lang` col
                ON (col.`id_configurator_opt` = pcoi.`id_configurator_opt` AND col.`id_lang` = '.(int)$context->language->id.')
            WHERE pcoi.`id_product` = '.(int)$id_product.'
            ORDER BY co.`position` ASC');
        
        return $result;
    }
    
    /**
     * Group Label : values
     * @param int $id_configurator
     * @return array[][]|unknown[][]
     */
    public static function getOptProductFlatten($id_configurator)
    {
        $context = Context::getContext();
        $id_lang = $context->language->id;
        
        $optFlatten = array();
        
        $sql = 'SELECT cs.`opt_value`, cs.`id_configurator_opt_group`, cogl.`name` AS groupName, col.`name` AS optName, cog.`reference`
            FROM `'._DB_PREFIX_.'configurator_storage` cs,
                `'._DB_PREFIX_.'configurator_opt_lang` col,
                `'._DB_PREFIX_.'configurator_opt_group_lang` cogl,
                `'._DB_PREFIX_.'configurator_opt_group` cog
            WHERE col.`id_configurator_opt` = cs.`id_configurator_opt` AND col.`id_lang` = '.(int)$id_lang.'
                AND cogl.`id_configurator_opt_group` = cs.`id_configurator_opt_group` AND cogl.`id_lang` = '.(int)$id_lang.'
                AND cog.`id_configurator_opt_group` = cs.`id_configurator_opt_group`
                AND cs.`id_configurator` = '.(int)$id_configurator;
        $optList = Db::getInstance()->executeS($sql);
        if (count($optList) > 0) {
            foreach ($optList as $o) {
                if (!array_key_exists($o['id_configurator_opt_group'], $optFlatten)) {
                    $optFlatten[$o['id_configurator_opt_group']] = array(
                        'name' => $o['groupName'],
                        'reference' => $o['reference'],
                        'value' => array(),
                    );
                }
                if ($o['opt_value'] == '') {
                    $optFlatten[$o['id_configurator_opt_group']]['value'][] = $o['optName'];
                } else {
                    $optFlatten[$o['id_configurator_opt_group']]['value'][] = $o['opt_value'];
                }
            }
        }
        
        return $optFlatten;
    }
    
    public static function getOptProductSelected($id_configurator)
    {
        $context = Context::getContext();
        $id_lang = $context->language->id;
        
        $optFlatten = array();
        
        $sql = 'SELECT cs.`opt_value`, cs.`id_configurator_opt`, cs.`id_configurator_opt_group`, cogl.`name` AS groupName, col.`name` AS optName, cog.`reference`
            FROM `'._DB_PREFIX_.'configurator_storage` cs,
                `'._DB_PREFIX_.'configurator_opt_lang` col,
                `'._DB_PREFIX_.'configurator_opt_group_lang` cogl,
                `'._DB_PREFIX_.'configurator_opt_group` cog
            WHERE col.`id_configurator_opt` = cs.`id_configurator_opt` AND col.`id_lang` = '.(int)$id_lang.'
                AND cogl.`id_configurator_opt_group` = cs.`id_configurator_opt_group` AND cogl.`id_lang` = '.(int)$id_lang.'
                AND cog.`id_configurator_opt_group` = cs.`id_configurator_opt_group`
                AND cs.`id_configurator` = '.(int)$id_configurator;
        $optList = Db::getInstance()->executeS($sql);
        if (count($optList) > 0) {
            foreach ($optList as $o) {
                if (!array_key_exists($o['id_configurator_opt_group'], $optFlatten)) {
                    $optFlatten[$o['id_configurator_opt_group']] = array();
                }
                if ($o['opt_value'] == '') {
                    $optFlatten[$o['id_configurator_opt_group']][] = $o['id_configurator_opt'];
                } else {
                    $optFlatten[$o['id_configurator_opt_group']][] = $o['opt_value'];
                }
            }
        }
        
        return $optFlatten;
    }
}