ComunicAPI/classes/components/posts.php
2017-12-24 17:45:05 +01:00

92 lines
2.1 KiB
PHP

<?php
/**
* Posts management class
*
* @author Pierre HUBERT
*/
class Posts {
/**
* Visibility levels
*/
//Posts that can be seen by anyone
const VISIBILITY_PUBLIC = 1;
//Posts that can be seen by the friends of the user
const VISIBILITY_FRIENDS = 2;
//Posts that can be seen by the user only
const VISIBILITY_USER = 3;
/**
* Table name
*/
const TABLE_NAME = "texte";
/**
* Get the visibility level of a user other another user
*
* @param int $userID The ID of the user to fetch
* @param int $targetID The ID of the user target
* @return int Visibility level
*/
public function getUserVisibility(int $userID, int $targetID) : int {
//If the user making the request and the target user are the same
if($userID == $targetID)
return $this::VISIBILITY_USER;
//Check user if is signed out
if($userID == 0)
return $this::VISIBILITY_PUBLIC;
//Check if the two users are friends or not
if(CS::get()->components->friends->are_friend($userID, $targetID))
//Users are friends
return $this::VISIBILITY_FRIENDS;
else
//Users are not friend
return $this::VISIBILITY_PUBLIC;
}
/**
* Get a list of post of a user
*
* @param int $userID The ID of the user making the request
* @param int $targetID The ID of the target user
* @param int $visibilityLevel Visibility level required
* @param int $startPoint The startpoint for the request (0 stands for none)
*/
public function getUserPosts(int $userID, int $targetID, int $visibilityLevel, int $startPoint = 0) : array {
//Prepare the request on the database
$conditions = "WHERE ID_personne = ? AND (";
$dataConds = array($targetID);
//Add the visibility level conditions
$conditions .= "(niveau_visibilite <= ?)";
$dataConds[] = $visibilityLevel;
//If user is signed in, include all the posts that he has created
if($userID > 0){
$conditions .= " OR (ID_amis = ?) ";
$dataConds[] = $userID;
}
//Close conditions
$conditions .= ")";
//Perform the request
return CS::get()->db->select(
$this::TABLE_NAME,
$conditions,
$dataConds
);
}
}
//Register component
Components::register("posts", new Posts());