<?php
$loader = $this->registry->get('load');
$loader->controller('product/productList');

class ControllerModuleProdutosrelacionados extends ControllerProductProductList
{

    public function index($setting = null)
    {
        $this->language->load('module/produtosrelacionados');

        if ($setting['layout_id'] == Type_IdLayout::Tp_IdProduct) {
            $resultCategory = $this->db->query("SELECT category_id FROM " . DB_PREFIX . "product  WHERE product_id = '" . (int)$this->request->get['product_id'] . "' ");
            $resultProductName = $this->db->query("SELECT name FROM " . DB_PREFIX . "product_description  WHERE product_id = '" . (int)$this->request->get['product_id'] . "' ");
            $resultGridID = $this->db->query("SELECT product_grid_id FROM oc_product_grid_detail WHERE product_id = '" . (int)$this->request->get['product_id'] . "' ");
            //validação caso o produto não tenha categoria o modulo não será exibido
            if (empty($resultCategory->row)) {
                $setting['category_id'] = false;
            } else {
                $setting['category_id'] = $resultCategory->row['category_id'];
            }
            if (empty($resultProductName->row)) {
                return false;
            }
            if (empty($resultGridID->row)) {
                $setting['product_grid_id'] = false;
            } else {
                $setting['product_grid_id'] = $resultGridID->row['product_grid_id'];
            }

            $setting['product_name'] = $resultProductName->row['name'];

        }

        $this->data['heading_title'] = ($this->config->get('produtosrelacionados_title')) ? $this->config->get('produtosrelacionados_title') : $this->language->get('heading_title');
        $this->data['text_see_all'] = $this->language->get('text_see_all');
        $this->data['category_id'] = $setting['category_id'];
        $this->data['product_name'] = base64_encode($setting['product_name']);
        $this->data['product_id'] = $this->request->get['product_id'];
        $this->data['product_grid_id'] = $setting['product_grid_id'];

        $this->data['productbox_size'] = $this->config->get('wg24_productbox_size');

        $this->data['grid'] = (isset($setting['entry_type'])) ? $setting['entry_type'] : 0;
        $this->data['link'] = $this->url->link('product/modules_in_page', '&name=' . str_replace('.php', '', basename(__FILE__)), 'SSL');
        $this->data['product_list_title_style'] =  $this->config->get('product_list_title_style');

        $this->childrenData = array(
            'product/productList/moduleprodutosrelacionados' => $setting
        );

        $this->render('module/produtosrelacionados');
    }

}
