<?php

class ControllerCommonContentBottom extends Controller
{
    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');

        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'])) {
            $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');
        }

        $module_data = array();

        $this->load->model('setting/extension');

        $extensions = $this->model_setting_extension->getExtensions('module');

        foreach ($extensions as $extension) {
            if ($extension['code'] == 'lpbc' && $this->config->get('lpbc_module_interests_status') && $this->customer->isLogged() && $this->customer->getInterestsArea($this->customer->getId())) {
                $modules = $this->customer->getInterestsArea($this->customer->getId());
                if ($this->config->get('lpbc_module_interests_limit')) {
                    $modules = array_slice($modules, 0, $this->config->get('lpbc_module_interests_limit'));
                }
            } elseif ($extension['code'] == 'featured') {
                // Retrocompatibilidade
                $featuredModule = $this->config->get($extension['code'] . '_module');
                $modules = [];
                if ($featuredModule) {
                    foreach ($featuredModule as $featuredKey => $featured) {
                        if (isset($featured['featured_module'])) {
                            foreach ($featured['featured_module'] as $featured_module_key => $featured_module) {
                                $featured_module['featured_title'] = $featured['featured_title'];
                                $featured_module['featured_index'] = $featuredKey;
                                $featured_module['featured_product'] = '';
                                if (!empty($featured['featured_product'])) {
                                    $featured_module['featured_product'] = implode(',', $featured['featured_product']);
                                }
                                $modules[] = $featured_module;
                            }
                        } else {
                            $modules[] = $featured;
                        }
                    }
                }
            } else {
                $modules = $this->config->get($extension['code'] . '_module');
            }

            if ($modules) {
                foreach ($modules as $module) {
                    if (isset($module['layout_id']) && $module['layout_id'] == $layout_id && $module['position'] == 'content_bottom' && $module['status']) {
                        $module_data[] = array(
                            'code' => $extension['code'],
                            'setting' => $module,
                            'sort_order' => $module['sort_order']
                        );
                    }
                }
            }
        }

        $sort_order = array();

        foreach ($module_data as $key => $value) {
            $sort_order[$key] = $value['sort_order'];
        }

        array_multisort($sort_order, SORT_ASC, $module_data);

        $this->data['modules'] = array();

        foreach ($module_data as $module) {
            $module = $this->getChild('module/' . $module['code'], $module['setting']);

            if ($module) {
                $this->data['modules'][] = $module;
            }
        }

        $this->render('common/content_bottom');
    }
}
