diff --git a/modules/emails/actions.php b/modules/emails/actions.php index 859455a10..3448978ba 100755 --- a/modules/emails/actions.php +++ b/modules/emails/actions.php @@ -22,13 +22,16 @@ include_once __DIR__.'/../../core.php'; switch (post('op')) { case 'add': $dbo->insert('em_templates', [ - 'name' => post('name'), 'id_module' => post('module'), 'id_account' => post('smtp'), - 'subject' => post('subject'), ]); - $id_record = $dbo->lastInsertedID(); + $dbo->insert('em_templates_lang', [ + 'name' => post('name'), + 'subject' => post('subject'), + 'id_lang' => setting('Lingua'), + 'id_record' => $id_record + ]); flash()->info(tr('Aggiunto nuovo template per le email!')); @@ -36,19 +39,22 @@ switch (post('op')) { case 'update': $dbo->update('em_templates', [ - 'name' => post('name'), 'id_account' => post('smtp'), 'icon' => post('icon'), - 'subject' => post('subject'), 'tipo_reply_to' => post('tipo_reply_to'), 'reply_to' => post('reply_to'), 'cc' => post('cc'), 'bcc' => post('bcc'), - 'body' => $_POST['body'], // post('body', true), 'read_notify' => post('read_notify'), - 'note_aggiuntive' => post('note_aggiuntive'), + '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'))]); + $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')]); @@ -57,22 +63,25 @@ switch (post('op')) { break; case 'delete': - $dbo->query('UPDATE em_templates SET deleted_at = NOW() WHERE id='.prepare($id_record)); + $dbo->query('UPDATE `em_templates` SET `deleted_at` = NOW() WHERE `id`='.prepare($id_record)); flash()->info(tr('Template delle email eliminato!')); break; case 'copy': - $dbo->query('CREATE TEMPORARY TABLE tmp SELECT * FROM em_templates WHERE id= '.prepare($id_record)); - $dbo->query('ALTER TABLE tmp DROP id'); - $dbo->query('INSERT INTO em_templates SELECT NULL,tmp. * FROM tmp'); - $id_record = $dbo->lastInsertedID(); - $dbo->query('DROP TEMPORARY TABLE tmp'); - - $dbo->query('UPDATE em_templates SET name = CONCAT (name, " (copia)"), predefined=0 WHERE id = '.prepare($id_record)); - + $database->beginTransaction(); + $database->query('CREATE TEMPORARY TABLE `tmp` SELECT * FROM `em_templates` WHERE `id`= '.prepare($id_record)); + $database->query('CREATE TEMPORARY TABLE `tmp_lang` SELECT * FROM `em_templates_lang` WHERE `id_record`= '.prepare($id_record)); + $database->query('ALTER TABLE `tmp` DROP `id`'); + $database->query('ALTER TABLE `tmp_lang` DROP `id_record`'); + $database->query('INSERT INTO `em_templates` SELECT NULL,tmp. * FROM tmp'); + $id_record = $database->lastInsertedID(); + $database->query('INSERT INTO `em_templates_lang` SELECT NULL, id_lang, '.$id_record.',name, subject, body FROM tmp_lang'); + $database->query('DROP TEMPORARY TABLE tmp'); + $database->query('DROP TEMPORARY TABLE tmp_lang'); + $database->query('UPDATE `em_templates_lang` SET `name` = CONCAT (`name`, " (copia)") WHERE id_record = '.prepare($id_record)); + $database->query('UPDATE `em_templates` SET `predefined` = 0 WHERE `id` = '.prepare($id_record)); flash()->info(tr('Template duplicato correttamente!')); - break; } diff --git a/modules/emails/edit.php b/modules/emails/edit.php index dc29cdd4b..3ddc24263 100755 --- a/modules/emails/edit.php +++ b/modules/emails/edit.php @@ -44,7 +44,7 @@ if (!$record['predefined']) {
- {[ "type": "span", "label": "", "name": "module", "values": "query=SELECT id, title AS descrizione FROM zz_modules WHERE enabled = 1", "value": "" ]} + {[ "type": "span", "label": "", "name": "module", "values": "query=SELECT `id`, `title` AS descrizione FROM `zz_modules` WHERE `enabled` = 1", "value": "" ]}
diff --git a/modules/emails/init.php b/modules/emails/init.php index e9845920b..aeeeda8e8 100755 --- a/modules/emails/init.php +++ b/modules/emails/init.php @@ -22,7 +22,7 @@ use Modules\Newsletter\Newsletter; include_once __DIR__.'/../../core.php'; if (isset($id_record)) { - $record = $dbo->fetchOne('SELECT * FROM em_templates WHERE id='.prepare($id_record).' AND deleted_at IS NULL'); + $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'); // 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 a7a491c87..56968fe43 100755 --- a/modules/emails/src/Template.php +++ b/modules/emails/src/Template.php @@ -60,4 +60,67 @@ class Template extends Model { return $this->belongsToMany(PrintTemplate::class, 'em_print_template', 'id_template', 'id_print'); } + + /** + * Ritorna l'attributo name del template. + * + * @return string + */ + public function getNameAttribute() + { + return database()->table($this->table.'_lang') + ->select('name') + ->where('id_record', '=', $this->id) + ->where('id_lang', '=', setting('Lingua')) + ->first()->name; + } + + /** + * Ritorna l'attributo subject del template. + * + * @return string + */ + public function getSubjectAttribute() + { + return database()->table($this->table.'_lang') + ->select('subject') + ->where('id_record', '=', $this->id) + ->where('id_lang', '=', setting('Lingua')) + ->first()->subject; + } + + /** + * Ritorna l'attributo body del template. + * + * @return string + */ + public function getBodyAttribute() + { + return database()->table($this->table.'_lang') + ->select('body') + ->where('id_record', '=', $this->id) + ->where('id_lang', '=', setting('Lingua')) + ->first()->body; + } + + /** + * Ritorna l'id del template a partire dal nome. + * + * @param string $name il nome da ricercare + * + * @return \Illuminate\Support\Collection + */ + public function getByName($name) + { + return database()->table($this->table.'_lang') + ->select('id_record') + ->where('name', '=', $name) + ->where('id_lang', '=', setting('Lingua')) + ->first(); + } + + public function translations() + { + return $this->hasMany(TemplateLang::class, 'id'); + } } diff --git a/modules/emails/src/TemplateLang.php b/modules/emails/src/TemplateLang.php new file mode 100644 index 000000000..bd65d3b4e --- /dev/null +++ b/modules/emails/src/TemplateLang.php @@ -0,0 +1,32 @@ +. + */ + +namespace Modules\Emails; + +use Common\SimpleModelTrait; +use Illuminate\Database\Eloquent\Model; +use Traits\LocalPoolTrait; + +class TemplateLang extends Model +{ + use SimpleModelTrait; + use LocalPoolTrait; + + protected $table = 'em_templates_lang'; +} \ No newline at end of file diff --git a/modules/interventi/bulk.php b/modules/interventi/bulk.php index 2548f2eef..c4d8634f7 100755 --- a/modules/interventi/bulk.php +++ b/modules/interventi/bulk.php @@ -471,7 +471,7 @@ $operations['send-mail'] = [ 'data' => [ 'title' => tr('Inviare mail?'), 'msg' => tr('Per ciascuna attività selezionata, verrà inviata una mail').'

- {[ "type": "select", "label": "'.tr('Template').'", "name": "id_template", "required": "1", "values": "query=SELECT id, name AS descrizione FROM em_templates WHERE id_module='.prepare($id_module).' AND deleted_at IS NULL;" ]}', + {[ "type": "select", "label": "'.tr('Template').'", "name": "id_template", "required": "1", "values": "query=SELECT `em_templates`.`id`, `em_templates_lang`.`name` AS descrizione 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 `id_module`='.prepare($id_module).' AND `deleted_at` IS NULL;" ]}', 'button' => tr('Invia'), 'class' => 'btn btn-lg btn-warning', ], diff --git a/modules/newsletter/add.php b/modules/newsletter/add.php index 14360714a..5f83c3000 100755 --- a/modules/newsletter/add.php +++ b/modules/newsletter/add.php @@ -26,7 +26,7 @@ echo '
- {[ "type": "select", "label": "'.tr('Template email').'", "name": "id_template", "values": "query=SELECT id, name AS descrizione FROM em_templates WHERE deleted_at IS NULL ORDER BY descrizione", "required": 1 ]} + {[ "type": "select", "label": "'.tr('Template email').'", "name": "id_template", "values": "query=SELECT `em_templates`.`id`, `em_templates_lang`.`name` AS descrizione 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 `deleted_at` IS NULL ORDER BY `name`", "required": 1 ]}
diff --git a/modules/newsletter/edit.php b/modules/newsletter/edit.php index eb64fe49a..f0f458c2a 100755 --- a/modules/newsletter/edit.php +++ b/modules/newsletter/edit.php @@ -59,7 +59,7 @@ echo '
'.Modules::link('Template email', $record['id_template'], null, null, 'class="pull-right"').' - {[ "type": "select", "label": "'.tr('Template email').'", "name": "id_template", "values": "query=SELECT id, name AS descrizione FROM em_templates WHERE deleted_at IS NULL ORDER BY descrizione", "required": 1, "value": "$id_template$", "readonly": 1 ]} + {[ "type": "select", "label": "'.tr('Template email').'", "name": "id_template", "values": "query=SELECT `em_templates`.`id`, `em_templates_lang`.`name` AS descrizione 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 `deleted_at` IS NULL ORDER BY `name`", "required": 1, "value": "$id_template$", "readonly": 1 ]}
diff --git a/modules/smtp/edit.php b/modules/smtp/edit.php index 9ce32462c..5b3348751 100755 --- a/modules/smtp/edit.php +++ b/modules/smtp/edit.php @@ -216,7 +216,7 @@ $(document).ready(function() { // Collegamenti diretti // Template email collegati a questo account -$elementi = $dbo->fetchArray('SELECT `id`, `name` FROM `em_templates` WHERE `id_account` = '.prepare($id_record)); +$elementi = $dbo->fetchArray('SELECT `em_templates`.`id`, `em_templates_lang`.`name` 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 `id_account` = '.prepare($id_record)); if (!empty($elementi)) { echo ' diff --git a/modules/stati_intervento/edit.php b/modules/stati_intervento/edit.php index ede921cdc..3fd80ca3a 100755 --- a/modules/stati_intervento/edit.php +++ b/modules/stati_intervento/edit.php @@ -58,7 +58,7 @@ if ($record['can_delete']) {
- {[ "type": "select", "label": "", "name": "email", "value": "$id_email$", "values": "query=SELECT id, name AS descrizione FROM em_templates WHERE id_module = AND deleted_at IS NULL", "disabled": ]} + {[ "type": "select", "label": "", "name": "email", "value": "$id_email$", "values": "query=SELECT `em_templates`.`id`, `em_templates_lang`.`name` AS descrizione FROM `em_templates` LEFT JOIN `em_templates_lang` ON (`em_templates`.`id` = `em_templates_lang`.`id_record` AND `em_templates_lang`.`id_lang` = ) WHERE `id_module` = AND `deleted_at` IS NULL", "disabled": ]}
diff --git a/src/HTMLBuilder/Manager/ButtonManager.php b/src/HTMLBuilder/Manager/ButtonManager.php index 0b684e81d..27f3dbe56 100755 --- a/src/HTMLBuilder/Manager/ButtonManager.php +++ b/src/HTMLBuilder/Manager/ButtonManager.php @@ -60,7 +60,7 @@ class ButtonManager implements ManagerInterface $result = [ 'link' => base_path().'/mail.php?id_module='.$options['id_module'].'&id_record='.$options['id_record'].'&id='.$options['id'].$options['parameters'], - 'title' => tr('Invia').' '.((strtoupper($template_email['name']) == $template_email['name']) ? $template_email['name'] : lcfirst($template_email['name'])), + 'title' => tr('Invia').' '.((strtoupper($template_email->name) == $template_email->name) ? $template_email->name : lcfirst($template_email->name)), 'icon' => $template_email['icon'], 'type' => 'modal', ]; @@ -118,7 +118,9 @@ class ButtonManager implements ManagerInterface if ($options['type'] == 'print') { $results = \Prints::getModulePrints($options['id_module']); } elseif ($options['type'] == 'email') { - $results = TemplateEmail::where('id_module', $options['id_module'])->orderBy('name')->get()->toArray(); + $results = (TemplateEmail::where('id_module', $options['id_module']))->with(['translations' => function($query) { + $query->orderBy('name'); + }])->get()->toArray(); } elseif ($options['type'] == 'sms') { $results = TemplateSMS::where('id_module', $options['id_module'])->orderBy('name')->get()->toArray(); } diff --git a/update/2_4_55.sql b/update/2_4_55.sql index 349ba8e07..a36f9d946 100644 --- a/update/2_4_55.sql +++ b/update/2_4_55.sql @@ -1093,4 +1093,60 @@ HAVING 2=2" WHERE `name` = 'Liste newsletter'; UPDATE `zz_views` INNER JOIN `zz_modules` ON `zz_views`.`id_module` = `zz_modules`.`id` SET `zz_views`.`query` = '`em_lists`.`id`' WHERE `zz_modules`.`name` = 'Liste newsletter' AND `zz_views`.`name` = 'id'; UPDATE `zz_views` INNER JOIN `zz_modules` ON `zz_views`.`id_module` = `zz_modules`.`id` SET `zz_views`.`query` = '`em_lists_lang`.`description`' WHERE `zz_modules`.`name` = 'Liste newsletter' AND `zz_views`.`name` = 'Descrizione'; -UPDATE `zz_views` INNER JOIN `zz_modules` ON `zz_views`.`id_module` = `zz_modules`.`id` SET `zz_views`.`query` = '`em_lists_lang`.`name`' WHERE `zz_modules`.`name` = 'Liste newsletter' AND `zz_views`.`name` = 'Nome'; \ No newline at end of file +UPDATE `zz_views` INNER JOIN `zz_modules` ON `zz_views`.`id_module` = `zz_modules`.`id` SET `zz_views`.`query` = '`em_lists_lang`.`name`' WHERE `zz_modules`.`name` = 'Liste newsletter' AND `zz_views`.`name` = 'Nome'; + +-- Aggiunta tabella em_templates_lang +CREATE TABLE IF NOT EXISTS `em_templates_lang` ( + `id` int NOT NULL, + `id_lang` int NOT NULL, + `id_record` int NOT NULL, + `name` VARCHAR(255) NOT NULL, + `subject` VARCHAR(255) NOT NULL, + `body` TEXT NOT NULL +); +ALTER TABLE `em_templates_lang` + ADD PRIMARY KEY (`id`); + +ALTER TABLE `em_templates_lang` + MODIFY `id` int NOT NULL AUTO_INCREMENT; + +INSERT INTO `em_templates_lang` (`id`, `id_lang`, `id_record`, `name`, `subject`, `body`) SELECT NULL, (SELECT `id` FROM `zz_langs` WHERE `iso_code` = 'it'), `id`, `name`, `subject`, `body` FROM `em_templates`; + +ALTER TABLE `em_templates` + DROP `name`, + DROP `subject`, + DROP `body`; + +ALTER TABLE `em_templates_lang` ADD CONSTRAINT `em_templates_lang_ibfk_1` FOREIGN KEY (`id_record`) REFERENCES `em_templates`(`id`) ON DELETE CASCADE ON UPDATE RESTRICT; + +-- Allineamento vista Template email +UPDATE `zz_modules` SET `options` = " +SELECT + |select| +FROM + `em_templates` + LEFT JOIN `em_templates_lang` ON (`em_templates_lang`.`id_record` = `em_templates`.`id` AND `em_templates_lang`.|lang|) + INNER JOIN `zz_modules` on `zz_modules`.`id` = `em_templates`.`id_module` +WHERE + 1=1 AND `deleted_at` IS NULL +HAVING + 2=2 +ORDER BY + `zz_modules`.`name`" WHERE `name` = 'Template email'; +UPDATE `zz_views` INNER JOIN `zz_modules` ON `zz_views`.`id_module` = `zz_modules`.`id` SET `zz_views`.`query` = '`em_templates_lang`.`subject`' WHERE `zz_modules`.`name` = 'Template email' AND `zz_views`.`name` = 'Oggetto'; +UPDATE `zz_views` INNER JOIN `zz_modules` ON `zz_views`.`id_module` = `zz_modules`.`id` SET `zz_views`.`query` = '`em_templates_lang`.`name`' WHERE `zz_modules`.`name` = 'Template email' AND `zz_views`.`name` = 'Nome'; + +-- Allineamento vista Newsletter +UPDATE `zz_modules` SET `options` = " +SELECT + |select| +FROM + `em_newsletters` + LEFT JOIN `em_templates` ON `em_newsletters`.`id_template` = `em_templates`.`id` + LEFT JOIN `em_templates_lang` ON (`em_templates_lang`.`id_record` = `em_templates`.`id` AND `em_templates_lang`.|lang|) + LEFT JOIN (SELECT `id_newsletter`, COUNT(*) AS totale FROM `em_newsletter_receiver` GROUP BY `id_newsletter`) AS riceventi ON `riceventi`.`id_newsletter` = `em_newsletters`.`id` +WHERE + 1=1 AND `em_newsletters`.`deleted_at` IS NULL +HAVING + 2=2" WHERE `name` = 'Newsletter'; +UPDATE `zz_views` INNER JOIN `zz_modules` ON `zz_views`.`id_module` = `zz_modules`.`id` SET `zz_views`.`query` = '`em_templates_lang`.`name`' WHERE `zz_modules`.`name` = 'Newsletter' AND `zz_views`.`name` = 'Template'; \ No newline at end of file diff --git a/update/tables.php b/update/tables.php index f80197fb8..6f7d75c03 100755 --- a/update/tables.php +++ b/update/tables.php @@ -79,6 +79,7 @@ return [ 'em_print_template', 'em_accounts', 'em_templates', + 'em_templates_lang', 'em_newsletters', 'em_mansioni_template', 'em_lists',