Roberto Santini 1 month ago
parent
commit
a2d9c6a334

+ 1
- 1
app/DataTables/SegnalazioneDataTable.php View File

@@ -314,7 +314,7 @@ class SegnalazioneDataTable extends DataTable
314 314
             ->minifiedAjax('', null, [
315 315
                 'show_chiuse' => '$("#filtro_segnalazioni_chiuse").is(":checked") ? 1 : 0',
316 316
             ])
317
-            ->orderBy(9, 'desc')
317
+            ->orderBy(10, 'desc')
318 318
             ->buttons($buttons)
319 319
             ->editor(
320 320
                 Editor::make()

+ 54
- 0
app/Notifications/AnnotazionePubblicaApertaDa.php View File

@@ -11,6 +11,7 @@ use Illuminate\Notifications\Messages\MailMessage;
11 11
 use Illuminate\Notifications\Notification;
12 12
 use Illuminate\Queue\InteractsWithQueue;
13 13
 use Illuminate\Queue\SerializesModels;
14
+use Illuminate\Support\Facades\Storage;
14 15
 
15 16
 class AnnotazionePubblicaApertaDa extends Notification implements ShouldQueue
16 17
 {
@@ -60,11 +61,64 @@ class AnnotazionePubblicaApertaDa extends Notification implements ShouldQueue
60 61
                 'autoreLabel' => $autoreLabel,
61 62
             ]);
62 63
 
64
+        $this->attachAllegatiAnnotazione($mail);
65
+
63 66
         return $annotazioneHtml->bind($this->segnalazione->applyEmailDestinatari($mail, [
64 67
             strtolower((string) ($notifiable->email ?? '')),
65 68
         ]));
66 69
     }
67 70
 
71
+    /**
72
+     * Allega alla mail i file caricati sull'annotazione (dropzone).
73
+     */
74
+    private function attachAllegatiAnnotazione(MailMessage $mail): void
75
+    {
76
+        $usedNames = [];
77
+
78
+        foreach ($this->annotazione->allegatiAnnotazione as $allegato) {
79
+            $relative = (string) $allegato->path_file;
80
+            if ($relative === '' || ! Storage::disk('allegatiAnnotazioni')->exists($relative)) {
81
+                continue;
82
+            }
83
+
84
+            $absolute = Storage::disk('allegatiAnnotazioni')->path($relative);
85
+            $name = $this->uniqueAttachmentName(
86
+                (string) ($allegato->nome ?: basename($relative)),
87
+                $usedNames
88
+            );
89
+            $mime = mime_content_type($absolute);
90
+
91
+            $mail->attach($absolute, [
92
+                'as' => $name,
93
+                'mime' => is_string($mime) && $mime !== '' ? $mime : 'application/octet-stream',
94
+            ]);
95
+        }
96
+    }
97
+
98
+    /**
99
+     * @param  array<string, int>  $usedNames
100
+     */
101
+    private function uniqueAttachmentName(string $name, array &$usedNames): string
102
+    {
103
+        $name = basename(str_replace('\\', '/', trim($name)));
104
+        if ($name === '' || $name === '.' || $name === '..') {
105
+            $name = 'allegato';
106
+        }
107
+
108
+        $key = strtolower($name);
109
+        $count = $usedNames[$key] ?? 0;
110
+        $usedNames[$key] = $count + 1;
111
+        if ($count === 0) {
112
+            return $name;
113
+        }
114
+
115
+        $extension = pathinfo($name, PATHINFO_EXTENSION);
116
+        $base = pathinfo($name, PATHINFO_FILENAME);
117
+        $suffix = '-'.($count + 1);
118
+
119
+        return $extension !== '' ? $base.$suffix.'.'.$extension : $base.$suffix;
120
+    }
121
+
68 122
     /**
69 123
      * @return array<string, mixed>
70 124
      */

Loading…
Cancel
Save