mirror of
https://github.com/pierre42100/ComunicAPI
synced 2024-11-23 22:09:29 +00:00
Upgraded API to use only Comments object instead of arrays
This commit is contained in:
parent
a3b005d184
commit
3cfe5df2a7
@ -5,7 +5,7 @@
|
|||||||
* @author Pierre HUBERT
|
* @author Pierre HUBERT
|
||||||
*/
|
*/
|
||||||
|
|
||||||
class commentsController {
|
class CommentsController {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a comment
|
* Create a comment
|
||||||
@ -181,7 +181,7 @@ class commentsController {
|
|||||||
* @param Comment $comment The comment to convert
|
* @param Comment $comment The comment to convert
|
||||||
* @return array Informations about the comment
|
* @return array Informations about the comment
|
||||||
*/
|
*/
|
||||||
private function commentToAPI(Comment $comment) : array {
|
public static function commentToAPI(Comment $comment) : array {
|
||||||
$data = array();
|
$data = array();
|
||||||
|
|
||||||
$data["ID"] = $comment->get_id();
|
$data["ID"] = $comment->get_id();
|
@ -528,6 +528,14 @@ class postsController {
|
|||||||
//Update visibility level
|
//Update visibility level
|
||||||
$infos['visibility_level'] = $this::VISIBILITY_LEVELS_API[$infos['visibility_level']];
|
$infos['visibility_level'] = $this::VISIBILITY_LEVELS_API[$infos['visibility_level']];
|
||||||
|
|
||||||
|
//Parse comments if required
|
||||||
|
if(isset($infos['comments'])){
|
||||||
|
if($infos['comments'] != null){
|
||||||
|
foreach($infos['comments'] as $num=>$src)
|
||||||
|
$infos['comments'][$num] = CommentsController::commentToAPI($src);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//Return the post ready to be shown
|
//Return the post ready to be shown
|
||||||
return $infos;
|
return $infos;
|
||||||
}
|
}
|
||||||
|
@ -46,7 +46,7 @@ class Comments {
|
|||||||
* @param int $postID The ID of the post
|
* @param int $postID The ID of the post
|
||||||
* @param bool $load_comments Specify whether the comments should be
|
* @param bool $load_comments Specify whether the comments should be
|
||||||
* loaded or not (default to true)
|
* loaded or not (default to true)
|
||||||
* @return array The list of comments of the post
|
* @return array The list of comments of the post (as Comment objects)
|
||||||
*/
|
*/
|
||||||
public function get(int $postID, bool $load_comments = true) : array {
|
public function get(int $postID, bool $load_comments = true) : array {
|
||||||
|
|
||||||
@ -61,7 +61,7 @@ class Comments {
|
|||||||
$comments = array();
|
$comments = array();
|
||||||
|
|
||||||
foreach($result as $entry){
|
foreach($result as $entry){
|
||||||
$comments[] = $this->parse_comment($entry, $load_comments);
|
$comments[] = $this->dbToComment($entry, $load_comments);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $comments;
|
return $comments;
|
||||||
@ -234,42 +234,6 @@ class Comments {
|
|||||||
) > 0;
|
) > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Parse a comment informations
|
|
||||||
*
|
|
||||||
* @param array $src Informations from the database
|
|
||||||
* @param bool $load_likes Specify if the likes have to be loaded or not
|
|
||||||
* @return array Informations about the comments ready to be sent
|
|
||||||
*/
|
|
||||||
private function parse_comment(array $src, bool $load_likes = true): array {
|
|
||||||
$info = array();
|
|
||||||
|
|
||||||
$info["ID"] = $src["ID"];
|
|
||||||
$info["userID"] = $src["ID_personne"];
|
|
||||||
$info["postID"] = $src["ID_texte"];
|
|
||||||
$info["time_sent"] = strtotime($src["date_envoi"]);
|
|
||||||
$info["content"] = $src["commentaire"];
|
|
||||||
|
|
||||||
//Check for image
|
|
||||||
if($src["image_commentaire"] != ""){
|
|
||||||
$info["img_path"] = str_replace("file:", "", $src["image_commentaire"]);
|
|
||||||
$info["img_url"] = path_user_data($info["img_path"], false);
|
|
||||||
|
|
||||||
} else {
|
|
||||||
$info["img_path"] = null;
|
|
||||||
$info["img_url"] = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
if($load_likes){
|
|
||||||
//Get informations about likes
|
|
||||||
$info["likes"] = CS::get()->components->likes->count($info["ID"], Likes::LIKE_COMMENT);
|
|
||||||
$info["userlike"] = user_signed_in() ? CS::get()->components->likes->is_liking(userID, $info["ID"], Likes::LIKE_COMMENT) : false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $info;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Turn a comment database entry into a Comment object
|
* Turn a comment database entry into a Comment object
|
||||||
*
|
*
|
||||||
|
Loading…
Reference in New Issue
Block a user