diff --git a/RestControllers/SettingsController.php b/RestControllers/SettingsController.php index 01580b8..f271377 100644 --- a/RestControllers/SettingsController.php +++ b/RestControllers/SettingsController.php @@ -16,7 +16,14 @@ class SettingsController { user_login_required(); //Login needed - + //Fetch user settings + $user_settings = components()->settings->get_general(userID); + + //Check for error + if(!$user_settings->isValid()) + Rest_fatal_error(500, "Could not get user settings !"); + + print_r($user_settings); } diff --git a/classes/components/SettingsComponent.php b/classes/components/SettingsComponent.php index adf73b8..b0c6295 100644 --- a/classes/components/SettingsComponent.php +++ b/classes/components/SettingsComponent.php @@ -7,7 +7,77 @@ class SettingsComponents { - + /** + * Get and return general settings of a user + * + * @param int $userID The ID of the target user + * @return GeneralSettings General settings about the user / invalid object in case + * of failure + */ + public function get_general(int $userID) : GeneralSettings { + + //Get user database entry + $entry = $this->getDBUserInfo($userID); + + //Check for error + if(count($entry) == 0) + return new GeneralSettings(); //Return invalid object + + //Parse database entry into GeneralSettings entry + return $this->dbToGeneralSettings($entry); + } + + /** + * Get Single User Infos from database and return its information as an array + * + * @param int $userID The user ID + * @return array Information about the user (empty array in case of failure) + */ + private function getDBUserInfo(int $userID) : array { + //Prepare database request + $tablesName = AccountComponent::USER_TABLE; + $conditions = "WHERE utilisateurs.ID = ?"; + $conditionsValues = array( + $userID*1, + ); + + //Perform request + $userInfos = CS::get()->db->select($tablesName, $conditions, $conditionsValues); + + //Check if result is correct or not + if(count($userInfos) == 0) + return array(); //No result + + //Return parsed result + return($userInfos[0]); + } + + /** + * Parse a user information entry into GeneralSettings object + * + * @param array $entry The database entry to process + * @return GeneralSettings Generated GeneralSettings entry + */ + private function dbToGeneralSettings(array $entry) : GeneralSettings { + + $obj = new GeneralSettings(); + + $obj->set_id($entry['ID']); + $obj->set_email($entry['mail']); + $obj->set_firstName($entry['prenom']); + $obj->set_lastName($entry['nom']); + $obj->set_publicPage($entry['public'] == 1); + $obj->set_openPage($entry['pageouverte'] == 1); + $obj->set_allowComments($entry['bloquecommentaire'] == 0); + $obj->set_allowPostsFriends($entry['autoriser_post_amis'] == 1); + $obj->set_allowComunicMails($entry['autorise_mail']); + $obj->set_friendsListPublic($entry['liste_amis_publique']); + $obj->set_virtualDirectory($entry['sous_repertoire'] == null ? "" : $entry['sous_repertoire']); + $obj->set_personnalWebsite($entry['site_web'] == null ? "" : $entry['site_web']); + + return $obj; + + } } diff --git a/classes/models/GeneralSettings.php b/classes/models/GeneralSettings.php index 61d719a..a1aff7e 100644 --- a/classes/models/GeneralSettings.php +++ b/classes/models/GeneralSettings.php @@ -10,14 +10,15 @@ class GeneralSettings { //Private fields private $id; private $email; - private $firstname; - private $lastname; + private $firstName; + private $lastName; private $publicPage; private $openPage; private $allowComments; private $allowPostsFriends; private $allowComunicMails; private $friendsListPublic; + private $virtualDirectory; private $personnalWebsite; //Set and get user ID @@ -127,6 +128,19 @@ class GeneralSettings { return $this->virtualDirectory != null ? $this->virtualDirectory : "null"; } + //Set and get the personnal website of the user + public function set_personnalWebsite(string $personnalWebsite){ + $this->personnalWebsite = $personnalWebsite == "" ? null : $personnalWebsite; + } + + public function has_personnalWebsite() : bool { + return $this->personnalWebsite != null; + } + + public function get_personnalWebsite() : string { + return $this->personnalWebsite != null ? $this->personnalWebsite : "null"; + } + /** * Check wether is object is valid or not *