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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user