<?php

loadcontroller('base/module');

class ControllerModuleLatest extends ControllerBaseModule
{

    protected $referencePathFile = 'module/latest';

    protected $arrayJson = ['latest_module'];

    protected function loadModel()
    {
        parent::loadModel();
    }

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

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

        $this->dataVue['layouts'] = $this->dmodel_design_layout->getLayouts();
    }

    public function validate()
    {
        $this->loadLanguage();

        $errors = [];
        $latestModuleHabilitado = [];

        $lpbcModule =  isset($this->request->post['latest_module']) ? $this->request->post['latest_module'] : [];

        if (isset($lpbcModule) && !empty($lpbcModule)) {
            foreach ($lpbcModule as $key => $value) {
                if ($value['status'] == Type_Status::Tp_Habilitado) {
                    array_push($latestModuleHabilitado, $value);
                }

                $module_errors = $this->validate->execute([
                    'latest_module[' . $key . '][limit]' => [
                        'value' => $value['limit'],
                        'validates' => ['isEmpty'],
                    ],
                ]);

                $errors = array_merge($errors, $module_errors);
            }
        }

        return $errors;
    }

    public function save()
    {
        parent::save();
        $this->session->data['success'] = null;
    }

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

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