Assegnazione url corretto alla singola circolare
This commit is contained in:
25
site/services/provider.php
Normal file
25
site/services/provider.php
Normal file
@ -0,0 +1,25 @@
|
||||
<?php
|
||||
\defined('_JEXEC') or die;
|
||||
|
||||
use Joomla\DI\Container;
|
||||
use Joomla\DI\ServiceProviderInterface;
|
||||
|
||||
return new class implements ServiceProviderInterface {
|
||||
public function register(Container $c)
|
||||
{
|
||||
// Joomla 4/5
|
||||
if (class_exists('Joomla\\CMS\\Extension\\Service\\Provider\\MVCComponent')) {
|
||||
$cls = 'Joomla\\CMS\\Extension\\Service\\Provider\\MVCComponent';
|
||||
$c->registerServiceProvider(new $cls('Pcrt\\Component\\Circolari'));
|
||||
}
|
||||
|
||||
// Fallback per ambienti older: comunque registra il namespace PSR-4
|
||||
if (class_exists('JLoader') && \defined('JPATH_SITE')) {
|
||||
\JLoader::registerNamespace(
|
||||
'Pcrt\\Component\\Circolari\\Site',
|
||||
JPATH_SITE . '/components/com_circolari/src',
|
||||
false, false, 'psr4'
|
||||
);
|
||||
}
|
||||
}
|
||||
};
|
||||
@ -5,22 +5,126 @@ namespace Pcrt\Component\Circolari\Site\Service;
|
||||
|
||||
use Joomla\CMS\Application\SiteApplication;
|
||||
use Joomla\CMS\Menu\AbstractMenu;
|
||||
use Joomla\CMS\Factory;
|
||||
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;
|
||||
use Joomla\CMS\Component\Router\Rules\MenuRules;
|
||||
use Joomla\CMS\Component\Router\Rules\NomenuRules;
|
||||
|
||||
class Router extends RouterView
|
||||
{
|
||||
public function __construct(SiteApplication $app, AbstractMenu $menu)
|
||||
{
|
||||
// Parent: category (annidata → più segmenti)
|
||||
$category = new RouterViewConfiguration('category');
|
||||
$category->setKey('id')->setNestable();
|
||||
$this->registerView($category);
|
||||
|
||||
// Child: circolare (lega la category tramite catid)
|
||||
$circolare = new RouterViewConfiguration('circolare');
|
||||
$circolare->setKey('id');
|
||||
$circolare->setKey('id')->setParent($category, 'catid', 'id');
|
||||
$this->registerView($circolare);
|
||||
|
||||
parent::__construct($app, $menu);
|
||||
|
||||
$this->attachRule(new MenuRules($this));
|
||||
// Regole standard di routing
|
||||
$this->attachRule(new StandardRules($this));
|
||||
$this->attachRule(new MenuRules($this));
|
||||
$this->attachRule(new NomenuRules($this));
|
||||
}
|
||||
|
||||
/* ---------------- BUILD ---------------- */
|
||||
|
||||
// Segmenti categoria = path completo (parent1/parent2/figlia)
|
||||
public function getCategorySegment($id, $query)
|
||||
{
|
||||
$id = (int) ($id ?: 0);
|
||||
if ($id <= 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$db = Factory::getContainer()->get('DatabaseDriver');
|
||||
$path = (string) $db->setQuery(
|
||||
$db->getQuery(true)
|
||||
->select('path')
|
||||
->from('#__categories')
|
||||
->where('id = ' . $id)
|
||||
->where("extension = 'com_content'")
|
||||
)->loadResult();
|
||||
|
||||
return $path !== '' ? array_filter(explode('/', $path)) : [(string) $id];
|
||||
}
|
||||
|
||||
// Ultimo segmento = SOLO alias della circolare
|
||||
public function getCircolareSegment($id, $query)
|
||||
{
|
||||
$db = Factory::getContainer()->get('DatabaseDriver');
|
||||
$alias = (string) $db->setQuery(
|
||||
$db->getQuery(true)
|
||||
->select('alias')
|
||||
->from('#__circolari')
|
||||
->where('id = ' . (int) $id)
|
||||
)->loadResult();
|
||||
|
||||
// Nessun fallback numerico: imponiamo l'alias
|
||||
return [$alias];
|
||||
}
|
||||
|
||||
/* ---------------- PARSE ---------------- */
|
||||
|
||||
// Ricava catid accumulando i segmenti e risolvendo per PATH completo
|
||||
public function getCategoryId($segment, $query)
|
||||
{
|
||||
static $segments = [];
|
||||
$segments[] = $segment;
|
||||
$path = implode('/', $segments);
|
||||
|
||||
$db = Factory::getContainer()->get('DatabaseDriver');
|
||||
|
||||
// match per path (univoco in com_content)
|
||||
$id = (int) $db->setQuery(
|
||||
$db->getQuery(true)
|
||||
->select('id')
|
||||
->from('#__categories')
|
||||
->where('path = ' . $db->quote($path))
|
||||
->where("extension = 'com_content'")
|
||||
)->loadResult();
|
||||
|
||||
if ($id > 0) {
|
||||
return $id;
|
||||
}
|
||||
|
||||
// fallback (singolo alias) se si visita direttamente un livello intermedio
|
||||
$id = (int) $db->setQuery(
|
||||
$db->getQuery(true)
|
||||
->select('id')
|
||||
->from('#__categories')
|
||||
->where('alias = ' . $db->quote($segment))
|
||||
->where("extension = 'com_content'")
|
||||
->order('level DESC')
|
||||
)->loadResult();
|
||||
|
||||
return $id ?: 0;
|
||||
}
|
||||
|
||||
// Alias circolare (+ catid già risolto) → id
|
||||
public function getCircolareId($segment, $query)
|
||||
{
|
||||
$catid = (int) ($query['catid'] ?? 0);
|
||||
|
||||
$db = Factory::getContainer()->get('DatabaseDriver');
|
||||
$q = $db->getQuery(true)
|
||||
->select('id')
|
||||
->from('#__circolari')
|
||||
->where('alias = ' . $db->quote($segment));
|
||||
|
||||
if ($catid > 0) {
|
||||
$q->where('catid = ' . $catid);
|
||||
}
|
||||
|
||||
$db->setQuery($q);
|
||||
|
||||
return (int) $db->loadResult() ?: 0;
|
||||
}
|
||||
}
|
||||
|
||||
33
site/tmpl/category/default.xml
Normal file
33
site/tmpl/category/default.xml
Normal file
@ -0,0 +1,33 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<metadata>
|
||||
<!-- titolo del tipo voce di menu, visibile nel backend -->
|
||||
<layout title="COM_CIRCOLARI_MENU_CATEGORY" option="com_circolari" />
|
||||
|
||||
<!-- campi che finiscono nella query dell'URL -->
|
||||
<fields name="request">
|
||||
<fieldset name="request">
|
||||
<!-- forza la view "category" -->
|
||||
<field name="view" type="hidden" default="category" />
|
||||
<!-- scegli la categoria di Joomla (com_content) che fa da base del percorso -->
|
||||
<field
|
||||
name="id"
|
||||
type="category"
|
||||
extension="com_content"
|
||||
label="JCATEGORY"
|
||||
description="Seleziona la categoria di com_content da usare come base del percorso URL."
|
||||
required="true"
|
||||
default="11"
|
||||
/>
|
||||
</fieldset>
|
||||
</fields>
|
||||
|
||||
<!-- parametri opzionali della voce (non obbligatori) -->
|
||||
<fields name="params">
|
||||
<fieldset name="basic" label="JOPTIONS">
|
||||
<field name="show_category_title" type="radio" label="COM_CIRCOLARI_SHOW_CATEGORY_TITLE" default="0">
|
||||
<option value="0">JHIDE</option>
|
||||
<option value="1">JSHOW</option>
|
||||
</field>
|
||||
</fieldset>
|
||||
</fields>
|
||||
</metadata>
|
||||
Reference in New Issue
Block a user