87 lines
2.6 KiB
PHP
87 lines
2.6 KiB
PHP
<?php
|
|
class Post extends Account {
|
|
private $current_user;
|
|
private $title;
|
|
private $content;
|
|
|
|
private $conn;
|
|
public $error;
|
|
|
|
public function __construct($current_user, $conn) {
|
|
parent::__construct($current_user, $conn);
|
|
$this->current_user = $current_user;
|
|
$this->conn = $conn;
|
|
}
|
|
|
|
public function getForm($title, $content) {
|
|
$this->title = $title;
|
|
$this->content = $content;
|
|
}
|
|
|
|
public function postUpdate() {
|
|
if (!$this->isLoggedIn())
|
|
return;
|
|
|
|
switch(true) {
|
|
case empty($this->content):
|
|
$this->error = "Your post cannot be left empty.";
|
|
break;
|
|
case strlen($this->content) > 2500:
|
|
$this->error = "Post exceeds the character limit. Are you trying to bypass it?";
|
|
break;
|
|
}
|
|
|
|
return $this->error;
|
|
}
|
|
|
|
public function insertPost() {
|
|
if (!$this->isLoggedIn())
|
|
return;
|
|
|
|
$new_content = $this->content;
|
|
|
|
if (empty($this->title)) {
|
|
// micro post
|
|
$type = 1;
|
|
$new_content = substr($this->content, 0, 300);
|
|
} else {
|
|
// marco post
|
|
$type = 2;
|
|
}
|
|
|
|
$stmt = $this->conn->prepare("INSERT INTO posts (title, content, author, type) VALUES (:title, :content, :author, :type)");
|
|
$stmt->bindParam(":title", $this->title);
|
|
$stmt->bindParam(":content", $new_content);
|
|
$stmt->bindParam(":author", $this->getDetails("username"));
|
|
$stmt->bindParam(":type", $type);
|
|
|
|
$stmt->execute();
|
|
return true;
|
|
}
|
|
|
|
public function getPosts($type) {
|
|
$stmt = $this->conn->prepare("SELECT * FROM posts WHERE type = :type ORDER BY date DESC");
|
|
$stmt->bindParam(":type", $type);
|
|
$stmt->execute();
|
|
|
|
return $stmt;
|
|
}
|
|
|
|
public function getSpecifcPost($id) {
|
|
$stmt = $this->conn->prepare("SELECT * FROM posts WHERE id = :id");
|
|
$stmt->bindParam(":id", $id);
|
|
$stmt->execute();
|
|
|
|
$post_details = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
return $post_details;
|
|
}
|
|
|
|
public function deletePost($id) {
|
|
$stmt = $this->conn->prepare("DELETE FROM posts WHERE id = :id");
|
|
$stmt->bindParam(":id", $id);
|
|
$stmt->execute();
|
|
|
|
return true;
|
|
}
|
|
}
|
|
?>
|