diff --git a/modules/emails/actions.php b/modules/emails/actions.php index 3448978ba..6587eda8d 100755 --- a/modules/emails/actions.php +++ b/modules/emails/actions.php @@ -17,43 +17,40 @@ * along with this program. If not, see . */ +use Modules\Emails\Template; + include_once __DIR__.'/../../core.php'; switch (post('op')) { case 'add': - $dbo->insert('em_templates', [ - 'id_module' => post('module'), - 'id_account' => post('smtp'), - ]); - $id_record = $dbo->lastInsertedID(); - $dbo->insert('em_templates_lang', [ - 'name' => post('name'), - 'subject' => post('subject'), - 'id_lang' => setting('Lingua'), - 'id_record' => $id_record - ]); + $id_module = post('module'); + $id_account = post('smtp'); + $name = post('name'); + $subject = post('subject'); + + $template = Template::build($id_module, $id_account); + $id_record = $template->id; + $template->name = $name; + $template->subject = $subject; + $template->save(); flash()->info(tr('Aggiunto nuovo template per le email!')); break; case 'update': - $dbo->update('em_templates', [ - 'id_account' => post('smtp'), - 'icon' => post('icon'), - 'tipo_reply_to' => post('tipo_reply_to'), - 'reply_to' => post('reply_to'), - 'cc' => post('cc'), - 'bcc' => post('bcc'), - 'read_notify' => post('read_notify'), - 'note_aggiuntive' => post('note_aggiuntive') - ], ['id' => $id_record]); - - $dbo->update('em_templates_lang', [ - 'name' => post('name'), - 'subject' => post('subject'), - 'body' => post('body') - ], ['id_record' => $id_record, 'id_lang' => (setting('Lingua'))]); + $template->name = post('name'); + $template->id_account = post('smtp'); + $template->icon = post('icon'); + $template->tipo_reply_to = post('tipo_reply_to'); + $template->reply_to = post('reply_to'); + $template->cc = post('cc'); + $template->bcc = post('bcc'); + $template->read_notify = post('read_notify'); + $template->note_aggiuntive = post('note_aggiuntive'); + $template->subject = post('subject'); + $template->body = post('body'); + $template->save(); $dbo->sync('em_print_template', ['id_template' => $id_record], ['id_print' => (array) post('prints')]); $dbo->sync('em_mansioni_template', ['id_template' => $id_record], ['idmansione' => (array) post('idmansioni')]); diff --git a/modules/emails/init.php b/modules/emails/init.php index aeeeda8e8..3dd2637c2 100755 --- a/modules/emails/init.php +++ b/modules/emails/init.php @@ -18,12 +18,15 @@ */ use Modules\Newsletter\Newsletter; +use Modules\Emails\Template; include_once __DIR__.'/../../core.php'; if (isset($id_record)) { $record = $dbo->fetchOne('SELECT * FROM em_templates LEFT JOIN `em_templates_lang` ON (`em_templates`.`id` = `em_templates_lang`.`id_record` AND `em_templates_lang`.`id_lang` = '.prepare(setting('Lingua')).') WHERE `em_templates`.`id`='.prepare($id_record).' AND `deleted_at` IS NULL'); + $template = Template::find($id_record); + // Controllo se ci sono newletter collegate a questo template $newsletters = Newsletter::where('id_template', $id_record)->get(); } diff --git a/modules/emails/src/Template.php b/modules/emails/src/Template.php index 56968fe43..3a5cc1078 100755 --- a/modules/emails/src/Template.php +++ b/modules/emails/src/Template.php @@ -44,6 +44,16 @@ class Template extends Model return (array) $variables; } + public static function build($id_module, $id_account) + { + $model = new static(); + $model->id_module = $id_module; + $model->id_account = $id_account; + $model->save(); + + return $model; + } + /* Relazioni Eloquent */ public function module() @@ -75,6 +85,29 @@ class Template extends Model ->first()->name; } + /** + * Imposta l'attributo name della lista. + */ + public function setNameAttribute($value) + { + $table = database()->table($this->table.'_lang'); + + $translated = $table + ->where('id_record', '=', $this->id) + ->where('id_lang', '=', setting('Lingua')); + + if ($translated->count() > 0) { + $translated->update([ + 'name' => $value + ]); + } else { + $table->insert([ + 'id_record' => $this->id, + 'id_lang' => setting('Lingua'), + 'name' => $value + ]); + } + } /** * Ritorna l'attributo subject del template. * @@ -89,6 +122,30 @@ class Template extends Model ->first()->subject; } + /** + * Imposta l'attributo subject del template. + */ + public function setSubjectAttribute($value) + { + $table = database()->table($this->table.'_lang'); + + $translated = $table + ->where('id_record', '=', $this->id) + ->where('id_lang', '=', setting('Lingua')); + + if ($translated->count() > 0) { + $translated->update([ + 'subject' => $value + ]); + } else { + $table->insert([ + 'id_record' => $this->id, + 'id_lang' => setting('Lingua'), + 'subject' => $value + ]); + } + } + /** * Ritorna l'attributo body del template. * @@ -102,6 +159,29 @@ class Template extends Model ->where('id_lang', '=', setting('Lingua')) ->first()->body; } + /** + * Imposta l'attributo body del template. + */ + public function setBodyAttribute($value) + { + $table = database()->table($this->table.'_lang'); + + $translated = $table + ->where('id_record', '=', $this->id) + ->where('id_lang', '=', setting('Lingua')); + + if ($translated->count() > 0) { + $translated->update([ + 'body' => $value + ]); + } else { + $table->insert([ + 'id_record' => $this->id, + 'id_lang' => setting('Lingua'), + 'body' => $value + ]); + } + } /** * Ritorna l'id del template a partire dal nome. diff --git a/modules/liste_newsletter/src/Lista.php b/modules/liste_newsletter/src/Lista.php index 55aa9535b..70b23d0c1 100644 --- a/modules/liste_newsletter/src/Lista.php +++ b/modules/liste_newsletter/src/Lista.php @@ -145,7 +145,7 @@ class Lista extends Model } /** - * Imposta l'attributo name della lista. + * Imposta l'attributo description della lista. */ public function setDescriptionAttribute($value) { @@ -169,7 +169,22 @@ class Lista extends Model } /** - * Imposta l'attributo description della lista. + * Ritorna l'attributo description della lista. + * + * @return string + */ + public function getDescriptionAttribute() + { + return database()->table($this->table.'_lang') + ->select('description') + ->where('id_record', '=', $this->id) + ->where('id_lang', '=', setting('Lingua')) + ->first()->description; + } + + + /** + * Imposta l'attributo name della lista. */ public function setNameAttribute($value) { @@ -191,21 +206,6 @@ class Lista extends Model ]); } } - - /** - * Ritorna l'attributo description della lista. - * - * @return string - */ - public function getDescriptionAttribute() - { - return database()->table($this->table.'_lang') - ->select('description') - ->where('id_record', '=', $this->id) - ->where('id_lang', '=', setting('Lingua')) - ->first()->description; - } - /** * Ritorna l'id della lista a partire dal nome.