update ads

This commit is contained in:
Thibault GUILLAUME 2015-10-23 14:46:01 +02:00
parent cea9bc9efb
commit 3602dc25db
5 changed files with 263 additions and 186 deletions

View File

@ -129,6 +129,7 @@ define('_PS_TMP_IMG_DIR_', _PS_IMG_DIR_.'tmp/');
// CMS
define('_CMS_CAT_IMG_DIR_', _PS_IMG_DIR_.'cms_c/');
define('_CMS_POST_IMG_DIR_', _PS_IMG_DIR_.'cms_post/');
define('_ADS_IMG_DIR_', _PS_IMG_DIR_.'ads/');
/* settings php */
define('_PS_TRANS_PATTERN_', '(.*[^\\\\])');

View File

@ -1,7 +1,7 @@
<?php
if (!defined('_CAN_LOAD_FILES_')) {
exit;
exit;
}
require(dirname(__FILE__).'/classes/BlockAds.php');
@ -9,208 +9,214 @@ 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)
public function __construct()
{
$idTab = Tab::getIdFromClassName($tabClass);
if ($idTab != 0)
{
$tab = new Tab($idTab);
$tab->delete();
return true;
}
$this->name = 'blockadshooks';
$this->tab = 'other';
$this->version = '1.0';
$this->author = 'Antadis';
$this->need_instance = 0;
return true;
}
parent::__construct();
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':
return Context::getContext()->controller->categorycms->id;
break;
default: return null; break;
}
}
public function runByHook($hook_name)
{
$page_name = Dispatcher::getInstance()->getController();
return BlockAds::getContentByHook(
$hook_name,
$page_name,
new DateTime(),
$this->getIdByPageName($page_name),
Context::getContext()->language->id
);
}
public function hookDisplayBoxPartenaire1($params)
{
return $this->runByHook('displayBoxPartenaire1');
$this->displayName = $this->l('Block ads hooks');
$this->description = $this->l('Display ads in your website');
}
public function hookDisplayBoxPartenaire2($params)
public function install()
{
return $this->runByHook('displayBoxPartenaire2');
$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 hookDisplayBoxPartenaire3($params)
public function uninstall()
{
return $this->runByHook('displayBoxPartenaire3');
$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 function hookDisplayPubliciteTop($params)
public static function createTab($id_parent, $module, $name, $class_name, $status=True)
{
return $this->runByHook('displayPubliciteTop');
$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':
return Context::getContext()->controller->categorycms->id;
break;
default: return null; break;
}
}
public function hookDisplayHomeRight1($params)
public function runByHook($hook_name)
{
return $this->runByHook('displayHomeRight1');
$page_name = Dispatcher::getInstance()->getController();
$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');
return $this->runByHook('displayHomeRight2');
}
public function hookDisplayPubliciteRight($params)
{
return $this->runByHook('displayPubliciteRight');
return $this->runByHook('displayPubliciteRight');
}
}
?>

View File

@ -104,7 +104,7 @@ class BlockAds extends ObjectModel
$publications = $this->getPublications($page_name, $date, $id, $id_lang = null);
if(count($publications) > 0)
return $publications[0]->content;
return $publications[0];
return $this->content;
}
@ -124,7 +124,31 @@ class BlockAds extends ObjectModel
if(empty($date))
$date = new DateTime();
return $ads->getContent($page_name, $date, $id, $id_lang);
$publication = $ads->getContent($page_name, $date, $id, $id_lang);
if ($publication instanceOf Publication) {
if(!empty($publication->content)) {
return array(
'content' => $publication->content,
'id' => $publication->id,
'link' => false
);
} else {
if(!empty($publication->link)) {
return array(
'content' => $publication->link,
'id' => $publication->id,
'link' => true
);
}
}
}
return array(
'content' => $publication,
'id' => NULL,
'link' => false
);
}
}

View File

@ -10,6 +10,7 @@ class Publication extends ObjectModel
public $id_block_ads;
public $id_shop;
public $content;
public $link;
public $active;
public $date_from;
public $date_to;
@ -24,10 +25,11 @@ class Publication extends ObjectModel
'primary' => 'id_publication',
'multilang' => true,
'fields' => array(
'id_block_ads' => array('type' => self::TYPE_INT, 'validate' => 'isInt'),
'id_block_ads' => array('type' => self::TYPE_INT, 'validate' => 'isInt'),
'name' => array('type' => self::TYPE_STRING, 'validate' => 'isGenericName'),
'id_shop' => array('type' => self::TYPE_INT, 'validate' => 'isInt'),
'content' => array('type' => self::TYPE_NOTHING, 'lang' => true, 'validate' => 'isAnything'),
'link' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isAnything'),
'date_from' => array('type' => self::TYPE_DATE, 'validate' => 'isDateFormat'),
'date_to' => array('type' => self::TYPE_DATE, 'validate' => 'isDateFormat'),
'active' => array('type' => self::TYPE_INT, 'validate' => 'isInt'),
@ -36,6 +38,15 @@ class Publication extends ObjectModel
),
);
public function __construct($id = NULL, $id_lang = NULL, $id_shop = NULL)
{
parent::__construct($id, $id_lang, $id_shop);
$this->id_image = ($this->id && file_exists(_ADS_IMG_DIR_.(int)$this->id.'.jpg')) ? (int)$this->id : false;
$this->image_dir = _ADS_IMG_DIR_;
}
public function getInfos($page_name = null)
{
if(!Validate::isLoadedObject($this))
@ -141,7 +152,7 @@ class Publication extends ObjectModel
DELETE FROM '._DB_PREFIX_.'ads_publication_page
WHERE id_publication = '.$this->id);
$req = array();
$req = array();
foreach($pages as $page_name)
$req[] = array(
'id_publication' => $this->id,

View File

@ -11,6 +11,10 @@ class AdminAdsPublicationController extends ModuleAdminController {
$this->table = 'ads_publication';
$this->className = 'Publication';
$this->bootstrap = true;
$this->fieldImageSettings = array(
'name' => 'image',
'dir' => 'ads'
);
$this->addRowAction('edit');
$this->addRowAction('delete');
@ -87,6 +91,19 @@ class AdminAdsPublicationController extends ModuleAdminController {
}
public function renderForm()
{
if (!($obj = $this->loadObject(TRUE)))
return;
$image = '';
$image_url = '';
$image_size = '';
if(file_exists(_ADS_IMG_DIR_.$obj->id.'.jpg')) {
$image = _ADS_IMG_DIR_.$obj->id.'.jpg';
$image_url = ImageManager::thumbnail($image, $this->table.'_'.(int)$obj->id.'.'.$this->imageType, 350,
$this->imageType, true, true);
$image_size = file_exists($image) ? filesize($image) / 1000 : false;
}
$pages = Publication::getAllPages();
foreach($pages as &$page)
{
@ -147,6 +164,22 @@ class AdminAdsPublicationController extends ModuleAdminController {
'name' => 'content',
'lang' => true
),
array(
'type' => 'text',
'label' => $this->l('Lien de la publicité'),
'name' => 'link',
'lang' => true,
'desc' => $this->l('Si le champ contenu de la publicité est vide, on affiche l\'image avec le lien'),
),
array(
'type' => 'file',
'label' => $this->l('Image'),
'name' => 'image',
'display_image' => true,
'image' => $image_url ? $image_url : false,
'size' => $image_size,
'delete_url' => self::$currentIndex.'&'.$this->identifier.'='.$this->object->id.'&token='.$this->token.'&deleteImage=1',
),
array(
'type' => 'datetime',
'label' => $this->l('Date de début'),
@ -195,7 +228,7 @@ class AdminAdsPublicationController extends ModuleAdminController {
),
)
);
);
$tpl = $this->createTemplate('input-ads.tpl');
$id_ads = Tools::getValue('id_default_ads', $this->object->id_block_ads);
@ -207,6 +240,8 @@ class AdminAdsPublicationController extends ModuleAdminController {
'ads' => $ads
));
$this->fields_value['ads'] = $tpl->fetch();
$this->fields_value['image'] = $image ? $image : FALSE;
$this->fields_value['size'] = $image ? filesize(_ADS_IMG_DIR_.'/'.$obj->id.'.jpg') / 1000 : FALSE;
return parent::renderForm();
}