131 lines
4.1 KiB
PHP
131 lines
4.1 KiB
PHP
<?php
|
|
namespace Pcrt\Component\Circolari\Site\Service;
|
|
|
|
\defined('_JEXEC') or die;
|
|
|
|
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\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 categoria_id)
|
|
$circolare = new RouterViewConfiguration('circolari');
|
|
$circolare->setKey('id')->setParent($category, 'categoria_id', 'id');
|
|
$this->registerView($circolare);
|
|
|
|
parent::__construct($app, $menu);
|
|
|
|
// 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('#__circolari_categorie')
|
|
->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 categoria_id 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('#__circolari_categorie')
|
|
->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('#__circolari_categorie')
|
|
->where('alias = ' . $db->quote($segment))
|
|
->where("extension = 'com_content'")
|
|
->order('level DESC')
|
|
)->loadResult();
|
|
|
|
return $id ?: 0;
|
|
}
|
|
|
|
// Alias circolare (+ categoria_id già risolto) → id
|
|
public function getCircolareId($segment, $query)
|
|
{
|
|
$categoria_id = (int) ($query['categoria_id'] ?? 0);
|
|
|
|
$db = Factory::getContainer()->get('DatabaseDriver');
|
|
$q = $db->getQuery(true)
|
|
->select('id')
|
|
->from('#__circolari')
|
|
->where('alias = ' . $db->quote($segment));
|
|
|
|
if ($categoria_id > 0) {
|
|
$q->where('categoria_id = ' . $categoria_id);
|
|
}
|
|
|
|
$db->setQuery($q);
|
|
|
|
return (int) $db->loadResult() ?: 0;
|
|
}
|
|
}
|