<?php
abstract class ControllerBaseScripts extends Controller
{
    protected $referencePathFile;

    protected function index()
    {
        if ($this->config->get('config_template') == Type_Theme::getNickFromValue(Type_Theme::Tp_appmobile)) {
            return false;
        }

        $this->load->dmodel('design/layout');
        $this->load->model('catalog/category');
        $this->load->model('catalog/product');
        $this->load->model('catalog/information');
        $this->load->dmodel('module/tracking_code');

        if (isset($this->request->get['route'])) {
            $route = (string) $this->request->get['route'];
        } else {
            $route = 'common/home';
        }

        $layout_id = 0;

        if ($route == 'product/category' && isset($this->request->get['path'])) {
            $path = explode('_', (string)$this->request->get['path']);

            $layout_id = $this->config->get('config_layout_category');
        }

        if ($route == 'product/product' && isset($this->request->get['product_id'])) {
            $layout_id = $this->model_catalog_product->getProductLayoutId($this->request->get['product_id']);
        }

        if ($route == 'information/information' && isset($this->request->get['information_id'])) {
            $layout_id = $this->model_catalog_information->getInformationLayoutId($this->request->get['information_id']);
        }

        if (!$layout_id) {
            $layout_id = $this->dmodel_design_layout->getLayoutRoute($route);
        }

        if (!$layout_id) {
            $layout_id = $this->config->get('config_layout_id');
        }

        $default = $this->dmodel_design_layout->getLayoutByName('Padrão');

        $layout_id = [$layout_id, $default['layout_id']];

        $scripts = $this->dmodel_module_tracking_code->getListEnabled();

        $scripts = array_filter($scripts, function ($script) use ($layout_id) {
            return ($script['all_layouts'] == Type_Status::Tp_Habilitado || in_array($script['layout_id'], $layout_id)) &&
                $script['position'] == $this->position &&
                $script['status'] == Type_Status::Tp_Habilitado;
        });

        $this->data['scripts'] = array_map(function($code) {
            $code['script'] = htmlspecialchars_decode($code['script']);
            return $code;
        }, $scripts);

        $this->render($this->referencePathFile);
    }
}
