Method getAdvancedUserInfo of API working

This commit is contained in:
Pierre 2018-04-15 14:26:26 +02:00
parent 1a2ab4f520
commit bec22cb607
4 changed files with 90 additions and 22 deletions

View File

@ -16,9 +16,11 @@ class userController
* *
* @url POST /user/getInfos * @url POST /user/getInfos
* @url POST /user/getInfosMultiple * @url POST /user/getInfosMultiple
* @url POST /user/getInfo
* @url POST /user/getInfoMultiple
* @return array The result * @return array The result
*/ */
public function getUserInfos() : array{ public function getUserInfo() : array{
//Determine userID //Determine userID
if(isset($_POST['userID'])){ if(isset($_POST['userID'])){
@ -64,8 +66,9 @@ class userController
* Get advanced user informations * Get advanced user informations
* *
* @url POST /user/getAdvancedUserInfos * @url POST /user/getAdvancedUserInfos
* @url POST /user/getAdvancedUserInfo
*/ */
public function getAdvancedInfos(){ public function getAdvancedInfo(){
//Get the ID of the target user //Get the ID of the target user
$userID = getPostUserID("userID"); $userID = getPostUserID("userID");
@ -75,30 +78,32 @@ class userController
Rest_fatal_error(401, "You are not allowed to access these information !"); Rest_fatal_error(401, "You are not allowed to access these information !");
//Get user informations //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 //Check if we got a response
if(count($userInfos) == 0) if(!$userInfos->isValid())
Rest_fatal_error(500, "Couldn't get informations about the user !"); 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) //Get the number of friends (if allowed)
if($userInfos['friend_list_public'] === true){ if($userInfos->is_friendListPublic()){
$userInfos['number_friends'] = CS::get()->components->friends->count_all($userID); $data['number_friends'] = CS::get()->components->friends->count_all($userID);
} }
else else
//User friends won't be displayed //User friends won't be displayed
$userInfos["number_friends"] = 0; $data["number_friends"] = 0;
//User can not post text on this page by default //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 //Get some informations only is user is signed in
if(user_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 //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 //If it is his page, yes by default
userID == $userID ? TRUE : userID == $userID ? TRUE :
//Else check friendship status //Else check friendship status
@ -106,7 +111,7 @@ class userController
} }
//Return user informations //Return user informations
return $userInfos; return $data;
} }
@ -152,4 +157,46 @@ class userController
return array("userID" => $id); 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;
}
} }

View File

@ -25,17 +25,31 @@ class UserComponent {
public function __construct(){ 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 int $userID The user ID
* @param bool $advanced Get advanced information about user, for its page for example * @return array Information about the user (empty array in case of failure)
* @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.
*/ */
public function getUserInfos(int $userID, bool $advanced = false) : User { private function getDBUserInfo(int $userID) : array {
//Prepare database request //Prepare database request
$tablesName = self::USER_TABLE; $tablesName = self::USER_TABLE;
$conditions = "WHERE utilisateurs.ID = ?"; $conditions = "WHERE utilisateurs.ID = ?";
@ -51,12 +65,10 @@ class UserComponent {
return array(); //No result return array(); //No result
//Return parsed result //Return parsed result
if(!$advanced) return($userInfos[0]);
return $this->parseDbToUser($userInfos[0]);
else
return $this->parseDbToAdvancedUser($userInfos[0]);
} }
/** /**
* Get Multiple Users Infos * Get Multiple Users Infos
* *
@ -407,7 +419,8 @@ class UserComponent {
$user->set_firstName($entry['prenom']); $user->set_firstName($entry['prenom']);
$user->set_lastName($entry['nom']); $user->set_lastName($entry['nom']);
$user->set_publicPage($entry['public'] == 1); $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( $user->set_accountImageURL(
CS::get()->components->accountImage->getPath($user->get_id())); CS::get()->components->accountImage->getPath($user->get_id()));

View File

@ -34,6 +34,10 @@ class AdvancedUser extends User {
$this->personnalWebsite = $personnalWebsite == "" ? null : $personnalWebsite; $this->personnalWebsite = $personnalWebsite == "" ? null : $personnalWebsite;
} }
public function has_personnalWebsite() : bool {
return $this->personnalWebsite != null;
}
public function get_personnalWebsite() : string { public function get_personnalWebsite() : string {
return $this->personnalWebsite != null ? $this->personnalWebsite : "null"; return $this->personnalWebsite != null ? $this->personnalWebsite : "null";
} }

View File

@ -80,6 +80,10 @@ class User {
$this->virtualDirectory = $virtualDirectory == "" ? null : $virtualDirectory; $this->virtualDirectory = $virtualDirectory == "" ? null : $virtualDirectory;
} }
public function has_virtualDirectory() : bool {
return $this->virtualDirectory != null;
}
public function get_virtualDirectory() : string { public function get_virtualDirectory() : string {
return $this->virtualDirectory != null ? $this->virtualDirectory : "null"; return $this->virtualDirectory != null ? $this->virtualDirectory : "null";
} }