<?php
class ControllerCommonColumnRefinar extends Controller {
	protected function index() {
    $this->data['filter_active'] = (!empty($this->request->get['filbrand']) || !empty($this->request->get['price_initial']) || !empty($this->request->get['price_laste']) || !empty($this->request->get['filcolor']) || !empty($this->request->get['filsize']) || !empty($this->request->get['filspecification'])) ? 1 : 0;

		$this->document->addScriptApp(array(
			'catalog/view/theme/appmobile/js/module.filters.js'
    ));

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

		$this->data['t_filters'] = $this->language->get('t_filters');
		$this->data['t_clean_all'] = $this->language->get('t_clean_all');

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

		$arrayModules = array (
			'futuragrade', 'futurafiltrovalor', 'futurafiltromarca', 'fidelity'
		);

		foreach ($extensions as $extension) {
      if ($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['status']) && (in_array($extension['code'], $arrayModules)) && !in_array($extension['code'] ,array_column($module_data, 'code'))) {
						$module_data[] = array(
							'code'       => $extension['code'],
							'setting'    => $module,
							'sort_order' => $module['sort_order']
						);
						$this->config->set('exist_content_right', true);
					}
				}
			}
		}

		$urlGet = "";
		/* CÓDIGO P/ FICAR COMPATIVEL COM A URL AMIGAVEL */

		if (isset($this->request->get['search'])) {
			if($urlGet == ''){
				$urlGet .= 'search=' . $this->request->get['search'];
			}else{
				$urlGet .= '&search=' . $this->request->get['search'];
			}
		}

		if (isset($this->request->get['tag'])) {
			if($urlGet == ''){
				$urlGet .= 'tag=' . $this->request->get['tag'];
			}else{
				$urlGet .= '&tag=' . $this->request->get['tag'];
			}
		}

		if (isset($this->request->get['description'])) {
			if($urlGet == ''){
				$urlGet .= 'description=' . $this->request->get['description'];
			}else{
				$urlGet .= '&description=' . $this->request->get['description'];
			}
		}

		if (isset($this->request->get['category_id'])) {
			if($urlGet == ''){
				$urlGet .= 'category_id=' . $this->request->get['category_id'];
			}else{
				$urlGet .= '&category_id=' . $this->request->get['category_id'];
			}
		}

		if (isset($this->request->get['sub_category'])) {
			if($urlGet == ''){
				$urlGet .= 'sub_category=' . $this->request->get['sub_category'];
			}else{
				$urlGet .= '&sub_category=' . $this->request->get['sub_category'];
			}
		}

		if (isset($this->request->get['sort'])) {
			if($urlGet == ''){
				$urlGet .= 'sort=' . $this->request->get['sort'];
			}else{
				$urlGet .= '&sort=' . $this->request->get['sort'];
			}
		}

		if (isset($this->request->get['order'])) {
			if($urlGet == ''){
				$urlGet .= 'order=' . $this->request->get['order'];
			}else{
				$urlGet .= '&order=' . $this->request->get['order'];
			}
		}

		if (isset($this->request->get['limit'])) {
			if($urlGet == ''){
				$urlGet .= 'limit=' . $this->request->get['limit'];
			}else{
				$urlGet .= '&limit=' . $this->request->get['limit'];
			}
		}


		if (isset($this->request->get['path'])) {
			if($urlGet == ''){
				$urlGet .= 'path=' . $this->request->get['path'];
			}else{
				$urlGet .= '&path=' . $this->request->get['path'];
			}
		}

		if (isset($this->request->get['manufacturer_id'])) {
			if($urlGet == ''){
				$urlGet .= 'manufacturer_id=' . $this->request->get['manufacturer_id'];
			}else{
				$urlGet .= '&manufacturer_id=' . $this->request->get['manufacturer_id'];
			}
		}

		//variavel criada para alterar o link de apagar dos filtros
		$remove = '';
		if (isset($this->request->get['route'])) {

			$this->data['linkclearall'] = $this->url->link($this->request->get['route'], $urlGet);

			if($urlGet == ''){
				$urlGet .= 'route2=' . $this->request->get['route'];
				$remove= 'route2='.$this->request->get['route'];
			}else{
				$urlGet .= '&route2=' . $this->request->get['route'];
				$remove= '&route2='.$this->request->get['route'];
			}
		}

		$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) {
			if ( $module['code'] == 'category' && (isset($this->request->get['path']) || isset($this->request->get['category_id'])) ) {

				if (isset($this->request->get['path'])){
					$subcategory = $this->request->get['path'] ;
				}else if(isset($this->request->get['category_id'])){
					$subcategory = $this->request->get['category_id'] ;
				}

				$mod = $this->getChild('module/' . $module['code'], array_merge($module['setting'],['subcategory' => $subcategory]) );
			} else if($module['code'] != 'category') {
				$mod = $this->getChild('module/' . $module['code'], $module['setting']);
			}

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

		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/common/column_refinar.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/common/column_refinar.tpl';
		} else {
			$this->template = 'default/template/common/column_refinar.tpl';
		}

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