fix doc
This commit is contained in:
@@ -201,7 +201,7 @@ class DocumentoController extends Controller
|
||||
$tipologieValidi = TipologiaDocumento::opzioni();
|
||||
$tipologieRule = 'in:' . implode(',', $tipologieValidi);
|
||||
|
||||
$data = $request->validate([
|
||||
$rules = [
|
||||
'nome_file' => 'required|string|max:255',
|
||||
'tipologia' => 'required|' . $tipologieRule,
|
||||
'contesto_tipo' => 'nullable|in:individuo,gruppo,evento,mailing',
|
||||
@@ -209,7 +209,13 @@ class DocumentoController extends Controller
|
||||
'note' => 'nullable|string',
|
||||
'tags' => 'nullable|array',
|
||||
'tags.*' => 'exists:tags,id',
|
||||
]);
|
||||
];
|
||||
|
||||
if ($documento->isLink()) {
|
||||
$rules['url'] = 'nullable|url|max:2048';
|
||||
}
|
||||
|
||||
$data = $request->validate($rules);
|
||||
|
||||
$contestoTipo = $data['contesto_tipo'] ?? null;
|
||||
$data['visibilita'] = $contestoTipo ?: 'pubblico';
|
||||
@@ -228,6 +234,10 @@ class DocumentoController extends Controller
|
||||
$data['visibilita_target_type'] = null;
|
||||
}
|
||||
|
||||
if ($documento->isLink()) {
|
||||
$data['url'] = $data['url'] ?? $documento->url;
|
||||
}
|
||||
|
||||
$documento->update($data);
|
||||
|
||||
if ($request->has('tags')) {
|
||||
@@ -246,10 +256,11 @@ class DocumentoController extends Controller
|
||||
$tipologieValidi = TipologiaDocumento::opzioni();
|
||||
$tipologieRule = 'in:' . implode(',', $tipologieValidi);
|
||||
|
||||
$data = $request->validate([
|
||||
$isLink = $request->input('tipo_caricamento') === 'link';
|
||||
|
||||
$rules = [
|
||||
'nome_file' => 'required|string|max:255',
|
||||
'tipologia' => 'required|' . $tipologieRule,
|
||||
'file' => 'required|file|max:10240',
|
||||
'visibilita' => 'required|in:pubblico,individuo,gruppo',
|
||||
'visibilita_target_id' => 'nullable|integer',
|
||||
'visibilita_target_type' => 'nullable|string',
|
||||
@@ -257,7 +268,47 @@ class DocumentoController extends Controller
|
||||
'repository_id' => 'nullable|integer|exists:storage_repositories,id',
|
||||
'tags' => 'nullable|array',
|
||||
'tags.*' => 'exists:tags,id',
|
||||
]);
|
||||
'tipo_caricamento' => 'required|in:file,link',
|
||||
'url' => 'required_if:tipo_caricamento,link|nullable|url|max:2048',
|
||||
];
|
||||
|
||||
if (!$isLink) {
|
||||
$rules['file'] = 'required|file|max:10240';
|
||||
}
|
||||
|
||||
$data = $request->validate($rules);
|
||||
|
||||
if ($isLink) {
|
||||
$linkVisibilitaTargetType = null;
|
||||
if ($data['visibilita'] === 'individuo' && $data['visibilita_target_id']) {
|
||||
$linkVisibilitaTargetType = Individuo::class;
|
||||
} elseif ($data['visibilita'] === 'gruppo' && $data['visibilita_target_id']) {
|
||||
$linkVisibilitaTargetType = Gruppo::class;
|
||||
}
|
||||
|
||||
$doc = Documento::create([
|
||||
'nome_file' => $data['nome_file'],
|
||||
'url' => $data['url'],
|
||||
'tipo' => 'link',
|
||||
'tipologia' => $data['tipologia'],
|
||||
'visibilita' => $data['visibilita'],
|
||||
'visibilita_target_id' => $data['visibilita_target_id'] ?? null,
|
||||
'visibilita_target_type' => $linkVisibilitaTargetType,
|
||||
'user_id' => auth()->id(),
|
||||
'cartella_id' => $data['cartella_id'] ?? null,
|
||||
'repository_id' => null,
|
||||
]);
|
||||
|
||||
if ($doc && $request->has('tags')) {
|
||||
$doc->tags()->sync($request->tags);
|
||||
}
|
||||
|
||||
$redirect = $request->_redirect ?? url()->previous();
|
||||
if ($request->filled('folder_id')) {
|
||||
$redirect = '/documenti?folder_id=' . $request->input('folder_id');
|
||||
}
|
||||
return redirect($redirect)->with('success', 'Documento (link) creato.');
|
||||
}
|
||||
|
||||
$repositoryId = $data['repository_id'] ?? null;
|
||||
|
||||
@@ -321,6 +372,10 @@ class DocumentoController extends Controller
|
||||
$this->authorizeRead('documenti');
|
||||
$documento = Documento::findOrFail($documento);
|
||||
|
||||
if ($documento->isLink()) {
|
||||
return redirect()->away($documento->url);
|
||||
}
|
||||
|
||||
$extension = pathinfo($documento->file_path, PATHINFO_EXTENSION);
|
||||
$filename = $extension ? $documento->nome_file . '.' . $extension : $documento->nome_file;
|
||||
|
||||
@@ -366,6 +421,12 @@ class DocumentoController extends Controller
|
||||
$this->authorizeRead('documenti');
|
||||
$documento = Documento::findOrFail($documento);
|
||||
|
||||
if ($documento->isLink()) {
|
||||
return response()->view('documenti.preview-fallback', [
|
||||
'documento' => $documento,
|
||||
])->header('X-Preview-Fallback', 'true');
|
||||
}
|
||||
|
||||
$previewableMimeTypes = [
|
||||
'image/jpeg', 'image/png', 'image/gif', 'image/webp', 'image/svg+xml',
|
||||
'application/pdf',
|
||||
@@ -416,7 +477,8 @@ class DocumentoController extends Controller
|
||||
$this->authorizeDelete('documenti');
|
||||
$documento = Documento::findOrFail($documento);
|
||||
|
||||
if ($documento->repository_id) {
|
||||
if (!$documento->isLink()) {
|
||||
if ($documento->repository_id) {
|
||||
$repo = StorageRepository::find($documento->repository_id);
|
||||
if ($repo) {
|
||||
$filesystem = $this->repoService->buildFilesystem($repo);
|
||||
@@ -424,10 +486,11 @@ class DocumentoController extends Controller
|
||||
$filesystem->delete($documento->file_path);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$disk = $this->resolveStorageDisk($documento);
|
||||
if (Storage::disk($disk)->exists($documento->file_path)) {
|
||||
Storage::disk($disk)->delete($documento->file_path);
|
||||
} else {
|
||||
$disk = $this->resolveStorageDisk($documento);
|
||||
if (Storage::disk($disk)->exists($documento->file_path)) {
|
||||
Storage::disk($disk)->delete($documento->file_path);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -604,8 +667,14 @@ class DocumentoController extends Controller
|
||||
return back()->with('error', 'Impossibile creare l\'archivio ZIP.');
|
||||
}
|
||||
|
||||
$skippedLinks = 0;
|
||||
$addedCount = 0;
|
||||
foreach ($documenti as $documento) {
|
||||
if ($documento->isLink()) {
|
||||
$skippedLinks++;
|
||||
continue;
|
||||
}
|
||||
|
||||
$extension = pathinfo($documento->file_path, PATHINFO_EXTENSION);
|
||||
$filename = $extension ? $documento->nome_file . '.' . $extension : $documento->nome_file;
|
||||
|
||||
@@ -638,11 +707,23 @@ class DocumentoController extends Controller
|
||||
$zip->close();
|
||||
|
||||
if ($addedCount === 0) {
|
||||
unlink($zipPath);
|
||||
return back()->with('error', 'Nessun file disponibile per il download.');
|
||||
if (file_exists($zipPath)) {
|
||||
@unlink($zipPath);
|
||||
}
|
||||
$message = 'Nessun file disponibile per il download.';
|
||||
if ($skippedLinks > 0) {
|
||||
$message .= " ({$skippedLinks} link esterni saltati)";
|
||||
}
|
||||
return back()->with('error', $message);
|
||||
}
|
||||
|
||||
return response()->download($zipPath, 'documenti.zip')->deleteFileAfterSend(true);
|
||||
$message = $addedCount . ' file scaricati.';
|
||||
if ($skippedLinks > 0) {
|
||||
$message .= " {$skippedLinks} link esterni saltati.";
|
||||
}
|
||||
|
||||
return response()->download($zipPath, 'documenti.zip')->deleteFileAfterSend(true)
|
||||
->with('success', $message);
|
||||
}
|
||||
|
||||
public function massDestroy(Request $request)
|
||||
@@ -659,18 +740,20 @@ class DocumentoController extends Controller
|
||||
$documenti = Documento::whereIn('id', $ids)->get();
|
||||
|
||||
foreach ($documenti as $documento) {
|
||||
if ($documento->repository_id) {
|
||||
$repo = StorageRepository::find($documento->repository_id);
|
||||
if ($repo) {
|
||||
$filesystem = $this->repoService->buildFilesystem($repo);
|
||||
if ($filesystem && $filesystem->fileExists($documento->file_path)) {
|
||||
$filesystem->delete($documento->file_path);
|
||||
if (!$documento->isLink()) {
|
||||
if ($documento->repository_id) {
|
||||
$repo = StorageRepository::find($documento->repository_id);
|
||||
if ($repo) {
|
||||
$filesystem = $this->repoService->buildFilesystem($repo);
|
||||
if ($filesystem && $filesystem->fileExists($documento->file_path)) {
|
||||
$filesystem->delete($documento->file_path);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$disk = $this->resolveStorageDisk($documento);
|
||||
if (Storage::disk($disk)->exists($documento->file_path)) {
|
||||
Storage::disk($disk)->delete($documento->file_path);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$disk = $this->resolveStorageDisk($documento);
|
||||
if (Storage::disk($disk)->exists($documento->file_path)) {
|
||||
Storage::disk($disk)->delete($documento->file_path);
|
||||
}
|
||||
}
|
||||
$documento->delete();
|
||||
|
||||
@@ -162,14 +162,18 @@ class EmailController extends Controller
|
||||
|
||||
$imapSuccess = false;
|
||||
$imapError = null;
|
||||
$attachments = $this->processAttachments($request);
|
||||
$attachmentResult = $this->processAttachments($request);
|
||||
$attachmentPaths = $attachmentResult['paths'];
|
||||
$linkDocs = $attachmentResult['links'];
|
||||
|
||||
$firmaId = $request->firma_id;
|
||||
|
||||
$body = $this->appendLinksToBody($validated['body'], $linkDocs);
|
||||
|
||||
foreach ($recipients as $recipient) {
|
||||
try {
|
||||
$bodyWithSig = $this->appendSignature($validated['body'], $firmaId, $settings);
|
||||
$this->sendViaImap($settings, $recipient, $validated['subject'], $bodyWithSig, $request->cc, $attachments);
|
||||
$bodyWithSig = $this->appendSignature($body, $firmaId, $settings);
|
||||
$this->sendViaImap($settings, $recipient, $validated['subject'], $bodyWithSig, $request->cc, $attachmentPaths);
|
||||
$imapSuccess = true;
|
||||
} catch (\Exception $e) {
|
||||
$imapError = $e->getMessage();
|
||||
@@ -178,7 +182,7 @@ class EmailController extends Controller
|
||||
}
|
||||
|
||||
try {
|
||||
$bodyWithSig = $this->appendSignature($validated['body'], $firmaId, $settings);
|
||||
$bodyWithSig = $this->appendSignature($body, $firmaId, $settings);
|
||||
$this->storeSentMessage($settings, $recipients, $validated['subject'], $bodyWithSig, $request->cc);
|
||||
} catch (\Exception $e) {
|
||||
\Illuminate\Support\Facades\Log::error('Store sent message error', ['error' => $e->getMessage()]);
|
||||
@@ -195,10 +199,13 @@ class EmailController extends Controller
|
||||
private function sendWithSender(Request $request, array $validated, array $recipients)
|
||||
{
|
||||
$sender = SenderAccount::findOrFail($request->mittente_id);
|
||||
$attachmentPaths = $this->resolveAttachmentPaths($request);
|
||||
$attachmentResult = $this->resolveAttachmentPaths($request);
|
||||
$attachmentPaths = $attachmentResult['paths'];
|
||||
$linkDocs = $attachmentResult['links'];
|
||||
|
||||
$settings = EmailSetting::getActive();
|
||||
$body = $this->appendSignature($validated['body'], $request->firma_id, $settings);
|
||||
$body = $this->appendLinksToBody($validated['body'], $linkDocs);
|
||||
$body = $this->appendSignature($body, $request->firma_id, $settings);
|
||||
|
||||
$ok = 0;
|
||||
$errors = [];
|
||||
@@ -583,6 +590,7 @@ class EmailController extends Controller
|
||||
private function processAttachments(Request $request): array
|
||||
{
|
||||
$paths = [];
|
||||
$links = [];
|
||||
|
||||
if ($request->hasFile('allegati')) {
|
||||
foreach ($request->file('allegati') as $file) {
|
||||
@@ -597,18 +605,21 @@ class EmailController extends Controller
|
||||
$ids = is_array($documentIds) ? $documentIds : explode(',', $documentIds);
|
||||
$docs = Documento::whereIn('id', $ids)->get();
|
||||
foreach ($docs as $doc) {
|
||||
if ($doc->file_path && Storage::exists($doc->file_path)) {
|
||||
if ($doc->isLink()) {
|
||||
$links[] = ['nome' => $doc->nome_file, 'url' => $doc->url];
|
||||
} elseif ($doc->file_path && Storage::exists($doc->file_path)) {
|
||||
$paths[] = Storage::path($doc->file_path);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $paths;
|
||||
return ['paths' => $paths, 'links' => $links];
|
||||
}
|
||||
|
||||
private function resolveAttachmentPaths(Request $request): array
|
||||
{
|
||||
$paths = [];
|
||||
$links = [];
|
||||
|
||||
if ($request->hasFile('allegati')) {
|
||||
foreach ($request->file('allegati') as $file) {
|
||||
@@ -623,13 +634,29 @@ class EmailController extends Controller
|
||||
$ids = is_array($documentIds) ? $documentIds : explode(',', $documentIds);
|
||||
$docs = Documento::whereIn('id', $ids)->get();
|
||||
foreach ($docs as $doc) {
|
||||
if ($doc->file_path && \Illuminate\Support\Facades\Storage::exists($doc->file_path)) {
|
||||
if ($doc->isLink()) {
|
||||
$links[] = ['nome' => $doc->nome_file, 'url' => $doc->url];
|
||||
} elseif ($doc->file_path && \Illuminate\Support\Facades\Storage::exists($doc->file_path)) {
|
||||
$paths[] = \Illuminate\Support\Facades\Storage::path($doc->file_path);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $paths;
|
||||
return ['paths' => $paths, 'links' => $links];
|
||||
}
|
||||
|
||||
private function appendLinksToBody(string $body, array $linkDocs): string
|
||||
{
|
||||
if (empty($linkDocs)) {
|
||||
return $body;
|
||||
}
|
||||
|
||||
$linksText = "\n\n---\nDocumenti collegati:\n";
|
||||
foreach ($linkDocs as $link) {
|
||||
$linksText .= "- {$link['nome']}: {$link['url']}\n";
|
||||
}
|
||||
|
||||
return $body . $linksText;
|
||||
}
|
||||
|
||||
private function storeSentMessage($settings, $recipients, $subject, $body, $cc = null)
|
||||
|
||||
@@ -37,6 +37,7 @@ class GruppoController extends Controller
|
||||
|
||||
$allColumnDefs = [
|
||||
['key' => 'nome', 'label' => 'Nome'],
|
||||
['key' => 'anno_fondazione', 'label' => 'Anno Fondazione'],
|
||||
['key' => 'descrizione', 'label' => 'Descrizione'],
|
||||
['key' => 'diocesi', 'label' => 'Diocesi'],
|
||||
['key' => 'livello', 'label' => 'Livello'],
|
||||
@@ -155,6 +156,7 @@ class GruppoController extends Controller
|
||||
$this->authorizeWrite('gruppi');
|
||||
$data = $request->validate([
|
||||
'nome' => 'required|string|max:255',
|
||||
'anno_fondazione' => 'nullable|integer|min:1800|max:' . (date('Y') + 1),
|
||||
'descrizione' => 'nullable|string',
|
||||
'parent_id' => 'nullable|exists:gruppi,id',
|
||||
'diocesi_ids' => 'nullable|array',
|
||||
@@ -244,6 +246,7 @@ class GruppoController extends Controller
|
||||
$gruppo = Gruppo::findOrFail($id);
|
||||
$data = $request->validate([
|
||||
'nome' => 'required|string|max:255',
|
||||
'anno_fondazione' => 'nullable|integer|min:1800|max:' . (date('Y') + 1),
|
||||
'descrizione' => 'nullable|string',
|
||||
'parent_id' => 'nullable|exists:gruppi,id',
|
||||
'diocesi_ids' => 'nullable|array',
|
||||
@@ -478,6 +481,7 @@ class GruppoController extends Controller
|
||||
try {
|
||||
Gruppo::create([
|
||||
'nome' => $nome,
|
||||
'anno_fondazione' => !empty($data['anno_fondazione']) ? (int) $data['anno_fondazione'] : null,
|
||||
'descrizione' => $data['descrizione'] ?? null,
|
||||
'parent_id' => !empty($data['parent_id']) ? (int) $data['parent_id'] : null,
|
||||
'diocesi_id' => !empty($data['diocesi_id']) ? (int) $data['diocesi_id'] : null,
|
||||
@@ -512,8 +516,8 @@ class GruppoController extends Controller
|
||||
public function downloadTemplate()
|
||||
{
|
||||
$this->authorizeRead('gruppi');
|
||||
$headers = ['nome', 'descrizione', 'parent_id', 'diocesi_id', 'indirizzo_incontro', 'cap_incontro', 'città_incontro', 'sigla_provincia_incontro'];
|
||||
$example = ['Gruppo Giovani', 'Giovani dai 18 ai 30 anni', '', '', 'Via Roma 10', '00100', 'Roma', 'RM'];
|
||||
$headers = ['nome', 'anno_fondazione', 'descrizione', 'parent_id', 'diocesi_id', 'indirizzo_incontro', 'cap_incontro', 'città_incontro', 'sigla_provincia_incontro'];
|
||||
$example = ['Gruppo Giovani', '2020', 'Giovani dai 18 ai 30 anni', '', '', 'Via Roma 10', '00100', 'Roma', 'RM'];
|
||||
|
||||
$csv = implode(',', $headers) . "\n" . implode(',', $example);
|
||||
|
||||
|
||||
@@ -120,7 +120,9 @@ class MailingController extends Controller
|
||||
return back()->with('error', 'Nessun destinatario email valido trovato.')->withInput();
|
||||
}
|
||||
|
||||
$attachmentPaths = $this->resolveMailingAttachmentPaths($documentiAllegati);
|
||||
$attachmentResult = $this->resolveMailingAttachmentPaths($documentiAllegati);
|
||||
$attachmentPaths = $attachmentResult['paths'];
|
||||
$linkDocs = $attachmentResult['links'];
|
||||
|
||||
$firmaId = $data['firma_id'] ?? null;
|
||||
if (!$firmaId && !empty($data['lista_id'])) {
|
||||
@@ -128,7 +130,7 @@ class MailingController extends Controller
|
||||
$firmaId = $lista?->firma?->id;
|
||||
}
|
||||
|
||||
$corpoConFirma = $data['corpo'];
|
||||
$corpoConFirma = $this->appendMailingLinksToBody($data['corpo'], $linkDocs);
|
||||
if ($firmaId) {
|
||||
$firma = Firma::find($firmaId);
|
||||
if ($firma && $firma->contenuto_html) {
|
||||
@@ -284,14 +286,16 @@ class MailingController extends Controller
|
||||
return back()->with('error', 'Nessun destinatario email valido trovato nelle liste selezionate.');
|
||||
}
|
||||
|
||||
$attachmentPaths = $this->resolveMailingAttachmentPaths($documentiAllegati);
|
||||
$attachmentResult = $this->resolveMailingAttachmentPaths($documentiAllegati);
|
||||
$attachmentPaths = $attachmentResult['paths'];
|
||||
$linkDocs = $attachmentResult['links'];
|
||||
|
||||
$firmaId = $data['firma_id'] ?? null;
|
||||
if (!$firmaId && $listeModels->count() === 1) {
|
||||
$firmaId = $listeModels->first()->firma_id;
|
||||
}
|
||||
|
||||
$corpoConFirma = $data['corpo'];
|
||||
$corpoConFirma = $this->appendMailingLinksToBody($data['corpo'], $linkDocs);
|
||||
if ($firmaId) {
|
||||
$firma = Firma::find($firmaId);
|
||||
if ($firma && $firma->contenuto_html) {
|
||||
@@ -419,17 +423,34 @@ class MailingController extends Controller
|
||||
private function resolveMailingAttachmentPaths(array $documentIds): array
|
||||
{
|
||||
$paths = [];
|
||||
$links = [];
|
||||
if (empty($documentIds)) {
|
||||
return $paths;
|
||||
return ['paths' => $paths, 'links' => $links];
|
||||
}
|
||||
|
||||
$docs = Documento::whereIn('id', $documentIds)->get();
|
||||
foreach ($docs as $doc) {
|
||||
if ($doc->file_path && Storage::exists($doc->file_path)) {
|
||||
if ($doc->isLink()) {
|
||||
$links[] = ['nome' => $doc->nome_file, 'url' => $doc->url];
|
||||
} elseif ($doc->file_path && Storage::exists($doc->file_path)) {
|
||||
$paths[] = Storage::path($doc->file_path);
|
||||
}
|
||||
}
|
||||
|
||||
return $paths;
|
||||
return ['paths' => $paths, 'links' => $links];
|
||||
}
|
||||
|
||||
private function appendMailingLinksToBody(string $body, array $linkDocs): string
|
||||
{
|
||||
if (empty($linkDocs)) {
|
||||
return $body;
|
||||
}
|
||||
|
||||
$linksText = "\n\n---\nDocumenti collegati:\n";
|
||||
foreach ($linkDocs as $link) {
|
||||
$linksText .= "- {$link['nome']}: {$link['url']}\n";
|
||||
}
|
||||
|
||||
return $body . $linksText;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user