Files
bio-concept-pharma/web/modules/prestaboost/prestaboost.php
2019-11-17 19:14:07 +01:00

655 lines
23 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
/**
* 2008 - 2017 (c) Prestaboost
*
* MODULE PrestaBoost
*
* @author Prestaboost
* @copyright Copyright (c) permanent, Prestaboost
* @license Commercial
* @version 4.0.5
*/
if (!defined('_PS_VERSION_')) {
exit;
}
use PrestaShop\PrestaShop\Core\Module\WidgetInterface;
include_once(dirname(__FILE__).'/class/prestaboost.class.php');
$subscription = Module::getInstanceByName('ps_emailsubscription');
if (isset($subscription)){
include_once(dirname(__FILE__).'/../ps_emailsubscription/ps_emailsubscription.php');
}
class PrestaBoost extends Module implements WidgetInterface
{
/****************************************/
/************** WIDGET 1.7 **************/
/****************************************/
public function renderWidget($hookname = null, array $configuration = array())
{
$template_file = 'module:'.$this->name.'/views/templates/hook/'.$hookname.'.tpl';
$this->smarty->assign($this->getWidgetVariables($hookname, $configuration));
return $this->fetch($template_file, $this->getCacheId());
}
public function getWidgetVariables($hookname = null, array $configuration = array())
{
$hookname = $hookname;
$configuration = $configuration;
return null;
}
/****************************************/
/************ / WIDGET 1.7 **************/
/****************************************/
public static function prestaboostContent($params)
{
return $params['return'];
}
public $actions_form = array('add', 'edit');
public $class_used = array(
'table' => array(
'PrestaBoostClass'
),
'dashboard' => array(),
'config' => array(),
);
/* this is my fix for php < 5.3 to correct instance dynamic class with string */
public static function createDynInstance($class, $params = array())
{
$reflection_class = new ReflectionClass($class);
return $reflection_class->newInstanceArgs($params);
}
public function __construct()
{
$this->name = 'prestaboost';
$this->tab = 'front_office_features';
$this->version = '4.0.5';
$this->author = 'Prestaboost';
$this->module_key = '353f99305ded8f74983b937500211e55';
$this->bootstrap = true;
$this->need_instance = 0;
parent::__construct();
$this->ps_versions_compliancy = array('min' => '1.7', 'max' => _PS_VERSION_);
$this->displayName = $this->l('PrestaBoost');
$this->description = $this->l('Add popups in your store and customize to your liking');
$this->confirmUninstall = $this->l('Warning ! This action definitely uninstall this module !');
$this->cc = 'configure='.$this->name;
$this->pp_conf = 'index.php?tab=AdminModules&'.$this->cc.'&token='.Tools::getValue('token');
$this->configurations = array(
$this->name.'_token' => md5($this->genererMDP(32)._COOKIE_KEY_),
);
}
public function postProcess()
{
$update_process = false;
$errors = array();
$warnings = array();
$output = '';
if ((int)Tools::getIsset('success')) {
$output .= '
<script type="text/javascript">
$(function () {
showSuccessMessage(\''.$this->l('Settings updated successfully').'\');
});
</script>';
}
if ((int)Tools::getIsset('undo') || (int)Tools::getIsset('error')) {
$output .= '
<script type="text/javascript">
$(function () {
showErrorMessage(\''.$this->l('The current action was canceled').'\');
});
</script>';
}
foreach ($this->class_used['table'] as $object_model) {
$pp = $this->pp_conf.'&class='.$object_model;
if (Tools::getValue('class') == $object_model) {
$definition = ObjectModel::getDefinition($object_model);
if (Tools::isSubmit('status'.$definition['table'])) {
$process_model = self::createDynInstance($object_model);
if (!$process_model->changeState((int)Tools::getValue($definition['primary']))) {
$errors[] = $this->l('Could not change status.');
} else {
Tools::redirectAdmin($pp.'&success');
}
$update_process = true;
}
if (Tools::isSubmit('newsletter'.$definition['table'])) {
$process_model = self::createDynInstance($object_model);
if (!$process_model->changeNewsletter((int)Tools::getValue($definition['primary']))) {
$errors[] = $this->l('Could not change newsletter.');
} else {
Tools::redirectAdmin($pp.'&success');
}
$update_process = true;
}
if (Tools::isSubmit('delete'.$definition['table'])) {
$process_model = self::createDynInstance(
$object_model,
array((int)Tools::getValue($definition['primary']))
);
if (!$process_model->delete()) {
$errors[] = $this->l('Could not delete.');
} else {
Tools::redirectAdmin($pp.'&success');
}
$update_process = true;
}
if (Tools::isSubmit('submitBulkdelete'.$definition['table'])) {
if (Tools::getValue($definition['table'].'Box')) {
foreach (Tools::getValue($definition['table'].'Box') as $id) {
$process_model = self::createDynInstance($object_model, array((int)$id));
if (!$process_model->delete()) {
$errors[] = sprintf($this->l('Could not delete %1$s'), $id);
}
}
if (!count($errors) > 0) {
Tools::redirectAdmin($pp.'&success');
}
}
$update_process = true;
}
foreach ($this->actions_form as $action) {
if (Tools::isSubmit($action)) {
if (!Tools::getValue('title_'.(int)$this->context->language->id)) {
$errors[] = $this->l('The title must be specified');
}
$content = Tools::getValue('content_'.(int)$this->context->language->id);
if (!$content) {
$errors[] = $this->l('The content or introduction must be specified');
}
if (!Tools::getValue('date_start')) {
$errors[] = $this->l('The date start must be specified');
}
if (!Tools::getValue('date_stop')) {
$errors[] = $this->l('The date stop must be specified');
}
if (!count($errors)) {
switch ($action) {
case 'add':
$process_model = self::createDynInstance($object_model);
$process_model->copyFromPost();
if ($process_model->add()) {
Tools::redirectAdmin($pp.'&success');
}
break;
case 'edit':
$process_model = self::createDynInstance(
$object_model,
array((int)Tools::getValue($definition['primary']))
);
$process_model->copyFromPost();
if ($process_model->update()) {
Tools::redirectAdmin(
$pp.'&'.$definition['primary'].'='.(int)Tools::getValue(
$definition['primary']
).'&update'.$this->name.'&success'
);
}
break;
}
$update_process = true;
}
}
}
}
}
if (count($errors) > 0) {
$output .= $this->displayError($errors);
}
if (count($warnings) > 0) {
$output .= $this->displayWarning($warnings);
}
if ($output != '') {
return $output;
}
if ($update_process) {
return $this->displayConfirmation($this->l('Settings updated successfully'));
} else {
return null;
}
}
public function getContent()
{
$pathM = AdminController::$currentIndex.'&'.$this->cc.'&token='.Tools::getAdminTokenLite('AdminModules');
$this->context->controller->addCSS($this->_path.'views/css/prestaboost.css');
if (!Tools::getValue('class')) {
Tools::redirectAdmin($pathM.'&class=PrestaBoostClass');
}
$output = '';
$output .='<script type="text/javascript" src="'.__PS_BASE_URI__.'js/jquery/plugins/jquery.colorpicker.js"></script>';
$output .='<script type="text/javascript">
$.fn.mColorPicker.init.replace = ".mColorPicker"</script>';
$output .= $this->postProcess();
$output .= $this->displayContent();
return $output;
}
public function displayContent()
{
$output = '';
if (Tools::getValue('class') && in_array(Tools::getValue('class'), $this->class_used['table'])) {
$current_class = Tools::getValue('class');
if (!class_exists($current_class)) {
$output .= $this->displayError($this->l('This class doesn\'t exists: ').$current_class);
} else {
$object_model = self::createDynInstance($current_class, array());
if (is_object($object_model)) {
$definition_lang = $object_model->definitionLang();
if (!Tools::isSubmit('add')
&& !Tools::isSubmit('edit')
&& !Tools::getIsset('add'.$definition_lang['tableName'])
&& !Tools::getIsset('update'.$definition_lang['tableName'])
) {
$output .= $object_model->displayList();
}
if (Tools::getIsset('add'.$definition_lang['tableName']) || Tools::isSubmit('add')) {
$output .= $object_model->displayForm('add');
}
if (Tools::getIsset('update'.$definition_lang['tableName']) || Tools::isSubmit('edit')) {
$output .= $object_model->displayForm('edit');
}
}
}
}
return $output;
}
public function isOkDisplay()
{
$id_prestaboost = (int)PrestaBoostClass::getIdFrontPopupPreFiltered();
if ((int)$id_prestaboost > 0) {
$prestaboost = new PrestaBoostClass((int)$id_prestaboost, (int)$this->context->language->id);
$popup_hash = md5(
$prestaboost->id
.$prestaboost->date_start
.$prestaboost->date_stop
.$prestaboost->delay
.$prestaboost->expire
.$prestaboost->expire_ratio
.$prestaboost->theme
.$prestaboost->restriction_rules
.$prestaboost->restriction_pages
.$prestaboost->footer
.$prestaboost->title
.$prestaboost->content
.$prestaboost->newstitle
.$prestaboost->boost_colorpicker_content
.$prestaboost->boost_colorpicker_modal
.$prestaboost->boost_colorpicker_btn
.$prestaboost->boost_colorpicker_btn_border
.$prestaboost->boost_opacity_content
.$prestaboost->boost_opacity_modal
.$prestaboost->boost_opacity_btn
);
if (!isset($_COOKIE['PrestaBoostCookie'.$popup_hash])) {
setcookie(
'PrestaBoostCookie'.$popup_hash,
'1',
(time() + ((int)$prestaboost->expire_ratio * (int)$prestaboost->expire))
);
parent::_clearCache('header.tpl');
return (int)$id_prestaboost;
}
}
}
public function hookDisplayHeader()
{
if ($id_prestaboost = $this->isOkDisplay()) {
$prestaboost = new PrestaBoostClass((int)$id_prestaboost, (int)$this->context->language->id);
$this->context->controller->addCss($this->_path.'views/css/bootstrap-modal.css');
$this->context->controller->addCSS($this->_path.'views/css/theme-'.$prestaboost->theme.'.css', 'all');
$this->context->controller->addJS($this->_path.'views/js/prestaboost-popup.js');
}
}
public function hookDisplayTop()
{
if ($id_prestaboost = $this->isOkDisplay()) {
return $this->displayPopup((int)$this->context->language->id, (int)$id_prestaboost);
}
}
public function hookDisplayBeforeBodyClosingTag()
{
if ($id_prestaboost = $this->isOkDisplay()) {
return $this->displayPopup((int)$this->context->language->id, (int)$id_prestaboost);
}
}
public static function isNotRestrictionPage($restriction_rules, $restriction_pages)
{
$urls = preg_split("/\r/", $restriction_pages);
$urls_ok = array();
$restriction_rules = (int)$restriction_rules;
$url_current = Tools::getCurrentUrlProtocolPrefix().$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
foreach ($urls as $url) {
$url = trim($url);
if (preg_match('/^(http|https):\/\/*/i', $url)) {
$urls_ok[] = $url;
}
}
$return = null;
switch ($restriction_rules) {
case 0: // on all pages
$return = true;
break;
case 1: // on all pages, except urls_ok
if (in_array($url_current, $urls_ok)) {
$return = false;
} else {
$return = true;
}
break;
case 2: // only on pages in urls_ok
if (in_array($url_current, $urls_ok)) {
$return = true;
} else {
$return = false;
}
break;
}
return $return;
}
public function displayFixJSConfirmDuplicate()
{
$pathM = AdminController::$currentIndex.'&'.$this->cc.'&token='.Tools::getAdminTokenLite('AdminModules');
$urlSupp = '&class=PrestaBoostClass&duplicateprestaboost';
return '
<script type="text/javascript">
$(function () {
var onclickBefore = $("ul.dropdown-menu li a[title='.$this->l('Duplicate').']").attr("onclick");
var id_prestaboost = onclickBefore.match(/\d+/)[0];
var onclickAfter = "if (confirm(
\''.$this->l('Confirm duplicate this popup?').'\'
)) {
document.location = \''.$pathM.$urlSupp.'&id_prestaboost=\'+id_prestaboost;
} else {
return false;
}";
$("ul.dropdown-menu li a[title='.$this->l('Duplicate').']").attr("onclick", onclickAfter);
});
</script>';
}
public function displayPopup($id_lang, $id_prestaboost = 0)
{
$prestaboost = new PrestaBoostClass((int)$id_prestaboost, (int)$id_lang);
$this->context->controller->addCSS($this->_path.'views/css/theme-'.$prestaboost->theme.'.css', 'all');
$this->smarty->assign(array(
'adminPreview' => true,
'id_lang' => (int)$id_lang,
'PrestaBoost' => $prestaboost
));
// permet d'échaper tout le contenu html / js
if (!isset($this->context->smarty->registered_plugins['function']['PrestaBoostContent'])) {
smartyRegisterFunction(
$this->context->smarty,
'function',
'PrestaBoostContent',
array('PrestaBoost', 'prestaboostContent')
);
}
parent::_clearCache($prestaboost->theme.'.tpl');
return $this->display(__FILE__, $prestaboost->theme.'.tpl');
}
public static function scanThemeTpl()
{
$return = array();
foreach (glob(dirname(__FILE__).'/views/templates/hook/*.{tpl}', GLOB_BRACE) as $file) {
if (!is_dir($file)) {
$return[] = array(
'id' => basename($file, '.tpl'),
'name' => basename($file, '.tpl')
);
}
}
return $return;
}
private function genererMDP($longueur = 16)
{
$mdp = '';
$possible = 'abcdfghijklmnopqrstuvwxyz012346789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$longueur_max = Tools::strlen($possible);
if ($longueur > $longueur_max) {
$longueur = $longueur_max;
}
$i = 0;
while ($i < $longueur) {
$mdp .= Tools::substr($possible, mt_rand(0, $longueur_max - 1), 1);
$i++;
}
return $mdp;
}
public function displayError($error)
{
$output = '
<div class="bootstrap">
<div class="alert alert-danger">
<button class="close" data-dismiss="alert" type="button">×</button>
<strong>'.$this->l('Error').'</strong><br>';
if (is_array($error)) {
$output .= '<ul><li>'.implode('</li><li>', $error).'</li></ul>';
} else {
$output .= $error;
}
$output .= '
</div>
</div>';
$this->error = true;
return $output;
}
public function displayWarning($warn)
{
$output = '
<div class="bootstrap">
<div class="alert alert-warning">
<button class="close" data-dismiss="alert" type="button">×</button>
<strong>'.$this->l('Warning').'</strong><br/>';
if (is_array($warn)) {
$output .= '<ul><li>'.implode('</li><li>', $warn).'</li></ul>';
} else {
$output .= $warn;
}
$output .= '
</div>
</div>';
return $output;
}
public function displayInfo($info)
{
$output = '
<div class="bootstrap">
<div class="alert alert-info">
<button class="close" data-dismiss="alert" type="button">×</button>
<strong>'.$this->l('Information').'</strong><br/>';
if (is_array($info)) {
$output .= '<ul><li>'.implode('</li><li>', $info).'</li></ul>';
} else {
$output .= $info;
}
$output .= '
</div>
</div>';
return $output;
}
public function hookDisplayBackOfficeHeader()
{
$ctrl = $this->context->controller;
if ($ctrl instanceof AdminController && method_exists($ctrl, 'addCss')) {
$ctrl->addCss($this->_path.'views/css/prestaboost-back-office.css');
}
}
public static function isPSVersion($compare, $version)
{
return version_compare(_PS_VERSION_, $version, $compare);
}
public function install()
{
$this->installQuickAccess();
if (!parent::install()
|| !PrestaBoostClass::createTables()
|| !$this->registerHook('displayHeader')
|| !$this->registerHook('displayBeforeBodyClosingTag')
//|| !$this->registerHook('displayTop')
|| !$this->registerHook('displayBackOfficeHeader')
|| !$this->updateConfiguration('add')
|| !$this->registerAdminTab()
) {
return false;
}
return true;
}
public function uninstall()
{
$this->uninstallQuickAccess();
if (!parent::uninstall()
|| !PrestaBoostClass::dropTables()
|| !$this->updateConfiguration('del')
|| !$this->deleteAdminTab()
) {
return false;
}
return true;
}
public function registerAdminTab()
{
$tab = new Tab();
$tab->class_name = 'AdminPrestaBoost';
foreach (Language::getLanguages(false) as $lang) {
$tab->name[$lang['id_lang']] = 'PrestaBoost';
}
$tab->id_parent = (int)Tab::getIdFromClassName('AdminTools');
$tab->module = 'prestaboost';
$tab->icon = 'photo_library';
return $tab->save();
}
public function deleteAdminTab()
{
foreach (array('AdminPrestaBoost') as $tab_name) {
$id_tab = (int)Tab::getIdFromClassName($tab_name);
if ($id_tab) {
$tab = new Tab($id_tab);
$tab->delete();
}
}
return true;
}
private function updateConfiguration($action)
{
switch ($action) {
case 'add':
foreach ($this->configurations as $configuration_key => $configuration_value) {
Configuration::updateValue($configuration_key, $configuration_value);
}
break;
case 'del':
foreach ($this->configurations as $configuration_key => $configuration_value) {
Configuration::deleteByName($configuration_key);
}
break;
}
return true;
}
private function checkConfiguration()
{
foreach ($this->configurations as $configuration_key => $configuration_value) {
if (!Configuration::getIdByName($configuration_key)) {
Configuration::updateValue($configuration_key, $configuration_value);
}
}
}
public function installQuickAccess()
{
if (!Configuration::get($this->name.'_QuickAccess')) {
$qa = new QuickAccess;
foreach (Language::getLanguages(true) as $language) {
$qa->name[(int)$language['id_lang']] = $this->displayName;
}
$qa->link = 'index.php?controller=AdminModules&configure='.$this->name.'&module_name='.$this->name;
$qa->new_window = 0;
$qa->Add();
Configuration::updateValue($this->name.'_QuickAccess', $qa->id);
}
return true;
}
public function uninstallQuickAccess()
{
$qa = new QuickAccess((int)Configuration::get($this->name.'_QuickAccess'));
$qa->delete();
Configuration::deleteByName($this->name.'_QuickAccess');
return true;
}
}