297 lines
14 KiB
PHP
297 lines
14 KiB
PHP
@extends('layouts.adminlte')
|
|
@section('title', 'Nuova Email')
|
|
@section('page_title', 'Composizione Email')
|
|
|
|
@section('content')
|
|
<div class="row">
|
|
<div class="col-md-12">
|
|
<div class="card card-primary">
|
|
<div class="card-header">
|
|
<h3 class="card-title"><i class="fas fa-edit mr-2"></i>Nuova Email</h3>
|
|
</div>
|
|
<div class="card-body">
|
|
<form method="POST" action="{{ route('email.send') }}" id="emailForm" enctype="multipart/form-data">
|
|
@csrf
|
|
|
|
@if($senderAccounts->count() > 0)
|
|
<div class="form-group">
|
|
<label for="mittente_id">Da (mittente)</label>
|
|
<select name="mittente_id" id="mittente_id" class="form-control">
|
|
<option value="0">Sistema — {{ optional(\App\Models\EmailSetting::getActive())->getEffectiveFromAddress() ?? 'Configura email' }}</option>
|
|
@foreach($senderAccounts as $sender)
|
|
<option value="{{ $sender->id }}">{{ $sender->email_name ?: $sender->email_address }} <{{ $sender->email_address }}></option>
|
|
@endforeach
|
|
</select>
|
|
<small class="text-muted">Seleziona un mittente alternativo per l'invio. Lascia "Sistema" per usare l'account predefinito.</small>
|
|
</div>
|
|
@endif
|
|
|
|
<div class="form-group">
|
|
<label>Destinatari *</label>
|
|
<div class="mb-2">
|
|
<select name="destinatario_tipo" id="destinatario_tipo" class="form-control" onchange="toggleDestinatari()">
|
|
<option value="manuale">Email Manuale</option>
|
|
<option value="individui">Singoli Individui</option>
|
|
<option value="gruppo">Membri di un Gruppo</option>
|
|
<option value="mailing_list">Mailing List</option>
|
|
</select>
|
|
</div>
|
|
|
|
<div id="manual-input" class="destinatari-section">
|
|
<input type="text" name="to" id="to_manual" class="form-control"
|
|
placeholder="destinatario@email.it" value="{{ $prefill['to'] ?? '' }}">
|
|
</div>
|
|
|
|
<div id="individui-input" class="destinatari-section" style="display: none;">
|
|
<select name="individui[]" id="individui-select" class="form-control" multiple size="5">
|
|
@foreach($individui as $ind)
|
|
<option value="{{ $ind->id }}">{{ $ind->nome }} {{ $ind->cognome }} ({{ $ind->email_primaria ?? 'senza email' }})</option>
|
|
@endforeach
|
|
</select>
|
|
<small class="text-muted">Tieni premuto Ctrl per selezionare più individui</small>
|
|
</div>
|
|
|
|
<div id="gruppo-input" class="destinatari-section" style="display: none;">
|
|
<select name="gruppo_id" id="gruppo-select" class="form-control mb-2" onchange="loadGruppiContatti()">
|
|
<option value="">Seleziona gruppo...</option>
|
|
@foreach($gruppi as $gruppo)
|
|
<option value="{{ $gruppo->id }}">{{ $gruppo->nome }}</option>
|
|
@endforeach
|
|
</select>
|
|
<div id="gruppo-contatti" class="border rounded p-2" style="max-height: 150px; overflow-y: auto;">
|
|
<em class="text-muted">Seleziona un gruppo per vedere i contatti</em>
|
|
</div>
|
|
</div>
|
|
|
|
<div id="mailing-list-input" class="destinatari-section" style="display: none;">
|
|
<select name="mailing_list_id" id="mailing-list-select" class="form-control">
|
|
<option value="">Seleziona mailing list...</option>
|
|
@foreach($mailingLists as $list)
|
|
<option value="{{ $list->id }}">{{ $list->nome }} ({{ $list->contatti->count() }} contatti)</option>
|
|
@endforeach
|
|
</select>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label for="subject">Oggetto *</label>
|
|
<input type="text" name="subject" id="subject" class="form-control"
|
|
value="{{ $prefill['subject'] ?? '' }}" required>
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label for="body">Messaggio *</label>
|
|
<textarea name="body" id="body" class="form-control" rows="15" required>{{ $prefill['body'] ?? '' }}</textarea>
|
|
</div>
|
|
|
|
@if($firme->count() > 0)
|
|
<div class="form-group">
|
|
<label for="firma_id">Firma</label>
|
|
<select name="firma_id" id="firma_id" class="form-control">
|
|
<option value="">Nessuna</option>
|
|
@foreach($firme as $firma)
|
|
<option value="{{ $firma->id }}">{{ $firma->nome }}</option>
|
|
@endforeach
|
|
</select>
|
|
<small class="text-muted">Seleziona la firma da appender al messaggio.</small>
|
|
</div>
|
|
@endif
|
|
|
|
<div class="form-group">
|
|
<label>Allegati</label>
|
|
<div class="border rounded p-3 bg-light">
|
|
<div class="mb-3">
|
|
<button type="button" class="btn btn-sm btn-primary" data-toggle="modal" data-target="#documentiModal">
|
|
<i class="fas fa-folder-open mr-1"></i> Seleziona da Documenti
|
|
</button>
|
|
<label class="btn btn-sm btn-success mb-0">
|
|
<i class="fas fa-upload mr-1"></i> Carica file
|
|
<input type="file" name="allegati[]" id="allegati" multiple class="d-none" onchange="handleFileUpload(this)">
|
|
</label>
|
|
</div>
|
|
<div id="allegati-list"></div>
|
|
<input type="hidden" name="documenti_selezionati" id="documenti_selezionati" value="">
|
|
</div>
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<button type="submit" class="btn btn-primary">
|
|
<i class="fas fa-paper-plane mr-1"></i> Invia
|
|
</button>
|
|
<button type="button" class="btn btn-secondary ml-2" onclick="saveDraft()">
|
|
<i class="fas fa-save mr-1"></i> Salva Bozze
|
|
</button>
|
|
<a href="{{ route('email.index') }}" class="btn btn-default ml-2">
|
|
Annulla
|
|
</a>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="modal fade" id="documentiModal" tabindex="-1" role="dialog">
|
|
<div class="modal-dialog modal-lg" role="document">
|
|
<div class="modal-content">
|
|
<div class="modal-header">
|
|
<h5 class="modal-title"><i class="fas fa-folder-open mr-2"></i>Seleziona Documenti</h5>
|
|
<button type="button" class="close" data-dismiss="modal">
|
|
<span>×</span>
|
|
</button>
|
|
</div>
|
|
<div class="modal-body">
|
|
@if($documenti->count() > 0)
|
|
<div class="table-responsive">
|
|
<table class="table table-bordered table-hover table-sm">
|
|
<thead>
|
|
<tr>
|
|
<th style="width: 40px;"></th>
|
|
<th>Nome File</th>
|
|
<th>Tipologia</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
@foreach($documenti as $doc)
|
|
<tr>
|
|
<td>
|
|
<div class="custom-control custom-checkbox">
|
|
<input type="checkbox" class="custom-control-input doc-checkbox" id="doc-{{ $doc->id }}" value="{{ $doc->id }}" data-nome="{{ $doc->nome_file }}">
|
|
<label class="custom-control-label" for="doc-{{ $doc->id }}"></label>
|
|
</div>
|
|
</td>
|
|
<td>{{ $doc->nome_file }}</td>
|
|
<td><span class="badge badge-info">{{ $doc->tipologia ?: $doc->tipo }}</span></td>
|
|
</tr>
|
|
@endforeach
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
@else
|
|
<p class="text-muted">Nessun documento disponibile.</p>
|
|
@endif
|
|
</div>
|
|
<div class="modal-footer">
|
|
<button type="button" class="btn btn-secondary" data-dismiss="modal">Annulla</button>
|
|
<button type="button" class="btn btn-primary" onclick="aggiungiDocumenti()">
|
|
<i class="fas fa-plus mr-1"></i> Aggiungi
|
|
</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
@endsection
|
|
|
|
@section('scripts')
|
|
<script>
|
|
let allegatiSelezionati = [];
|
|
|
|
function toggleDestinatari() {
|
|
const tipo = document.getElementById('destinatario_tipo').value;
|
|
|
|
document.querySelectorAll('.destinatari-section').forEach(el => el.style.display = 'none');
|
|
|
|
if (tipo === 'manuale') {
|
|
document.getElementById('manual-input').style.display = 'block';
|
|
} else if (tipo === 'individui') {
|
|
document.getElementById('individui-input').style.display = 'block';
|
|
} else if (tipo === 'gruppo') {
|
|
document.getElementById('gruppo-input').style.display = 'block';
|
|
} else if (tipo === 'mailing_list') {
|
|
document.getElementById('mailing-list-input').style.display = 'block';
|
|
}
|
|
}
|
|
|
|
function loadGruppiContatti() {
|
|
const gruppoId = document.getElementById('gruppo-select').value;
|
|
const container = document.getElementById('gruppo-contatti');
|
|
|
|
if (!gruppoId) {
|
|
container.innerHTML = '<em class="text-muted">Seleziona un gruppo per vedere i contatti</em>';
|
|
return;
|
|
}
|
|
|
|
fetch('/gruppi/' + gruppoId + '/individui-email')
|
|
.then(response => response.json())
|
|
.then(data => {
|
|
if (data.length === 0) {
|
|
container.innerHTML = '<em class="text-muted">Nessun contatto email nel gruppo</em>';
|
|
return;
|
|
}
|
|
|
|
let html = '';
|
|
data.forEach(ind => {
|
|
if (ind.email) {
|
|
html += `<div class="form-check">
|
|
<input class="form-check-input" type="checkbox" name="gruppo_emails[]" value="${ind.email}" id="email-${ind.id}">
|
|
<label class="form-check-label" for="email-${ind.id}">${ind.cogname} ${ind.nome} - ${ind.email}</label>
|
|
</div>`;
|
|
}
|
|
});
|
|
container.innerHTML = html || '<em class="text-muted">Nessun contatto email nel gruppo</em>';
|
|
});
|
|
}
|
|
|
|
function aggiungiDocumenti() {
|
|
const checkboxes = document.querySelectorAll('.doc-checkbox:checked');
|
|
checkboxes.forEach(function(cb) {
|
|
const id = parseInt(cb.value);
|
|
const nome = cb.dataset.nome;
|
|
|
|
if (!allegatiSelezionati.includes(id)) {
|
|
allegatiSelezionati.push(id);
|
|
|
|
const container = document.getElementById('allegati-list');
|
|
const div = document.createElement('div');
|
|
div.className = 'd-flex align-items-center justify-content-between bg-white p-2 mb-2 rounded';
|
|
div.id = 'allegato-' + id;
|
|
div.innerHTML = '<span><i class="fas fa-file mr-2"></i>' + nome + '</span>' +
|
|
'<button type="button" class="btn btn-xs btn-danger" onclick="removeAllegato(' + id + ')">' +
|
|
'<i class="fas fa-times"></i></button>';
|
|
container.appendChild(div);
|
|
}
|
|
|
|
cb.checked = false;
|
|
});
|
|
|
|
document.getElementById('documenti_selezionati').value = allegatiSelezionati.join(',');
|
|
$('#documentiModal').modal('hide');
|
|
}
|
|
|
|
function removeAllegato(id) {
|
|
allegatiSelezionati = allegatiSelezionati.filter(function(a) { return a !== id; });
|
|
const el = document.getElementById('allegato-' + id);
|
|
if (el) el.remove();
|
|
document.getElementById('documenti_selezionati').value = allegatiSelezionati.join(',');
|
|
}
|
|
|
|
function handleFileUpload(input) {
|
|
if (!input.files) return;
|
|
const container = document.getElementById('allegati-list');
|
|
for (const file of input.files) {
|
|
const div = document.createElement('div');
|
|
div.className = 'd-flex align-items-center justify-content-between bg-white p-2 mb-2 rounded';
|
|
div.innerHTML = '<span><i class="fas fa-file mr-2"></i>' + file.name + ' (' + (file.size / 1024).toFixed(1) + ' KB)</span>' +
|
|
'<button type="button" class="btn btn-xs btn-danger" onclick="this.parentElement.remove()">' +
|
|
'<i class="fas fa-times"></i></button>';
|
|
container.appendChild(div);
|
|
}
|
|
input.value = '';
|
|
}
|
|
|
|
function saveDraft() {
|
|
const form = document.getElementById('emailForm');
|
|
const formData = new FormData(form);
|
|
|
|
fetch('{{ route('email.draft') }}', {
|
|
method: 'POST',
|
|
headers: { 'X-CSRF-TOKEN': '{{ csrf_token() }}' },
|
|
body: formData
|
|
})
|
|
.then(response => response.json())
|
|
.then(data => {
|
|
alert('Bozza salvata!');
|
|
});
|
|
}
|
|
</script>
|
|
@endsection |