AdvSlider avec prise en compte des dates et des groupes utilisateurs

This commit is contained in:
Michael RICOIS 2017-08-25 17:34:59 +02:00
parent e54b05cd03
commit 41f45d1404
6 changed files with 287 additions and 20 deletions

View File

@ -19,7 +19,7 @@ class AdvSlider extends Module
parent::__construct();
$this->displayName = $this->l('Slider avancé');
$this->description = $this->l('Gestion du slider');
$this->description = $this->l('Gestion du slider avec spécificités PrivilegeDeMarque');
}
public function install()
@ -54,7 +54,14 @@ class AdvSlider extends Module
PRIMARY KEY (`id_slide`, `id_shop`)
) ENGINE=' . _MYSQL_ENGINE_ . ' DEFAULT CHARSET=utf8';
$sql[] = 'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'advslider_group` (
`id_slide` int(11) NOT NULL,
`id_group` int(11) NOT NULL
) ENGINE=' . _MYSQL_ENGINE_ . ' DEFAULT CHARSET=utf8';
$sql[] = "ALTER TABLE `ps_advslider` ADD `start_at` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00' AFTER `light`,
ADD `end_at` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00' AFTER `start_at`;";
foreach ($sql as $_sql) {
Db::getInstance()->Execute($_sql);
}
@ -102,6 +109,35 @@ class AdvSlider extends Module
return parent::uninstall();
}
public function getHookController($hook_name)
{
// Include the controller file
require_once(dirname(__FILE__).'/controllers/hook/'. $hook_name.'.php');
// Build dynamically the controller name
$controller_name = $this->name.$hook_name.'Controller';
// Instantiate controller
$controller = new $controller_name($this, __FILE__, $this->_path);
// Return the controller
return $controller;
}
public function getContent()
{
$ajax_hook = Tools::getValue('ajax_hook');
if ($ajax_hook != '') {
$ajax_method = 'hook'.ucfirst($ajax_hook);
if (method_exists($this, $ajax_method)) {
die($this->{$ajax_method}(array()));
}
}
$controller = $this->getHookController('getContent');
return $controller->run();
}
public function hookDisplaySlider($params)
{
if (!$this->isCached('advslider.tpl', $this->getCacheId())) {

View File

@ -12,6 +12,8 @@ class AdvSlide extends ObjectModel
public $url;
public $description;
public $light;
public $start_at;
public $end_at;
public static $definition = array(
'table' => 'advslider',
@ -22,6 +24,8 @@ class AdvSlide extends ObjectModel
'position' => array('type' => self::TYPE_INT, 'validate' => 'isInt'),
'active' => array('type' => self::TYPE_INT, 'validate' => 'isInt'),
'light' => array('type' => self::TYPE_INT, 'validate' => 'isInt'),
'start_at' => array('type' => self::TYPE_DATE, 'validate' => 'isDate'),
'end_at' => array('type' => self::TYPE_DATE, 'validate' => 'isDate'),
// Lang fields
'title' => array('type' => self::TYPE_STRING, 'lang' => TRUE, 'validate' => 'isGenericName', 'size' => 255),
@ -29,7 +33,10 @@ class AdvSlide extends ObjectModel
'label' => array('type' => self::TYPE_STRING, 'lang' => TRUE, 'validate' => 'isGenericName', 'size' => 255),
'url' => array('type' => self::TYPE_STRING, 'lang' => TRUE, 'validate' => 'isUrl', 'size' => 255),
'description' => array('type' => self::TYPE_STRING, 'lang' => TRUE, 'validate' => 'isCleanHtml')
)
),
'associations' => array(
'groups' => array('type' => self::HAS_MANY, 'field' => 'id_group', 'object' => 'Group', 'association' => 'advslider_group'),
),
);
public function __construct($id = NULL, $id_lang = NULL, $id_shop = NULL)
@ -54,6 +61,26 @@ class AdvSlide extends ObjectModel
return $result;
}
public function updateGroups($data)
{
$dataToInsert = array();
if (count($data) > 0) {
// Reset
Db::getInstance()->delete('advslider_group', 'id_slide='.(int)$this->id);
// Prepare update
foreach($data as $k => $v) {
$dataToInsert[] = array(
'id_slide' => $this->id,
'id_group' => $v,
);
}
}
if (count($dataToInsert) > 0) {
Db::getInstance()->insert('advslider_group', $dataToInsert);
}
}
public function delete($null_values = FALSE)
{
@ -66,7 +93,42 @@ class AdvSlide extends ObjectModel
public static function getSlides()
{
$context = Context::getContext();
$sql = 'SELECT * FROM `'._DB_PREFIX_.'advslider` adv
JOIN `'._DB_PREFIX_.'advslider_lang` advl ON (adv.`id_slide` = advl.`id_slide` AND id_lang='. (int)$context->language->id .')
WHERE adv.`active` = 1';
// Check user group
if (Configuration::get('ADVSLIDER_RESTRICT_GROUP')) {
$groups = Customer::getGroupsStatic($context->customer->id);
$sql.= ' AND EXISTS(SELECT * FROM `'._DB_PREFIX_.'advslider_group` ag WHERE ag.id_group IN('.join(',', $groups).')';
}
// Check date
if (Configuration::get('ADVSLIDER_RESTRICT_DATE')) {
$sql.= ' AND adv.`start_at` < NOW() AND adv.`end_at` > NOW() ';
}
$sql.= ' ORDER BY `position` ASC';
$slides = Db::getInstance()->executeS($sql);
// Remove slider without image
if (count($slides) > 0) {
foreach($slides as $key => $slide) {
if(!file_exists(_PS_IMG_DIR_ . 'slider/' . $slide['id_slide'] . '.jpg')) {
//unset($slides[$key]);
}
}
}
return $slides;
}
public static function getSlidesWithGroups()
{
$context = Context::getContext();
$slides = Db::getInstance()->executeS('
SELECT *
FROM `'._DB_PREFIX_.'advslider` adv
@ -74,15 +136,39 @@ class AdvSlide extends ObjectModel
WHERE adv.`active` = 1
ORDER BY `position` ASC
');
foreach($slides as $key => $slide) {
if(!file_exists(_PS_IMG_DIR_ . 'slider/' . $slide['id_slide'] . '.jpg')) {
unset($slides[$key]);
if (count($slides) > 0) {
foreach($slides as $key => $slide) {
if(!file_exists(_PS_IMG_DIR_ . 'slider/' . $slide['id_slide'] . '.jpg')) {
unset($slides[$key]);
continue;
}
$slides[$key]['groups'] = $this->getGroupsFormatted($slide['id_slide']);
}
}
return $slides;
}
public static function getGroupsFormatted($id_slide)
{
$groups = array();
$result = Db::getInstance()->executeS('SELECT `id_group` FROM `'._DB_PREFIX_.'advslider_group` WHERE `id_slide`=');
if (count($result) > 0) {
foreach($result as $r) {
$groups[] = $r['id_group'];
}
}
return $groups;
}
public function getGroups()
{
$groups = Db::getInstance()->executeS('SELECT `id_group` FROM `'._DB_PREFIX_.'advslider_group` WHERE `id_slide`='.(int)$this->id);
return $groups;
}
public function deleteImage($force_delete = false)
{

View File

@ -35,6 +35,12 @@ class AdminAdvSliderController extends ModuleAdminController
'title' => array(
'title' => $this->module->l('Titre'),
),
'start_at' => array(
'title' => $this->module->l('Début'),
),
'end_at' => array(
'title' => $this->module->l('Fin'),
),
'url' => array(
'title' => $this->module->l('Url'),
'width' => 45,
@ -44,7 +50,7 @@ class AdminAdvSliderController extends ModuleAdminController
'align' => 'center',
'position' => 'position',
'filter_key' => 'a!position'
)
),
);
if (Shop::isFeatureActive() && Shop::getContext() != Shop::CONTEXT_ALL){
@ -96,6 +102,36 @@ class AdminAdvSliderController extends ModuleAdminController
'name' => 'subtitle',
'lang' => TRUE,
),
array(
'type' => 'datetime',
'label' => $this->l('Date de début'),
'name' => 'start_at',
'lang' => false,
),
array(
'type' => 'datetime',
'label' => $this->l('Date de fin'),
'name' => 'end_at',
'lang' => false,
),
array(
'type' => 'checkbox',
'label' => $this->l('Groupe(s) d\'utilisateur'),
'name' => 'groups',
'values' => array(
'query' => array(
array('id_group' => 3, 'name' => 'Particulier'),
array('id_group' => 4, 'name' => 'Pro'),
),
'id' => 'id_group',
'name' => 'name',
),
'expand' => array(
'default' => 'show',
'show' => array('text' => $this->l('show'), 'icon' => 'plus-sign-alt'),
'hide' => array('text' => $this->l('hide'), 'icon' => 'minus-sign-alt')
),
),
array(
'type' => 'switch',
'label' => $this->l('Texte foncé ?'),
@ -164,17 +200,28 @@ class AdminAdvSliderController extends ModuleAdminController
)
);
$obj = $this->loadObject(TRUE);
$obj = $this->loadObject(true);
$image = FALSE;
$selectedGroups = array();
$image = false;
$image_url = '';
$image_size = '';
$image_mobile = FALSE;
$image_mobile = false;
$image_url_mobile = '';
$image_size_mobile = '';
if($obj) {
// Groupes
$selectedGroups = $obj->getGroups();
if (count($selectedGroups) > 0) {
foreach ($selectedGroups as $id) {
$this->fields_value['groups_'.$id['id_group']] = 'on';
}
}
// Images
$image = _PS_IMG_DIR_ . 'slider/' . $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;
@ -207,7 +254,7 @@ class AdminAdvSliderController extends ModuleAdminController
'size' => $image_size_mobile,
'delete_url' => self::$currentIndex.'&'.$this->identifier.'='.$this->object->id.'&token='.$this->token.'&deleteImage=1&imgName=image_mobile'
);
return parent::renderForm();
}
@ -217,8 +264,9 @@ class AdminAdvSliderController extends ModuleAdminController
if(Shop::isFeatureActive()) {
$object->id_shop_list = array();
foreach (Tools::getValue('checkBoxShopAsso_advslider') as $id_shop => $value)
foreach (Tools::getValue('checkBoxShopAsso_advslider') as $id_shop => $value) {
$object->id_shop_list[] = $id_shop;
}
}
}
@ -228,14 +276,27 @@ class AdminAdvSliderController extends ModuleAdminController
$this->processForceDeleteImage();
$this->refreshPreview();
}
parent::postProcess();
$obj = $this->loadObject(TRUE);
// Groupes
$groupsData = array();
foreach ($_POST as $pKey => $pValue) {
$groupKey = 'groups_';
if (substr($pKey, 0, strlen($groupKey)) == $groupKey && $pValue == 'on') {
$groupsData[] = substr($pKey, strlen($groupKey));
}
}
if (count($groupsData) > 0) {
$obj->updateGroups($groupsData);
}
// Images
$images = array('image_mobile');
foreach($images as $imageName) {
if(isset($_FILES[$imageName]) && !empty($_FILES[$imageName]['tmp_name'])) {
$obj = $this->loadObject(TRUE);
$fileTemp = $_FILES[$imageName]['tmp_name'];
$fileParts = pathinfo($_FILES[$imageName]['name']);

View File

@ -0,0 +1,83 @@
<?php
class AdvSliderGetContentController
{
public function __construct($module, $file, $path)
{
$this->file = $file;
$this->module = $module;
$this->context = Context::getContext();
$this->_path = $path;
}
public function processConfiguration()
{
if (Tools::isSubmit('submitAdvsliderconfig')) {
$enable_date = Tools::getValue('enable_date');
$enable_groups = Tools::getValue('enable_groups');
Configuration::updateValue('ADVSLIDER_RESTRICT_DATE', $enable_date);
Configuration::updateValue('ADVSLIDER_RESTRICT_GROUP', $enable_groups);
$this->context->smarty->assign('confirmation', 'ok');
}
}
public function renderForm()
{
$fields_form = array(
'form' => array(
'legend' => array(
'title' => $this->module->l('AdvSlider configuration'),
'icon' => 'icon-envelope'
),
'input' => array(
array(
'type' => 'switch',
'label' => $this->module->l('Enable Date restriction:'),
'name' => 'enable_date',
'desc' => $this->module->l('Enable restriction by date.'),
'values' => array(
array('id' => 'enable_date_1', 'value' => 1, 'label' => $this->module->l('Enabled')),
array('id' => 'enable_date_0', 'value' => 0, 'label' => $this->module->l('Disabled'))
),
),
array(
'type' => 'switch',
'label' => $this->module->l('Enable Groups Restriction:'),
'name' => 'enable_groups',
'desc' => $this->module->l('Enable restriction by user groups.'),
'values' => array(
array('id' => 'enable_groups_1', 'value' => 1, 'label' => $this->module->l('Enabled')),
array('id' => 'enable_groups_0', 'value' => 0, 'label' => $this->module->l('Disabled'))
),
),
),
'submit' => array('title' => $this->module->l('Save'))
)
);
$helper = new HelperForm();
$helper->table = 'advslider';
$helper->default_form_language = (int)Configuration::get('PS_LANG_DEFAULT');
$helper->allow_employee_form_lang = (int)Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG');
$helper->submit_action = 'submitAdvsliderconfig';
$helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false).'&configure='.$this->module->name.'&tab_module='.$this->module->tab.'&module_name='.$this->module->name;
$helper->token = Tools::getAdminTokenLite('AdminModules');
$helper->tpl_vars = array(
'fields_value' => array(
'enable_date' => Tools::getValue('enable_date', Configuration::get('ADVSLIDER_RESTRICT_DATE')),
'enable_groups' => Tools::getValue('enable_groups', Configuration::get('ADVSLIDER_RESTRICT_GROUP')),
),
'languages' => $this->context->controller->getLanguages()
);
return $helper->generateForm(array($fields_form));
}
public function run()
{
$this->processConfiguration();
$html_confirmation_message = $this->module->display($this->file, 'getContent.tpl');
$html_form = $this->renderForm();
return $html_confirmation_message.$html_form;
}
}

View File

@ -1,3 +1 @@
<!-- Block Advmenu module -->
<!-- /Block Advmenu module -->
{$slides|p}

View File

@ -0,0 +1,3 @@
{if isset($confirmation)}
<div class="alert alert-success">{l s='Settings updated' mod='advslider'}</div>
{/if}