* @copyright 2024 Project-Pro */ if (!defined('_PS_VERSION_')) { exit; } require_once _PS_MODULE_DIR_ . 'projectproblog/classes/BlogPost.php'; require_once _PS_MODULE_DIR_ . 'projectproblog/classes/BlogCategory.php'; class AdminProjectproBlogPostsController extends ModuleAdminController { /** Katalog przechowywania miniaturek */ const IMG_DIR = 'projectproblog/views/img/posts/'; /** Flaga zapobiegająca podwójnemu wstrzyknięciu pól do formularza */ private $formEnriched = false; public function __construct() { $this->bootstrap = true; $this->table = 'projectproblog_post'; $this->className = 'BlogPost'; $this->lang = true; $this->identifier = 'id_post'; $this->_defaultOrderBy = 'date_add'; $this->_defaultOrderWay = 'DESC'; parent::__construct(); $this->addRowAction('edit'); $this->addRowAction('delete'); /* -------------------------------------------------------- */ /* Lista */ /* -------------------------------------------------------- */ $this->fields_list = [ 'id_post' => [ 'title' => $this->l('ID'), 'align' => 'center', 'class' => 'fixed-width-xs', ], 'title' => [ 'title' => $this->l('Tytuł'), 'filter_key' => 'b!title', ], 'date_add' => [ 'title' => $this->l('Data dodania'), 'type' => 'datetime', 'align' => 'center', ], 'active' => [ 'title' => $this->l('Aktywny'), 'active' => 'status', 'type' => 'bool', 'align' => 'center', 'class' => 'fixed-width-sm', ], ]; /* -------------------------------------------------------- */ /* Formularz — pola statyczne */ /* (miniaturka i kategorie dołączane dynamicznie */ /* w renderForm() żeby obsłużyć podgląd i zaznaczenia) */ /* -------------------------------------------------------- */ $this->fields_form = [ 'legend' => [ 'title' => $this->l('Wpis bloga'), 'icon' => 'icon-edit', ], 'input' => [ [ 'type' => 'switch', 'label' => $this->l('Aktywny'), 'name' => 'active', 'required' => false, 'is_bool' => true, 'values' => [ ['id' => 'active_on', 'value' => 1, 'label' => $this->l('Tak')], ['id' => 'active_off', 'value' => 0, 'label' => $this->l('Nie')], ], ], [ 'type' => 'text', 'label' => $this->l('Tytuł'), 'name' => 'title', 'lang' => true, 'required' => true, 'col' => 6, ], [ 'type' => 'text', 'label' => $this->l('Przyjazny URL (slug)'), 'name' => 'link_rewrite', 'lang' => true, 'required' => true, 'col' => 6, 'hint' => $this->l('Tylko małe litery, cyfry i myślniki. Generowany automatycznie z tytułu.'), ], [ 'type' => 'textarea', 'label' => $this->l('Wstęp'), 'name' => 'intro', 'lang' => true, 'rows' => 5, 'autoload_rte' => true, 'hint' => $this->l('Krótki opis wyświetlany na liście wpisów.'), ], [ 'type' => 'textarea', 'label' => $this->l('Treść'), 'name' => 'content', 'lang' => true, 'rows' => 20, 'autoload_rte' => true, ], [ 'type' => 'text', 'label' => $this->l('Meta title'), 'name' => 'meta_title', 'lang' => true, 'col' => 6, ], [ 'type' => 'text', 'label' => $this->l('Meta keywords'), 'name' => 'meta_keywords', 'lang' => true, 'col' => 6, 'hint' => $this->l('Słowa kluczowe oddzielone przecinkami.'), ], [ 'type' => 'textarea', 'label' => $this->l('Meta description'), 'name' => 'meta_description', 'lang' => true, 'rows' => 3, ], // miniaturka i kategorie → renderForm() ], 'submit' => [ 'title' => $this->l('Zapisz'), ], ]; } /* ------------------------------------------------------------------ */ /* Formularz — dynamiczne pola: miniaturka + kategorie */ /* ------------------------------------------------------------------ */ public function renderForm() { // Ochrona przed podwójnym wstrzyknięciem (np. przy re-renderze po błędzie walidacji) if ($this->formEnriched) { return parent::renderForm(); } $this->formEnriched = true; $idPost = (int) Tools::getValue('id_post'); $existingThumbnail = null; $selectedCategories = []; if ($idPost) { $post = new BlogPost($idPost); if (Validate::isLoadedObject($post)) { $existingThumbnail = $post->thumbnail; $selectedCategories = array_map('intval', BlogPost::getCategories($idPost)); } } // --- pole miniaturki --- $thumbnailHtml = '
' . $this->l('Dozwolone formaty: jpg, png, webp, gif.') . '
'; $thumbnailHtml .= '' . $this->l('Brak aktywnych kategorii. Dodaj kategorie w zakładce "Kategorie bloga".') . '
'; } $categoriesHtml .= '