From d76bdecc147f5915155bff5aeb99635cff4793a6 Mon Sep 17 00:00:00 2001 From: Thomas Zilio Date: Thu, 29 Aug 2019 10:25:14 +0200 Subject: [PATCH] Correzione gestione email --- actions.php | 11 +- lib/functions.php | 1 - mail.php | 14 +- modules/emails/actions.php | 6 +- modules/emails/add.php | 2 +- modules/emails/edit.php | 4 +- modules/emails/init.php | 2 + .../emails/src/Account.php | 6 +- modules/emails/src/EmailHook.php | 4 +- {src/Models => modules/emails/src}/Mail.php | 154 ++++++------------ modules/emails/src/Receiver.php | 29 ++++ .../emails/src/Template.php | 13 +- modules/fatture/bulk.php | 2 +- modules/fatture/variables.php | 2 +- modules/interventi/actions.php | 10 +- modules/interventi/modutil.php | 6 +- modules/newsletter/actions.php | 10 +- modules/newsletter/edit.php | 2 +- modules/newsletter/src/Newsletter.php | 18 +- modules/scadenzario/variables.php | 2 +- modules/smtp/edit.php | 2 +- modules/stato_email/edit.php | 38 +++++ modules/stato_email/init.php | 11 ++ reset.php | 6 +- src/Extensions/MessageHandler.php | 4 +- src/HTMLBuilder/Manager/ButtonManager.php | 6 +- src/HTMLBuilder/Manager/EmailManager.php | 24 +-- src/Models/Module.php | 4 +- src/Models/OperationLog.php | 8 +- src/Notifications/EmailNotification.php | 41 +---- src/Update.php | 3 + update/2_4_11.php | 25 ++- update/2_4_11.sql | 71 ++++++-- update/tables.php | 9 +- 34 files changed, 308 insertions(+), 242 deletions(-) rename src/Models/MailAccount.php => modules/emails/src/Account.php (68%) rename {src/Models => modules/emails/src}/Mail.php (61%) create mode 100644 modules/emails/src/Receiver.php rename src/Models/MailTemplate.php => modules/emails/src/Template.php (65%) create mode 100644 modules/stato_email/edit.php create mode 100644 modules/stato_email/init.php diff --git a/actions.php b/actions.php index d14e04b47..5a8b7d2bf 100644 --- a/actions.php +++ b/actions.php @@ -2,11 +2,11 @@ include_once __DIR__.'/core.php'; -use Models\MailTemplate; use Models\Note; use Models\OperationLog; use Modules\Checklists\Check; use Modules\Checklists\Checklist; +use Modules\Emails\Template; if (empty($structure) || empty($structure['enabled'])) { die(tr('Accesso negato')); @@ -207,12 +207,11 @@ elseif (filter('op') == 'sort_checks') { // Inizializzazione email elseif (post('op') == 'send-email') { - $template = MailTemplate::find(post('template')); + $template = Template::find(post('template')); - $mail = \Models\Mail::build($user, $template, $id_record); + $mail = \Modules\Emails\Mail::build($user, $template, $id_record); // Rimozione allegati predefiniti - $mail->resetAttachments(); $mail->resetPrints(); // Destinatari @@ -236,9 +235,9 @@ elseif (post('op') == 'send-email') { } // Allegati originali - $files = post('attachments'); + $files = post('uploads'); foreach ($files as $file) { - $mail->addAttachment($file); + $mail->addUpload($file); } $mail->save(); diff --git a/lib/functions.php b/lib/functions.php index 6d208e143..eb0046b7c 100644 --- a/lib/functions.php +++ b/lib/functions.php @@ -5,7 +5,6 @@ * * @since 2.3 */ - use Models\OperationLog; /** diff --git a/mail.php b/mail.php index fd1ced26f..1df3bc4ee 100644 --- a/mail.php +++ b/mail.php @@ -1,10 +1,10 @@ module; $smtp = $template->account; @@ -81,7 +81,7 @@ echo ' '; // Stampe -$selected_prints = $dbo->fetchArray('SELECT id_print FROM em_template_print WHERE id_email = '.prepare($template['id'])); +$selected_prints = $dbo->fetchArray('SELECT id_print FROM em_print_template WHERE id_template = '.prepare($template['id'])); $selected = array_column($selected_prints, 'id_print'); echo ' @@ -91,17 +91,17 @@ echo ' {[ "type": "select", "multiple": "1", "label": "'.tr('Stampe').'", "name": "prints[]", "value": "'.implode(',', $selected).'", "values": "query=SELECT id, title AS text FROM zz_prints WHERE id_module = '.prepare($id_module).' AND enabled=1" ]} '; -$attachments = []; +$uploads = []; if ($template['name'] == 'Fattura Elettronica') { - $attachments = $dbo->fetchArray('SELECT id FROM zz_files WHERE id_module = '.prepare($module['id']).' AND id_record = '.prepare($id_record).' AND category = \'Fattura Elettronica\''); - $attachments = array_column($attachments, 'id'); + $uploads = $dbo->fetchArray('SELECT id FROM zz_files WHERE id_module = '.prepare($module['id']).' AND id_record = '.prepare($id_record).' AND category = \'Fattura Elettronica\''); + $uploads = array_column($uploads, 'id'); } // Allegati echo '
- {[ "type": "select", "multiple": "1", "label": "'.tr('Allegati').'", "name": "attachments[]", "value": "'.implode(',', $attachments).'", "values": "query=SELECT id, name AS text FROM zz_files WHERE id_module = '.prepare($id_module).' AND id_record = '.prepare($id_record)." UNION SELECT id, CONCAT(name, ' (Azienda)') AS text FROM zz_files WHERE id_module = ".prepare(Modules::get('Anagrafiche')['id'])." AND id_record = (SELECT valore FROM zz_settings WHERE nome = 'Azienda predefinita')\" ]} + {[ "type": "select", "multiple": "1", "label": "'.tr('Allegati').'", "name": "uploads[]", "value": "'.implode(',', $uploads).'", "values": "query=SELECT id, name AS text FROM zz_files WHERE id_module = '.prepare($id_module).' AND id_record = '.prepare($id_record)." UNION SELECT id, CONCAT(name, ' (Azienda)') AS text FROM zz_files WHERE id_module = ".prepare(Modules::get('Anagrafiche')['id'])." AND id_record = (SELECT valore FROM zz_settings WHERE nome = 'Azienda predefinita')\" ]}
"; diff --git a/modules/emails/actions.php b/modules/emails/actions.php index 97dadd007..8466062d6 100644 --- a/modules/emails/actions.php +++ b/modules/emails/actions.php @@ -7,7 +7,7 @@ switch (post('op')) { $dbo->insert('em_templates', [ 'name' => post('name'), 'id_module' => post('module'), - 'id_smtp' => post('smtp'), + 'id_account' => post('smtp'), 'subject' => post('subject'), ]); @@ -20,7 +20,7 @@ switch (post('op')) { case 'update': $dbo->update('em_templates', [ 'name' => post('name'), - 'id_smtp' => post('smtp'), + 'id_account' => post('smtp'), 'icon' => post('icon'), 'subject' => post('subject'), 'reply_to' => post('reply_to'), @@ -30,7 +30,7 @@ switch (post('op')) { 'read_notify' => post('read_notify'), ], ['id' => $id_record]); - $dbo->sync('em_template_print', ['id_email' => $id_record], ['id_print' => (array) post('prints')]); + $dbo->sync('em_print_template', ['id_template' => $id_record], ['id_print' => (array) post('prints')]); flash()->info(tr('Informazioni salvate correttamente!')); diff --git a/modules/emails/add.php b/modules/emails/add.php index 6b3eac6ec..9647c2678 100644 --- a/modules/emails/add.php +++ b/modules/emails/add.php @@ -25,7 +25,7 @@ include_once __DIR__.'/../../core.php';
- {[ "type": "select", "label": "", "name": "smtp", "value": "$id_smtp$", "ajax-source": "smtp" ]} + {[ "type": "select", "label": "", "name": "smtp", "value": "$id_account$", "ajax-source": "smtp" ]}
diff --git a/modules/emails/edit.php b/modules/emails/edit.php index c70094c45..39c4c8e84 100644 --- a/modules/emails/edit.php +++ b/modules/emails/edit.php @@ -26,7 +26,7 @@ include_once __DIR__.'/../../core.php';
- {[ "type": "select", "label": "", "name": "smtp", "value": "$id_smtp$", "ajax-source": "smtp" ]} + {[ "type": "select", "label": "", "name": "smtp", "value": "$id_account$", "ajax-source": "smtp" ]}
@@ -62,7 +62,7 @@ include_once __DIR__.'/../../core.php'; fetchArray('SELECT id_print FROM em_template_print WHERE id_email = '.prepare($id_record)); +$selected_prints = $dbo->fetchArray('SELECT id_print FROM em_print_template WHERE id_template = '.prepare($id_record)); $selected = array_column($selected_prints, 'id_print'); echo ' diff --git a/modules/emails/init.php b/modules/emails/init.php index ecad92ee3..cd509545b 100644 --- a/modules/emails/init.php +++ b/modules/emails/init.php @@ -1,5 +1,7 @@ fetchOne('SELECT * FROM em_templates WHERE id='.prepare($id_record).' AND deleted_at IS NULL'); } diff --git a/src/Models/MailAccount.php b/modules/emails/src/Account.php similarity index 68% rename from src/Models/MailAccount.php rename to modules/emails/src/Account.php index 42003968b..d81a24d5d 100644 --- a/src/Models/MailAccount.php +++ b/modules/emails/src/Account.php @@ -1,12 +1,12 @@ hasMany(MailTemplate::class, 'id_smtp'); + return $this->hasMany(Template::class, 'id_account'); } } diff --git a/modules/emails/src/EmailHook.php b/modules/emails/src/EmailHook.php index 0b02da458..3461e4b5b 100644 --- a/modules/emails/src/EmailHook.php +++ b/modules/emails/src/EmailHook.php @@ -3,15 +3,13 @@ namespace Modules\Emails; use Hooks\Manager; -use Models\Mail; -use Models\MailAccount; use Notifications\EmailNotification; class EmailHook extends Manager { public function execute() { - $accounts = MailAccount::all(); + $accounts = Account::all(); $diff = date('Y-m-d', strtotime('-4 hours')); $list = []; diff --git a/src/Models/Mail.php b/modules/emails/src/Mail.php similarity index 61% rename from src/Models/Mail.php rename to modules/emails/src/Mail.php index 60bc34783..fe5bb7e3d 100644 --- a/src/Models/Mail.php +++ b/modules/emails/src/Mail.php @@ -1,19 +1,17 @@ created_by = $user->id; - $model->id_template = $template->id; + if (!empty($template)) { + $model->id_template = $template->id; + $model->id_account = $template->account->id; + } + $model->id_record = $id_record; + $model->save(); + if (!empty($template)) { $model->resetFromTemplate(); } @@ -43,18 +47,9 @@ class Mail extends Model * * @param string $file_id */ - public function addAttachment($file_id) + public function addUpload($file_id, $name = null) { - if (!isset($this->attachments)) { - $this->attachments = []; - } - - $this->attachments[] = $file_id; - } - - public function resetAttachments() - { - $this->attachments = []; + $this->uploads()->attach($file_id, ['id_email' => $this->id, 'name' => $name]); } /** @@ -65,25 +60,16 @@ class Mail extends Model */ public function addPrint($print_id, $name = null) { - if (!isset($this->prints)) { - $this->prints = []; - } - - $print = PrintTemplate::find($print_id); - - if (empty($name)) { - $name = $print['title'].'.pdf'; - } - - $this->prints[] = [ - 'id' => $print['id'], - 'name' => $name, - ]; + $this->prints()->attach($print_id, ['id_email' => $this->id, 'name' => $name]); } public function resetPrints() { - $this->prints = []; + $prints = $this->prints; + + foreach ($prints as $print) { + $this->prints()->detach($print->id, ['id_email' => $this->id]); + } } /** @@ -98,33 +84,16 @@ class Mail extends Model return; } - if (!isset($this->receivers)) { - $this->receivers = []; - } - $list = explode(';', $value); - foreach ($list as $element) { - $this->receivers[] = [ - 'email' => $element, - 'type' => $type, - ]; + foreach ($list as $address) { + if (!empty($address)) { + $receiver = Receiver::build($this, $address, $type); + } } } public function save(array $options = []) { - if (isset($this->receivers)) { - $this->setReceiversAttribute($this->receivers); - } - - if (isset($this->attachments)) { - $this->setAttachmentsAttribute($this->attachments); - } - - if (isset($this->prints)) { - $this->setPrintsAttribute($this->prints); - } - if (isset($this->options)) { $this->setOptionsAttribute($this->options); } @@ -139,36 +108,6 @@ class Mail extends Model // Attributi Eloquent - public function setReceiversAttribute($value) - { - $this->attributes['receivers'] = json_encode($value); - } - - public function getReceiversAttribute() - { - return json_decode($this->attributes['receivers'], true); - } - - public function setAttachmentsAttribute($value) - { - $this->attributes['attachments'] = json_encode($value); - } - - public function getAttachmentsAttribute() - { - return json_decode($this->attributes['attachments'], true); - } - - public function setPrintsAttribute($value) - { - $this->attributes['prints'] = json_encode($value); - } - - public function getPrintsAttribute() - { - return json_decode($this->attributes['prints'], true); - } - public function setOptionsAttribute($value) { $this->attributes['options'] = json_encode($value); @@ -189,7 +128,17 @@ class Mail extends Model return $this->options['read-notify']; } - public function setSubjecyAttribute($value) + /** + * Imposta il titolo della notifica. + * + * @param bool $value + */ + public function setReadNotifyAttribute($value) + { + $this->options['read-notify'] = boolval($value); + } + + public function setSubjectAttribute($value) { if (isset($this->template)) { $module = $this->template->module; @@ -211,26 +160,16 @@ class Mail extends Model $this->attributes['content'] = $value; } - /** - * Imposta il titolo della notifica. - * - * @param bool $value - */ - public function setReadNotifyAttribute($value) - { - $this->options['read-notify'] = boolval($value); - } - /* Relazioni Eloquent */ public function account() { - return $this->belongsTo(MailAccount::class, 'id_account')->withTrashed(); + return $this->belongsTo(Account::class, 'id_account')->withTrashed(); } public function template() { - return $this->belongsTo(MailTemplate::class, 'id_template')->withTrashed(); + return $this->belongsTo(Template::class, 'id_template')->withTrashed(); } public function user() @@ -240,7 +179,22 @@ class Mail extends Model public function newsletter() { - return $this->belongsTo(Newsletter::class, 'id_campaign'); + return $this->belongsTo(Newsletter::class, 'id_newsletter'); + } + + public function receivers() + { + return $this->hasMany(Receiver::class, 'id_email'); + } + + public function uploads() + { + return $this->belongsToMany(Upload::class, 'em_email_upload', 'id_email', 'id_file')->withPivot('name'); + } + + public function prints() + { + return $this->belongsToMany(PrintTemplate::class, 'em_email_print', 'id_email', 'id_print')->withPivot('name'); } protected function resetFromTemplate() @@ -270,9 +224,9 @@ class Mail extends Model } // Incluesione stampe predefinite - $prints = database()->fetchArray('SELECT id_print FROM em_template_print WHERE id_email = '.prepare($template['id'])); + $prints = $template->prints; foreach ($prints as $print) { - $this->addPrint($print['id_print']); + $this->addPrint($print['id']); } } } diff --git a/modules/emails/src/Receiver.php b/modules/emails/src/Receiver.php new file mode 100644 index 000000000..a3ffc9565 --- /dev/null +++ b/modules/emails/src/Receiver.php @@ -0,0 +1,29 @@ +email()->associate($mail); + + $model->address = $address; + $model->type = $type ?: 'a'; + + $model->save(); + } + + public function email() + { + return $this->belongsTo(Mail::class, 'id_email'); + } +} diff --git a/src/Models/MailTemplate.php b/modules/emails/src/Template.php similarity index 65% rename from src/Models/MailTemplate.php rename to modules/emails/src/Template.php index 845359b23..c50bd66ed 100644 --- a/src/Models/MailTemplate.php +++ b/modules/emails/src/Template.php @@ -1,12 +1,14 @@ belongsTo(MailAccount::class, 'id_smtp')->withTrashed(); + return $this->belongsTo(Account::class, 'id_account')->withTrashed(); + } + + public function prints() + { + return $this->belongsToMany(PrintTemplate::class, 'em_print_template', 'id_template', 'id_print'); } } diff --git a/modules/fatture/bulk.php b/modules/fatture/bulk.php index 3b8e6b6bb..345e32485 100644 --- a/modules/fatture/bulk.php +++ b/modules/fatture/bulk.php @@ -141,7 +141,7 @@ switch (post('op')) { if ($result) { ++$added; - //operationLog('export-xml-bulk', ['id_record' => $r['id']]); + //operationLog('export-xml-bulk', ['id_record' => $r['id']]); } else { $failed[] = $fattura->numero_esterno; } diff --git a/modules/fatture/variables.php b/modules/fatture/variables.php index db5f07269..d011bc69f 100644 --- a/modules/fatture/variables.php +++ b/modules/fatture/variables.php @@ -7,7 +7,7 @@ $r = $dbo->fetchOne('SELECT co_documenti.*, an_anagrafiche.pec, an_anagrafiche.ragione_sociale, co_tipidocumento.descrizione AS tipo_documento, - (SELECT pec FROM em_accounts WHERE em_accounts.id='.prepare($template['id_smtp']).') AS is_pec + (SELECT pec FROM em_accounts WHERE em_accounts.id='.prepare($template['id_account']).') AS is_pec FROM co_documenti INNER JOIN an_anagrafiche ON co_documenti.idanagrafica=an_anagrafiche.idanagrafica INNER JOIN co_tipidocumento ON co_tipidocumento.id=co_documenti.idtipodocumento diff --git a/modules/interventi/actions.php b/modules/interventi/actions.php index 155733d29..f5697ef93 100644 --- a/modules/interventi/actions.php +++ b/modules/interventi/actions.php @@ -2,10 +2,10 @@ include_once __DIR__.'/../../core.php'; -use Models\Mail; -use Models\MailTemplate; use Modules\Anagrafiche\Anagrafica; use Modules\Articoli\Articolo as ArticoloOriginale; +use Modules\Emails\Mail; +use Modules\Emails\Template; use Modules\Interventi\Components\Articolo; use Modules\Interventi\Components\Riga; use Modules\Interventi\Components\Sconto; @@ -51,7 +51,7 @@ switch (post('op')) { // Notifica chiusura intervento $stato = $dbo->selectOne('in_statiintervento', '*', ['idstatointervento' => post('idstatointervento')]); if (!empty($stato['notifica']) && !empty($stato['destinatari']) && $stato['idstatointervento'] != $record['idstatointervento']) { - $template = MailTemplate::find($stato['id_email']); + $template = Template::find($stato['id_email']); $mail = Mail::build(auth()->getUser(), $template, $id_record); $mail->addReceiver($stato['destinatari']); @@ -477,7 +477,7 @@ switch (post('op')) { $stato = $dbo->selectOne('in_statiintervento', '*', ['descrizione' => 'Completato']); // Notifica chiusura intervento if (!empty($stato['notifica']) && !empty($stato['destinatari'])) { - $template = MailTemplate::find($stato['id_email']); + $template = Template::find($stato['id_email']); $mail = Mail::build(auth()->getUser(), $template, $id_record); $mail->addReceiver($stato['destinatari']); @@ -521,7 +521,7 @@ switch (post('op')) { // Notifica rimozione dell' intervento al tecnico if (!empty($tecnico['email'])) { - $template = MailTemplate::get('Notifica rimozione intervento'); + $template = Template::get('Notifica rimozione intervento'); $mail = Mail::build(auth()->getUser(), $template, $id_record); $mail->addReceiver($tecnico['email']); diff --git a/modules/interventi/modutil.php b/modules/interventi/modutil.php index ed6db032b..f750707f6 100644 --- a/modules/interventi/modutil.php +++ b/modules/interventi/modutil.php @@ -2,9 +2,9 @@ include_once __DIR__.'/../../core.php'; -use Models\Mail; -use Models\MailTemplate; use Modules\Anagrafiche\Anagrafica; +use Modules\Emails\Mail; +use Modules\Emails\Template; use Modules\Fatture\Components\Descrizione; use Modules\Fatture\Components\Riga; use Modules\Fatture\Fattura; @@ -63,7 +63,7 @@ function add_tecnico($idintervento, $idtecnico, $inizio, $fine, $idcontratto = n // Notifica nuovo intervento al tecnico if (!empty($tecnico['email'])) { - $template = MailTemplate::get('Notifica intervento'); + $template = Template::get('Notifica intervento'); $mail = Mail::build(auth()->getUser(), $template, $idintervento); $mail->addReceiver($anagrafica['email']); diff --git a/modules/newsletter/actions.php b/modules/newsletter/actions.php index ee733b988..9cc690061 100644 --- a/modules/newsletter/actions.php +++ b/modules/newsletter/actions.php @@ -1,13 +1,13 @@ id; @@ -48,16 +48,16 @@ switch (filter('op')) { continue; } - $mail = \Models\Mail::build($user, $template, $anagrafica->id); + $mail = \Modules\Emails\Mail::build($user, $template, $anagrafica->id); $mail->addReceiver($anagrafica['email']); $mail->subject = $newsletter->subject; $mail->content = $newsletter->content; - $mail->id_campaign = $newsletter->id; + $mail->id_newsletter = $newsletter->id; foreach ($uploads as $upload) { - $mail->addAttachment($upload); + $mail->addUpload($upload); } $mail->save(); diff --git a/modules/newsletter/edit.php b/modules/newsletter/edit.php index 96aa95522..d95bf0988 100644 --- a/modules/newsletter/edit.php +++ b/modules/newsletter/edit.php @@ -1,6 +1,6 @@ belongsToMany(Anagrafica::class, 'em_campaign_anagrafica', 'id_campaign', 'id_anagrafica')->withPivot('id_email'); + return $this->belongsToMany(Anagrafica::class, 'em_newsletter_anagrafica', 'id_newsletter', 'id_anagrafica')->withPivot('id_email'); } public function emails() { - return $this->hasMany(Mail::class, 'id_campaign'); + return $this->hasMany(Mail::class, 'id_newsletter'); } public function account() { - return $this->belongsTo(MailAccount::class, 'id_account'); + return $this->belongsTo(Account::class, 'id_account'); } public function template() { - return $this->belongsTo(MailTemplate::class, 'id_template'); + return $this->belongsTo(Template::class, 'id_template'); } public function user() diff --git a/modules/scadenzario/variables.php b/modules/scadenzario/variables.php index 7ef10cbe8..fb6e1dfa2 100644 --- a/modules/scadenzario/variables.php +++ b/modules/scadenzario/variables.php @@ -5,7 +5,7 @@ $r = $dbo->fetchOne('SELECT co_scadenziario.*, co_documenti.*, an_anagrafiche.pec, an_anagrafiche.ragione_sociale, co_scadenziario.da_pagare - co_scadenziario.pagato AS totale, - (SELECT pec FROM em_accounts WHERE em_accounts.id='.prepare($template['id_smtp']).') AS is_pec, + (SELECT pec FROM em_accounts WHERE em_accounts.id='.prepare($template['id_account']).') AS is_pec, (SELECT descrizione FROM co_pagamenti WHERE co_pagamenti.id = co_documenti.idpagamento) AS pagamento FROM co_scadenziario INNER JOIN co_documenti ON co_documenti.id = co_scadenziario.iddocumento diff --git a/modules/smtp/edit.php b/modules/smtp/edit.php index 266970a94..6216f3e71 100644 --- a/modules/smtp/edit.php +++ b/modules/smtp/edit.php @@ -82,7 +82,7 @@ include_once __DIR__.'/../../core.php'; fetchArray('SELECT `id`, `name` FROM `em_templates` WHERE `id_smtp` = '.prepare($id_record)); +$elementi = $dbo->fetchArray('SELECT `id`, `name` FROM `em_templates` WHERE `id_account` = '.prepare($id_record)); if (!empty($elementi)) { echo ' diff --git a/modules/stato_email/edit.php b/modules/stato_email/edit.php new file mode 100644 index 000000000..1ab1d3a6f --- /dev/null +++ b/modules/stato_email/edit.php @@ -0,0 +1,38 @@ + +
+
+

'.tr('Informazioni').'

+
+ +
+
+
+ {[ "type": "select", "label": "'.tr('Template email').'", "name": "id_template", "values": "query=SELECT id, name AS descrizione FROM em_templates", "required": 1, "value": "$id_template$", "disabled": 1 ]} +
+ +
+ {[ "type": "span", "label": "'.tr('Data di invio').'", "name": "sent_at", "value": "$sent_at$" ]} +
+ +
+ {[ "type": "span", "label": "'.tr('Ultimo tentativo').'", "name": "failed_at", "value": "$failed_at$" ]} +
+
+ +
+
+ +

'.tr('Oggetto').'

+
+'.$mail->subject.' +
+ +

'.tr('Contenuto').'

+
+'.$mail->content.' +
'; diff --git a/modules/stato_email/init.php b/modules/stato_email/init.php new file mode 100644 index 000000000..ad0d40654 --- /dev/null +++ b/modules/stato_email/init.php @@ -0,0 +1,11 @@ +toArray(); +} diff --git a/reset.php b/reset.php index 35ed2d6df..ae46e038b 100644 --- a/reset.php +++ b/reset.php @@ -3,9 +3,9 @@ $skip_permissions = true; include_once __DIR__.'/core.php'; -use Models\Mail; -use Models\MailTemplate; use Models\User; +use Modules\Emails\Mail; +use Modules\Emails\Template; use Notifications\EmailNotification; $token = get('reset_token'); @@ -26,7 +26,7 @@ switch (post('op')) { $utente->reset_token = secure_random_string(); $utente->save(); - $template = MailTemplate::get('Reset password'); + $template = Template::get('Reset password'); $mail = Mail::build($user, $template, $utente->id); $mail->addReceiver($utente->email); diff --git a/src/Extensions/MessageHandler.php b/src/Extensions/MessageHandler.php index 19df93336..9811556ef 100644 --- a/src/Extensions/MessageHandler.php +++ b/src/Extensions/MessageHandler.php @@ -21,7 +21,9 @@ class MessageHandler extends AbstractProcessingHandler if (auth()->check()) { $message .= ' - '.tr('Se il problema persiste siete pregati di chiedere assistenza tramite la sezione Bug').'. '; + '.tr('Se il problema persiste siete pregati di chiedere assistenza tramite il forum apposito (_LINK_FORUM_)', [ + '_LINK_FORUM_' => 'https://forum.openstamanager.com/', + ]).'.'; if (auth()->isAdmin()) { $message .= ' diff --git a/src/HTMLBuilder/Manager/ButtonManager.php b/src/HTMLBuilder/Manager/ButtonManager.php index 81eaf3f27..ca72c9054 100644 --- a/src/HTMLBuilder/Manager/ButtonManager.php +++ b/src/HTMLBuilder/Manager/ButtonManager.php @@ -2,7 +2,7 @@ namespace HTMLBuilder\Manager; -use Models\MailTemplate; +use Modules\Emails\Template; /** * @since 2.4 @@ -38,7 +38,7 @@ class ButtonManager implements ManagerInterface 'icon' => $print['icon'], ]; } else { - $template = MailTemplate::find($options['id']); + $template = Template::find($options['id']); $result = [ 'link' => ROOTDIR.'/mail.php?id_module='.$options['id_module'].'&id_record='.$options['id_record'].'&id='.$options['id'], @@ -87,7 +87,7 @@ class ButtonManager implements ManagerInterface if ($options['type'] == 'print') { $results = \Prints::getModulePrints($options['id_module']); } else { - $results = MailTemplate::where('id_module', $options['id_module'])->get()->toArray(); + $results = Template::where('id_module', $options['id_module'])->get()->toArray(); } return $results; diff --git a/src/HTMLBuilder/Manager/EmailManager.php b/src/HTMLBuilder/Manager/EmailManager.php index 5f6fbd16d..540b36948 100644 --- a/src/HTMLBuilder/Manager/EmailManager.php +++ b/src/HTMLBuilder/Manager/EmailManager.php @@ -2,9 +2,7 @@ namespace HTMLBuilder\Manager; -use Models\Mail; -use Prints; -use Translator; +use Modules\Emails\Mail; /** * Gestione allegati. @@ -49,28 +47,24 @@ class EmailManager implements ManagerInterface
    '; foreach ($emails as $operation) { - $receivers = array_column($operation->receivers, 'email'); + $receivers = $operation->receivers->pluck('address')->toArray(); $prints = []; $list = $operation->prints; foreach ($list as $print) { - $print = Prints::get($print['id']); - $prints[] = $print['title']; } - $attachments = []; - $list = $operation->attachments; - foreach ($list as $attachment) { - $attachment = $database->selectOne('zz_files', '*', ['id' => $attachment]); - - $attachments[] = $attachment['name']; + $uploads = []; + $list = $operation->uploads; + foreach ($list as $upload) { + $uploads[] = $upload['name']; } $sent = !empty($operation['sent_at']) ? tr('inviata il _DATE_ alle _HOUR_', [ '_DATE_' => dateFormat($operation['sent_at']), '_HOUR_' => timeFormat($operation['sent_at']), - ]) : tr('in coda di invio') ; + ]) : tr('in coda di invio'); $result .= '
  • @@ -86,9 +80,9 @@ class EmailManager implements ManagerInterface
  • '.tr('Stampe').': '.implode(', ', $prints).'.
  • '; } - if (!empty($attachments)) { + if (!empty($uploads)) { $result .= ' -
  • '.tr('Allegati').': '.implode(', ', $attachments).'.
  • '; +
  • '.tr('Allegati').': '.implode(', ', $uploads).'.
  • '; } $result .= ' diff --git a/src/Models/Module.php b/src/Models/Module.php index cdce789ff..6d3a3ead9 100644 --- a/src/Models/Module.php +++ b/src/Models/Module.php @@ -123,9 +123,9 @@ class Module extends Model return $this->hasMany(PrintTemplate::class, 'id_module'); } - public function mailTemplates() + public function Templates() { - return $this->hasMany(MailTemplate::class, 'id_module'); + return $this->hasMany(Template::class, 'id_module'); } public function views() diff --git a/src/Models/OperationLog.php b/src/Models/OperationLog.php index 975ae2ea6..d8350b3a9 100644 --- a/src/Models/OperationLog.php +++ b/src/Models/OperationLog.php @@ -10,11 +10,13 @@ class OperationLog extends Model protected static $info = []; - public static function setInfo($name, $value){ + public static function setInfo($name, $value) + { self::$info[$name] = $value; } - public static function getInfo($name){ + public static function getInfo($name) + { return self::$info[$name]; } @@ -26,7 +28,7 @@ class OperationLog extends Model $model = parent::build(); - foreach (self::$info as $key => $value){ + foreach (self::$info as $key => $value) { $model->{$key} = $value; } diff --git a/src/Notifications/EmailNotification.php b/src/Notifications/EmailNotification.php index 1ebb16152..427391958 100644 --- a/src/Notifications/EmailNotification.php +++ b/src/Notifications/EmailNotification.php @@ -2,7 +2,7 @@ namespace Notifications; -use Models\MailAccount; +use Modules\Emails\Account; use PHPMailer\PHPMailer\PHPMailer; use Prints; use Uploads; @@ -21,9 +21,9 @@ class EmailNotification extends PHPMailer implements NotificationInterface $this->CharSet = 'UTF-8'; // Configurazione di base - $config = MailAccount::find($account); + $config = Account::find($account); if (empty($config)) { - $config = MailAccount::where('predefined', true)->first(); + $config = Account::where('predefined', true)->first(); } // Preparazione email @@ -71,7 +71,7 @@ class EmailNotification extends PHPMailer implements NotificationInterface $this->WordWrap = 78; } - public static function build(\Models\Mail $mail, $exceptions = null) + public static function build(\Modules\Emails\Mail $mail, $exceptions = null) { $result = new self($mail->account->id, $exceptions); @@ -153,7 +153,7 @@ class EmailNotification extends PHPMailer implements NotificationInterface $this->mail->failed_at = date('Y-m-d H:i:s'); } - $this->mail->save(); + //$this->mail->save(); } $this->SmtpClose(); @@ -192,32 +192,6 @@ class EmailNotification extends PHPMailer implements NotificationInterface return false; } - /** - * Restituisce gli allegati della notifica. - * - * @return array - */ - public function getAttachments() - { - return $this->attachments; - } - - /** - * Imposta gli allegati della notifica. - * - * @param array $values - */ - public function setAttachments(array $values) - { - $this->attachments = []; - - foreach ($values as $value) { - $path = is_array($value) ? $value['path'] : $value; - $name = is_array($value) ? $value['name'] : null; - $this->addAttachment($path, $name); - } - } - /** * Aggiunge un allegato del gestionale alla notifica. * @@ -226,9 +200,8 @@ class EmailNotification extends PHPMailer implements NotificationInterface public function addUpload($file_id) { $attachment = database()->fetchOne('SELECT * FROM zz_files WHERE id = '.prepare($file_id)); - $this->addAttachment(DOCROOT.'/'.Uploads::getDirectory($attachment['id_module'], $attachment['id_plugin']).'/'.$attachment['filename']); - $this->logs['attachments'][] = $attachment['id']; + $this->addAttachment(DOCROOT.'/'.Uploads::getDirectory($attachment['id_module'], $attachment['id_plugin']).'/'.$attachment['filename']); } /** @@ -253,8 +226,6 @@ class EmailNotification extends PHPMailer implements NotificationInterface $name = $name ?: $info['name']; $this->addAttachment($info['path'], $name); - - $this->logs['prints'][] = $print['id']; } /** diff --git a/src/Update.php b/src/Update.php index 96a778727..4a1b63221 100644 --- a/src/Update.php +++ b/src/Update.php @@ -269,6 +269,9 @@ class Update } } + // Normalizzazione di charset e collation + self::normalizeDatabase($database->getDatabaseName()); + // Normalizzazione dei campi per l'API self::executeScript(DOCROOT.'/update/api.php'); diff --git a/update/2_4_11.php b/update/2_4_11.php index d5981330c..b2f335f1c 100644 --- a/update/2_4_11.php +++ b/update/2_4_11.php @@ -1,27 +1,36 @@ query('ALTER TABLE `zz_operations` DROP FOREIGN KEY `zz_operations_ibfk_3`'); $logs = $database->fetchArray("SELECT * FROM `zz_operations` WHERE `op` = 'send-email'"); foreach ($logs as $log) { $user = User::find($log['id_utente']); - $template = MailTemplate::find($log['id_email']); + $template = Template::find($log['id_email']); + $mail = Mail::build($user, $template, $log['id_record']); + $mail->resetPrints(); $options = json_decode($log['options'], true); - $mail->attachments = $options['attachments'] ?: []; - $mail->prints = $options['prints'] ?: []; - foreach ($options['receivers'] as $receiver){ + foreach ($options['receivers'] as $receiver) { $mail->addReceiver($receiver); } - $mail->created_at = $log['created_at']; - $mail->sent_at = $log['created_at']; + foreach ($options['attachments'] as $upload) { + $mail->addUpload($upload); + } + + foreach ($options['prints'] as $print) { + $mail->addPrint($print); + } + + $sent_at = $log['created_at'] ?: date('Y-m-d H:i:s'); + $mail->created_at = $sent_at; + $mail->sent_at = $sent_at; $mail->save(); diff --git a/update/2_4_11.sql b/update/2_4_11.sql index 32d5a23c1..1a5f26855 100644 --- a/update/2_4_11.sql +++ b/update/2_4_11.sql @@ -318,26 +318,27 @@ ALTER TABLE `mg_articoli` ADD `deleted_at` timestamp NULL DEFAULT NULL; UPDATE `zz_modules` SET `options` = 'SELECT |select| FROM `mg_articoli` WHERE 1=1 AND `deleted_at` IS NULL HAVING 2=2 ORDER BY `descrizione`' WHERE `name` = 'Articoli'; -- Ampliamento hooks -ALTER TABLE `zz_hooks` ADD `processing` BOOLEAN DEFAULT FALSE; +ALTER TABLE `zz_hooks` ADD `processing_at` TIMESTAMP NULL DEFAULT NULL, ADD `processing_token` varchar(255); INSERT INTO `zz_hooks` (`id`, `name`, `class`, `frequency`, `id_module`) VALUES (NULL, 'Backup', 'Modules\\Backups\\BackupHook', '1 day', (SELECT `id` FROM `zz_modules` WHERE `name` = 'Backup')); -- Miglioramento gestione email ALTER TABLE `zz_emails` RENAME TO `em_templates`; ALTER TABLE `zz_smtps` RENAME TO `em_accounts`; -ALTER TABLE `zz_email_print` RENAME TO `em_template_print`; +ALTER TABLE `zz_email_print` RENAME TO `em_print_template`; UPDATE zz_modules SET options = REPLACE(options, 'zz_emails', 'em_templates'), options2 = REPLACE(options2, 'zz_emails', 'em_templates'); UPDATE zz_modules SET options = REPLACE(options, 'zz_smtps', 'em_accounts'), options2 = REPLACE(options2, 'zz_smtps', 'em_accounts'); UPDATE zz_views SET query = REPLACE(query, 'zz_emails', 'em_templates'); UPDATE zz_views SET query = REPLACE(query, 'zz_smtps', 'em_accounts'); -CREATE TABLE IF NOT EXISTS `em_campaigns` ( +CREATE TABLE IF NOT EXISTS `em_newsletters` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL, `id_template` int(11) NOT NULL, `state` varchar(25) NOT NULL, `subject` varchar(255) NOT NULL, `content` TEXT NOT NULL, + `notes` TEXT, `created_by` int(11) NOT NULL, `completed_at` TIMESTAMP NULL DEFAULT NULL, `deleted_at` TIMESTAMP NULL DEFAULT NULL, @@ -350,40 +351,84 @@ CREATE TABLE IF NOT EXISTS `em_emails` ( `id` int(11) NOT NULL AUTO_INCREMENT, `id_account` int(11) NOT NULL, `id_template` int(11), - `id_campaign` int(11), + `id_newsletter` int(11), `id_record` int(11), `subject` varchar(255), `content` TEXT, - `receivers` TEXT, - `attachments` TEXT, - `prints` TEXT, `options` TEXT, `sent_at` TIMESTAMP NULL DEFAULT NULL, `failed_at` TIMESTAMP NULL DEFAULT NULL, + `processing_at` TIMESTAMP NULL DEFAULT NULL, `created_by` int(11) NOT NULL, PRIMARY KEY (`id`), FOREIGN KEY (`id_account`) REFERENCES `em_accounts`(`id`) ON DELETE CASCADE, FOREIGN KEY (`id_template`) REFERENCES `em_templates`(`id`) ON DELETE CASCADE, - FOREIGN KEY (`id_campaign`) REFERENCES `em_campaigns`(`id`) ON DELETE CASCADE, + FOREIGN KEY (`id_newsletter`) REFERENCES `em_newsletters`(`id`) ON DELETE CASCADE, FOREIGN KEY (`created_by`) REFERENCES `zz_users`(`id`) ON DELETE CASCADE ) ENGINE=InnoDB; -CREATE TABLE IF NOT EXISTS `em_campaign_anagrafica` ( - `id_campaign` int(11) NOT NULL, +CREATE TABLE IF NOT EXISTS `em_email_receiver` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `id_email` int(11) NOT NULL, + `type` varchar(255) NOT NULL, + `address` varchar(255) NOT NULL, + PRIMARY KEY (`id`), + FOREIGN KEY (`id_email`) REFERENCES `em_emails`(`id`) ON DELETE CASCADE +) ENGINE=InnoDB; + +CREATE TABLE IF NOT EXISTS `em_email_upload` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `id_email` int(11) NOT NULL, + `id_file` int(11) NOT NULL, + `name` varchar(255), + PRIMARY KEY (`id`), + FOREIGN KEY (`id_email`) REFERENCES `em_emails`(`id`) ON DELETE CASCADE, + FOREIGN KEY (`id_file`) REFERENCES `zz_files`(`id`) ON DELETE CASCADE +) ENGINE=InnoDB; + +CREATE TABLE IF NOT EXISTS `em_email_print` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `id_email` int(11) NOT NULL, + `id_print` int(11) NOT NULL, + `name` varchar(255), + PRIMARY KEY (`id`), + FOREIGN KEY (`id_email`) REFERENCES `em_emails`(`id`) ON DELETE CASCADE, + FOREIGN KEY (`id_print`) REFERENCES `zz_prints`(`id`) ON DELETE CASCADE +) ENGINE=InnoDB; + +CREATE TABLE IF NOT EXISTS `em_newsletter_anagrafica` ( + `id_newsletter` int(11) NOT NULL, `id_anagrafica` int(11) NOT NULL, `id_email` int(11), - FOREIGN KEY (`id_campaign`) REFERENCES `em_campaigns`(`id`) ON DELETE CASCADE, + FOREIGN KEY (`id_newsletter`) REFERENCES `em_newsletters`(`id`) ON DELETE CASCADE, FOREIGN KEY (`id_anagrafica`) REFERENCES `an_anagrafiche`(`idanagrafica`) ON DELETE CASCADE, FOREIGN KEY (`id_email`) REFERENCES `em_emails`(`id`) ON DELETE CASCADE ) ENGINE=InnoDB; +-- Hook per la gestione della coda di invio ALTER TABLE `zz_hooks` CHANGE `id_module` `id_module` INT(11) NULL; INSERT INTO `zz_hooks` (`id`, `name`, `class`, `frequency`, `id_module`) VALUES (NULL, 'Email', 'Modules\\Emails\\EmailHook', '1 minute', NULL); -INSERT INTO `zz_modules` (`id`, `name`, `title`, `directory`, `options`, `options2`, `icon`, `version`, `compatibility`, `order`, `parent`, `default`, `enabled`) VALUES (NULL, 'Newsletter', 'Newsletter', 'newsletter', 'SELECT |select| FROM `em_campaigns` WHERE 1=1 AND deleted_at IS NULL HAVING 2=2', '', 'fa fa-newspaper-o ', '2.4.11', '2.4.11', '1', (SELECT `id` FROM `zz_modules` t WHERE t.`name` = 'Gestione email'), '1', '1'); +-- Modulo Newsletter +INSERT INTO `zz_modules` (`id`, `name`, `title`, `directory`, `options`, `options2`, `icon`, `version`, `compatibility`, `order`, `parent`, `default`, `enabled`) VALUES (NULL, 'Newsletter', 'Newsletter', 'newsletter', 'SELECT |select| FROM `em_newsletters` WHERE 1=1 AND deleted_at IS NULL HAVING 2=2', '', 'fa fa-newspaper-o ', '2.4.11', '2.4.11', '1', (SELECT `id` FROM `zz_modules` t WHERE t.`name` = 'Gestione email'), '1', '1'); INSERT INTO `zz_views` (`id_module`, `name`, `query`, `order`, `search`, `slow`, `default`, `visible`) VALUES ((SELECT `id` FROM `zz_modules` WHERE `name` = 'Newsletter'), 'id', 'id', 1, 0, 0, 1, 0), ((SELECT `id` FROM `zz_modules` WHERE `name` = 'Newsletter'), 'Nome', 'name', 2, 1, 0, 0, 1), -((SELECT `id` FROM `zz_modules` WHERE `name` = 'Newsletter'), 'Template', '(SELECT name FROM em_templates WHERE id = em_campaigns.id_template)', 3, 1, 0, 1, 1), +((SELECT `id` FROM `zz_modules` WHERE `name` = 'Newsletter'), 'Template', '(SELECT name FROM em_templates WHERE id = em_newsletters.id_template)', 3, 1, 0, 1, 1), ((SELECT `id` FROM `zz_modules` WHERE `name` = 'Newsletter'), 'Completato', 'IF(completed_at IS NULL, ''No'', ''Si'')', 4, 1, 0, 1, 1); + +-- Modulo Stato email +INSERT INTO `zz_modules` (`id`, `name`, `title`, `directory`, `options`, `options2`, `icon`, `version`, `compatibility`, `order`, `parent`, `default`, `enabled`) VALUES (NULL, 'Stato email', 'Stato email', 'stato_email', 'SELECT |select| FROM `em_emails` WHERE 1=1 AND (`em_emails`.`created_at` BETWEEN ''|period_start|'' AND ''|period_end|'' OR `em_emails`.`sent_at` IS NULL) HAVING 2=2', '', 'fa fa-spinner ', '2.4.11', '2.4.11', '1', (SELECT `id` FROM `zz_modules` t WHERE t.`name` = 'Gestione email'), '1', '1'); + +INSERT INTO `zz_views` (`id_module`, `name`, `query`, `order`, `search`, `slow`, `default`, `visible`, `format`) VALUES +((SELECT `id` FROM `zz_modules` WHERE `name` = 'Stato email'), 'id', 'id', 1, 0, 0, 1, 0, 0), +((SELECT `id` FROM `zz_modules` WHERE `name` = 'Stato email'), 'Oggetto', 'subject', 2, 1, 0, 0, 1, 0), +((SELECT `id` FROM `zz_modules` WHERE `name` = 'Stato email'), 'Contenuto', 'content', 3, 1, 0, 0, 1, 0), +((SELECT `id` FROM `zz_modules` WHERE `name` = 'Stato email'), 'Template', '(SELECT name FROM em_templates WHERE id = em_emails.id_template)', 3, 1, 0, 1, 1, 0), +((SELECT `id` FROM `zz_modules` WHERE `name` = 'Stato email'), 'Data invio', 'sent_at', 4, 1, 0, 1, 1, 1), +((SELECT `id` FROM `zz_modules` WHERE `name` = 'Stato email'), 'Ultimo tentativo', 'failed_at', 5, 1, 0, 1, 1, 1), +((SELECT `id` FROM `zz_modules` WHERE `name` = 'Stato email'), '_bg_', 'IF(sent_at IS NULL, IF(failed_at IS NULL, ''#CC9837'', ''#CC4D37''), ''#38CD4E'')', 6, 1, 0, 0, 0, 0); + +ALTER TABLE `em_templates` CHANGE `id_smtp` `id_account` INT(11) NOT NULL; +ALTER TABLE `em_print_template` CHANGE `id_email` `id_template` INT(11) NOT NULL; diff --git a/update/tables.php b/update/tables.php index f9cb0fb62..37b7a0362 100644 --- a/update/tables.php +++ b/update/tables.php @@ -48,12 +48,15 @@ return [ 'dt_spedizione', 'dt_statiddt', 'dt_tipiddt', - 'em_template_print', + 'em_print_template', 'em_accounts', 'em_templates', - 'em_campaigns', + 'em_newsletters', 'em_emails', - 'em_campaign_anagrafica', + 'em_email_receiver', + 'em_email_upload', + 'em_email_print', + 'em_newsletter_anagrafica', 'fe_causali_pagamento_ritenuta', 'fe_modalita_pagamento', 'fe_natura',