From edb8d4e873d11920f80e6b56755c9461ff8a2e60 Mon Sep 17 00:00:00 2001 From: tommaso Date: Tue, 26 Aug 2025 11:18:54 +0200 Subject: [PATCH] aggiunta vista singola circolare --- site/src/Controller/DisplayController.php | 68 +++------------- site/src/Model/CircolareModel.php | 33 ++++++++ site/src/Service/Router.php | 97 ++++------------------- site/src/View/Circolare/HtmlView.php | 37 +++++++++ site/tmpl/circolare/default.php | 26 ++++++ site/tmpl/circolare/default.xml | 29 +++++++ 6 files changed, 152 insertions(+), 138 deletions(-) create mode 100644 site/src/Model/CircolareModel.php create mode 100644 site/src/View/Circolare/HtmlView.php create mode 100644 site/tmpl/circolare/default.php create mode 100644 site/tmpl/circolare/default.xml diff --git a/site/src/Controller/DisplayController.php b/site/src/Controller/DisplayController.php index fd2170f..c0e48eb 100644 --- a/site/src/Controller/DisplayController.php +++ b/site/src/Controller/DisplayController.php @@ -1,64 +1,22 @@ - * @copyright 2025 Tommaso Cippitelli - * @license GNU General Public License version 2 or later; see LICENSE.txt - */ - namespace Pcrt\Component\Circolari\Site\Controller; \defined('_JEXEC') or die; -use Joomla\CMS\Component\ComponentHelper; -use Joomla\CMS\Factory; -use Joomla\CMS\Language\Text; -use Joomla\CMS\MVC\Factory\MVCFactoryInterface; +use Joomla\CMS\MVC\Controller\BaseController; -/** - * Display Component Controller - * - * @since 1.0.0 - */ -class DisplayController extends \Joomla\CMS\MVC\Controller\BaseController +class DisplayController extends BaseController { - /** - * Constructor. - * - * @param array $config An optional associative array of configuration settings. - * Recognized key values include 'name', 'default_task', 'model_path', and - * 'view_path' (this list is not meant to be comprehensive). - * @param MVCFactoryInterface $factory The factory. - * @param CMSApplication $app The JApplication for the dispatcher - * @param Input $input Input - * - * @since 1.0.0 - */ - public function __construct($config = array(), ?MVCFactoryInterface $factory = null, $app = null, $input = null) - { - parent::__construct($config, $factory, $app, $input); - } + protected $default_view = 'circolare'; // default se manca ?view= - /** - * Method to display a view. - * - * @param boolean $cachable If true, the view output will be cached. - * @param boolean $urlparams An array of safe URL parameters and their variable types, for valid values see {@link InputFilter::clean()}. - * - * @return \Joomla\CMS\MVC\Controller\BaseController This object to support chaining. - * - * @since 1.0.0 - */ - public function display($cachable = false, $urlparams = false) - { - - $view = $this->input->getCmd('view', 'circolares'); - $view = $view == "featured" ? 'circolares' : $view; - $this->input->set('view', $view); - - - parent::display($cachable, $urlparams); - return $this; - } + public function display($cachable = false, $urlparams = []) + { + // Se qualcuno forza un view strano, non rompiamo + $view = $this->input->getCmd('view', $this->default_view); + if (!in_array($view, ['circolare'], true)) { + $view = $this->default_view; + $this->input->set('view', $view); + } + return parent::display($cachable, $urlparams); + } } diff --git a/site/src/Model/CircolareModel.php b/site/src/Model/CircolareModel.php new file mode 100644 index 0000000..9168335 --- /dev/null +++ b/site/src/Model/CircolareModel.php @@ -0,0 +1,33 @@ +setState('circolare.id', (int) $app->input->getInt('id')); + parent::populateState(); + } + + public function getItem($pk = null) + { + $pk = $pk ?: (int) $this->getState('circolare.id'); + if (!$pk) return null; + + $db = Factory::getContainer()->get('DatabaseDriver'); + $q = $db->getQuery(true) + ->select('*') + ->from($db->quoteName('#__circolari')) + ->where($db->quoteName('id') . ' = ' . (int) $pk) + ->where($db->quoteName('state') . ' = 1'); + $db->setQuery($q); + + return $db->loadObject(); + } +} diff --git a/site/src/Service/Router.php b/site/src/Service/Router.php index cdb2142..6938101 100644 --- a/site/src/Service/Router.php +++ b/site/src/Service/Router.php @@ -1,95 +1,26 @@ - * @copyright 2025 Tommaso Cippitelli - * @license GNU General Public License version 2 or later; see LICENSE.txt - */ - namespace Pcrt\Component\Circolari\Site\Service; -// No direct access -defined('_JEXEC') or die; +\defined('_JEXEC') or die; -use Joomla\CMS\Component\Router\RouterViewConfiguration; -use Joomla\CMS\Component\Router\RouterView; -use Joomla\CMS\Component\Router\Rules\StandardRules; -use Joomla\CMS\Component\Router\Rules\NomenuRules; -use Joomla\CMS\Component\Router\Rules\MenuRules; -use Joomla\CMS\Factory; -use Joomla\CMS\Categories\Categories; use Joomla\CMS\Application\SiteApplication; -use Joomla\CMS\Categories\CategoryFactoryInterface; -use Joomla\CMS\Categories\CategoryInterface; -use Joomla\Database\DatabaseInterface; use Joomla\CMS\Menu\AbstractMenu; -use Joomla\CMS\Component\ComponentHelper; +use Joomla\CMS\Component\Router\RouterView; +use Joomla\CMS\Component\Router\RouterViewConfiguration; +use Joomla\CMS\Component\Router\Rules\MenuRules; +use Joomla\CMS\Component\Router\Rules\StandardRules; -/** - * Class CircolariRouter - * - */ class Router extends RouterView { - private $noIDs; - /** - * The category factory - * - * @var CategoryFactoryInterface - * - * @since 1.0.0 - */ - private $categoryFactory; + public function __construct(SiteApplication $app, AbstractMenu $menu) + { + $circolare = new RouterViewConfiguration('circolare'); + $circolare->setKey('id'); + $this->registerView($circolare); - /** - * The category cache - * - * @var array - * - * @since 1.0.0 - */ - private $categoryCache = []; + parent::__construct($app, $menu); - public function __construct(SiteApplication $app, AbstractMenu $menu, CategoryFactoryInterface $categoryFactory, DatabaseInterface $db) - { - $params = ComponentHelper::getParams('com_circolari'); - $this->noIDs = (bool) $params->get('sef_ids'); - $this->categoryFactory = $categoryFactory; - - - - parent::__construct($app, $menu); - - $this->attachRule(new MenuRules($this)); - $this->attachRule(new StandardRules($this)); - $this->attachRule(new NomenuRules($this)); - } - - - - - - - /** - * Method to get categories from cache - * - * @param array $options The options for retrieving categories - * - * @return CategoryInterface The object containing categories - * - * @since 1.0.0 - */ - private function getCategories(array $options = []): CategoryInterface - { - $key = serialize($options); - - if (!isset($this->categoryCache[$key])) - { - $this->categoryCache[$key] = $this->categoryFactory->createCategory($options); - } - - return $this->categoryCache[$key]; - } + $this->attachRule(new MenuRules($this)); + $this->attachRule(new StandardRules($this)); + } } diff --git a/site/src/View/Circolare/HtmlView.php b/site/src/View/Circolare/HtmlView.php new file mode 100644 index 0000000..08146db --- /dev/null +++ b/site/src/View/Circolare/HtmlView.php @@ -0,0 +1,37 @@ +item = $this->get('Item'); + + if (!$this->item) { + throw new \Exception(Text::_('COM_CIRCOLARI_ITEM_NOT_FOUND'), 404); + } + + // Se il layout non c'è, stampa un fallback minimale (debug-friendly) + $tplPath = JPATH_COMPONENT_SITE . '/tmpl/circolare/default.php'; + if (!is_file($tplPath)) { + $title = htmlspecialchars($this->item->title ?? 'Circolare'); + $body = $this->item->description ?? $this->item->testo ?? $this->item->descrizione ?? ''; + $html = '
' + . '' + . '
'.$body.'
' + . '
'; + + $this->document->setBuffer($html, 'component'); + return; + } + + parent::display($tpl); + } +} diff --git a/site/tmpl/circolare/default.php b/site/tmpl/circolare/default.php new file mode 100644 index 0000000..3744677 --- /dev/null +++ b/site/tmpl/circolare/default.php @@ -0,0 +1,26 @@ +item; +?> +
+ + +
+ description ?: $item->testo ?: $item->descrizione ?: '(Nessun testo)'; ?> +
+ + attachment)) : ?> +

+ + + +

+ +
diff --git a/site/tmpl/circolare/default.xml b/site/tmpl/circolare/default.xml new file mode 100644 index 0000000..8f21b18 --- /dev/null +++ b/site/tmpl/circolare/default.xml @@ -0,0 +1,29 @@ + + + + + + + + +
+ + + +
+
+ + + +
+ + + + + +
+
+