toutpratique/modules/cookiesinfos/cookiesinfos.php
2016-08-09 15:52:19 +02:00

182 lines
6.3 KiB
PHP

<?php
/**
*
* @author Antadis <contact@antadis.com>
* @copyright 2013-2014 Antadis
* @version 1.0
*/
if (!defined('_PS_VERSION_'))
exit;
class CookiesInfos extends Module
{
public function __construct()
{
$this->name = 'cookiesinfos';
$this->tab = 'font_office_featues';
$this->version = '1.0';
$this->author = 'Antadis';
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->l('Cookies Informatiosn');
$this->description = $this->l('Display a cookies informations top bar with link to redirect where you want');
if (!Configuration::get('COOKIESINFO_LINK') ||
!Configuration::get('COOKIESINFO_INTRO'))
$this->warning = $this->l('Wrong settings');
}
public function install()
{
if(!parent::install() ||
!$this->registerHook('displayCookie') ||
!$this->registerHook('displayHeader') ||
!Configuration::updateValue('COOKIESINFO_LINK', '') ||
!Configuration::updateValue('COOKIESINFO_INTRO', ''))
return false;
return true;
}
public function uninstall()
{
if (!parent::uninstall() ||
!Configuration::updateValue('COOKIESINFO_LINK', '') ||
!Configuration::updateValue('COOKIESINFO_INTRO', ''))
return false;
return true;
}
public function getContent()
{
$output = '';
if (Tools::isSubmit('submit' . $this->name)) {
$link = array();
$text = array();
$languages = Language::getLanguages(FALSE);
foreach ($languages as $key => $lang) {
$link[(int) $lang['id_lang']] = Tools::getValue('COOKIESINFO_LINK_'. (int)$lang['id_lang']);
$text[(int) $lang['id_lang']] = Tools::getValue('COOKIESINFO_INTRO_'. (int)$lang['id_lang']);
}
if ( Configuration::updateValue('COOKIESINFO_LINK', $link)
&& Configuration::updateValue('COOKIESINFO_INTRO', $text) ) {
$output .= $this->displayConfirmation($this->l('Settings updated'));
}
}
return $output . $this->displayForm();
}
public function displayForm()
{
// Get default Language
$default_lang = (int) Configuration::get('PS_LANG_DEFAULT');
//Init Fields form array
$fields_form[0]['form'] = array(
'legend' => array(
'title' => $this->l('Cookies disclaimer Settings')
),
'input' => array(
array(
'type' => 'text',
'label' => $this->l('Intro'),
'name' => 'COOKIESINFO_INTRO',
'size' => 100,
'required' => true,
'lang' => true,
'desc' => $this->l('The introduction text that displayed on top of the front office')
),
array(
'type' => 'text',
'label' => $this->l('Link'),
'name' => 'COOKIESINFO_LINK',
'size' => 50,
'required' => true,
'lang' => true,
'desc' => $this->l('Link to redirect when clicking on cookies top information bar on front office')
)
),
'submit' => array(
'title' => $this->l('Save'),
'class' => 'button btn btn-default pull-right'
)
);
$languages = Language::getLanguages(FALSE);
foreach ($languages as $key => $lang) {
if ($lang['id_lang'] == Context::getContext()->language->id) {
$languages[$key]['is_default'] = true;
} else {
$languages[$key]['is_default'] = false;
}
}
$helper = new HelperForm();
//Module, Token and currentIndex
$helper->module = $this;
$helper->name_controller = $this->name;
$helper->token = Tools::getAdminTokenLite('AdminModules');
$helper->currentIndex = AdminController::$currentIndex . '&configure=' . $this->name;
//Language
$helper->default_form_language = $default_lang;
$helper->allow_employee_form_lang = $default_lang;
//Title and toolbar
$helper->title = $this->displayName;
$helper->languages = $languages;
$helper->show_toolbar = true;
$helper->toolbar_scroll = true;
$helper->submit_action = 'submit' . $this->name;
$helper->toolbar_btn = array(
'save' => array(
'desc' => $this->l('Save'),
'href' => AdminController::$currentIndex . '&configure=' . $this->name . '&save' . $this->name . '&token=' . Tools::getAdminTokenLite('AdminModules')
),
'back' => array(
'href' => AdminController::$currentIndex . '&token=' . Tools::getAdminTokenLite('AdminModules'),
'desc' => $this->l('Back to list')
)
);
//Load current value
$helper->fields_value['COOKIESINFO_LINK'] = Configuration::getInt('COOKIESINFO_LINK');
$helper->fields_value['COOKIESINFO_INTRO'] = Configuration::getInt('COOKIESINFO_INTRO');
return $helper->generateForm($fields_form);
}
public function hookDisplayHeader($params)
{
if (Tools::getIsset('validation_cookies')) {
setcookie('accepted_cookies', 1, time() + (365 * 24 * 60 * 60) , '/');
Tools::redirect($_SERVER['HTTP_REFERER']);
}
}
public function hookDisplayCookie($params)
{
if(!self::isAccepted()) {
$this->context->smarty->assign(array(
'cookies_intro' => Configuration::get('COOKIESINFO_INTRO', $this->context->language->id),
'cookies_link' => Configuration::get('COOKIESINFO_LINK', $this->context->language->id)
));
return $this->display($this->_path, 'views/templates/hook/displayTop.tpl');
}
}
public static function setCookie()
{
setcookie('accepted_cookies', 1, time()+3600*24*365, '/');
}
public static function isAccepted()
{
return (bool) (isset($_COOKIE['accepted_cookies']) ? true : false);
}
}