{source}<?php
defined('_JEXEC') or die;
use Joomla\CMS\Factory;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Mail\MailHelper;
use Joomla\CMS\Session\Session;
use Joomla\CMS\Uri\Uri;
$app = Factory::getApplication();
$input = $app->getInput();
$config = Factory::getConfig();
$siteFromEmail = (string) $config->get('mailfrom');
$siteFromName = (string) $config->get('fromname');
if (!function_exists('gw_e')) {
function gw_e($str) {
return htmlspecialchars((string)$str, ENT_QUOTES, 'UTF-8');
}
}
$sentOk = false;
$errors = [];
$nome = $input->getString('gw_nome', '');
$cognome = $input->getString('gw_cognome', '');
$telefono= $input->getString('gw_telefono', '');
$email = $input->getString('gw_email', '');
$motivo = $input->getString('gw_motivo', '');
if ($input->getMethod() === 'POST' && $input->post->getInt('gw_submit', 0) === 1)
{
if (!Session::checkToken())
{
$errors[] = 'Sessione scaduta. Ricarica la pagina e riprova.';
}
$nome = trim($nome);
$cognome = trim($cognome);
$telefono = trim($telefono);
$email = trim($email);
$motivo = trim($motivo);
if ($nome === '') $errors[] = 'Inserisci il nome.';
if ($cognome === '') $errors[] = 'Inserisci il cognome.';
if ($telefono === '') $errors[] = 'Inserisci il telefono.';
if ($email === '' || !MailHelper::isEmailAddress($email)) $errors[] = 'Inserisci una email valida.';
if ($motivo === '') $errors[] = 'Inserisci il motivo del contatto.';
$telefono_norm = preg_replace('/[^0-9+\s().-]/', '', $telefono);
if (empty($errors))
{
$mailer = Factory::getMailer();
$subjectUser = 'Conferma ricezione richiesta';
$subjectAdmin = 'Nuova richiesta dal sito';
$fullName = $nome . ' ' . $cognome;
$bodyUser =
"Hi $fullName,
abbiamo ricevuto la tua richiesta. Ti ricontatteremo al più presto.
Riepilogo:
- Nome: $nome
- Cognome: $cognome
- Telefono: $telefono_norm
- Email: $email
- Motivo: $motivo
Grazie.";
$bodyAdmin =
"Nuova richiesta dal sito:
- Nome: $nome
- Cognome: $cognome
- Telefono: $telefono_norm
- Email: $email
- Motivo: $motivo
IP: " . $input->server->getString('REMOTE_ADDR', 'n/d') . "
Pagina: " . Uri::current();
$mailer->clearAllRecipients();
$mailer->setSender([$siteFromEmail, $siteFromName]);
$mailer->addRecipient($siteFromEmail);
$mailer->setSubject($subjectAdmin);
$mailer->setBody($bodyAdmin);
$sendAdmin = $mailer->Send();
$mailer->clearAllRecipients();
$mailer->setSender([$siteFromEmail, $siteFromName]);
$mailer->addRecipient($email);
$mailer->addReplyTo([$siteFromEmail, $siteFromName]);
$mailer->setSubject($subjectUser);
$mailer->setBody($bodyUser);
$sendUser = $mailer->Send();
if ($sendAdmin !== true) $errors[] = 'Errore invio al gestore.';
if ($sendUser !== true) $errors[] = 'Errore invio conferma utente.';
if (empty($errors))
{
$sentOk = true;
$nome = $cognome = $telefono = $email = $motivo = '';
}
}
}
?>
<style>
.gw-form-wrap{
max-width: 720px;
margin: 18px auto;
padding: 22px;
border-radius: 16px;
background: #f6f4f6;
border: 1px solid rgba(160,90,168,.18);
box-shadow: 0 10px 26px rgba(55,40,63,.08);
color: #2b2430;
}
.gw-form-title{
font-size: 22px;
font-weight: 700;
margin-bottom: 14px;
color: #2b2430;
}
.gw-grid{
display: grid;
grid-template-columns: 1fr 1fr;
gap: 12px;
}
@media (max-width: 640px){
.gw-grid{ grid-template-columns: 1fr; }
}
.gw-field label{
font-size: 12px;
font-weight: 600;
margin-bottom: 6px;
display: block;
color: #7d3f8a;
}
.gw-field input,
.gw-field textarea{
width:100%;
padding: 12px;
border-radius: 12px;
border: 1px solid rgba(160,90,168,.22);
background: #ffffff;
color: #2b2430;
outline: none;
transition: all .2s ease;
}
.gw-field input:focus,
.gw-field textarea:focus{
border-color: #a05aa8;
box-shadow: 0 0 0 3px rgba(160,90,168,.15);
}
.gw-btn{
border-radius: 12px;
padding: 12px 18px;
font-weight: 700;
background: linear-gradient(135deg,#a05aa8,#7d3f8a);
color:#fff;
border:0;
cursor:pointer;
transition: all .15s ease;
}
.gw-btn:hover{
transform: translateY(-1px);
box-shadow: 0 10px 20px rgba(125,63,138,.2);
}
.gw-alert{
margin: 10px 0 14px;
padding: 12px;
border-radius: 12px;
font-size: 14px;
}
.gw-alert.ok{
background: rgba(200,213,140,.2);
border: 1px solid rgba(200,213,140,.4);
}
.gw-alert.err{
background: rgba(160,90,168,.08);
border: 1px solid rgba(160,90,168,.25);
}
.gw-alert ul{
margin: 6px 0 0 18px;
}
</style>
<div class="gw-form-wrap">
<div class="gw-form-title">Contattaci</div>
<?php if ($sentOk): ?>
<div class="gw-alert ok">Richiesta inviata. Controlla la tua email.</div>
<?php endif; ?>
<?php if (!empty($errors)): ?>
<div class="gw-alert err">
<ul>
<?php foreach ($errors as $er): ?>
<li><?php echo gw_e($er); ?></li>
<?php endforeach; ?>
</ul>
</div>
<?php endif; ?>
<form method="post" action="<?php echo gw_e(Uri::current()); ?>" autocomplete="on">
<div class="gw-grid">
<div class="gw-field">
<label>Nome</label>
<input name="gw_nome" value="<?php echo gw_e($nome); ?>" required>
</div>
<div class="gw-field">
<label>Cognome</label>
<input name="gw_cognome" value="<?php echo gw_e($cognome); ?>" required>
</div>
<div class="gw-field">
<label>Telefono</label>
<input name="gw_telefono" value="<?php echo gw_e($telefono); ?>" required>
</div>
<div class="gw-field">
<label>Email</label>
<input name="gw_email" type="email" value="<?php echo gw_e($email); ?>" required>
</div>
<div class="gw-field" style="grid-column:1/-1;">
<label>Motivo del contatto</label>
<textarea name="gw_motivo" rows="5" required><?php echo gw_e($motivo); ?></textarea>
</div>
</div>
<div style="margin-top:16px;">
<button class="gw-btn" type="submit">Invia</button>
</div>
<input type="hidden" name="gw_submit" value="1">
<?php echo HTMLHelper::_('form.token'); ?>
</form>
</div>{/source}