aggiunta firma circolari
This commit is contained in:
@ -1,10 +1,12 @@
|
||||
<?php
|
||||
|
||||
namespace Pcrt\Component\Circolari\Site\Model;
|
||||
|
||||
\defined('_JEXEC') or die;
|
||||
|
||||
use Joomla\CMS\MVC\Model\ItemModel;
|
||||
use Joomla\CMS\Factory;
|
||||
use Joomla\CMS\User\UserHelper;
|
||||
|
||||
class CircolareModel extends ItemModel
|
||||
{
|
||||
@ -55,23 +57,90 @@ class CircolareModel extends ItemModel
|
||||
|
||||
return $row ?: null;
|
||||
}
|
||||
}
|
||||
|
||||
public function getBottoniFirma(int $firmatipoId): array
|
||||
{
|
||||
$buttons = []; // <- sempre inizializzato
|
||||
|
||||
/**
|
||||
* Incrementa gli hits della circolare.
|
||||
*/
|
||||
public function hit($pk = null)
|
||||
{
|
||||
$pk = $pk ?: (int) $this->getState($this->getName() . '.id');
|
||||
if (!$pk) {
|
||||
return false;
|
||||
if ($firmatipoId <= 0) {
|
||||
return $buttons;
|
||||
}
|
||||
|
||||
$db = Factory::getContainer()->get('DatabaseDriver');
|
||||
|
||||
// 1) Tabella relazionale
|
||||
$q = $db->getQuery(true)
|
||||
->select($db->quoteName(['id', 'label']))
|
||||
->from($db->quoteName('#__circolari_firmetipi_bottoni'))
|
||||
->where($db->quoteName('firmatipo_id') . ' = ' . (int) $firmatipoId)
|
||||
->order($db->escape('ordering ASC, id ASC'));
|
||||
$db->setQuery($q);
|
||||
$buttons = (array) $db->loadObjectList();
|
||||
|
||||
// 2) Fallback JSON
|
||||
if (!$buttons) {
|
||||
$q2 = $db->getQuery(true)
|
||||
->select($db->quoteName('bottoni_firma'))
|
||||
->from($db->quoteName('#__circolari_firmetipi'))
|
||||
->where($db->quoteName('id') . ' = ' . (int) $firmatipoId);
|
||||
$db->setQuery($q2);
|
||||
$json = (string) $db->loadResult();
|
||||
|
||||
if ($json) {
|
||||
$rows = json_decode($json, true) ?: [];
|
||||
foreach ($rows as $r) {
|
||||
$label = trim((string) ($r['etichetta'] ?? ''));
|
||||
if ($label !== '') {
|
||||
$buttons[] = (object) ['id' => 0, 'label' => $label];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $buttons;
|
||||
}
|
||||
|
||||
public function userCanFirmare(int $circolareId, ?int $userId = null): bool
|
||||
{
|
||||
$user = Factory::getUser($userId);
|
||||
if ($user->guest) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Gruppi (inclusa ereditarietà). In J4/J5 puoi usare UserHelper::getUserGroups
|
||||
$userGroups = array_map('intval', UserHelper::getUserGroups($user->id));
|
||||
if (!$userGroups) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$db = Factory::getContainer()->get('DatabaseDriver');
|
||||
|
||||
$q = $db->getQuery(true)
|
||||
->select('COUNT(*)')
|
||||
->from($db->quoteName('#__circolari_usergroups'))
|
||||
->where($db->quoteName('circolare_id') . ' = ' . (int) $circolareId)
|
||||
->where($db->quoteName('usergroup_id') . ' IN (' . implode(',', $userGroups) . ')');
|
||||
|
||||
$db->setQuery($q);
|
||||
return ((int) $db->loadResult()) > 0;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
$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;
|
||||
}
|
||||
|
||||
@ -14,14 +14,13 @@ class CircolariModel extends ListModel
|
||||
protected $filter_fields = [
|
||||
'id', 'c.id',
|
||||
'title', 'c.title',
|
||||
'created', 'c.created',
|
||||
'hits', 'c.hits',
|
||||
'categoria_id', 'c.categoria_id',
|
||||
'state', 'c.state',
|
||||
];
|
||||
|
||||
protected function populateState($ordering = 'c.created', $direction = 'DESC')
|
||||
{
|
||||
{
|
||||
$app = Factory::getApplication();
|
||||
|
||||
// paging
|
||||
@ -80,9 +79,6 @@ class CircolariModel extends ListModel
|
||||
}
|
||||
|
||||
// ordinamento
|
||||
$orderCol = $this->getState('list.ordering', 'c.created');
|
||||
$orderDir = $this->getState('list.direction', 'DESC');
|
||||
$q->order($db->escape($orderCol . ' ' . $orderDir));
|
||||
|
||||
return $q;
|
||||
}
|
||||
@ -116,4 +112,4 @@ class CircolariModel extends ListModel
|
||||
return \is_object($it) && !empty($it->id);
|
||||
}));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -22,7 +22,7 @@ class Router extends RouterView
|
||||
$this->registerView($category);
|
||||
|
||||
// Child: circolare (lega la category tramite categoria_id)
|
||||
$circolare = new RouterViewConfiguration('circolare');
|
||||
$circolare = new RouterViewConfiguration('circolari');
|
||||
$circolare->setKey('id')->setParent($category, 'categoria_id', 'id');
|
||||
$this->registerView($circolare);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user