From 179ac1e4ea3d1b213fc423fd11840dff6ce941e4 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Wed, 15 Aug 2018 08:16:22 +0200 Subject: [PATCH] Added language settings --- RestControllers/SettingsController.php | 63 +++++++++++++++++++++++ classes/components/SettingsComponent.php | 65 ++++++++++++++++++++++++ classes/models/LanguageSettings.php | 28 ++++++++++ 3 files changed, 156 insertions(+) create mode 100644 classes/models/LanguageSettings.php diff --git a/RestControllers/SettingsController.php b/RestControllers/SettingsController.php index 715bbb3..ede5b69 100644 --- a/RestControllers/SettingsController.php +++ b/RestControllers/SettingsController.php @@ -114,6 +114,54 @@ class SettingsController { return array("success" => "The directory is available!"); } + /** + * Get language settings + * + * @url POST /settings/get_language + */ + public function getLanguage(){ + + //User login required + user_login_required(); + + //Get the settings of the user + $settings = components()->settings->get_language(userID); + + if(!$settings->isValid()) + Rest_fatal_error(500, "Could not get language settings!"); + + //Return parsed settings object + return $this->LanguageSettingsToAPI($settings); + } + + /** + * Set (update) language settings + * + * @url POST /settings/set_language + */ + public function setLanguage(){ + + //User login required + user_login_required(); + + //Get specified language + $lang = postString("lang", 2); + + if(!in_array($lang, LanguageSettings::LANGUAGES)) + Rest_fatal_error(401, "Language not recognized !"); + + $settings = new LanguageSettings(); + $settings->set_id(userID); + $settings->set_lang($lang); + + //Save language in database + if(!components()->settings->save_language($settings)) + Rest_fatal_error(500, "Could not save language settings!"); + + //Success + return array("success" => "Language settings have been successfully updated!"); + } + /** * Get security settings * @@ -311,6 +359,21 @@ class SettingsController { return $data; } + /** + * Turn a LanguageSettings object into a valid API object + * + * @param LanguageSettings $settings The object to convert + * @return array Generated API object + */ + private function LanguageSettingsToAPI(LanguageSettings $settings) : array { + + $data = array(); + + $data["lang"] = $settings->get_lang(); + + return $data; + } + /** * Turn a SecuritySettings object into a valid API object * diff --git a/classes/components/SettingsComponent.php b/classes/components/SettingsComponent.php index e023ece..abb57db 100644 --- a/classes/components/SettingsComponent.php +++ b/classes/components/SettingsComponent.php @@ -64,6 +64,42 @@ class SettingsComponents { return $folderUserID == $userID; } + /** + * Get and return language settings of a user + * + * @param int $userID Target user ID + * @return LanguageSettings The language settings of the user / invalid object + * in case of failure + */ + public function get_language(int $userID) : LanguageSettings { + + //Get user database entry + $entry = $this->getDBUserInfo($userID); + + //Check for error + if(count($entry) == 0) + return new LanguageSettings(); //Return invalid object + + //Parse database entry into LanguageSettings entry + return $this->dbToLanguageSettings($entry); + + } + + /** + * Save new version of the language settings of a user + * + * @param LanguageSettings $settings The settings to save in the database + * @return bool TRUE in case of success / FALSE else + */ + public function save_language(LanguageSettings $settings) : bool { + + //Convert LanguageSettings object into database entry + $entry = $this->LanguageSettingsToDb($settings); + + //Save information in the database + return $this->saveDBUserInfo($settings->get_id(), $entry); + } + /** * Get and return security settings of a user * @@ -217,6 +253,21 @@ class SettingsComponents { return $data; } + /** + * Parse a user information into LanguageSettings object + * + * @param array $entry The entry to parse + * @return LanguageSettings Generated model + */ + private function dbToLanguageSettings(array $entry) : LanguageSettings { + $obj = new LanguageSettings(); + + $obj->set_id($entry['ID']); + $obj->set_lang($entry["lang"]); + + return $obj; + } + /** * Parse a user information entry into SecuritySettings object * @@ -237,6 +288,20 @@ class SettingsComponents { } + /** + * Turn LanguageSettings object into database entry + * + * @param LanguageSettings $settings Language settings to turn into database entry + * @return array Generated entry + */ + private function LanguageSettingsToDb(LanguageSettings $settings) : array { + $data = array(); + + $data["lang"] = $settings->get_lang(); + + return $data; + } + /** * Turn SecuritySettings object into database entry * diff --git a/classes/models/LanguageSettings.php b/classes/models/LanguageSettings.php new file mode 100644 index 0000000..d636781 --- /dev/null +++ b/classes/models/LanguageSettings.php @@ -0,0 +1,28 @@ +lang = $lang; + } + + public function has_lang() : bool { + return $this->lang != null; + } + + public function get_lang() : string { + return $this->lang; + } +} \ No newline at end of file