<?php

if (!defined('_CAN_LOAD_FILES_'))
    exit;

include_once(dirname(__FILE__) . '/classes/AdvSiteReview.php');

class AdvSiteReviews extends Module
{
    public function __construct()
    {
        $this->name = 'advsitereviews';
        $this->tab = 'front_office_features';
        $this->version = '1.0';
        $this->author = 'Antadis';
        $this->need_instance = 0;

        $this->bootstrap = true;
        parent::__construct();

        $this->displayName = $this->l('Commentaires boutique');
        $this->description = $this->l('Gestion des commentaires boutique');
    }

    public function install()
    {
        $sql = array();

        $sql[] = 
            'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'advsitereviews` (
              `id_comment` int(10) unsigned NOT NULL auto_increment,
              `position` INT(11) UNSIGNED NOT NULL default 0,
              `rank` INT(11) UNSIGNED NOT NULL default 0,
              `active` INT(11) UNSIGNED NOT NULL default 1,
              PRIMARY KEY  (`id_comment`)
            ) ENGINE=' . _MYSQL_ENGINE_ . ' DEFAULT CHARSET=utf8';
            
        $sql[] = 
            'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'advsitereviews_lang` (
              `id_comment` int(10) unsigned NOT NULL,
              `id_lang` int(10) unsigned NOT NULL,
              `firstname` varchar(255) NOT NULL,
              `lastname` varchar(255) NOT NULL,
              `content` TEXT,
              `title` varchar(255) NOT NULL,
              `place` varchar(255) NOT NULL,
              `date` varchar(255) NOT NULL,
              PRIMARY KEY (`id_comment`,`id_lang`)
            ) ENGINE=' . _MYSQL_ENGINE_ . ' DEFAULT CHARSET=utf8';

        $sql[] = 
            'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'advsitereviews_shop` (
              `id_comment` int(10) unsigned NOT NULL auto_increment,
              `id_shop` int(10) unsigned NOT NULL,
              PRIMARY KEY (`id_comment`, `id_shop`)
            ) ENGINE=' . _MYSQL_ENGINE_ . ' DEFAULT CHARSET=utf8';
            

        foreach ($sql as $_sql) {
            Db::getInstance()->Execute($_sql);
        }

        $tab = new Tab();
        $tab->class_name = 'AdminAdvSiteReviews';
        $tab->id_parent = Tab::getCurrentParentId();
        $tab->module = $this->name;
        $languages = Language::getLanguages();
        foreach ($languages as $language) {
            $tab->name[$language['id_lang']] = 'Commentaires boutique';
        }

        return parent::install() && $tab->add() && $this->registerHook('displaySiteReviews') && $this->registerHook('displayHeader') && $this->registerHook('actionRefreshReviews');
    }

    public function uninstall() 
    {
        $sql = 'DROP TABLE IF EXISTS
            `' . _DB_PREFIX_ . 'advsitereviews_lang`,
            `' . _DB_PREFIX_ . 'advsitereviews_shop`,
            `' . _DB_PREFIX_ . 'advsitereviews`
        ';
        
        Db::getInstance()->Execute($sql);

        $idTab = Tab::getIdFromClassName('AdminAdvSiteReviews');
        if ($idTab) {
            $tab = new Tab($idTab);
            $tab->delete();
        }
        
        return parent::uninstall();
    }

    public function hookDisplaySiteReviews($params)
    {
        if (!$this->isCached('advsitereviews.tpl', $this->getCacheId()))
        {
            $reviews = AdvSiteReview::getReviews();

            if (!$reviews) {
                return false;
            }

            $this->smarty->assign('reviews', $reviews);
        }

        return $this->display(__FILE__, 'advsitereviews.tpl', $this->getCacheId());
    }
    
    public function hookHeader($params)
    {
        $jsFiles =  $this->context->controller->js_files;
        foreach($jsFiles as $jsFile)
        {
            if(strpos($jsFile, 'owl') !== false)
            {
                return false;
            }
        }

        $this->context->controller->addJS($this->_path.'js/owl.js');
    }

    public function hookActionRefreshReviews()
    {
        $this->_clearCache('advsitereviews'); 
    }

}