Get the comments associated to a post

This commit is contained in:
Pierre 2017-12-25 09:58:30 +01:00
parent d3cdad6572
commit fda0b10a7c
4 changed files with 115 additions and 3 deletions

View File

@ -33,7 +33,10 @@ class postsController {
//Get visibility level for the post
$visibility_level = CS::get()->components->posts->getUserVisibility(userID, $userID);
return CS::get()->components->posts->getUserPosts(userID, $userID, $visibility_level, $startFrom);
//Get the post of the user
$posts = CS::get()->components->posts->getUserPosts(userID, $userID, $visibility_level, $startFrom);
return $posts;
}
}

View File

@ -0,0 +1,72 @@
<?php
/**
* Comments component class
*
* @author Pierre HUBERT
*/
class Comments {
/**
* Comments table
*/
const COMMENTS_TABLE = "commentaires";
/**
* Fetch the comments of a post
*
* @param int $postID The ID of the post
* @return array The list of comments of the post
*/
public function get(int $postID) : array {
//Perform a request on the database
$conditions = "WHERE ID_texte = ? ORDER BY ID";
$condValues = array($postID);
//Fetch the messages on the database
$result = CS::get()->db->select($this::COMMENTS_TABLE, $conditions, $condValues);
//Process comments list
$comments = array();
foreach($result as $entry){
$comments[] = $this->parse_comment($entry);
}
return $comments;
}
/**
* Parse a comment informations
*
* @param array $src Informations from the database
* @return array Informations about the comments ready to be sent
*/
private function parse_comment(array $src): array {
$info = array();
$info["ID"] = $src["ID"];
$info["userID"] = $src["ID_personne"];
$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;
}
return $info;
}
}
//Register class
Components::register("comments", new Comments());

View File

@ -114,10 +114,13 @@ class Posts {
$dataConds
);
//Check if the user allow comments on his page or not
$getComments = CS::get()->components->user->allowComments($targetID);
//Parse posts
$posts = array();
foreach($list as $post){
$posts[] = $this->parse_post($post);
$posts[] = $this->parse_post($post, $getComments);
}
return $posts;
@ -129,9 +132,10 @@ class Posts {
* the standardized version of post structure
*
* @param array $src Informations about the post from the database
* @param bool $load_comments Get comments, if required
* @return array Parsed informations about the post
*/
private function parse_post(array $src) : array {
private function parse_post(array $src, bool $load_comments) : array {
$info = array();
@ -173,6 +177,11 @@ class Posts {
$info["link_description"] = $src["description_page"];
$info["link_image"] = $src["image_page"];
//Load comments, if required
if($load_comments)
$info["comments"] = CS::get()->components->comments->get($info["ID"]);
else
$info["comments"] = null;
return $info;

View File

@ -426,6 +426,34 @@ class User{
}
/**
* Check whether a user allow comments on his page or not
*
* @param int $userID The ID of the user
* @return bool True if comments are allowed / False else
*/
public function allowComments(int $userID) : bool {
//Fetch the information in the database
$conditions = "WHERE ID = ?";
$condValues = array($userID);
$fields = array("bloquecommentaire");
//Perform the request
$result = CS::get()->db->select(
$this->userTable,
$conditions,
$condValues,
$fields
);
//Check for errors
if(count($result) == 0)
return FAlSE;
//Return result
return $result[0]["bloquecommentaire"] == 0;
}
/**
* Crypt user password