diff --git a/RestControllers/userController.php b/RestControllers/userController.php index 1a1a48e..ad59cba 100644 --- a/RestControllers/userController.php +++ b/RestControllers/userController.php @@ -16,9 +16,11 @@ class userController * * @url POST /user/getInfos * @url POST /user/getInfosMultiple + * @url POST /user/getInfo + * @url POST /user/getInfoMultiple * @return array The result */ - public function getUserInfos() : array{ + public function getUserInfo() : array{ //Determine userID if(isset($_POST['userID'])){ @@ -64,8 +66,9 @@ class userController * Get advanced user informations * * @url POST /user/getAdvancedUserInfos + * @url POST /user/getAdvancedUserInfo */ - public function getAdvancedInfos(){ + public function getAdvancedInfo(){ //Get the ID of the target user $userID = getPostUserID("userID"); @@ -75,30 +78,32 @@ class userController Rest_fatal_error(401, "You are not allowed to access these information !"); //Get user informations - $userInfos = CS::get()->components->user->getUserInfos($userID, true); + $userInfos = CS::get()->components->user->getUserAdvancedInfo($userID, true); //Check if we got a response - if(count($userInfos) == 0) + if(!$userInfos->isValid()) Rest_fatal_error(500, "Couldn't get informations about the user !"); + //Parse user information for the API + $data = $this->advancedUserToAPI($userInfos); //Get the number of friends (if allowed) - if($userInfos['friend_list_public'] === true){ - $userInfos['number_friends'] = CS::get()->components->friends->count_all($userID); + if($userInfos->is_friendListPublic()){ + $data['number_friends'] = CS::get()->components->friends->count_all($userID); } else //User friends won't be displayed - $userInfos["number_friends"] = 0; + $data["number_friends"] = 0; //User can not post text on this page by default - $userInfos["can_post_texts"] = FALSE; + $data["can_post_texts"] = FALSE; //Get some informations only is user is signed in if(user_signed_in()){ - $userInfos["user_like_page"] = CS::get()->components->likes->is_liking(userID, $userID, Likes::LIKE_USER); + $data["user_like_page"] = CS::get()->components->likes->is_liking(userID, $userID, Likes::LIKE_USER); //Check if the user can post texts on this page - $userInfos["can_post_texts"] = + $data["can_post_texts"] = //If it is his page, yes by default userID == $userID ? TRUE : //Else check friendship status @@ -106,7 +111,7 @@ class userController } //Return user informations - return $userInfos; + return $data; } @@ -152,4 +157,46 @@ class userController return array("userID" => $id); } + + /** + * Turn a User object into an API array + * + * @param User $user Information about the user + * @return array Information about the user compatible with the API + */ + public function userToAPI(User $user) : array { + + $data = array(); + + $data['userID'] = $user->get_id(); + $data['firstName'] = $user->get_firstName(); + $data['lastName'] = $user->get_lastName(); + $data['publicPage'] = $user->is_publicPage() ? "true" : "false"; + $data['openPage'] = $user->is_openPage() ? "true" : "false"; + $data['virtualDirectory'] = $user->has_virtualDirectory() ? $user->get_virtualDirectory() : ""; + $data['accountImage'] = $user->get_accountImageURL(); + + return $data; + } + + /** + * Turn an AdvancedUser object into an API array + * + * @param AdvancedUser $user Information about the user + * @return array Data compatible with the API + */ + private function advancedUserToAPI(AdvancedUser $user) : array { + + $data = $this->userToAPI($user); + + $data['friend_list_public'] = $user->is_friendListPublic(); + $data['personnalWebsite'] = $user->has_personnalWebsite() ? $user->get_personnalWebsite() : ""; + $data['noCommentOnHisPage'] = $user->is_disallowComments(); + $data['allowPostFromFriendOnHisPage'] = $user->is_allowPostFromFriends(); + $data['account_creation_time'] = $user->get_creation_time(); + $data['backgroundImage'] = $user->get_backgroundImage(); + $data['pageLikes'] = $user->get_pageLikes(); + + return $data; + } } \ No newline at end of file diff --git a/classes/components/user.php b/classes/components/user.php index 39ff170..91bac01 100644 --- a/classes/components/user.php +++ b/classes/components/user.php @@ -25,17 +25,31 @@ class UserComponent { public function __construct(){ } + + /** + * Get advanced information about a user + * + * @param int $userID Target user ID + * @return AdvancedUser Informations about the user (invalid object in case of failure) + */ + public function getUserAdvancedInfo(int $userID) : AdvancedUser { + + //Perform a request over the database + $data = $this->getDBUserInfo($userID); + + if(count($data) == 0) + return new AdvancedUser(); //Return invalid object + + return $this->parseDbToAdvancedUser($data); + } /** - * Get Single User Infos + * Get Single User Infos from database and return its information as an array * * @param int $userID The user ID - * @param bool $advanced Get advanced information about user, for its page for example - * @return User Information about the user (invalid object in case of failure) - * Notice : If advanced information are request, then the User object can be casted to - * AdvancedUser object. + * @return array Information about the user (empty array in case of failure) */ - public function getUserInfos(int $userID, bool $advanced = false) : User { + private function getDBUserInfo(int $userID) : array { //Prepare database request $tablesName = self::USER_TABLE; $conditions = "WHERE utilisateurs.ID = ?"; @@ -51,12 +65,10 @@ class UserComponent { return array(); //No result //Return parsed result - if(!$advanced) - return $this->parseDbToUser($userInfos[0]); - else - return $this->parseDbToAdvancedUser($userInfos[0]); + return($userInfos[0]); } + /** * Get Multiple Users Infos * @@ -407,7 +419,8 @@ class UserComponent { $user->set_firstName($entry['prenom']); $user->set_lastName($entry['nom']); $user->set_publicPage($entry['public'] == 1); - $user->set_virtualDirectory($entry['sous_repertoire']); + $user->set_openPage($entry['pageouverte'] == 1); + $user->set_virtualDirectory($entry['sous_repertoire'] == null ? "" : $entry['sous_repertoire']); $user->set_accountImageURL( CS::get()->components->accountImage->getPath($user->get_id())); diff --git a/classes/models/AdvancedUser.php b/classes/models/AdvancedUser.php index 6ba9451..3792129 100644 --- a/classes/models/AdvancedUser.php +++ b/classes/models/AdvancedUser.php @@ -34,6 +34,10 @@ class AdvancedUser extends User { $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"; } diff --git a/classes/models/User.php b/classes/models/User.php index 9e0f556..99460c8 100644 --- a/classes/models/User.php +++ b/classes/models/User.php @@ -80,6 +80,10 @@ class User { $this->virtualDirectory = $virtualDirectory == "" ? null : $virtualDirectory; } + public function has_virtualDirectory() : bool { + return $this->virtualDirectory != null; + } + public function get_virtualDirectory() : string { return $this->virtualDirectory != null ? $this->virtualDirectory : "null"; }