Skip to content

Commit f98ef58

Browse files
feat: salvataggio allegati originali in fase d'invio mail
1 parent 5b8f2f1 commit f98ef58

4 files changed

Lines changed: 78 additions & 38 deletions

File tree

modules/emails/src/Mail.php

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,27 @@ public static function build(?User $user = null, $template = null, $id_record =
7070
*/
7171
public function addUpload($file_id, $name = null)
7272
{
73-
$this->uploads()->attach($file_id, ['id_email' => $this->id, 'name' => $name]);
73+
$file = Upload::find($file_id);
74+
75+
// duplica il file
76+
$upload = $file->copia([
77+
'id_module' => \Models\Module::where('name', 'Stato email')->first()->id,
78+
'id_record' => $this->id,
79+
]);
80+
81+
$name = $name ?: $file->name;
82+
83+
$this->attachments()->attach($upload->id, ['id_email' => $this->id, 'name' => $name, 'type' => 'file']);
7484
}
7585

7686
public function resetUploads()
7787
{
78-
$this->uploads()->detach();
88+
// Rimuove gli allegati associati
89+
$uploads = $this->attachments()->where('type', 'file')->get();
90+
foreach ($uploads as $upload) {
91+
$upload->delete();
92+
}
93+
$this->attachments()->where('type', 'file')->detach();
7994
}
8095

8196
/**
@@ -85,12 +100,35 @@ public function resetUploads()
85100
*/
86101
public function addPrint($print_id, $name = null)
87102
{
88-
$this->prints()->attach($print_id, ['id_email' => $this->id, 'name' => $name]);
103+
// Genera la stampa come PDF
104+
$print = \Prints::render($print_id, $this->id_record, null, true);
105+
106+
// Ottieni il modulo "Coda d'invio"
107+
$id_module = \Models\Module::where("name", "Stato email")->first()->id;
108+
109+
$name = $name ?: $print['name'];
110+
111+
// Salva il file nella tabella zz_files
112+
$upload = \Uploads::upload($print['pdf'], [
113+
'name' => $name,
114+
'original_name' => $name,
115+
'id_category' => null,
116+
'id_module' => $id_module,
117+
'id_record' => $this->id,
118+
]);
119+
120+
// Collega il file alla tabella em_email_attachment
121+
$this->attachments()->attach($upload->id, ['id_email' => $this->id, 'name' => $name, 'type' => 'print']);
89122
}
90123

91124
public function resetPrints()
92125
{
93-
$this->prints()->detach();
126+
// Rimuove le stampe associate
127+
$uploads = $this->attachments()->where('type', 'print')->get();
128+
foreach ($uploads as $upload) {
129+
$upload->delete();
130+
}
131+
$this->attachments()->where('type', 'print')->detach();
94132
}
95133

96134
/**
@@ -236,6 +274,12 @@ public function prints()
236274
return $this->belongsToMany(PrintTemplate::class, 'em_email_print', 'id_email', 'id_print')->withPivot('name');
237275
}
238276

277+
278+
public function attachments()
279+
{
280+
return $this->belongsToMany(Upload::class, 'em_email_attachment', 'id_email', 'id_file')->withPivot('name');
281+
}
282+
239283
protected function resetFromTemplate()
240284
{
241285
$template = $this->template;

modules/stato_email/edit.php

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,27 +98,36 @@
9898
</div>';
9999

100100
// Stampe
101-
$prints = $mail->prints;
101+
$prints_old = $mail->prints;
102+
$prints = $mail->attachments()->where('type', 'print')->get();
102103
echo '
103104
<div class="col-md-4">
104105
<h4>'.tr('Stampe').'</h4>
105106
<table class="table table-sm table-striped">
106107
<thead>
107108
<tr>
108-
<th>'.tr('Stampa').'</th>
109109
<th>'.tr('Nome').'</th>
110110
</tr>
111111
</thead>
112112
113113
<tbody>';
114114

115-
foreach ($prints as $print) {
115+
foreach ($prints_old as $print) {
116116
echo '
117117
<tr>
118118
<td>
119119
<a href="'.Prints::getHref($print->getTranslation('title'), $mail->id_record).'" target="_blank">'.$print->getTranslation('title').'</a>
120120
</td>
121-
<td>'.$print->pivot->name.'</td>
121+
</tr>';
122+
}
123+
124+
125+
foreach ($prints as $print) {
126+
echo '
127+
<tr>
128+
<td>
129+
<a href="'.base_path().'/view.php?file_id='.$print->id.'" target="_blank">'.$print->original.'</a>
130+
</td>
122131
</tr>';
123132
}
124133

@@ -128,27 +137,34 @@
128137
</div>';
129138

130139
// Stampe
131-
$uploads = $mail->uploads;
140+
$uploads_old = $mail->uploads;
141+
$uploads = $mail->attachments()->where('type', 'file')->get();
132142
echo '
133143
<div class="col-md-4">
134144
<h4>'.tr('Allegati').'</h4>
135145
<table class="table table-sm table-striped">
136146
<thead>
137147
<tr>
138-
<th>'.tr('Allegato').'</th>
139148
<th>'.tr('Nome').'</th>
140149
</tr>
141150
</thead>
142151
143152
<tbody>';
144153

154+
foreach ($uploads_old as $upload) {
155+
echo '
156+
<tr>
157+
<td>
158+
<a href="'.base_path().'/view.php?file_id='.$upload->id.'" target="_blank">'.$upload->original.'</a>
159+
</td>
160+
</tr>';
161+
}
145162
foreach ($uploads as $upload) {
146163
echo '
147164
<tr>
148165
<td>
149-
<a href="'.base_path().'/view.php?file_id='.$upload->id.'" target="_blank">'.$upload->name.'</a>
166+
<a href="'.base_path().'/view.php?file_id='.$upload->id.'" target="_blank">'.$upload->original.'</a>
150167
</td>
151-
<td>'.$upload->pivot->name.'</td>
152168
</tr>';
153169
}
154170

src/Notifications/EmailNotification.php

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -133,18 +133,12 @@ public function setMail($mail)
133133
$this->addReceiver($receiver['address'], $receiver['type']);
134134
}
135135

136-
// Allegati
137-
$uploads = $mail->uploads;
136+
// Allegati e stampe
137+
$uploads = $mail->attachments()->get();
138138
foreach ($uploads as $upload) {
139139
$this->addUpload($upload->id);
140140
}
141141

142-
// Stampe
143-
$prints = $mail->prints;
144-
foreach ($prints as $print) {
145-
$this->addPrint($print['id'], $mail->id_record);
146-
}
147-
148142
// Conferma di lettura
149143
if (!empty($mail->read_notify)) {
150144
$this->ConfirmReadingTo = $this->From;
@@ -253,23 +247,6 @@ public function addUpload($file_id)
253247
$this->addAttachment(base_dir().'/'.\Uploads::getDirectory($attachment['id_module'], $attachment['id_plugin']).'/'.$attachment['filename'], $attachment['original']);
254248
}
255249

256-
/**
257-
* Aggiunge una stampa alla notifica.
258-
*
259-
* @param string|int $print
260-
* @param int $id_record
261-
* @param string $name
262-
*/
263-
public function addPrint($print, $id_record, $name = null)
264-
{
265-
$print = \Prints::get($print);
266-
267-
$info = \Prints::render($print['id'], $id_record, null, true);
268-
$name = $name ?: $info['path'];
269-
270-
$this->AddStringAttachment($info['pdf'], $name);
271-
}
272-
273250
/**
274251
* Aggiunge un destinatario.
275252
*

update/2_9.sql

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,4 +269,7 @@ INSERT INTO `zz_settings_lang` (`id_lang`, `id_record`, `title`, `help`) VALUES
269269
INSERT INTO `zz_settings` (`nome`, `valore`, `tipo`, `editable`, `sezione`, `order`, `is_user_setting`) VALUES ('Conto predefinito per gli ammortamenti', (SELECT id FROM `co_pianodeiconti2` WHERE `descrizione` = 'Fondi ammortamento'), 'query=SELECT id, descrizione FROM co_pianodeiconti2 WHERE idpianodeiconti1=(SELECT id FROM co_pianodeiconti1 WHERE descrizione=\'Patrimoniale\')', '1', 'Piano dei conti', NULL, '0');
270270

271271
INSERT INTO `zz_settings_lang` (`id_lang`, `id_record`, `title`, `help`) VALUES ('1', (SELECT id FROM `zz_settings` WHERE `nome` = 'Conto predefinito per gli ammortamenti'), 'Conto predefinito per gli ammortamenti', '');
272-
INSERT INTO `zz_settings_lang` (`id_lang`, `id_record`, `title`, `help`) VALUES ('2', (SELECT id FROM `zz_settings` WHERE `nome` = 'Conto predefinito per gli ammortamenti'), 'Default account for depreciation', '');
272+
INSERT INTO `zz_settings_lang` (`id_lang`, `id_record`, `title`, `help`) VALUES ('2', (SELECT id FROM `zz_settings` WHERE `nome` = 'Conto predefinito per gli ammortamenti'), 'Default account for depreciation', '');
273+
274+
-- Gestione salvataggio allegati email
275+
CREATE TABLE `em_email_attachment` (`id` INT NOT NULL AUTO_INCREMENT , `id_email` INT NOT NULL , `id_file` INT NOT NULL , `name` VARCHAR(255) NULL , `type` VARCHAR(255) NOT NULL , PRIMARY KEY (`id`));

0 commit comments

Comments
 (0)