Allineamento add template email

This commit is contained in:
Pek5892 2024-03-07 14:52:41 +01:00
parent 877379134b
commit 675d1f11da
4 changed files with 124 additions and 44 deletions

View File

@ -17,43 +17,40 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
use Modules\Emails\Template;
include_once __DIR__.'/../../core.php'; include_once __DIR__.'/../../core.php';
switch (post('op')) { switch (post('op')) {
case 'add': case 'add':
$dbo->insert('em_templates', [ $id_module = post('module');
'id_module' => post('module'), $id_account = post('smtp');
'id_account' => post('smtp'), $name = post('name');
]); $subject = post('subject');
$id_record = $dbo->lastInsertedID();
$dbo->insert('em_templates_lang', [ $template = Template::build($id_module, $id_account);
'name' => post('name'), $id_record = $template->id;
'subject' => post('subject'), $template->name = $name;
'id_lang' => setting('Lingua'), $template->subject = $subject;
'id_record' => $id_record $template->save();
]);
flash()->info(tr('Aggiunto nuovo template per le email!')); flash()->info(tr('Aggiunto nuovo template per le email!'));
break; break;
case 'update': case 'update':
$dbo->update('em_templates', [ $template->name = post('name');
'id_account' => post('smtp'), $template->id_account = post('smtp');
'icon' => post('icon'), $template->icon = post('icon');
'tipo_reply_to' => post('tipo_reply_to'), $template->tipo_reply_to = post('tipo_reply_to');
'reply_to' => post('reply_to'), $template->reply_to = post('reply_to');
'cc' => post('cc'), $template->cc = post('cc');
'bcc' => post('bcc'), $template->bcc = post('bcc');
'read_notify' => post('read_notify'), $template->read_notify = post('read_notify');
'note_aggiuntive' => post('note_aggiuntive') $template->note_aggiuntive = post('note_aggiuntive');
], ['id' => $id_record]); $template->subject = post('subject');
$template->body = post('body');
$dbo->update('em_templates_lang', [ $template->save();
'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_print_template', ['id_template' => $id_record], ['id_print' => (array) post('prints')]);
$dbo->sync('em_mansioni_template', ['id_template' => $id_record], ['idmansione' => (array) post('idmansioni')]); $dbo->sync('em_mansioni_template', ['id_template' => $id_record], ['idmansione' => (array) post('idmansioni')]);

View File

@ -18,12 +18,15 @@
*/ */
use Modules\Newsletter\Newsletter; use Modules\Newsletter\Newsletter;
use Modules\Emails\Template;
include_once __DIR__.'/../../core.php'; include_once __DIR__.'/../../core.php';
if (isset($id_record)) { 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'); $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 // Controllo se ci sono newletter collegate a questo template
$newsletters = Newsletter::where('id_template', $id_record)->get(); $newsletters = Newsletter::where('id_template', $id_record)->get();
} }

View File

@ -44,6 +44,16 @@ class Template extends Model
return (array) $variables; 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 */ /* Relazioni Eloquent */
public function module() public function module()
@ -75,6 +85,29 @@ class Template extends Model
->first()->name; ->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. * Ritorna l'attributo subject del template.
* *
@ -89,6 +122,30 @@ class Template extends Model
->first()->subject; ->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. * Ritorna l'attributo body del template.
* *
@ -102,6 +159,29 @@ class Template extends Model
->where('id_lang', '=', setting('Lingua')) ->where('id_lang', '=', setting('Lingua'))
->first()->body; ->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. * Ritorna l'id del template a partire dal nome.

View File

@ -145,7 +145,7 @@ class Lista extends Model
} }
/** /**
* Imposta l'attributo name della lista. * Imposta l'attributo description della lista.
*/ */
public function setDescriptionAttribute($value) 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) public function setNameAttribute($value)
{ {
@ -192,21 +207,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. * Ritorna l'id della lista a partire dal nome.
* *