mirror of
https://github.com/pierre42100/ComunicAPI
synced 2024-11-27 15:59:29 +00:00
Implement Comment object for the get_single method
This commit is contained in:
parent
e20403e8ab
commit
cf4b75c61a
@ -76,14 +76,14 @@ class commentsController {
|
|||||||
$commentID = getPostCommentIDWithAccess("commentID");
|
$commentID = getPostCommentIDWithAccess("commentID");
|
||||||
|
|
||||||
//Get informations about the comment
|
//Get informations about the comment
|
||||||
$infos = components()->comments->get_single($commentID, TRUE);
|
$comment = components()->comments->get_single($commentID, TRUE);
|
||||||
|
|
||||||
//Check for errors
|
//Check for errors
|
||||||
if(count($infos) == 0)
|
if(!$comment->isValid())
|
||||||
Rest_fatal_error(500, "Couldn't fetch informations about the comment !");
|
Rest_fatal_error(500, "Couldn't fetch informations about the comment !");
|
||||||
|
|
||||||
//Return informations about the comment
|
//Return informations about the comment
|
||||||
return $infos;
|
return $this->commentToAPI($comment);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -174,4 +174,30 @@ class commentsController {
|
|||||||
//Return comment conent
|
//Return comment conent
|
||||||
return $comment_content;
|
return $comment_content;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Turn a comment object into a readable object
|
||||||
|
*
|
||||||
|
* @param Comment $comment The comment to convert
|
||||||
|
* @return array Informations about the comment
|
||||||
|
*/
|
||||||
|
private function commentToAPI(Comment $comment) : array {
|
||||||
|
$data = array();
|
||||||
|
|
||||||
|
$data["ID"] = $comment->get_id();
|
||||||
|
$data["userID"] = $comment->get_userID();
|
||||||
|
$data["postID"] = $comment->get_postID();
|
||||||
|
$data["time_sent"] = $comment->get_time_sent();
|
||||||
|
$data["content"] = $comment->get_content();
|
||||||
|
|
||||||
|
$data["img_path"] = $comment->has_img_path() ? $comment->get_img_path() : null;
|
||||||
|
$data["img_url"] = $comment->has_img_url() ? $comment->get_img_url() : null;
|
||||||
|
|
||||||
|
if($comment->has_likes()){
|
||||||
|
$data["likes"] = $comment->get_likes();
|
||||||
|
$data["userlike"] = $comment->get_userlike();
|
||||||
|
}
|
||||||
|
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
}
|
}
|
@ -73,9 +73,9 @@ class Comments {
|
|||||||
*
|
*
|
||||||
* @param int $commentID The ID of the comment to get
|
* @param int $commentID The ID of the comment to get
|
||||||
* @param bool $include_likes Specify if likes has to be loaded
|
* @param bool $include_likes Specify if likes has to be loaded
|
||||||
* @return array Information about the comment
|
* @return Comment Information about the comment (invalid comment in case of failure)
|
||||||
*/
|
*/
|
||||||
public function get_single(int $commentID, bool $include_likes = false) : array {
|
public function get_single(int $commentID, bool $include_likes = false) : Comment {
|
||||||
|
|
||||||
//Perform a request on the database
|
//Perform a request on the database
|
||||||
$conditions = "WHERE ID = ?";
|
$conditions = "WHERE ID = ?";
|
||||||
@ -86,10 +86,10 @@ class Comments {
|
|||||||
|
|
||||||
//Check for results
|
//Check for results
|
||||||
if(count($result) == 0)
|
if(count($result) == 0)
|
||||||
return array();
|
return new Comment();
|
||||||
|
|
||||||
//Return result
|
//Return result
|
||||||
return $this->parse_comment($result[0], $include_likes);
|
return $this->dbToComment($result[0], $include_likes);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -127,7 +127,7 @@ class Comments {
|
|||||||
$commentInfos = $this->get_single($commentID, false);
|
$commentInfos = $this->get_single($commentID, false);
|
||||||
|
|
||||||
//Check for errors
|
//Check for errors
|
||||||
if(count($commentInfos) == 0)
|
if(!$commentInfos->isValid())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
//Process deletion
|
//Process deletion
|
||||||
@ -137,18 +137,18 @@ class Comments {
|
|||||||
/**
|
/**
|
||||||
* Process comment deletion
|
* Process comment deletion
|
||||||
*
|
*
|
||||||
* @param array $commentInfos Informations about the comment to delete
|
* @param Comment $commentInfos Informations about the comment to delete
|
||||||
* @return bool TRUE for a success / FALSE else
|
* @return bool TRUE for a success / FALSE else
|
||||||
*/
|
*/
|
||||||
private function process_delete(array $commentInfos) : bool {
|
private function process_delete(Comment $commentInfos) : bool {
|
||||||
|
|
||||||
//Get comment ID
|
//Get comment ID
|
||||||
$commentID = $commentInfos["ID"];
|
$commentID = $commentInfos->get_id();
|
||||||
|
|
||||||
//Check if an image is associated to the comment
|
//Check if an image is associated to the comment
|
||||||
if(strlen($commentInfos['img_path']) > 2){
|
if($commentInfos->has_img_path()){
|
||||||
|
|
||||||
$image_path = path_user_data($commentInfos['img_path'], true);
|
$image_path = path_user_data($commentInfos->get_img_path(), true);
|
||||||
|
|
||||||
//Delete the image if it exists
|
//Delete the image if it exists
|
||||||
if(file_exists($image_path))
|
if(file_exists($image_path))
|
||||||
@ -213,10 +213,10 @@ class Comments {
|
|||||||
public function getAssociatedPost(int $commentID) : int {
|
public function getAssociatedPost(int $commentID) : int {
|
||||||
|
|
||||||
//Get a single comment informations
|
//Get a single comment informations
|
||||||
$commentInfos = $this->get_single($commentID);
|
$comment = $this->get_single($commentID);
|
||||||
|
|
||||||
//Check if we have got the required information and return it
|
//Check if we have got the required information and return it
|
||||||
return isset($commentInfos["postID"]) ? $commentInfos["postID"] : 0;
|
return $comment->has_postID() ? $comment->get_postID() : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -270,6 +270,38 @@ class Comments {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Turn a comment database entry into a Comment object
|
||||||
|
*
|
||||||
|
* @param array $data Database entry
|
||||||
|
* @param bool $load_likes Specify if the likes have to be loaded or not
|
||||||
|
* @return Comment Generated comment object
|
||||||
|
*/
|
||||||
|
private function dbToComment(array $data, bool $load_likes = true) : Comment {
|
||||||
|
|
||||||
|
$comment = new Comment();
|
||||||
|
|
||||||
|
$comment->set_id($data["ID"]);
|
||||||
|
$comment->set_userID($data["ID_personne"]);
|
||||||
|
$comment->set_postID($data["ID_texte"]);
|
||||||
|
$comment->set_time_sent(strtotime($data["date_envoi"]));
|
||||||
|
$comment->set_content($data["commentaire"]);
|
||||||
|
|
||||||
|
//Check for image
|
||||||
|
if($data["image_commentaire"] != ""){
|
||||||
|
$comment->set_img_path(str_replace("file:", "", $data["image_commentaire"]));
|
||||||
|
$info->set_img_url(path_user_data($info->get_img_path(), false));
|
||||||
|
}
|
||||||
|
|
||||||
|
if($load_likes){
|
||||||
|
//Get informations about likes
|
||||||
|
$comment->set_likes(CS::get()->components->likes->count($comment->get_id(), Likes::LIKE_COMMENT));
|
||||||
|
$comment->set_userLike(user_signed_in() ? CS::get()->components->likes->is_liking(userID, $comment->get_id(), Likes::LIKE_COMMENT) : false);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $comment;
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Register class
|
//Register class
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
* @author Pierre HUBERT
|
* @author Pierre HUBERT
|
||||||
*/
|
*/
|
||||||
|
|
||||||
abstract class BaseUniqueObject {
|
class BaseUniqueObject {
|
||||||
|
|
||||||
//Private fields
|
//Private fields
|
||||||
private $id = 0;
|
private $id = 0;
|
||||||
|
@ -12,11 +12,21 @@ class Comment extends BaseUniqueObject {
|
|||||||
private $postID;
|
private $postID;
|
||||||
private $time_sent;
|
private $time_sent;
|
||||||
private $content;
|
private $content;
|
||||||
private $image_path;
|
private $img_path;
|
||||||
private $image_url;
|
private $img_url;
|
||||||
private $likes;
|
private $likes;
|
||||||
private $userLike;
|
private $userLike;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Public constructor
|
||||||
|
*/
|
||||||
|
public function __construct(){
|
||||||
|
//Initialize some values
|
||||||
|
$this->postID = 0;
|
||||||
|
$this->userID = 0;
|
||||||
|
$this->likes = -1;
|
||||||
|
}
|
||||||
|
|
||||||
//Set and get user ID
|
//Set and get user ID
|
||||||
public function set_userID(int $userID){
|
public function set_userID(int $userID){
|
||||||
$this->userID = $userID;
|
$this->userID = $userID;
|
||||||
@ -31,6 +41,10 @@ class Comment extends BaseUniqueObject {
|
|||||||
$this->postID = $postID;
|
$this->postID = $postID;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function has_postID() : bool {
|
||||||
|
return $this->postID > 0;
|
||||||
|
}
|
||||||
|
|
||||||
public function get_postID() : int {
|
public function get_postID() : int {
|
||||||
return $this->postID;
|
return $this->postID;
|
||||||
}
|
}
|
||||||
@ -88,6 +102,10 @@ class Comment extends BaseUniqueObject {
|
|||||||
$this->likes = $likes;
|
$this->likes = $likes;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function has_likes() : bool {
|
||||||
|
return $this->likes > -1;
|
||||||
|
}
|
||||||
|
|
||||||
public function get_likes() : int {
|
public function get_likes() : int {
|
||||||
return $this->likes;
|
return $this->likes;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user