Assegnazione url corretto alla singola circolare
This commit is contained in:
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user