gestione categorie interne & fix

This commit is contained in:
2025-09-01 12:56:30 +02:00
parent 16ac92a59e
commit f9b01eb01e
40 changed files with 2252 additions and 424 deletions

View File

@ -3,31 +3,75 @@ namespace Pcrt\Component\Circolari\Site\Model;
\defined('_JEXEC') or die;
use Joomla\CMS\Factory;
use Joomla\CMS\MVC\Model\ItemModel;
use Joomla\CMS\Factory;
class CircolareModel extends ItemModel
{
protected function populateState(): void
protected function populateState()
{
$app = Factory::getApplication();
$this->setState('circolare.id', (int) $app->input->getInt('id'));
$id = $app->input->getInt('id', 0);
$this->setState('circolare.id', $id);
$categoria_id = $app->input->getInt('categoria_id', 0);
$this->setState('filter.categoria_id', $categoria_id);
parent::populateState();
}
public function getItem($pk = null)
{
$pk = $pk ?: (int) $this->getState('circolare.id');
if (!$pk) return null;
$pk = (int) ($pk ?: $this->getState('circolare.id'));
$db = Factory::getContainer()->get('DatabaseDriver');
if ($pk <= 0) {
return null;
}
$db = $this->getDatabase();
$q = $db->getQuery(true)
->select('*')
->from($db->quoteName('#__circolari'))
->where($db->quoteName('id') . ' = ' . (int) $pk)
->where($db->quoteName('state') . ' = 1');
$db->setQuery($q);
->select('c.*') // <<< evita errori di colonne
->from($db->quoteName('#__circolari') . ' AS c')
->where('c.id = ' . (int) $pk);
return $db->loadObject();
// Se esistono le colonne, applica filtri
$cols = array_change_key_case(
$db->getTableColumns($db->replacePrefix('#__circolari'), false),
CASE_LOWER
);
// categoria_id (se impostato e colonna esiste)
$categoria_id = (int) $this->getState('filter.categoria_id', 0);
if ($categoria_id > 0 && isset($cols['categoria_id'])) {
$q->where('c.categoria_id = ' . $categoria_id);
}
// stato (se colonna esiste)
if (isset($cols['state'])) {
$q->where('COALESCE(c.state, 1) = 1');
}
$db->setQuery($q);
$row = $db->loadObject();
return $row ?: null;
}
}
/**
* Incrementa gli hits della circolare.
*/
public function hit($pk = null)
{
$pk = $pk ?: (int) $this->getState($this->getName() . '.id');
if (!$pk) {
return false;
}
$db = $this->getDatabase();
$query = $db->getQuery(true)
->update($db->quoteName('#__circolari'))
->set($db->quoteName('hits') . ' = ' . $db->quoteName('hits') . ' + 1')
->where($db->quoteName('id') . ' = ' . (int)$pk);
$db->setQuery($query)->execute();
return true;
}