From 9b7b78b3061ab037f41c5405e43557f310ccf114 Mon Sep 17 00:00:00 2001 From: osaajani Date: Mon, 11 Nov 2019 04:05:26 +0100 Subject: [PATCH] update settings --- controllers/internals/Setting.php | 48 +++++++++++++++++++++++++------ controllers/publics/Connect.php | 5 ++++ controllers/publics/Setting.php | 13 ++------- createDatabase.sql | 2 +- models/Setting.php | 26 +++++++++++++---- templates/contact/add.php | 4 +-- templates/contact/edit.php | 4 +-- templates/discussion/show.php | 2 +- templates/incs/footer.php | 2 +- templates/scheduled/add.php | 14 ++++----- templates/scheduled/edit.php | 10 +++---- templates/setting/show.php | 14 ++++----- 12 files changed, 92 insertions(+), 52 deletions(-) diff --git a/controllers/internals/Setting.php b/controllers/internals/Setting.php index 7006972..22bb82d 100755 --- a/controllers/internals/Setting.php +++ b/controllers/internals/Setting.php @@ -21,22 +21,52 @@ namespace controllers\internals; } /** - * Return all settings. - * - * @return array || false + * Return all settings of a user. + * @param int $id_user : user id + * @return array */ - public function get_all() + public function gets_for_user (int $id_user) { - return $this->model_setting->get_all(); + $settings = $this->model_setting->gets_for_user($id_user); + $settings_array = []; + + foreach ($settings as $setting) + { + $settings_array[$setting['name']] = $setting['value']; + } + + return $settings_array; } + /** - * Update a setting by his name. - * + * Update a setting by his name and user id. + * @param int $id_user : user id + * @param string $name : setting name * @param mixed $value + * @return int : number of modified lines */ - public function update(string $name, $value): bool + public function update (int $id_user, string $name, $value) : bool { - return (bool) $this->model_setting->update($name, $value); + return (bool) $this->model_setting->update($id_user, $name, $value); + } + + + /** + * Create a new setting + * @param int $id_user : user id + * @param string $name : setting name + * @param mixed $value : value of the setting + * @return bool + */ + public function insert (int $id_user, string $name, $value) : bool + { + $setting = [ + 'id_user' => $id_user, + 'name' => $name, + 'value' => $value, + ]; + + return (bool) $this->model_setting->insert($setting); } } diff --git a/controllers/publics/Connect.php b/controllers/publics/Connect.php index 920010c..fb53b68 100755 --- a/controllers/publics/Connect.php +++ b/controllers/publics/Connect.php @@ -17,6 +17,7 @@ namespace controllers\publics; class Connect extends \descartes\Controller { private $internal_user; + private $internal_setting; /** * Cette fonction est appelée avant toute les autres :. @@ -28,6 +29,7 @@ namespace controllers\publics; $bdd = \descartes\Model::_connect(DATABASE_HOST, DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD); $this->internal_user = new \controllers\internals\User($bdd); + $this->internal_setting = new \controllers\internals\Setting($bdd); } /** @@ -62,6 +64,9 @@ namespace controllers\publics; return $this->redirect(\descartes\Router::url('Connect', 'login')); } + $settings = $this->internal_setting->gets_for_user($user['id']); + $user['settings'] = $settings; + $_SESSION['connect'] = true; $_SESSION['user'] = $user; diff --git a/controllers/publics/Setting.php b/controllers/publics/Setting.php index 746e044..3145bcf 100755 --- a/controllers/publics/Setting.php +++ b/controllers/publics/Setting.php @@ -51,24 +51,16 @@ namespace controllers\publics; return $this->redirect(\descartes\Router::url('Setting', 'show')); } - - if (!\controllers\internals\Tool::is_admin()) - { - \FlashMessage\FlashMessage::push('danger', 'Vous devez être administrateur pour pouvoir modifier un réglage.'); - - return $this->redirect(\descartes\Router::url('Setting', 'show')); - } - + $setting_value = $_POST['setting_value'] ?? false; if (false === $setting_value) { \FlashMessage\FlashMessage::push('danger', 'Vous devez renseigner une valeure pour le réglage.'); - return $this->redirect(\descartes\Router::url('Setting', 'show')); } - $update_setting_result = $this->internal_setting->update($setting_name, $setting_value); + $update_setting_result = $this->internal_setting->update($_SESSION['user']['id'], $setting_name, $setting_value); if (false === $update_setting_result) { \FlashMessage\FlashMessage::push('danger', 'Impossible de mettre à jour ce réglage.'); @@ -77,7 +69,6 @@ namespace controllers\publics; } \FlashMessage\FlashMessage::push('success', 'Le réglage a bien été mis à jour.'); - return $this->redirect(\descartes\Router::url('Setting', 'show')); } } diff --git a/createDatabase.sql b/createDatabase.sql index e2e502d..a32b5ce 100755 --- a/createDatabase.sql +++ b/createDatabase.sql @@ -22,7 +22,7 @@ CREATE TABLE IF NOT EXISTS setting value VARCHAR(1000) NOT NULL, PRIMARY KEY (id), FOREIGN KEY (id_user) REFERENCES user (id) ON DELETE CASCADE ON UPDATE CASCADE, - UNIQUE (name) + UNIQUE (id_user, name) ); CREATE TABLE IF NOT EXISTS scheduled diff --git a/models/Setting.php b/models/Setting.php index 2db9c3c..0a5d7ef 100755 --- a/models/Setting.php +++ b/models/Setting.php @@ -18,21 +18,35 @@ namespace models; { /** * Return array of all settings. + * @param int $id_user : user id + * @return array */ - public function get_all(): array + public function gets_for_user (int $id_user): array { - return $this->_select('setting'); + return $this->_select('setting', ['id_user' => $id_user]); } + + /** + * Create a new setting + * @param array $setting + * @return bool + */ + public function insert (array $setting) : bool + { + return (bool) $this->_insert('setting', $setting); + } + + /** * Update a setting by his name. - * + * @param int $id_user : user id + * @param string $name : setting name * @param mixed $value - * * @return int : number of modified lines */ - public function update(string $name, $value): int + public function update (int $id_user, string $name, $value): int { - return $this->_update('setting', ['value' => $value], ['name' => $name]); + return $this->_update('setting', ['value' => $value], ['id_user' => $id_user, 'name' => $name]); } } diff --git a/templates/contact/add.php b/templates/contact/add.php index 3ece64d..6c1d4a4 100755 --- a/templates/contact/add.php +++ b/templates/contact/add.php @@ -67,8 +67,8 @@ var number_input = jQuery('#phone-international-input')[0]; var iti_number_input = window.intlTelInput(number_input, { hiddenInput: 'number', - defaultCountry: 's(RASPISMS_SETTINGS_DEFAULT_PHONE_COUNTRY); ?>', - preferredCountries: s(json_encode(explode(',', RASPISMS_SETTINGS_PREFERRED_PHONE_COUNTRY)), false, false); ?>, + defaultCountry: 's($_SESSION['user']['settings']['default_phone_country']); ?>', + preferredCountries: s(json_encode(explode(',', $_SESSION['user']['settings']['preferred_phone_country'])), false, false); ?>, nationalMode: true, utilsScript: '/intlTelInput/utils.js' }); diff --git a/templates/contact/edit.php b/templates/contact/edit.php index 1b1d358..f1a1fe5 100755 --- a/templates/contact/edit.php +++ b/templates/contact/edit.php @@ -74,8 +74,8 @@ var hidden_input_name = 'contacts[' + jQuery(number_input).attr('contact-id') + '][number]'; var iti_number_input = window.intlTelInput(number_input, { hiddenInput: hidden_input_name, - defaultCountry: 's(RASPISMS_SETTINGS_DEFAULT_PHONE_COUNTRY); ?>', - preferredCountries: s(json_encode(explode(',', RASPISMS_SETTINGS_PREFERRED_PHONE_COUNTRY)), false, false); ?>, + defaultCountry: 's($_SESSION['user']['settings']['default_phone_country']); ?>', + preferredCountries: s(json_encode(explode(',', $_SESSION['user']['settings']['preferred_phone_country'])), false, false); ?>, nationalMode: true, utilsScript: '/intlTelInput/utils.js' }); diff --git a/templates/discussion/show.php b/templates/discussion/show.php index 44760a2..ba66540 100755 --- a/templates/discussion/show.php +++ b/templates/discussion/show.php @@ -70,7 +70,7 @@ $.each(data.messages, function(key, message) { - + //On ajoute la detection de lien dans le texte du message message.text = Autolinker.link(message.text, {newWindow:true}); diff --git a/templates/incs/footer.php b/templates/incs/footer.php index 228dd31..faadbe7 100755 --- a/templates/incs/footer.php +++ b/templates/incs/footer.php @@ -3,7 +3,7 @@ Copyright 2014. RaspiSMS est un programme sous licence GNU GPL.
- +