Files
pkg_circolari/site/tmpl/form/default.php
2025-09-08 17:38:47 +02:00

210 lines
8.5 KiB
PHP

<?php
\defined('_JEXEC') or die;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Router\Route;
use Joomla\CMS\Factory;
use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Uri\Uri;
HTMLHelper::_('bootstrap.tooltip');
HTMLHelper::_('behavior.formvalidator');
$item = $this->item ?: (object) [];
$action = Route::_('index.php?option=com_circolari&task=form.save');
// Trova un Itemid valido per la lista (per il link "Annulla")
$menu = Factory::getApplication()->getMenu();
$component = ComponentHelper::getComponent('com_circolari');
$cancelItemId = 0;
foreach ((array) $menu->getItems('component_id', (int) $component->id) as $mi) {
$q = is_array($mi->query ?? null) ? $mi->query : [];
if (($q['option'] ?? '') === 'com_circolari' && ($q['view'] ?? '') === 'circolari') {
$cancelItemId = (int) $mi->id;
break;
}
}
$cancelUrl = $cancelItemId
? Route::_('index.php?Itemid=' . $cancelItemId)
: Uri::root() . 'index.php?option=com_circolari&view=circolari';
?>
<div class="container py-3">
<h2 class="mb-3"><?php echo Text::_('Nuova circolare'); ?></h2>
<form action="<?php echo $action; ?>" method="post" class="form-validate">
<div class="mb-3">
<label class="form-label" for="title"><?php echo Text::_('Titolo'); ?></label>
<input type="text" id="title" name="title" required
class="form-control"
value="<?php echo htmlspecialchars($item->title ?? '', ENT_QUOTES, 'UTF-8'); ?>">
</div>
<div class="mb-3">
<label class="form-label" for="alias"><?php echo Text::_('Alias (opzionale)'); ?></label>
<input type="text" id="alias" name="alias" class="form-control"
value="<?php echo htmlspecialchars($item->alias ?? '', ENT_QUOTES, 'UTF-8'); ?>">
</div>
<div class="mb-3">
<label class="form-label" for="categoria_id"><?php echo Text::_('Categoria'); ?></label>
<select id="categoria_id" name="categoria_id" class="form-select" required>
<option value="0">-- <?php echo Text::_('Seleziona'); ?> --</option>
<?php foreach ((array) $this->categorie as $c):
$cid = (int) ($c['id'] ?? 0);
$ctitle = (string) ($c['title'] ?? '');
$cstate = (int) ($c['state'] ?? 1);
if ($cstate !== 1) continue; // solo pubblicate
?>
<option value="<?php echo $cid; ?>"
<?php echo (!empty($item->categoria_id) && (int)$item->categoria_id === $cid) ? 'selected' : ''; ?>>
<?php echo htmlspecialchars($ctitle, ENT_QUOTES, 'UTF-8'); ?>
</option>
<?php endforeach; ?>
</select>
</div>
<div class="mb-3">
<label class="form-label" for="description"><?php echo Text::_('Testo'); ?></label>
<textarea id="description" name="description" class="form-control" rows="8"><?php
echo htmlspecialchars($item->description ?? '', ENT_QUOTES, 'UTF-8');
?></textarea>
</div>
<div class="row g-3">
<div class="col-md-4">
<label class="form-label" for="state"><?php echo Text::_('Stato'); ?></label>
<select id="state" name="state" class="form-select">
<option value="1" <?php echo ((int)($item->state ?? 1) === 1 ? 'selected' : ''); ?>>
<?php echo Text::_('Pubblicata'); ?>
</option>
<option value="0" <?php echo ((int)($item->state ?? 1) === 0 ? 'selected' : ''); ?>>
<?php echo Text::_('Bozza/Non pubblicata'); ?>
</option>
</select>
</div>
<div class="col-md-4">
<label class="form-label" for="jform_firma_obbligatoria"><?php echo Text::_('Firma obbligatoria'); ?></label>
<?php $fo = (int) ($item->firma_obbligatoria ?? 0); ?>
<select name="firma_obbligatoria" id="jform_firma_obbligatoria" class="form-select">
<option value="0" <?php echo $fo===0?'selected':''; ?>><?php echo Text::_('No'); ?></option>
<option value="1" <?php echo $fo===1?'selected':''; ?>><?php echo Text::_('Sì'); ?></option>
</select>
</div>
<div class="col-md-4 firma-block" style="display:none;">
<label class="form-label" for="jform_tipologia_firma_id"><?php echo Text::_('Tipologia firma'); ?></label>
<select name="tipologia_firma_id" id="jform_tipologia_firma_id" class="form-select">
<option value="0">-- <?php echo Text::_('Seleziona'); ?> --</option>
<?php
$selTipo = (int) ($item->tipologia_firma_id ?? 0);
foreach ((array)$this->firmetipi as $t) {
$tid = (int) $t['id'];
$nm = (string) $t['nome'];
echo '<option value="'.$tid.'" '.($selTipo===$tid?'selected':'').'>'
. htmlspecialchars($nm, ENT_QUOTES, 'UTF-8') . '</option>';
}
?>
</select>
<div class="form-text"><?php echo Text::_('Anteprima bottoni:'); ?></div>
<div id="anteprima-bottoni" class="mt-1"></div>
</div>
</div>
<div class="row g-3 mt-2 firma-block" style="display:none;">
<div class="col-md-6">
<label class="form-label required" id="jform_scadenza-lbl" for="jform_scadenza">
<?php echo Text::_('Scadenza firma'); ?><span class="star" aria-hidden="true">&nbsp;*</span>
</label>
<input type="datetime-local" name="scadenza" id="jform_scadenza" class="form-control"
value="<?php
$sc = (string) ($item->scadenza ?? '');
if ($sc !== '') { echo htmlspecialchars(str_replace(' ', 'T', substr($sc, 0, 16)), ENT_QUOTES, 'UTF-8'); }
?>">
</div>
</div>
<div class="mt-4 d-flex gap-2">
<button type="submit" class="btn btn-primary"><?php echo Text::_('Salva'); ?></button>
<a href="<?php echo $cancelUrl; ?>" class="btn btn-outline-secondary"><?php echo Text::_('Annulla'); ?></a>
</div>
<input type="hidden" name="id" value="<?php echo (int) ($item->id ?? 0); ?>">
<?php echo HTMLHelper::_('form.token'); ?>
<script type="application/json" id="bottoni-map"><?php
echo json_encode($this->bottoniMap ?? [], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
?></script>
<script>
(function(){
function byId(id){ return document.getElementById(id); }
const obb = byId('jform_firma_obbligatoria');
const tipo = byId('jform_tipologia_firma_id');
const scad = byId('jform_scadenza');
const blocks= document.querySelectorAll('.firma-block');
const mapEl = byId('bottoni-map');
const prev = byId('anteprima-bottoni');
const map = mapEl ? JSON.parse(mapEl.textContent || '{}') : {};
function renderPreview(){
if (!prev || !tipo) return;
const fid = tipo.value;
prev.innerHTML = '';
if (!fid || !map[fid] || !map[fid].length) return;
map[fid].forEach(function(lbl){
const span = document.createElement('span');
span.className = 'badge bg-secondary me-1 mb-1';
span.textContent = lbl;
prev.appendChild(span);
});
}
function toggleFirma(){
const need = obb && obb.value === '1';
blocks.forEach(function(el){ el.style.display = need ? '' : 'none'; });
if (scad){
if (need){
scad.setAttribute('required','required');
scad.setAttribute('aria-required','true');
// aggiungi asterisco al label se manca
var lbl = document.getElementById('jform_scadenza-lbl');
if (lbl){
lbl.classList.add('required');
if (!lbl.querySelector('.star')){
var s = document.createElement('span');
s.className = 'star';
s.setAttribute('aria-hidden','true');
s.innerHTML = '&nbsp;*';
lbl.appendChild(s);
}
}
} else {
scad.removeAttribute('required');
scad.removeAttribute('aria-required');
var lbl = document.getElementById('jform_scadenza-lbl');
if (lbl){
lbl.classList.remove('required');
var st = lbl.querySelector('.star'); if (st) st.remove();
}
}
}
renderPreview();
}
document.addEventListener('change', function(e){
if (e.target === obb) toggleFirma();
if (e.target === tipo) renderPreview();
});
document.addEventListener('DOMContentLoaded', function(){
toggleFirma();
renderPreview();
});
})();
</script>
</form>
</div>