diff --git a/RestControllers/friendsController.php b/RestControllers/friendsController.php index e5ae327..95c86a0 100644 --- a/RestControllers/friendsController.php +++ b/RestControllers/friendsController.php @@ -16,7 +16,7 @@ class friendsController{ user_login_required(); //Login required //Try to get friends list - $friendsList = false; + $friendsList = CS::get()->components->friends->getList(userID); //Check for errors if($friendsList === false) diff --git a/classes/components/friends.php b/classes/components/friends.php index 71c427e..5e78f0f 100644 --- a/classes/components/friends.php +++ b/classes/components/friends.php @@ -7,6 +7,11 @@ class friends { + /** + * @var String $friendsTable The name of the table for friends + */ + private $friendsTable = "amis"; + /** * Public construcor */ @@ -14,6 +19,45 @@ class friends { //Nothing now } + /** + * Get and returns the list of the friends of a user + * + * @param Integer $userID The ID of the user + * @return Array The list of the friends of the user + */ + public function getList($userID) : array { + + //Prepare the request on the database + $tableName = $this->friendsTable.", utilisateurs"; + $condition = "WHERE ID_personne = ? AND amis.ID_amis = utilisateurs.ID ORDER BY utilisateurs.last_activity"; + $condValues = array($userID); + + //Specify which fields to get + $fieldsList = array( + "utilisateurs.last_activity", + $this->friendsTable.".ID_amis", + $this->friendsTable.".actif", + $this->friendsTable.".abonnement", + ); + + //Perform the request on the database + $results = CS::get()->db->select($tableName, $condition, $condValues, $fieldsList); + + //Process results + $friendsList = array(); + foreach($results as $process){ + $friendsList[] = array( + "ID_friend" => $process["ID_amis"], + "active" => $process["actif"], + "following" => $process["abonnement"], + "time_last_activity" => $process["last_activity"] + ); + } + + //Return result + return $friendsList; + + } } //Register component