From 0bdadffd2aa831c05dfb87946d0f175edf8c9723 Mon Sep 17 00:00:00 2001 From: Luca Date: Sat, 8 Oct 2022 17:28:43 +0200 Subject: [PATCH] Introdotto build per referenti --- modules/anagrafiche/src/Referente.php | 25 ++++++++++++++++++++++++ plugins/referenti/actions.php | 28 +++++++++++++++------------ 2 files changed, 41 insertions(+), 12 deletions(-) diff --git a/modules/anagrafiche/src/Referente.php b/modules/anagrafiche/src/Referente.php index 5bb83c7b7..22fec8706 100644 --- a/modules/anagrafiche/src/Referente.php +++ b/modules/anagrafiche/src/Referente.php @@ -27,6 +27,31 @@ class Referente extends Model use SimpleModelTrait; protected $table = 'an_referenti'; + + /** + * Crea un nuovo referente. + * + * @param string $nome + * + * @return self + */ + public static function build($idanagrafica, $nome, $idmansione, $idsede) + { + $model = new static(); + + + $model->idanagrafica = $idanagrafica; + + $model->nome = $nome; + + $model->idmansione = $idmansione; + $model->idsede = $idsede; + + $model->save(); + + return $model; + } + /** * The attributes that aren't mass assignable. diff --git a/plugins/referenti/actions.php b/plugins/referenti/actions.php index 17befdf59..5736c9cf0 100755 --- a/plugins/referenti/actions.php +++ b/plugins/referenti/actions.php @@ -19,26 +19,30 @@ include_once __DIR__.'/../../core.php'; +use Modules\Anagrafiche\Referente; + $operazione = filter('op'); switch ($operazione) { case 'addreferente': if (!empty(post('nome'))) { - $opt_out_newsletter = post('disable_newsletter'); - $dbo->insert('an_referenti', [ - 'idanagrafica' => $id_parent, - 'nome' => post('nome'), - 'idmansione' => post('idmansione'), - 'telefono' => post('telefono'), - 'email' => post('email'), - 'idsede' => post('idsede'), - 'enable_newsletter' => empty($opt_out_newsletter), - ]); - $id_record = $dbo->lastInsertedID(); + $nome = post('nome'); + $idmansione = post('idmansione'); + $idsede = post('idsede'); + $opt_out_newsletter = post('disable_newsletter'); + + $referente = Referente::build($id_parent, $nome, $idmansione, $idsede); + $id_record = $referente->id; + + $referente->telefono = post('telefono'); + $referente->email = post('email'); + $referente->enable_newsletter = empty($opt_out_newsletter); + + $referente->save(); if (isAjaxRequest() && !empty($id_record)) { - echo json_encode(['id' => $id_record, 'text' => post('nome')]); + echo json_encode(['id' => $id_record, 'text' => $referente->nome]); } flash()->info(tr('Aggiunto nuovo referente!'));