<?php

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

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

class AdvBlockLink2 extends Module
{
    public function __construct()
    {
        $this->name = 'advblocklink2';
        $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('Bloc liens avancé 2');
        $this->description = $this->l('Gestion des blocs de liens du footer');
    }

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

        $sql[] = 
            'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'advblocklink2` (
                `id_link` int(10) unsigned NOT NULL auto_increment,
                `id_parent` varchar(255) NOT NULL,
                `position` INT(11) UNSIGNED NOT NULL default 0,
                `external` INT(3) NOT NULL,
                `active` INT(3) NOT NULL,
                PRIMARY KEY  (`id_link`)
            ) ENGINE=' . _MYSQL_ENGINE_ . ' DEFAULT CHARSET=utf8';
            
        $sql[] = 
            'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'advblocklink2_shop` ( 
                `id_link` int(10) unsigned NOT NULL auto_increment,
                `id_shop` int(10) unsigned NOT NULL,
                PRIMARY KEY (`id_link`, `id_shop`)
            ) ENGINE=' . _MYSQL_ENGINE_ . ' DEFAULT CHARSET=utf8';
            
        $sql[] = 
            'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'advblocklink2_lang` (
                `id_link` int(10) unsigned NOT NULL,
                `id_lang` int(10) unsigned NOT NULL,
                `title` varchar(255) NOT NULL,
                `url` varchar(255),
              PRIMARY KEY (`id_link`,`id_lang`)
            ) ENGINE=' . _MYSQL_ENGINE_ . ' DEFAULT CHARSET=utf8';

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

        AdvLink2::refreshPositionsAll();

        $new_tab = new Tab();
        $new_tab->class_name = 'AdminAdvBlockLink2';
        $new_tab->id_parent = Tab::getCurrentParentId();
        $new_tab->module = $this->name;
        $languages = Language::getLanguages();
        foreach ($languages as $language) {
            $new_tab->name[$language['id_lang']] = 'Liens du footer 2';
        }

        $new_tab->add();
        
        return parent::install() && $this->registerHook('displayFooterBelow') && $this->registerHook('actionRefreshBLocklink');
    }

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

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

    public function hookDisplayFooter($params)
    {
        if (!$this->isCached('advblocklink2.tpl', $this->getCacheId())) {
            $blockLinks = AdvLink2::getBlockLinks();
            if (!$blockLinks) {
                return false;
            }
            $this->smarty->assign('blockLinks', $blockLinks);
        }
        return $this->display(__FILE__, 'advblocklink2.tpl', $this->getCacheId());
    }
    public function hookDisplayFooterBelow($params)
    {
        if (!$this->isCached('advblocklink2.tpl', $this->getCacheId())) {
            $blockLinks = AdvLink2::getBlockLinks();
            if (!$blockLinks) {
                return false;
            }
            $this->smarty->assign('blockLinks', $blockLinks);
        }
        return $this->display(__FILE__, 'advblocklink2.tpl', $this->getCacheId());
    }

    public function hookActionRefreshBLocklink()
    {
        $this->_clearCache('advblocklink2.tpl');
    }

}