* @copyright 2007-2017 ST-themes * @license Use, by you or one client for one Prestashop instance. */ if (!defined('_PS_VERSION_')) exit; use PrestaShop\PrestaShop\Core\Module\WidgetInterface; require_once _PS_MODULE_DIR_.'stnotification/classes/StNotificationClass.php'; class StNotification extends Module implements WidgetInterface { private $templateFile; protected static $access_rights = 0775; private $_html = ''; public $fields_list; public $fields_form; public $fields_value; private $validation_errors = array(); private $_prefix_st = 'ST_NF_'; public static $location = array(); protected $templatePath; public function __construct() { $this->name = 'stnotification'; $this->tab = 'front_office_features'; $this->version = '1.0'; $this->author = 'SUNNYTOO.COM'; $this->need_instance = 0; $this->bootstrap = true; parent::__construct(); $this->displayName = $this->getTranslator()->trans('Notification', array(), 'Modules.Stnotification.Admin'); $this->description = $this->getTranslator()->trans('Display a responsive and custom warning notification on your site.', array(), 'Modules.Stnotification.Admin'); $this->ps_versions_compliancy = array('min' => '1.7', 'max' => _PS_VERSION_); $this->templateFile = 'module:stnotification/views/templates/hook/stnotification.tpl'; self::$location = array( array('id' => 0, 'name'=>$this->getTranslator()->trans('Bottom center', array(), 'Admin.Theme.Panda')), array('id' => 1, 'name'=>$this->getTranslator()->trans('Bottom right', array(), 'Admin.Theme.Panda')), array('id' => 2, 'name'=>$this->getTranslator()->trans('Bottom left', array(), 'Admin.Theme.Panda')), array('id' => 3, 'name'=>$this->getTranslator()->trans('Center center', array(), 'Admin.Theme.Panda')), array('id' => 4, 'name'=>$this->getTranslator()->trans('Center right', array(), 'Admin.Theme.Panda')), array('id' => 5, 'name'=>$this->getTranslator()->trans('Center left', array(), 'Admin.Theme.Panda')), array('id' => 6, 'name'=>$this->getTranslator()->trans('Top center', array(), 'Admin.Theme.Panda')), array('id' => 7, 'name'=>$this->getTranslator()->trans('Top right', array(), 'Admin.Theme.Panda')), array('id' => 8, 'name'=>$this->getTranslator()->trans('Top left', array(), 'Admin.Theme.Panda')), array('id' => 9, 'name'=>$this->getTranslator()->trans('Top full screen static', array(), 'Admin.Theme.Panda')), ); $this->templatePath = 'module:'.$this->name.'/views/templates/hook/'; } public function install() { $res = parent::install() && $this->installDB() && $this->registerHook('displayHeader') && $this->registerHook('displayBeforeBodyClosingTag') && $this->registerHook('displayBanner'); if ($res) foreach(Shop::getShops(false) as $shop) $res &= $this->sampleData($shop['id_shop']); $this->clearStNotificationCache(); return (bool)$res; } public function installDB() { $return = (bool)Db::getInstance()->execute(' CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'st_notification` ( `id_st_notification` INT UNSIGNED NOT NULL AUTO_INCREMENT, `template` tinyint(3) unsigned NOT NULL DEFAULT 0, `location` tinyint(3) unsigned NOT NULL DEFAULT 0, `width` int(10) unsigned NOT NULL DEFAULT 0, `x_offset` smallint(6) signed DEFAULT 0, `y_offset` smallint(6) signed DEFAULT 0, `more_info_link` int(10) unsigned NOT NULL DEFAULT 0, `x_button` tinyint(1) unsigned NOT NULL DEFAULT 1, `show_box` tinyint(1) unsigned NOT NULL DEFAULT 0, `delay` tinyint(3) unsigned NOT NULL DEFAULT 2, `hide_on_mobile` tinyint(1) unsigned NOT NULL DEFAULT 0, `tb_padding` varchar(4) DEFAULT NULL, `lr_padding` varchar(4) DEFAULT NULL, `text_color` varchar(7) DEFAULT NULL, `link_hover_color` varchar(7) DEFAULT NULL, `text_bg` varchar(7) DEFAULT NULL, `bg_opacity` float(4,2) unsigned NOT NULL DEFAULT 0.8, `border_width` tinyint(3) unsigned NOT NULL DEFAULT 0, `border_color` varchar(7) DEFAULT NULL, `button_color` varchar(7) DEFAULT NULL, `button_hover_color` varchar(7) DEFAULT NULL, `button_bg` varchar(7) DEFAULT NULL, `button_hover_bg` varchar(7) DEFAULT NULL, `h_shadow` tinyint(3) signed NOT NULL DEFAULT 0, `v_shadow` tinyint(3) signed NOT NULL DEFAULT 0, `shadow_blur` tinyint(3) unsigned NOT NULL DEFAULT 0, `shadow_color` varchar(7) DEFAULT NULL, `shadow_opacity` float(4,2) unsigned NOT NULL DEFAULT 0.8, `active` tinyint(1) unsigned NOT NULL DEFAULT 1, `position` int(10) unsigned NOT NULL DEFAULT 0, `bg_img` varchar(255) DEFAULT NULL, PRIMARY KEY (`id_st_notification`) ) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8 ;'); $return &= Db::getInstance()->execute(' CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'st_notification_lang` ( `id_st_notification` INT UNSIGNED NOT NULL AUTO_INCREMENT, `id_lang` int(10) unsigned NOT NULL, `accept_button` varchar(256) DEFAULT NULL, `more_info` varchar(256) DEFAULT NULL, `more_info_link_custom` varchar(512) DEFAULT NULL, `content` text, PRIMARY KEY (`id_st_notification`, `id_lang`) ) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8 ;'); $return &= Db::getInstance()->execute(' CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'st_notification_shop` ( `id_st_notification` int(10) UNSIGNED NOT NULL, `id_shop` int(11) NOT NULL, PRIMARY KEY (`id_st_notification`,`id_shop`), KEY `id_shop` (`id_shop`) ) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8 ;'); return $return; } public function sampleData($id_shop) { $return = true; $samples = array( array( 'template' => 0, 'location' => 9, 'active' => 1, 'accept_button' => 'Got it', 'show_box' => 0, 'delay' => 2, 'hide_on_mobile'=> 0, 'content' => '
',
),
array(
'id' => 'template_1',
'value' => 1,
'label' => '
',
),
array(
'id' => 'template_2',
'value' => 2,
'label' => '
',
),
),
),
array(
'type' => 'select',
'label' => $this->getTranslator()->trans('Display on:', array(), 'Admin.Theme.Panda'),
'name' => 'location',
'options' => array(
'query' => self::$location,
'id' => 'id',
'name' => 'name'
),
'validation' => 'isUnsignedInt',
),
array(
'type' => 'text',
'label' => $this->getTranslator()->trans('Width:', array(), 'Admin.Theme.Panda'),
'name' => 'width',
'validation' => 'isUnsignedId',
'default_value' => 0,
'prefix' => 'px',
'class' => 'fixed-width-lg',
'desc' => $this->getTranslator()->trans('Set the value to 0 to make this block be full width.', array(), 'Modules.Stnotification.Admin'),
),
array(
'type' => 'text',
'label' => $this->getTranslator()->trans('X offset:', array(), 'Admin.Theme.Panda'),
'name' => 'x_offset',
'validation' => 'isInt',
'prefix' => 'px',
'class' => 'fixed-width-lg',
'desc' => array(
$this->getTranslator()->trans('Do not work for full width notifications.', array(), 'Modules.Stnotification.Admin'),
$this->getTranslator()->trans('Accept positive and negative numbers.', array(), 'Admin.Theme.Panda'),
)
),
array(
'type' => 'text',
'label' => $this->getTranslator()->trans('Y offset:', array(), 'Admin.Theme.Panda'),
'name' => 'y_offset',
'validation' => 'isInt',
'prefix' => 'px',
'class' => 'fixed-width-lg',
'desc' => $this->getTranslator()->trans('Accept positive and negative numbers.', array(), 'Admin.Theme.Panda'),
),
array(
'type' => 'text',
'label' => $this->getTranslator()->trans('"Got it" button text:', array(), 'Modules.Stnotification.Admin'),
'name' => 'accept_button',
'lang' => true,
'size' => 64,
// 'default_value' => $this->getTranslator()->trans('Got it', array(), 'Modules.Stnotification.Admin'), the default value would be save if the field is empty
'validation' => 'isGenericName',
'desc' => array(
$this->getTranslator()->trans('Clicking on the button would close the notification.', array(), 'Modules.Stnotification.Admin'),
$this->getTranslator()->trans('If you do not fill in this filed, the "Got it" button would not show out.', array(), 'Modules.Stnotification.Admin'),
),
),
array(
'type' => 'text',
'label' => $this->getTranslator()->trans('Read more button text:', array(), 'Modules.Stnotification.Admin'),
'name' => 'more_info',
'lang' => true,
'size' => 64,
// 'default_value' => $this->getTranslator()->trans('Read more', array(), 'Modules.Stnotification.Admin'),
'validation' => 'isGenericName',
'desc' => $this->getTranslator()->trans('If you do not fill in this filed or link fields below, the more info button would not show out.', array(), 'Modules.Stnotification.Admin'),
),
array(
'type' => 'select',
'label' => $this->getTranslator()->trans('More info link:', array(), 'Modules.Stnotification.Admin'),
'name' => 'more_info_link',
'options' => array(
'query' => $this->getCMSOptions(),
'id' => 'id',
'name' => 'name',
'default_value' => 0,
'default' => array(
'value' => 0,
'label' => $this->getTranslator()->trans('--', array(), 'Admin.Theme.Panda'),
),
),
'validation' => 'isUnsignedInt',
),
array(
'type' => 'text',
'label' => $this->getTranslator()->trans('Custom more info link:', array(), 'Modules.Stnotification.Admin'),
'name' => 'more_info_link_custom',
'lang' => true,
'size' => 64,
'validation' => 'isAnything',
),
array(
'type' => 'switch',
'label' => $this->getTranslator()->trans('Display a X close button on the top right corner:', array(), 'Modules.Stnotification.Admin'),
'name' => 'x_button',
'is_bool' => true,
'default_value' => 1,
'values' => array(
array(
'id' => 'x_button_on',
'value' => 1,
'label' => $this->getTranslator()->trans('Yes', array(), 'Admin.Theme.Panda')),
array(
'id' => 'x_button_off',
'value' => 0,
'label' => $this->getTranslator()->trans('No', array(), 'Admin.Theme.Panda')),
),
'validation' => 'isBool',
),
array(
'type' => 'radio',
'label' => $this->getTranslator()->trans('How to show this notification:', array(), 'Modules.Stnotification.Admin'),
'name' => 'show_box',
'default_value' => 0,
'is_bool' => true,
'validation' => 'isUnsignedInt',
'values' => array(
array(
'id' => 'show_box_0',
'value' => 0,
'label' => $this->getTranslator()->trans('At all time, do not show again if customers close it by clicking on the close buttons.', array(), 'Modules.Stnotification.Admin')),
array(
'id' => 'show_box_1',
'value' => 1,
'label' => $this->getTranslator()->trans('At all time', array(), 'Modules.Stnotification.Admin')),
array(
'id' => 'show_box_2',
'value' => 2,
'label' => $this->getTranslator()->trans('First time only', array(), 'Modules.Stnotification.Admin')),
),
),
array(
'type' => 'text',
'label' => $this->getTranslator()->trans('Delay:', array(), 'Modules.Stnotification.Admin'),
'name' => 'delay',
'default_value' => 2,
'class' => 'fixed-width-sm',
'suffix' => 's',
'validation' => 'isUnsignedInt',
),
array(
'type' => 'textarea',
'label' => $this->getTranslator()->trans('Content:', array(), 'Admin.Theme.Panda'),
'lang' => true,
'name' => 'content',
'cols' => 40,
'rows' => 10,
'autoload_rte' => true,
),
array(
'type' => 'radio',
'label' => $this->getTranslator()->trans('Visibility:', array(), 'Admin.Theme.Panda'),
'name' => 'hide_on_mobile',
'default_value' => 0,
'validation' => 'isUnsignedInt',
'values' => array(
array(
'id' => 'hide_on_mobile_0',
'value' => 0,
'label' => $this->getTranslator()->trans('No', array(), 'Admin.Theme.Panda')),
array(
'id' => 'hide_on_mobile_1',
'value' => 1,
'label' => $this->getTranslator()->trans('Hide on mobile (screen width < 992px)', array(), 'Admin.Theme.Panda')),
array(
'id' => 'hide_on_mobile_2',
'value' => 2,
'label' => $this->getTranslator()->trans('Hide on PC (screen width > 992px)', array(), 'Admin.Theme.Panda')),
),
),
array(
'type' => 'switch',
'label' => $this->getTranslator()->trans('Status:', array(), 'Admin.Theme.Panda'),
'name' => 'active',
'is_bool' => true,
'default_value' => 1,
'values' => array(
array(
'id' => 'active_on',
'value' => 1,
'label' => $this->getTranslator()->trans('Yes', array(), 'Admin.Theme.Panda')),
array(
'id' => 'active_off',
'value' => 0,
'label' => $this->getTranslator()->trans('No', array(), 'Admin.Theme.Panda')),
),
'validation' => 'isBool',
),
),
'buttons' => array(
array(
'type' => 'submit',
'title' => $this->getTranslator()->trans('Save', array(), 'Admin.Actions'),
'icon' => 'process-icon-save',
'class'=> 'pull-right'
),
),
'submit' => array(
'title' => $this->getTranslator()->trans('Save and stay', array(), 'Admin.Actions'),
'stay' => true
),
);
$this->fields_form[1]['form'] = array(
'legend' => array(
'title' => $this->getTranslator()->trans('Other settings', array(), 'Modules.Stnotification.Admin'),
'icon' => 'icon-cogs'
),
'input' => array(
array(
'type' => 'text',
'label' => $this->getTranslator()->trans('Top and bottom paddings:', array(), 'Admin.Theme.Panda'),
'name' => 'tb_padding',
'validation' => 'isNullOrUnsignedId',
'prefix' => 'px',
'class' => 'fixed-width-lg',
'desc' => $this->getTranslator()->trans('Leave it empty to use the default value.', array(), 'Admin.Theme.Panda').' 10px',
),
array(
'type' => 'text',
'label' => $this->getTranslator()->trans('Left and right paddings:', array(), 'Admin.Theme.Panda'),
'name' => 'lr_padding',
'validation' => 'isNullOrUnsignedId',
'prefix' => 'px',
'class' => 'fixed-width-lg',
'desc' => $this->getTranslator()->trans('Leave it empty to use the default value.', array(), 'Admin.Theme.Panda').' 10px',
),
array(
'type' => 'color',
'label' => $this->getTranslator()->trans('Text color:', array(), 'Admin.Theme.Panda'),
'name' => 'text_color',
'class' => 'color',
'size' => 20,
'validation' => 'isColor',
),
array(
'type' => 'color',
'label' => $this->getTranslator()->trans('Link hover color:', array(), 'Admin.Theme.Panda'),
'name' => 'link_hover_color',
'class' => 'color',
'size' => 20,
'validation' => 'isColor',
),
array(
'type' => 'color',
'label' => $this->getTranslator()->trans('Background color:', array(), 'Admin.Theme.Panda'),
'name' => 'text_bg',
'class' => 'color',
'size' => 20,
'validation' => 'isColor',
),
array(
'type' => 'text',
'label' => $this->getTranslator()->trans('Background opacity:', array(), 'Admin.Theme.Panda'),
'name' => 'bg_opacity',
'validation' => 'isFloat',
'default_value' => 1,
'class' => 'fixed-width-lg',
'desc' => $this->getTranslator()->trans('From 0 (fully transparent) to 1.0 (fully opaque).', array(), 'Admin.Theme.Panda'),
),
'bg_img' => array(
'type' => 'file',
'label' => $this->getTranslator()->trans('Upload background image:', array(), 'Admin.Theme.Panda'),
'name' => 'bg_img',
'desc' => '',
),
array(
'type' => 'text',
'label' => $this->getTranslator()->trans('Border size:', array(), 'Admin.Theme.Panda'),
'name' => 'border_width',
'validation' => 'isUnsignedInt',
'prefix' => 'px',
'default_value' => 0,
'class' => 'fixed-width-lg',
),
array(
'type' => 'color',
'label' => $this->getTranslator()->trans('Broder color:', array(), 'Admin.Theme.Panda'),
'name' => 'border_color',
'class' => 'color',
'size' => 20,
'validation' => 'isColor',
),
array(
'type' => 'color',
'label' => $this->getTranslator()->trans('Button text color:', array(), 'Admin.Theme.Panda'),
'name' => 'button_color',
'size' => 33,
'validation' => 'isColor',
),
array(
'type' => 'color',
'label' => $this->getTranslator()->trans('Button text hover color:', array(), 'Admin.Theme.Panda'),
'name' => 'button_hover_color',
'size' => 33,
'validation' => 'isColor',
),
array(
'type' => 'color',
'label' => $this->getTranslator()->trans('Button background color:', array(), 'Admin.Theme.Panda'),
'name' => 'button_bg',
'size' => 33,
'validation' => 'isColor',
),
array(
'type' => 'color',
'label' => $this->getTranslator()->trans('Button background hover color:', array(), 'Admin.Theme.Panda'),
'name' => 'button_hover_bg',
'size' => 33,
'validation' => 'isColor',
),
array(
'type' => 'color',
'label' => $this->getTranslator()->trans('Shadow color:', array(), 'Admin.Theme.Panda'),
'name' => 'shadow_color',
'class' => 'color',
'size' => 20,
'validation' => 'isColor',
),
array(
'type' => 'text',
'label' => $this->getTranslator()->trans('H-shadow:', array(), 'Admin.Theme.Panda'),
'name' => 'h_shadow',
'validation' => 'isInt',
'default_value' => 0,
'prefix' => 'px',
'class' => 'fixed-width-lg',
'desc' => $this->getTranslator()->trans('The position of the horizontal shadow. Negative values are allowed.', array(), 'Admin.Theme.Panda'),
),
array(
'type' => 'text',
'label' => $this->getTranslator()->trans('V-shadow:', array(), 'Admin.Theme.Panda'),
'name' => 'v_shadow',
'default_value' => 0,
'validation' => 'isInt',
'prefix' => 'px',
'class' => 'fixed-width-lg',
'desc' => $this->getTranslator()->trans('The position of the vertical shadow. Negative values are allowed.', array(), 'Admin.Theme.Panda'),
),
array(
'type' => 'text',
'label' => $this->getTranslator()->trans('The blur distance of shadow:', array(), 'Modules.Stnotification.Admin'),
'name' => 'shadow_blur',
'validation' => 'isUnsignedInt',
'default_value' => 4,
'prefix' => 'px',
'class' => 'fixed-width-lg',
),
array(
'type' => 'text',
'label' => $this->getTranslator()->trans('Shadow opacity:', array(), 'Modules.Stnotification.Admin'),
'name' => 'shadow_opacity',
'validation' => 'isFloat',
'default_value' => '0.8',
'class' => 'fixed-width-lg',
'desc' => $this->getTranslator()->trans('From 0.0 (fully transparent) to 1.0 (fully opaque).', array(), 'Admin.Theme.Panda'),
),
),
'buttons' => array(
array(
'type' => 'submit',
'title' => $this->getTranslator()->trans('Save', array(), 'Admin.Actions'),
'icon' => 'process-icon-save',
'class'=> 'pull-right'
),
),
'submit' => array(
'title' => $this->getTranslator()->trans('Save and stay', array(), 'Admin.Actions'),
'stay' => true
),
);
if (Shop::isFeatureActive())
{
$this->fields_form[1]['form']['input'][] = array(
'type' => 'shop',
'label' => $this->getTranslator()->trans('Shop association:', array(), 'Admin.Theme.Panda'),
'name' => 'checkBoxShopAsso',
);
}
$this->fields_form[0]['form']['input'][] =
$this->fields_form[1]['form']['input'][] = array(
'type' => 'html',
'id' => 'a_cancel',
'label' => '',
'name' => ' '.$this->getTranslator()->trans('Back to list', array(), 'Admin.Theme.Panda').'',
);
$id_st_notification = (int)Tools::getValue('id_st_notification');
$notification = new StNotificationClass($id_st_notification);
if ($notification->id && $notification->bg_img) {
StNotificationClass::fetchMediaServer($notification->bg_img);
$this->fields_form[1]['form']['input']['bg_img']['image'] = '