mirror of
https://github.com/Aviortheking/Blog_IMIE.git
synced 2025-06-14 19:19:18 +00:00
changements
This commit is contained in:
@ -2,6 +2,8 @@
|
||||
|
||||
namespace App\DB;
|
||||
|
||||
use App\Functions;
|
||||
|
||||
class Post {
|
||||
|
||||
|
||||
@ -15,12 +17,130 @@ class Post {
|
||||
|
||||
private $short;
|
||||
|
||||
private $categorie;
|
||||
private $category;
|
||||
|
||||
private $author;
|
||||
|
||||
private $tags;
|
||||
|
||||
private $dt;
|
||||
|
||||
public function setId($id) {
|
||||
$this->id = $id;
|
||||
}
|
||||
|
||||
|
||||
public function setTitle($title) {
|
||||
$this->title = $title;
|
||||
}
|
||||
|
||||
|
||||
public function setUrl($url) {
|
||||
$this->url = $url;
|
||||
}
|
||||
|
||||
public function setContent($content) {
|
||||
$this->content = $content;
|
||||
}
|
||||
|
||||
public function setShort($short) {
|
||||
$this->short = $short;
|
||||
}
|
||||
|
||||
public function setCategory($category) {
|
||||
$this->category = $category;
|
||||
}
|
||||
|
||||
public function setAuthor($author) {
|
||||
$this->author = $author;
|
||||
}
|
||||
|
||||
public function setDateTime($dt) {
|
||||
$this->dt = $dt;
|
||||
}
|
||||
|
||||
public function setTags($taglist) {
|
||||
$this->tags = $taglist;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public function getId() {
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function getTitle() {
|
||||
return $this->title;
|
||||
}
|
||||
|
||||
public function getUrl() {
|
||||
return $this->url;
|
||||
}
|
||||
|
||||
public function getContent() {
|
||||
return $this->content;
|
||||
}
|
||||
|
||||
public function getShort() {
|
||||
return $this->short;
|
||||
}
|
||||
|
||||
public function getCategory() {
|
||||
return Category::get($this->category);
|
||||
}
|
||||
|
||||
public function getAuthor() {
|
||||
return Author::get($this->author);
|
||||
}
|
||||
|
||||
public function getDateTime() {
|
||||
return $this->dt;
|
||||
}
|
||||
|
||||
public function getTags() {
|
||||
$temp = array();
|
||||
|
||||
if ($this->tags == null) return $temp;
|
||||
foreach ($this->tags as $tag) {
|
||||
$temp[] = Tag::get($tag);
|
||||
}
|
||||
return $temp;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public function __construct() {
|
||||
$this->dt = new \DateTime();
|
||||
}
|
||||
|
||||
public static function fromArray($array) {
|
||||
$post = new Post();
|
||||
$post->setId($array["id"]);
|
||||
$post->setTitle($array["title"]);
|
||||
$post->setUrl($array["url"]);
|
||||
$post->setContent($array["content"]);
|
||||
$post->setShort($array["short"]);
|
||||
if(isset($array["category"])) $post->setCategory($array["category"]);
|
||||
$post->setAuthor($array["author"]);
|
||||
$post->setDateTime($array["dt"]);
|
||||
return $post;
|
||||
}
|
||||
|
||||
/**
|
||||
* list of posts
|
||||
*
|
||||
@ -29,9 +149,52 @@ class Post {
|
||||
*
|
||||
* @return array(Post)
|
||||
*/
|
||||
|
||||
public static function list($recent = true, $limit = 100) {
|
||||
$sort = $recent ? "DESC" : "ASC";
|
||||
$query = "SELECT * FROM posts ORDER BY dt " . $sort . " LIMIT " . $limit;
|
||||
|
||||
$pdo = Functions::connect();
|
||||
$posts = $pdo->query($query)->fetchAll();
|
||||
|
||||
$res = array();
|
||||
|
||||
foreach ($posts as $post) {
|
||||
$res[] = Post::fromArray($post);
|
||||
}
|
||||
|
||||
return $res;
|
||||
}
|
||||
|
||||
public static function listByCategory($categoryId = null, $recent = true, $limit = 100) {
|
||||
$sort = $recent ? "DESC" : "ASC";
|
||||
$cat = $categoryId !== null ? "WHERE category=" . $categoryId : "";
|
||||
$query = "SELECT * FROM posts " . $cat . " ORDER BY dt " . $sort . " LIMIT " . $limit;
|
||||
|
||||
$pdo = Functions::connect();
|
||||
$posts = $pdo->query($query)->fetchAll();
|
||||
|
||||
$res = array();
|
||||
|
||||
foreach ($posts as $post) {
|
||||
$post = Post::fromArray($post);
|
||||
$query = "SELECT * FROM post_tag WHERE post_id=". $post->getId();
|
||||
$tagList = array();
|
||||
$bool = $pdo->query($query);
|
||||
// var_dump($bool->fetchAll());
|
||||
if($bool) foreach ($pdo->query($query)->fetchAll() as $tag) {
|
||||
$tagList[] = $tag["tag"];
|
||||
}
|
||||
$post->setTags($tagList);
|
||||
|
||||
$res[] = $post;
|
||||
}
|
||||
|
||||
return $res;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* get a specific Post
|
||||
@ -40,7 +203,21 @@ class Post {
|
||||
*
|
||||
* @return Post
|
||||
*/
|
||||
public static function get(int $id) {
|
||||
public static function get($id) {
|
||||
$post = Post::fromArray(Functions::connect()->query("SELECT * FROM posts WHERE id=" . $id)->fetch());
|
||||
$query = "SELECT * FROM post_tag WHERE post_id=". $post->getId();
|
||||
$pdo = Functions::connect();
|
||||
$bool = $pdo->query($query);
|
||||
|
||||
$tagList = array();
|
||||
if($bool) {
|
||||
foreach ($bool->fetchAll() as $tag) {
|
||||
$tagList[] = $tag["tag"];
|
||||
}
|
||||
if(count($tagList) >= 1) $post->setTags($tagList);
|
||||
}
|
||||
|
||||
return $post;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -50,22 +227,55 @@ class Post {
|
||||
*
|
||||
*/
|
||||
public static function add(Post $post) {
|
||||
$query = "INSERT INTO posts (id, title, url, content, short, categorie, author, dt)
|
||||
VALUES (NULL, ':title', ':url', ':content', ':short', ':categorie', ':author', ':dt');";
|
||||
|
||||
$pdo = Functions::connect();
|
||||
$prepared = $pdo->prepare($query);
|
||||
$prepared->bindParam(":title", $post->getTitle());
|
||||
$prepared->bindParam(":url", $post->getUrl());
|
||||
$prepared->bindParam(":content", $post->getContent());
|
||||
$prepared->bindParam(":short", $post->getShort());
|
||||
$prepared->bindParam(":categorie", $post->getCategory());
|
||||
$prepared->bindParam(":author", $post->getAuthor());
|
||||
$prepared->bindParam(":dt", $post->getDateTime());
|
||||
$prepared->execute();
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* remove the post
|
||||
*
|
||||
* @param Post $post
|
||||
*
|
||||
*
|
||||
*/
|
||||
public static function remove(Post $post) {}
|
||||
public static function remove(Post $post) {
|
||||
Functions::connect()->prepare("DELETE FROM posts WHERE id=:id")->execute(array(":id" => $post->getId()));
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* update the post data on the db
|
||||
*
|
||||
* @param Post $post
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function update(Post $post) {}
|
||||
public static function update(Post $post) {
|
||||
Functions::connect()->prepare("UPDATE posts SET title=':title', url=':url', content=':content', short=':short', category=':category', author=':author', dt=':dt' WHERE id=:id")->execute(array(
|
||||
":title" => $post->getTitle(),
|
||||
":url" => $post->getUrl(),
|
||||
":content" => $post->getContent(),
|
||||
":short" => $post->getShort(),
|
||||
":categorie" => $post->getCategorie(),
|
||||
":author" => $post->getAuthor(),
|
||||
":dt" => $post->getDt(),
|
||||
":id" => $post->getId()
|
||||
));
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user