From fb3f9425d1c85842f8233024386223a2439c946a Mon Sep 17 00:00:00 2001 From: osaajani <> Date: Sun, 17 Sep 2023 16:12:31 +0200 Subject: [PATCH] User setting update now create setting if it doesn't exists yet instead of returning an error --- controllers/internals/Setting.php | 12 ++++++++++++ controllers/publics/Setting.php | 23 +++++++++++++++++++---- models/Setting.php | 12 ++++++++++++ 3 files changed, 43 insertions(+), 4 deletions(-) diff --git a/controllers/internals/Setting.php b/controllers/internals/Setting.php index 84f62cf..d5d9e29 100644 --- a/controllers/internals/Setting.php +++ b/controllers/internals/Setting.php @@ -35,6 +35,18 @@ namespace controllers\internals; return $settings_array; } + /** + * Get a user setting by his name for a user. + * + * @param int $id_user : user id + * + * @return array + */ + public function get_by_name_for_user(int $id_user, string $name) + { + return $this->get_model()->get_by_name_for_user($id_user, $name); + } + /** * Update a setting by his name and user id. * diff --git a/controllers/publics/Setting.php b/controllers/publics/Setting.php index c5a34e6..fbf710e 100644 --- a/controllers/publics/Setting.php +++ b/controllers/publics/Setting.php @@ -75,12 +75,27 @@ namespace controllers\publics; $setting_value = json_encode($setting_value); } - $update_setting_result = $this->internal_setting->update_for_user($_SESSION['user']['id'], $setting_name, $setting_value); - if (false === $update_setting_result) + // If setting dont exists yet, create it, else update + $setting = $this->internal_setting->get_by_name_for_user($_SESSION['user']['id'], $setting_name); + if (!$setting) { - \FlashMessage\FlashMessage::push('danger', 'Impossible de mettre à jour ce réglage.'); + $success = $this->internal_setting->create($_SESSION['user']['id'], $setting_name, $setting_value); + if (false === $success) + { + \FlashMessage\FlashMessage::push('danger', 'Impossible de mettre à jour ce réglage.'); - return $this->redirect(\descartes\Router::url('Setting', 'show')); + return $this->redirect(\descartes\Router::url('Setting', 'show')); + } + } + else + { + $update_setting_result = $this->internal_setting->update_for_user($_SESSION['user']['id'], $setting_name, $setting_value); + if (false === $update_setting_result) + { + \FlashMessage\FlashMessage::push('danger', 'Impossible de mettre à jour ce réglage.'); + + return $this->redirect(\descartes\Router::url('Setting', 'show')); + } } $settings = $this->internal_setting->gets_for_user($_SESSION['user']['id']); diff --git a/models/Setting.php b/models/Setting.php index f2e6d43..2473e60 100644 --- a/models/Setting.php +++ b/models/Setting.php @@ -27,6 +27,18 @@ namespace models; return $this->_update($this->get_table_name(), ['value' => $value], ['id_user' => $id_user, 'name' => $name]); } + /** + * Get a user setting by his name for a user. + * + * @param int $id_user : user id + * + * @return array + */ + public function get_by_name_for_user(int $id_user, string $name) + { + return $this->_select_one($this->get_table_name(), ['name' => $name, 'id_user' => $id_user]); + } + /** * Return table name. */