<?php

loadcontroller('base/module');

class ControllerModuleBoughtAlso extends ControllerBaseModule {

    protected $referencePathFile = 'module/boughtalso';

    protected function loadModel()
    {
        parent::loadModel();
        $this->load->dmodel('design/layout');
    }

    protected function loadData()
    {
        parent::loadData();

        $this->dataVue['TypesForJs'] = array (
            Type_Status::getTypeForJs(),
            Type_ModulePosition::getTypeForJs(),
            Type_IdLayout::getTypeForJs()
        );
    }

    protected function validate()
    {

        $this->loadLanguage();

        $this->load->helper('ftr_geral');
        $this->load->helper('ftr_validacao');

        extract($this->request->post);

        foreach ($boughtalso_module as $key => $module){
            $errors = $this->validate->execute([
                'boughtaslo_module['.$key.'][limit]' => [
                    'value' => $module['limit'],
                    'validates' => [
                        'isEmpty',
                        'isIntegerNumber',
                        [
                            'method' => 'isMinMaxNumber',
                            'min' => 1,
                            'max' => 24
                        ]
                    ],
                    'condition' => 'boughtaslo_module['.$key.'][status]'
                ]
            ]);
        }

        return $errors;

    }

    protected function saveSetting(){
        if (isset($this->request->post['boughtalso_module'])) {
            foreach ($this->request->post['boughtalso_module'] as $key => $value) {
                //Forçando salvar o layoutid apenas para a tela de produtos
                $this->request->post['boughtalso_module'][$key]['layout_id'] = Type_IdLayout::Tp_IdProduct;
            }
        }
        parent::saveSetting();
    }

    public function install()
    {
        $this->load->model('setting/extension');
        $this->model_setting_extension->install('module', 'boughtalso');
    }

    public function uninstall()
    {
        $this->load->model('setting/extension');
        $this->model_setting_extension->uninstallSetting('module', 'boughtalso');
    }


}
