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

153 lines
5.0 KiB
PHP

<?php
require_once _PS_MODULE_DIR_.'customdevslider/classes/CustomDevSliderSlide.php';
class AdminCustomDevSliderController extends ModuleAdminController
{
public function __construct()
{
$this->bootstrap = true;
$this->table = 'customdevslider_slide';
$this->className = 'CustomDevSliderSlide';
$this->identifier = 'id_customdevslider_slide';
$this->lang = true;
parent::__construct();
$this->_defaultOrderBy = 'position';
$this->_defaultOrderWay = 'ASC';
$this->fields_list = [
'id_customdevslider_slide' => ['title' => 'ID', 'class' => 'fixed-width-xs'],
'title' => ['title' => $this->l('Title')],
'link' => ['title' => $this->l('Link')],
'position' => ['title' => $this->l('Position'), 'class' => 'fixed-width-sm'],
'active' => ['title' => $this->l('Active'), 'type' => 'bool', 'active' => 'status', 'class' => 'fixed-width-sm'],
];
$this->addRowAction('edit');
$this->addRowAction('delete');
// Кнопка "Додати"
$this->bulk_actions = [];
}
public function renderForm()
{
/** @var CustomDevSliderSlide $obj */
$obj = $this->object;
$imagePreview = '';
if ($obj && !empty($obj->image)) {
$imageUrl = __PS_BASE_URI__.'modules/customdevslider/uploads/'.$obj->image;
$imagePreview = '<div style="margin-top:10px;"><img src="'.$imageUrl.'" style="max-width:220px;height:auto;border:1px solid #ddd;padding:5px;"></div>';
}
$this->fields_form = [
'legend' => ['title' => $this->l('Slide')],
'input' => [
[
'type' => 'file',
'label' => $this->l('Image'),
'name' => 'image_file',
'desc' => $this->l('Allowed: jpg, jpeg, png, webp, gif.').$imagePreview,
],
[
'type' => 'text',
'label' => $this->l('Link'),
'name' => 'link',
'desc' => $this->l('Optional URL (https://...)'),
],
[
'type' => 'text',
'label' => $this->l('Title'),
'name' => 'title',
'lang' => true,
],
[
'type' => 'textarea',
'label' => $this->l('Text'),
'name' => 'text',
'lang' => true,
'autoload_rte' => true,
],
[
'type' => 'text',
'label' => $this->l('Position'),
'name' => 'position',
'class' => 'fixed-width-sm',
'desc' => $this->l('Sorting order (0..n). Lower goes first.'),
],
[
'type' => 'switch',
'label' => $this->l('Active'),
'name' => 'active',
'is_bool' => true,
'values' => [
['id' => 'active_on', 'value' => 1, 'label' => $this->l('Enabled')],
['id' => 'active_off', 'value' => 0, 'label' => $this->l('Disabled')],
],
],
],
'submit' => ['title' => $this->l('Save')],
];
return parent::renderForm();
}
public function processAdd()
{
$this->handleImageUpload();
return parent::processAdd();
}
public function processUpdate()
{
$this->handleImageUpload();
return parent::processUpdate();
}
private function handleImageUpload()
{
if (!isset($_FILES['image_file']) || empty($_FILES['image_file']['name'])) {
return;
}
$file = $_FILES['image_file'];
if (!empty($file['error'])) {
$this->errors[] = $this->l('Image upload error.');
return;
}
$ext = Tools::strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
$allowed = ['jpg', 'jpeg', 'png', 'webp', 'gif'];
if (!in_array($ext, $allowed, true)) {
$this->errors[] = $this->l('Invalid image format.');
return;
}
$uploadDir = _PS_MODULE_DIR_.'customdevslider/uploads/';
if (!is_dir($uploadDir)) {
if (!@mkdir($uploadDir, 0755, true)) {
$this->errors[] = $this->l('Cannot create uploads directory.');
return;
}
}
// Нормальне безпечне ім'я
$safeName = sha1(uniqid('customdevslider_', true)).'.'.$ext;
$dest = $uploadDir.$safeName;
if (!move_uploaded_file($file['tmp_name'], $dest)) {
$this->errors[] = $this->l('Image upload failed.');
return;
}
// Записуємо ім'я файлу в поле image
$_POST['image'] = $safeName;
}
}