Files
redline.com.pl/modules/customdevslider/customdevslider.php
2026-01-13 15:18:38 +01:00

196 lines
6.0 KiB
PHP

<?php
if (!defined('_PS_VERSION_')) {
exit;
}
class CustomDevSlider extends Module implements \PrestaShop\PrestaShop\Core\Module\WidgetInterface
{
public function __construct()
{
$this->name = 'customdevslider';
$this->tab = 'front_office_features';
$this->version = '1.0.0';
$this->author = 'CustomDev';
$this->need_instance = 0;
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->l('Custom Dev Slider');
$this->description = $this->l('Custom slider with dynamic slides (image, title, text, link).');
}
public function install()
{
return parent::install()
&& $this->installDb()
&& $this->installTab()
&& $this->registerHook('displayHome')
&& $this->registerHook('displayTop')
&& $this->registerHook('header');
}
public function uninstall()
{
return $this->uninstallTab()
&& $this->uninstallDb()
&& parent::uninstall();
}
public function hookDisplayTop($params)
{
return $this->renderWidget('displayTop', []);
}
public function hookDisplayHome($params)
{
return $this->renderWidget('displayHome', []);
}
public function getContent()
{
Tools::redirectAdmin($this->context->link->getAdminLink('AdminCustomDevSlider'));
}
public function hookHeader()
{
// Swiper (локально)
$this->context->controller->registerStylesheet(
'customdevslider-swiper',
'modules/'.$this->name.'/plugins/swiperjs/swiper.min.css',
['media' => 'all', 'priority' => 140]
);
// Твій CSS (після swiper)
$this->context->controller->registerStylesheet(
'customdevslider-front',
'modules/'.$this->name.'/css/front.css',
['media' => 'all', 'priority' => 150]
);
// Swiper JS (перед твоїм JS)
$this->context->controller->registerJavascript(
'customdevslider-swiper',
'modules/'.$this->name.'/plugins/swiperjs/swiper.min.js',
['position' => 'bottom', 'priority' => 140]
);
// Твій JS ініціалізації
$this->context->controller->registerJavascript(
'customdevslider-front',
'modules/'.$this->name.'/js/front.js',
['position' => 'bottom', 'priority' => 150]
);
}
// WidgetInterface
public function renderWidget($hookName, array $configuration)
{
$this->smarty->assign($this->getWidgetVariables($hookName, $configuration));
return $this->fetch('module:'.$this->name.'/views/templates/hook/slider.tpl');
}
public function getWidgetVariables($hookName, array $configuration)
{
$idLang = (int)$this->context->language->id;
$slides = Db::getInstance()->executeS('
SELECT s.*, sl.title, sl.text
FROM `'._DB_PREFIX_.'customdevslider_slide` s
LEFT JOIN `'._DB_PREFIX_.'customdevslider_slide_lang` sl
ON (s.id_customdevslider_slide = sl.id_customdevslider_slide AND sl.id_lang = '.$idLang.')
WHERE s.active = 1
ORDER BY s.position ASC
');
foreach ($slides as &$s) {
$s['image_url'] = $s['image']
? $this->context->link->getMediaLink(_MODULE_DIR_.$this->name.'/uploads/'.$s['image'])
: null;
}
unset($s);
// UID щоб кілька віджетів не конфліктували
$uid = Tools::passwdGen(10, 'NO_NUMERIC');
return [
'customdevslider_slides' => $slides,
'customdevslider_uid' => $uid,
];
}
private function installDb()
{
$sql = [];
$sql[] = 'CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'customdevslider_slide` (
`id_customdevslider_slide` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`image` VARCHAR(255) DEFAULT NULL,
`link` VARCHAR(255) DEFAULT NULL,
`position` INT UNSIGNED NOT NULL DEFAULT 0,
`active` TINYINT(1) NOT NULL DEFAULT 1,
`date_add` DATETIME NULL,
`date_upd` DATETIME NULL,
PRIMARY KEY (`id_customdevslider_slide`)
) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8;';
$sql[] = 'CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'customdevslider_slide_lang` (
`id_customdevslider_slide` INT UNSIGNED NOT NULL,
`id_lang` INT UNSIGNED NOT NULL,
`title` VARCHAR(255) DEFAULT NULL,
`text` TEXT DEFAULT NULL,
PRIMARY KEY (`id_customdevslider_slide`, `id_lang`)
) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8;';
foreach ($sql as $q) {
if (!Db::getInstance()->execute($q)) {
return false;
}
}
return true;
}
private function uninstallDb()
{
$sql = [];
$sql[] = 'DROP TABLE IF EXISTS `'._DB_PREFIX_.'customdevslider_slide_lang`';
$sql[] = 'DROP TABLE IF EXISTS `'._DB_PREFIX_.'customdevslider_slide`';
foreach ($sql as $q) {
if (!Db::getInstance()->execute($q)) {
return false;
}
}
return true;
}
private function installTab()
{
$idParent = (int)Tab::getIdFromClassName('AdminParentModulesSf');
$tab = new Tab();
$tab->active = 1;
$tab->class_name = 'AdminCustomDevSlider';
$tab->module = $this->name;
$tab->id_parent = $idParent;
$tab->name = [];
foreach (Language::getLanguages(true) as $lang) {
$tab->name[(int)$lang['id_lang']] = 'Custom Dev Slider';
}
return (bool)$tab->add();
}
private function uninstallTab()
{
$idTab = (int)Tab::getIdFromClassName('AdminCustomDevSlider');
if ($idTab) {
$tab = new Tab($idTab);
return (bool)$tab->delete();
}
return true;
}
}