255 lines
8.3 KiB
PHP

<?php
require_once(dirname(__FILE__).'/../../blockadshooks.php');
class AdminAdsPublicationController extends ModuleAdminController {
public function __construct()
{
$this->identifier = 'id_publication';
$this->lang = true;
$this->table = 'ads_publication';
$this->className = 'Publication';
$this->bootstrap = true;
$this->addRowAction('edit');
$this->addRowAction('delete');
$this->_group .= ' GROUP BY app.id_publication';
$this->_select .= ' GROUP_CONCAT(app.page_name) as page_list';
$this->_join .= 'LEFT JOIN `'._DB_PREFIX_.'ads_publication_page` app ON (app.`id_publication` = a.`id_publication`)';
parent::__construct();
}
public function setMedia()
{
parent::setMedia();
$this->addJS(_PS_MODULE_DIR_.'blockadshooks/js/admin.js');
}
public function renderList()
{
$this->fields_list = array(
'id_publication' => array(
'title' => 'ID',
'width' => 25,
'class' => 'text-center',
),
'date_from' => array(
'title' => $this->l('Date début'),
),
'date_to' => array(
'title' => $this->l('Date fin'),
),
'active' => array(
'title' => $this->l('Actif'),
'align' => 'text-center',
'active' => 'status',
'type' => 'bool',
'orderby' => false,
'filter_key' => 'a!active'
),
'page_list' => array(
'title' => 'list des pages',
'callback' => 'cleanPageList'
)
);
return parent::renderList();
}
public function cleanPageList($line, $value)
{
$array = explode(',', $line);
$content = '';
foreach($array as $arr)
$content .= (!empty($content) ? '<br />' : '').Publication::getPageNameTranslated($arr);
return $content;
}
public function processSave()
{
parent::processSave();
if (empty($this->errors))
{
$pages = Tools::getValue('pages');
$this->object->setPages($pages);
foreach($pages as $page)
{
$this->object->setPageInfo($page, Tools::getValue('input-'.$page.'', array()));
}
}
}
public function renderForm()
{
$pages = Publication::getAllPages();
foreach($pages as &$page)
{
$page = array(
'id' => $page,
'name' => Publication::getPageNameTranslated($page)
);
}
if(Validate::isLoadedObject($this->object))
{
$this->fields_value['pages[]'] = $this->object->getSelectedPages();
}
$this->fields_form = array(
'multilang' => true,
'tinymce' => true,
'legend' => array(
'title' => $this->l('Ads'),
),
'submit' => array(
'title' => $this->l('Save'),
),
'input' => array(
array(
'type' => 'free',
'name' => 'ads',
'label' => $this->l('Emplacement publicitaire')
),
array(
'type' => 'switch',
'name' => 'active',
'label' => $this->l('Activé'),
'class' => 't',
'is_bool' => true,
'values' => array(
array(
'id' => 'active_on',
'value' => 1,
'label' => $this->l('Enabled')
),
array(
'id' => 'active_off',
'value' => 0,
'label' => $this->l('Disabled')
)
),
),
array(
'type' => 'textarea',
'label' => $this->l('Contenu de la publicité'),
'name' => 'content',
'lang' => true
),
array(
'type' => 'datetime',
'label' => $this->l('Date de début'),
'name' => 'date_from',
),
array(
'type' => 'datetime',
'label' => $this->l('Date de fin'),
'name' => 'date_to',
),
array(
'type' => 'select',
'multiple' => true,
'label' => $this->l('Liste des pages'),
'name' => 'pages[]',
'id' => 'pages',
'options' => array(
'query' => $pages,
'id' => 'id',
'name' => 'name'
)
),
array(
'type' => 'relation_posts_cms',
'name' => 'input-postcms',
'label' => $this->l('Sélectionner des articles'),
'id' => 'postcms',
'query' => $this->object->getInfosWithValues('postcms'),
'form_group_class' => 'form-group-page form-group-postcms',
),
array(
'type' => 'relation_posts_edito',
'name' => 'input-postedito',
'label' => $this->l('Sélectionner des éditos'),
'id' => 'postedito',
'query' => $this->object->getInfosWithValues('postedito'),
'form_group_class' => 'form-group-page form-group-postedito',
),
array(
'type' => 'relation_category_cms',
'name' => 'input-categorycms',
'label' => $this->l("Sélectionner les catégories d article"),
'id' => 'categorycms',
'query' => $this->object->getInfosWithValues('categorycms'),
'form_group_class' => 'form-group-page form-group-categorycms',
),
)
);
$tpl = $this->createTemplate('input-ads.tpl');
$id_ads = Tools::getValue('id_default_ads', $this->object->id_block_ads);
$ads = null;
if(!empty($id_ads))
$ads = new BlockAds($id_ads, Context::getContext()->language->id);
$tpl->assign(array(
'ads' => $ads
));
$this->fields_value['ads'] = $tpl->fetch();
return parent::renderForm();
}
public function delete()
{
$id = $this->id;
$res = parent::delete();
if($res)
{
Db::getInstance()->delete('ads_publication_page', 'id_publication = '.$id);
Db::getInstance()->delete('ads_publication_other', 'id_publication = '.$id);
}
return $res;
}
public function displayEditLink($token = null, $id, $name = null)
{
$tpl = $this->createTemplate('helpers/list/list_action_edit.tpl');
$tpl->assign(array(
'href' => Context::getContext()->link->getAdminLink('AdminAdsPublication', true).'&'.$this->identifier.'='.$id.'&update'.$this->table,
'action' => $this->l('Modifier'),
));
return $tpl->fetch();
}
public function displayDeleteLink($token = null, $id, $name = null)
{
$tpl = $this->createTemplate('helpers/list/list_action_delete.tpl');
$tpl->assign(array(
'href' => Context::getContext()->link->getAdminLink('AdminAdsPublication', true).'&'.$this->identifier.'='.$id.'&delete'.$this->table,
'action' => $this->l('Supprimer'),
));
return $tpl->fetch();
}
public function setHelperDisplay(Helper $helper)
{
$helper = parent::setHelperDisplay($helper);
return $helper;
}
public function listByAds( $ads_id = array() )
{
if(count($ads_id) == 0)
return;
$this->_where .= ' AND a.id_block_ads IN ('.implode(',', $ads_id).')';
}
}