toutpratique/modules/blockadshooks/blockadshooks.php
2015-12-14 16:07:18 +01:00

257 lines
6.9 KiB
PHP

<?php
if (!defined('_CAN_LOAD_FILES_')) {
exit;
}
require(dirname(__FILE__).'/classes/BlockAds.php');
require(dirname(__FILE__).'/classes/Publication.php');
class Blockadshooks extends Module
{
public function __construct()
{
$this->name = 'blockadshooks';
$this->tab = 'other';
$this->version = '1.0';
$this->author = 'Antadis';
$this->need_instance = 0;
parent::__construct();
$this->displayName = $this->l('Block ads hooks');
$this->description = $this->l('Display ads in your website');
}
public function install()
{
$return = true;
include(dirname(__FILE__).'/sql/sql-install.php');
foreach ($sql as $s)
{
if (!Db::getInstance()->execute($s))
$return=false;
}
if($return === true)
$this->createAds();
return $return
&& parent::install()
&& $this->createAdminTabs()
&& $this->registerHook('displayBoxPartenaire1')
&& $this->registerHook('displayBoxPartenaire2')
&& $this->registerHook('displayBoxPartenaire3');
}
public function createAds()
{
$adsArray = array(
array(
'hook_name' => 'displayBoxPartenaire1',
'label' => 'Pub partenaire 1',
'width' => '250',
'height' => '250',
),
array(
'hook_name' => 'displayBoxPartenaire2',
'label' => 'Pub partenaire 2',
'width' => '250',
'height' => '250',
),
array(
'hook_name' => 'displayBoxPartenaire3',
'label' => 'Pub partenaire 3',
'width' => '250',
'height' => '250',
),
);
foreach($adsArray as $adsTmp)
{
$ads = new BlockAds();
foreach($adsTmp as $k => $v)
$ads->$k = $v;
$ads->save();
}
}
public function uninstall()
{
$return = true;
include(dirname(__FILE__).'/sql/sql-uninstall.php');
foreach ($sqlu as $s)
{
if (!Db::getInstance()->execute($s))
$return=false;
}
return $return
&& parent::uninstall()
&& self::uninstallTab('AdminAds')
&& self::uninstallTab('AdminAdsPublication');
}
public static function uninstallTab($tabClass)
{
$idTab = Tab::getIdFromClassName($tabClass);
if ($idTab != 0)
{
$tab = new Tab($idTab);
$tab->delete();
return true;
}
return true;
}
public static function createTab($id_parent, $module, $name, $class_name, $status=True)
{
$tab = new Tab();
$tab->module = $module;
foreach (Language::getLanguages(True) as $languages)
{
$tab->name[$languages['id_lang']] = $name;
}
$tab->id_parent = $id_parent;
$tab->class_name = $class_name;
$tab->active = $status;
$r = $tab->save();
if ($r === False)
return False;
return $tab->id;
}
private function createAdminTabs()
{
$parent_tab = Db::getInstance()->getValue('SELECT id_tab FROM '._DB_PREFIX_.'tab WHERE id_parent = 0 AND class_name = "AdminCmsPs"');
if(!$parent_tab)
$parent_tab = self::createTab(0, $this->name, $this->l('Gestion des publicités'), 'AdminAds');
return (
!!self::createTab($parent_tab, $this->name, $this->l('Gestion des emplacements publicitaires'), 'AdminAds')
&& !!self::createTab($parent_tab, $this->name, $this->l('Gestion des publicités'), 'AdminAdsPublication')
);
}
public function getContent()
{
Tools::redirectAdmin($this->context->link->getAdminLink('AdminAds'));
}
public function getIdByPageName($page_name)
{
switch($page_name)
{
case 'postcms':
return Context::getContext()->controller->postcms->id;
break;
case 'postedito':
return Context::getContext()->controller->postedito->id;
break;
case 'categorycms':
$root_category = CmsPsCategory::getRootCategory(Context::getContext()->controller->categorycms->id);
return (int)$root_category->id;
break;
default: return null; break;
}
}
public function runByHook($hook_name)
{
$page_name = Dispatcher::getInstance()->getController();
if ($page_name == 'category'
|| $page_name == 'product'
|| $page_name == 'order'
|| $page_name == 'manufacturer'
) {
$page_name = 'homestore';
}
if ($page_name == 'videos'
|| $page_name == 'authentication'
|| $page_name == 'search'
|| $page_name == 'cms') {
$page_name = 'index';
}
$publicite = BlockAds::getContentByHook(
$hook_name,
$page_name,
new DateTime(),
$this->getIdByPageName($page_name),
Context::getContext()->language->id
);
if ($publicite['link']) {
$this->smarty->assign(array(
'link' => $publicite['content'],
'id' => $publicite['id']
));
return $this->display(__FILE__, 'ads.tpl');
} else {
return $publicite['content'];
}
}
public function hookDisplayBoxPartenaire1($params)
{
return $this->runByHook('displayBoxPartenaire1');
}
public function hookDisplayBoxPartenaire2($params)
{
return $this->runByHook('displayBoxPartenaire2');
}
public function hookDisplayBoxPartenaire3($params)
{
return $this->runByHook('displayBoxPartenaire3');
}
public function hookDisplayPubliciteTop($params)
{
return $this->runByHook('displayPubliciteTop');
}
public function hookDisplayHomeRight1($params)
{
return $this->runByHook('displayHomeRight1');
}
public function hookDisplayHomeRight2($params)
{
return $this->runByHook('displayHomeRight2');
}
public function hookDisplayPubliciteRight($params)
{
return $this->runByHook('displayPubliciteRight');
}
public function hookDisplayPubliciteRightBottom($params)
{
return $this->runByHook('displayPubliciteRightBottom');
}
public function hookDisplayPubliciteBottom($params)
{
return $this->runByHook('displayPubliciteBottom');
}
public function hookDisplayCmsPostLeft($params)
{
return $this->runByHook('displayCmsPostLeft');
}
}