Files
pkg_circolari/site/tmpl/form/default.php
2025-09-23 11:54:33 +02:00

445 lines
21 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;
use Joomla\CMS\Editor\Editor;
use Joomla\CMS\Plugin\PluginHelper;
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';
// Editor preferito (CKEditor se attivo, altrimenti editor di default)
$preferred = 'ckeditor';
$editorName = PluginHelper::isEnabled('editors', $preferred)
? $preferred
: Factory::getApplication()->get('editor');
$editor = Editor::getInstance($editorName);
// Helper piccole funzioni
function e($s){ return htmlspecialchars((string)$s, ENT_QUOTES, 'UTF-8'); }
?>
<div class="container py-3">
<h2 class="mb-3"><?php echo $item->id ?? '' ? Text::_('Modifica circolare') : Text::_('Nuova circolare'); ?></h2>
<form action="<?php echo $action; ?>" method="post" name="adminForm" id="adminForm"
class="form-validate" enctype="multipart/form-data">
<ul class="nav nav-tabs" id="circolareTabs" role="tablist">
<li class="nav-item" role="presentation">
<button class="nav-link active" id="tab-contenuto" data-bs-toggle="tab" data-bs-target="#pane-contenuto" type="button" role="tab" aria-controls="pane-contenuto" aria-selected="true">
<?php echo Text::_('Contenuto'); ?>
</button>
</li>
<li class="nav-item" role="presentation">
<button class="nav-link" id="tab-media" data-bs-toggle="tab" data-bs-target="#pane-media" type="button" role="tab" aria-controls="pane-media" aria-selected="false">
<?php echo Text::_('Immagini/Allegati'); ?>
</button>
</li>
<li class="nav-item" role="presentation">
<button class="nav-link" id="tab-firma" data-bs-toggle="tab" data-bs-target="#pane-firma" type="button" role="tab" aria-controls="pane-firma" aria-selected="false">
<?php echo Text::_('Firma'); ?>
</button>
</li>
</ul>
<div class="tab-content border border-top-0 rounded-bottom p-3 shadow-sm" id="circolareTabsContent">
<div class="tab-pane fade show active" id="pane-contenuto" role="tabpanel" aria-labelledby="tab-contenuto" tabindex="0">
<div class="row g-3">
<div class="col-lg-8">
<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 e($item->title ?? ''); ?>">
</div>
<div class="mb-3">
<label class="form-label" for="description"><?php echo Text::_('Testo'); ?></label>
<?php
$desc = !empty($item->description) ? $item->description : '';
echo $editor->display('description', $desc, '100%', '320', '60', '20', true);
?>
<div class="form-text"><?php echo Text::_('Inserisci il contenuto della circolare.'); ?></div>
</div>
</div>
<div class="col-lg-4">
<div class="card h-100">
<div class="card-body">
<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 e($item->alias ?? ''); ?>">
</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;
?>
<option value="<?php echo $cid; ?>"
<?php echo (!empty($item->categoria_id) && (int)$item->categoria_id === $cid) ? 'selected' : ''; ?>>
<?php echo e($ctitle); ?>
</option>
<?php endforeach; ?>
</select>
</div>
<div class="mb-0">
<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>
</div>
</div>
</div>
</div>
<div class="tab-pane fade" id="pane-media" role="tabpanel" aria-labelledby="tab-media" tabindex="0">
<?php $existingImage = (string)($item->image ?? ''); ?>
<div class="mb-4">
<label class="form-label" for="image_file"><?php echo Text::_('Immagine'); ?></label>
<?php if ($existingImage) : ?>
<div class="d-flex align-items-center gap-3 mb-2">
<img src="<?php echo e($existingImage); ?>" alt="Anteprima immagine"
style="max-height:80px; width:auto; border:1px solid #eee; padding:2px;">
<div class="form-check">
<input class="form-check-input" type="checkbox" id="image_remove" name="image_remove" value="1">
<label class="form-check-label" for="image_remove"><?php echo Text::_('Rimuovi immagine'); ?></label>
</div>
</div>
<input type="hidden" name="image_existing" value="<?php echo e($existingImage); ?>">
<?php endif; ?>
<input type="file" id="image_file" name="image_file" class="form-control"
accept="image/*,.jpg,.jpeg,.JPG,.JPEG,.png,.gif,.webp,.svg,.heic,.HEIC,.heif,.bmp,.tif,.tiff">
<div class="form-text"><?php echo Text::_('Carica una nuova immagine (opzionale).'); ?></div>
</div>
<?php
$existingAttachments = [];
if (!empty($this->attachments) && is_array($this->attachments)) {
$existingAttachments = $this->attachments;
} elseif (!empty($item->attachments) && is_array($item->attachments)) {
$existingAttachments = $item->attachments;
}
?>
<div class="mb-2">
<label class="form-label d-block"><?php echo Text::_('Allegati'); ?></label>
<div id="attachments-list" class="row g-3">
<?php $idx = 0;
foreach ($existingAttachments as $att) :
$path = (string)(is_array($att) ? ($att['path'] ?? '') : ($att->path ?? ''));
$title = (string)(is_array($att) ? ($att['title'] ?? '') : ($att->title ?? ''));
$ordering = (int)(is_array($att) ? ($att['ordering'] ?? 0) : ($att->ordering ?? 0));
if ($path === '') continue;
$pathEsc = e($path);
$titleEsc = e($title);
?>
<div class="col-12" data-att-row>
<div class="border rounded p-3 bg-light">
<div class="mb-2">
<strong class="d-block mb-1"><?php echo Text::_('File esistente'); ?>:</strong>
<a href="<?php echo $pathEsc; ?>" target="_blank" rel="noopener">
<?php echo e(basename(htmlspecialchars_decode($path))); ?>
</a>
</div>
<div class="row g-2 align-items-end">
<div class="col-md-5">
<label class="form-label" for="att_<?php echo $idx; ?>_title"><?php echo Text::_('Titolo'); ?></label>
<input type="text" class="form-control"
id="att_<?php echo $idx; ?>_title"
name="attachments[<?php echo $idx; ?>][title]"
value="<?php echo $titleEsc; ?>">
</div>
<div class="col-md-2">
<label class="form-label" for="att_<?php echo $idx; ?>_ordering"><?php echo Text::_('Ordine'); ?></label>
<input type="number" class="form-control"
id="att_<?php echo $idx; ?>_ordering"
name="attachments[<?php echo $idx; ?>][ordering]"
value="<?php echo (int)$ordering; ?>">
</div>
<div class="col-md-4">
<label class="form-label" for="att_<?php echo $idx; ?>_file"><?php echo Text::_('Sostituisci file'); ?></label>
<input type="file" class="form-control"
id="att_<?php echo $idx; ?>_file"
name="attachments[<?php echo $idx; ?>][file]"
accept="*/*,image/*,.pdf,.doc,.docx,.xls,.xlsx,.ppt,.pptx,.zip,.jpg,.jpeg,.JPG,.JPEG,.png,.gif,.webp,.svg,.heic,.HEIC,.heif,.bmp,.tif,.tiff">
<div class="form-text"><?php echo Text::_('Lascia vuoto per mantenere il file esistente.'); ?></div>
</div>
<div class="col-md-1">
<div class="form-check mt-4">
<input class="form-check-input" type="checkbox"
id="att_<?php echo $idx; ?>_remove"
name="attachments[<?php echo $idx; ?>][remove]"
value="1">
<label class="form-check-label" for="att_<?php echo $idx; ?>_remove"><?php echo Text::_('Rimuovi'); ?></label>
</div>
</div>
</div>
<input type="hidden" name="attachments[<?php echo $idx; ?>][path]" value="<?php echo $pathEsc; ?>">
</div>
</div>
<?php $idx++; endforeach; ?>
</div>
<div class="d-flex gap-2 mt-2">
<button type="button" class="btn btn-outline-primary" id="add-attachment-btn">
+ <?php echo Text::_('Aggiungi allegato'); ?>
</button>
<button type="button" class="btn btn-outline-danger d-none" id="remove-last-attachment-btn">
<?php echo Text::_('Rimuovi ultima riga'); ?>
</button>
</div>
<template id="attachment-row-template">
<div class="col-12" data-att-row>
<div class="border rounded p-3">
<div class="row g-2 align-items-end">
<div class="col-md-5">
<label class="form-label"><?php echo Text::_('Titolo'); ?></label>
<input type="text" class="form-control" name="__NAME__[title]" value="">
</div>
<div class="col-md-2">
<label class="form-label"><?php echo Text::_('Ordine'); ?></label>
<input type="number" class="form-control" name="__NAME__[ordering]" value="0">
</div>
<div class="col-md-4">
<label class="form-label"><?php echo Text::_('File'); ?></label>
<input type="file" class="form-control" name="__NAME__[file]"
accept="*/*,image/*,.pdf,.doc,.docx,.xls,.xlsx,.ppt,.pptx,.zip,.jpg,.jpeg,.JPG,.JPEG,.png,.gif,.webp,.svg,.heic,.HEIC,.heif,.bmp,.tif,.tiff" required>
</div>
<div class="col-md-1 d-flex justify-content-end">
<button type="button" class="btn btn-sm btn-outline-danger mt-4" data-remove-row>
<?php echo Text::_('Rimuovi'); ?>
</button>
</div>
</div>
</div>
</div>
</template>
</div>
</div>
<div class="tab-pane fade" id="pane-firma" role="tabpanel" aria-labelledby="tab-firma" tabindex="0">
<div class="row g-3">
<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' : '') . '>'
. e($nm) . '</option>';
}
?>
</select>
<div class="form-text"><?php echo Text::_('Anteprima bottoni:'); ?></div>
<div id="anteprima-bottoni" class="mt-1"></div>
</div>
<div class="col-md-4 firma-block">
<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 e(str_replace(' ', 'T', substr($sc, 0, 16)));
?>">
</div>
<div class="col-md-6 firma-block" style="display:none;">
<label class="form-label" for="jform_usergroup_ids"><?php echo Text::_('Gruppi che possono firmare'); ?></label>
<select id="jform_usergroup_ids" name="usergroup_ids[]" class="form-select" multiple size="8">
<?php foreach ((array) ($this->allUserGroups ?? []) as $g):
$gid = (int) ($g['id'] ?? 0);
$gtitle = (string) ($g['title'] ?? '');
$sel = in_array($gid, (array) ($this->selectedGroupIds ?? []), true) ? 'selected' : '';
?>
<option value="<?php echo $gid; ?>" <?php echo $sel; ?>><?php echo e($gtitle); ?></option>
<?php endforeach; ?>
</select>
<div class="form-text"><?php echo Text::_('Seleziona i gruppi utenti abilitati alla firma.'); ?></div>
</div>
<div class="col-md-6 firma-block">
<label class="form-label" for="jform_notify"><?php echo Text::_('Invia email agli aventi diritto'); ?></label>
<input type="hidden" name="notify" value="0">
<div class="form-check form-switch">
<input class="form-check-input" type="checkbox" id="jform_notify" name="notify" value="1">
<label class="form-check-label" for="jform_notify"><?php echo Text::_('Sì, invia subito'); ?></label>
</div>
<div class="form-text"><?php echo Text::_('Invia una notifica agli utenti dei gruppi selezionati.'); ?></div>
</div>
</div>
</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) ($this->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() {
// Allegati repeater
var addBtn = document.getElementById('add-attachment-btn');
var removeLastBtn = document.getElementById('remove-last-attachment-btn');
var list = document.getElementById('attachments-list');
var tpl = document.getElementById('attachment-row-template')?.content;
var nextIndex = <?php echo (int)($idx ?? 0); ?>;
function toggleRemoveLast() {
var rows = list?.querySelectorAll('[data-att-row].is-new') || [];
if (removeLastBtn) removeLastBtn.classList.toggle('d-none', rows.length === 0);
}
function addRow() {
if (!tpl || !list) return;
var clone = document.importNode(tpl, true);
clone.querySelectorAll('input[name*="__NAME__"]').forEach(function(el) {
el.name = el.name.replace('__NAME__', 'attachments[' + nextIndex + ']');
});
var row = clone.querySelector('[data-att-row]');
row.classList.add('is-new');
list.appendChild(clone);
nextIndex++;
toggleRemoveLast();
}
function removeRow(btn) {
var row = btn.closest('[data-att-row]');
if (row) { row.remove(); toggleRemoveLast(); }
}
addBtn && addBtn.addEventListener('click', addRow);
removeLastBtn && removeLastBtn.addEventListener('click', function() {
var rows = list.querySelectorAll('[data-att-row].is-new');
if (rows.length) { rows[rows.length - 1].remove(); toggleRemoveLast(); }
});
list && list.addEventListener('click', function(e) {
if (e.target && e.target.hasAttribute('data-remove-row')) removeRow(e.target);
});
toggleRemoveLast();
// Firma: show/hide blocchi + anteprima bottoni
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');
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>