Added language settings

This commit is contained in:
Pierre HUBERT 2018-08-15 08:16:22 +02:00
parent 5a21389017
commit 179ac1e4ea
3 changed files with 156 additions and 0 deletions

View File

@ -114,6 +114,54 @@ class SettingsController {
return array("success" => "The directory is available!"); 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 * Get security settings
* *
@ -311,6 +359,21 @@ class SettingsController {
return $data; 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 * Turn a SecuritySettings object into a valid API object
* *

View File

@ -64,6 +64,42 @@ class SettingsComponents {
return $folderUserID == $userID; 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 * Get and return security settings of a user
* *
@ -217,6 +253,21 @@ class SettingsComponents {
return $data; 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 * 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 * Turn SecuritySettings object into database entry
* *

View File

@ -0,0 +1,28 @@
<?php
/**
* Language settings base model
*
* @author Pierre HUBERT
*/
class LanguageSettings extends BaseUserModel {
//Available languages
const LANGUAGES = array("fr", "en");
//Private fields
private $lang;
//Set and get the language the user
public function set_lang(string $lang){
$this->lang = $lang;
}
public function has_lang() : bool {
return $this->lang != null;
}
public function get_lang() : string {
return $this->lang;
}
}