Use Post object to create posts

This commit is contained in:
Pierre 2018-04-22 14:38:26 +02:00
parent ae7dd0bed3
commit 168f56eb08
3 changed files with 91 additions and 78 deletions

View File

@ -119,6 +119,9 @@ class PostsController {
user_login_required(); //Need login user_login_required(); //Need login
//Initialize post object
$post = new Post();
//Get the kind of page //Get the kind of page
if(!isset($_POST['kind-page']) || !isset($_POST['kind-id'])) if(!isset($_POST['kind-page']) || !isset($_POST['kind-id']))
Rest_fatal_error(400, "Please specify the kind of target page and its ID !"); Rest_fatal_error(400, "Please specify the kind of target page and its ID !");
@ -143,17 +146,23 @@ class PostsController {
default: default:
Rest_fatal_error(500, "Unsupported kind of page !"); Rest_fatal_error(500, "Unsupported kind of page !");
} }
$post->set_kind_page($kind_page);
$post->set_kind_page_id($kind_page_id);
//Get the kind of post //Get the kind of post
if(!isset($_POST['kind'])) if(!isset($_POST['kind']))
Rest_fatal_error(400, "Please specify the kind of post !"); Rest_fatal_error(400, "Please specify the kind of post !");
$kind = $_POST['kind']; $kind = $_POST['kind'];
$post->set_kind($kind);
//Get the content of the post //Get the content of the post
$content = getPostContent("content"); $content = getPostContent("content");
$post->set_content($content);
//Get the visibility of the post //Get the visibility of the post
$visibility = $this->getPostVisibilityLevel("visibility"); $visibility = $this->getPostVisibilityLevel("visibility");
$post->set_visibility_level($visibility);
//Act differently depending of the post content //Act differently depending of the post content
//For text post //For text post
@ -179,6 +188,10 @@ class PostsController {
$file_sys_path = path_user_data($file_path, true); $file_sys_path = path_user_data($file_path, true);
$file_type = mime_content_type($file_sys_path); $file_type = mime_content_type($file_sys_path);
$file_size = filesize($file_sys_path); $file_size = filesize($file_sys_path);
$post->set_file_path($file_path);
$post->set_file_type($file_type);
$post->set_file_size($file_size);
} }
//For YouTube posts //For YouTube posts
@ -195,6 +208,7 @@ class PostsController {
//Save video informations //Save video informations
$file_path = $youtube_id; $file_path = $youtube_id;
$post->set_file_path($file_path);
} }
@ -209,7 +223,7 @@ class PostsController {
Rest_fatal_error(400, "You are not allowed to use this movie in your posts !"); Rest_fatal_error(400, "You are not allowed to use this movie in your posts !");
//Save movie informations //Save movie informations
$video_id = $movieID; $post->set_movie_id($movieID);
} }
//For weblinks //For weblinks
@ -228,6 +242,12 @@ class PostsController {
$link_title = isset($page_infos["title"]) ? $page_infos["title"] : null; $link_title = isset($page_infos["title"]) ? $page_infos["title"] : null;
$link_description = isset($page_infos["description"]) ? $page_infos["description"] : null; $link_description = isset($page_infos["description"]) ? $page_infos["description"] : null;
$link_image = isset($page_infos["image"]) ? $page_infos["image"] : null; $link_image = isset($page_infos["image"]) ? $page_infos["image"] : null;
$post->set_link_url($link_url);
$post->set_link_title($link_title);
$post->set_link_description($link_description);
$post->set_link_image($link_image);
} }
//For PDFs //For PDFs
@ -254,6 +274,10 @@ class PostsController {
$file_type = "application/pdf"; $file_type = "application/pdf";
$file_size = filesize($target_file_sys_path); $file_size = filesize($target_file_sys_path);
$file_path = $target_file_path; $file_path = $target_file_path;
$post->set_file_path($file_path);
$post->set_file_type($file_type);
$post->set_file_size($file_size);
} }
//For countdown timer //For countdown timer
@ -262,6 +286,8 @@ class PostsController {
//Get end timestamp //Get end timestamp
$time_end = getPostTimeStamp("time-end"); $time_end = getPostTimeStamp("time-end");
$post->set_time_end($time_end);
} }
//For survey //For survey
@ -304,6 +330,8 @@ class PostsController {
$choice->set_name($name); $choice->set_name($name);
$survey->add_choice($choice); $survey->add_choice($choice);
} }
$post->set_survey($survey);
} }
//The content type is not supported //The content type is not supported
@ -311,40 +339,11 @@ class PostsController {
Rest_fatal_error(500, "This kind of post is not supported !"); Rest_fatal_error(500, "This kind of post is not supported !");
} }
//Complete post information
$post->set_userID(userID);
//Create the post //Create the post
$postID = CS::get()->components->posts->create( $postID = CS::get()->components->posts->create($post);
//Informations about the kind of page
$kind_page,
$kind_page_id,
//Generic informations about the post
userID,
$content,
$visibility,
$kind,
//Specific informations about the post
//Posts with files
isset($file_size) ? $file_size : 0,
isset($file_type) ? $file_type : null,
isset($file_path) ? $file_path : null,
//For video post
isset($video_id) ? $video_id : 0,
//For countdown post
isset($time_end) ? $time_end : 0,
//For weblink posts
isset($link_url) ? $link_url : null,
isset($link_title) ? $link_title : null,
isset($link_description) ? $link_description : null,
isset($link_image) ? $link_image : null,
//For survey posts
isset($survey) ? $survey : null
);
//Check for errors //Check for errors
if($postID < 0) if($postID < 0)

View File

@ -105,6 +105,7 @@ class Posts {
* @param int $targetID The ID of the target user * @param int $targetID The ID of the target user
* @param int $startPoint The startpoint for the request (0 stands for none) * @param int $startPoint The startpoint for the request (0 stands for none)
* @param int $limit The maximum number of messages to fetch * @param int $limit The maximum number of messages to fetch
* @return array The list of posts of the user
*/ */
public function getUserPosts(int $userID, int $targetID, int $startPoint = 0, int $limit = 10) : array { public function getUserPosts(int $userID, int $targetID, int $startPoint = 0, int $limit = 10) : array {
@ -333,37 +334,18 @@ class Posts {
/** /**
* Create a new post * Create a new post
* *
* @param string $kind_page The kind of target page * @param Post $post Information about the post to create
* @param int $kind_page_id The ID of the target page
* @param int $userID The ID of the user creating the post
* @param string $content The content of the post
* @param int $visibility The visibility of the post
* @param string $kind The kind of post
* @param int $file_size The size of the associated file (0 by default - no file)
* @param string $file_type The mime type of the associated file
* @param string $file_path The path in user data pointing on the file
* @param int $videoID The ID of the associated personnal user video (0 for no video)
* @param int $time_end The end of the countdown timer (if any)
* @param string $link_url The URL of the link associated with the post (if any)
* @param string $link_title The title of the link associated with the post (if any)
* @param string $link_description The description of the link associated with the post (if any)
* @param string $link_image The link pointing on the image associated with the post (if any)
* @param Survey $survey Information about a related survey (if any)
* @return int The ID of the created post or -1 in case of failure * @return int The ID of the created post or -1 in case of failure
*/ */
public function create(string $kind_page, int $kind_page_id, int $userID, string $content, public function create(Post $post) : int {
int $visibility, string $kind, int $file_size = 0,
string $file_type = null, string $file_path = null, int $videoID = 0,
int $time_end = 0, string $link_url = null, string $link_title = null,
string $link_description = null, string $link_image = null, Survey $survey = null) : int {
//Generate new post informations //Generate new post informations
//Get the corresponding kind of post for the database //Get the corresponding kind of post for the database
$post_kind_db = array_flip($this::POSTS_DB_TYPES)[$kind]; $post_kind_db = array_flip($this::POSTS_DB_TYPES)[$post->get_kind()];
//Generate valid date form for time_end value //Generate valid date form for time_end value
if($time_end != 0){ if($post->get_time_end() != 0){
$date_end = date("d/m/Y", $time_end); $date_end = date("d/m/Y", $post->get_time_end());
$array_date_end = explode("/", $date_end); $array_date_end = explode("/", $date_end);
//Save the values //Save the values
@ -373,15 +355,15 @@ class Posts {
} }
//Process user page posts //Process user page posts
if($kind_page == $this::PAGE_KIND_USER){ if($post->get_kind_page() == $this::PAGE_KIND_USER){
//Determine who is creating the post //Determine who is creating the post
$post_user_id = $kind_page_id; $post_user_id = $post->get_kind_page_id();
$post_friend_id = $kind_page_id == $userID ? 0 : $userID; $post_friend_id = $post->get_kind_page_id() == $post->get_userID() ? 0 : $post->get_userID();
} }
else { else {
throw new Exception("Unsupported kind of page : ".$kind_page." !"); throw new Exception("Unsupported kind of page : ".$post->get_kind_page()." !");
} }
//Generate database entry //Generate database entry
@ -389,17 +371,17 @@ class Posts {
"ID_personne" => $post_user_id, "ID_personne" => $post_user_id,
"ID_amis" => $post_friend_id, "ID_amis" => $post_friend_id,
"date_envoi" => mysql_date(), "date_envoi" => mysql_date(),
"texte" => $content, "texte" => $post->has_content() ? $post->get_content() : "",
"niveau_visibilite" => $visibility, "niveau_visibilite" => $post->get_visibility_level(),
"type" => $post_kind_db, "type" => $post_kind_db,
//Generic files informations //Generic files informations
"size" => $file_size != 0 ? $file_size : null, "size" => $post->has_file_size() ? $post->get_file_size() : null,
"file_type" => $file_type, "file_type" => $post->has_file_type() ? $post->get_file_type() : null,
"path" => $file_path, "path" => $post->has_file_path() ? $post->get_file_path() : null,
//Movie posts //Movie posts
"idvideo" => $videoID != 0 ? $videoID : null, "idvideo" => $post->has_movie_id() ? $post->get_movie_id() : null,
//Countdown timer //Countdown timer
"jour_fin" => isset($day_end) ? $day_end : null, "jour_fin" => isset($day_end) ? $day_end : null,
@ -407,10 +389,10 @@ class Posts {
"annee_fin" => isset($year_end) ? $year_end : null, "annee_fin" => isset($year_end) ? $year_end : null,
//Weblink page //Weblink page
"url_page" => $link_url, "url_page" => $post->has_link_url() ? $post->get_link_url() : null,
"titre_page" => $link_title, "titre_page" => $post->has_link_title() ? $post->get_link_title() : null,
"description_page" => $link_description, "description_page" => $post->has_link_description() ? $post->get_link_description() : null,
"image_page" => $link_image "image_page" => $post->has_link_image() ? $post->get_link_image() : null
); );
//Insert the post //Insert the post
@ -421,10 +403,11 @@ class Posts {
$postID = CS::get()->db->getLastInsertedID(); $postID = CS::get()->db->getLastInsertedID();
//Create the survey if required //Create the survey if required
if($kind == $this::POST_KIND_SURVEY){ if($post->get_kind() == $this::POST_KIND_SURVEY){
//Complete the request //Complete the request
$survey = $post->get_survey();
$survey->set_postID($postID); $survey->set_postID($postID);
$survey->set_userID($userID); $survey->set_userID($post->get_userID());
components()->survey->create($survey); components()->survey->create($survey);
} }

View File

@ -8,7 +8,8 @@
class Post extends BaseUniqueObjectFromUser { class Post extends BaseUniqueObjectFromUser {
//Private fields //Private fields
private $user_page_id = -1; private $kind_page;
private $kind_page_id = -1;
private $content; private $content;
private $visibility_level; private $visibility_level;
private $kind; private $kind;
@ -30,18 +31,48 @@ class Post extends BaseUniqueObjectFromUser {
private $has_comments = false; private $has_comments = false;
private $user_access; private $user_access;
//Set and get the kind of page
public function set_kind_page(string $kind_page){
$this->kind_page = $kind_page == "" ? null : $kind_page;
}
public function has_kind_page() : bool {
return $this->kind_page != null;
}
public function get_kind_page() : string {
return $this->kind_page != null ? $this->kind_page : "null";
}
//Set and get the ID of the kind of page the posts belongs to
public function set_kind_page_id(int $kind_page_id){
$this->kind_page_id = $kind_page_id;
}
public function has_kind_page_id() : bool {
return $this->kind_page_id > -1;
}
public function get_kind_page_id() : int {
return $this->kind_page_id;
}
//Set and get the target page ID //Set and get the target page ID
public function set_user_page_id(int $user_page_id){ public function set_user_page_id(int $user_page_id){
$this->user_page_id = $user_page_id; if($user_page_id > 0)
$this->set_kind_page(Posts::PAGE_KIND_USER);
$this->kind_page_id = $user_page_id;
} }
public function has_user_page_id() : bool { public function has_user_page_id() : bool {
return $this->user_page_id > -1; return $this->kind_page_id > 0 && $this->kind_page == Posts::PAGE_KIND_USER;
} }
public function get_user_page_id() : int { public function get_user_page_id() : int {
return $this->user_page_id; return $this->kind_page == Posts::PAGE_KIND_USER ? $this->kind_page_id : 0;
} }