mirror of
https://github.com/Aviortheking/Blog_IMIE.git
synced 2025-06-15 03:29:19 +00:00
changements
This commit is contained in:
@ -2,11 +2,118 @@
|
||||
|
||||
namespace App\DB;
|
||||
|
||||
use App\Functions;
|
||||
|
||||
class Author {
|
||||
|
||||
private $id;
|
||||
|
||||
private $username;
|
||||
|
||||
private $linkedin;
|
||||
private $password;
|
||||
|
||||
private $job;
|
||||
|
||||
public function __construct(){}
|
||||
|
||||
public function getId() {
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function getUsername() {
|
||||
return $this->username;
|
||||
}
|
||||
|
||||
public function checkPassword($password) {
|
||||
return password_verify($password, $this->password);
|
||||
}
|
||||
|
||||
public function getPassword() {
|
||||
return $this->password;
|
||||
}
|
||||
|
||||
public function getJob() {
|
||||
return $this->job;
|
||||
}
|
||||
|
||||
public function setId($id) {
|
||||
$this->id = $id;
|
||||
}
|
||||
|
||||
public function setUsername($username) {
|
||||
$this->username = $username;
|
||||
}
|
||||
|
||||
public function setPassword($password) {
|
||||
$this->password = \password_hash($password, PASSWORD_DEFAULT);
|
||||
}
|
||||
|
||||
public function setJob($job) {
|
||||
$this->job = $job;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static function fromArray($array) {
|
||||
$au = new Self();
|
||||
$au->setId($array["id"]);
|
||||
$au->setUsername($array["username"]);
|
||||
$au->setPassword($array["password"]);
|
||||
$au->setJob($array["job"]);
|
||||
return $au;
|
||||
}
|
||||
|
||||
public static function list($recent = true, $limit = 100) {
|
||||
$sort = $recent ? "DESC" : "ASC";
|
||||
$query = "SELECT * FROM author ORDER BY " . $sort . " LIMIT " . $limit;
|
||||
|
||||
$pdo = Functions::connect();
|
||||
$cats = $pdo->query($query)->fetchAll();
|
||||
|
||||
$res = array();
|
||||
|
||||
foreach ($cats as $cat) {
|
||||
$res[] = Author::fromArray($cat);
|
||||
}
|
||||
|
||||
return $res;
|
||||
}
|
||||
|
||||
public static function get(int $id) {
|
||||
return Author::fromArray(Functions::connect()->query("SELECT * FROM users WHERE id=" . $id)->fetch());
|
||||
}
|
||||
|
||||
public static function add(Author $author) {
|
||||
$query = "INSERT INTO author (id, username, password, job)
|
||||
VALUES (NULL, ':username', ':password', ':job');";
|
||||
|
||||
$pdo = Functions::connect();
|
||||
$prepared = $pdo->prepare($query);
|
||||
$prepared->bindParam(":username", $author->getUsername());
|
||||
$prepared->bindParam(":password", $author->getPassword());
|
||||
$prepared->bindParam(":job", $author->getjob());
|
||||
$prepared->execute();
|
||||
}
|
||||
|
||||
public static function remove(Author $author) {
|
||||
Functions::connect()->prepare("DELETE FROM author WHERE id=:id")->execute(array(":id" => $author->getId()));
|
||||
|
||||
}
|
||||
|
||||
public static function update(Author $author) {
|
||||
Functions::connect()->prepare("UPDATE author SET name=':name', password=':password', job=':job' WHERE id=:id")->execute(array(
|
||||
":username" => $author->getUsername(),
|
||||
":password" => $author->getPassword(),
|
||||
":job" => $author->getJob(),
|
||||
":id" => $author->getId()
|
||||
));
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
@ -1,99 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\DB;
|
||||
|
||||
use App\Functions;
|
||||
use PDO;
|
||||
|
||||
|
||||
class Categorie {
|
||||
|
||||
private $id;
|
||||
|
||||
private $name;
|
||||
|
||||
public function __construct($id, $name) {
|
||||
$this->id = $id;
|
||||
$this->name = $name;
|
||||
}
|
||||
|
||||
public static function fromArray($array) {
|
||||
return new Self($array["id"], $array["name"]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @param boolean $recent sort by most recent of less recent
|
||||
* @param int $limit
|
||||
*
|
||||
* @return Categorie[]
|
||||
*/
|
||||
public static function list($recent = true, $limit = 100) {
|
||||
$sort = $recent ? "DESC" : "ASC";
|
||||
$query = "SELECT * FROM categories ORDER BY " . $sort . " LIMIT " . $limit;
|
||||
|
||||
$pdo = Functions::connect();
|
||||
$cats = $pdo->query($query)->fetchAll();
|
||||
|
||||
$res = array();
|
||||
|
||||
foreach ($cats as $cat) {
|
||||
$res[] = new Categorie($cat["id"], $cat["name"]);
|
||||
}
|
||||
|
||||
return $res;
|
||||
}
|
||||
|
||||
public static function get(int $id) {
|
||||
return Categorie::fromArray(Functions::connect()->query("SELECT * FROM categories WHERE id=" . $id)->fetch());
|
||||
}
|
||||
|
||||
public static function add(Categorie $categorie) {
|
||||
$query = "INSERT INTO categories (id, name)
|
||||
VALUES (NULL, ':name');";
|
||||
|
||||
$pdo = Functions::connect();
|
||||
$prepared = $pdo->prepare($query);
|
||||
$prepared->bindParam(":name", $categorie->getName());
|
||||
$prepared->execute();
|
||||
}
|
||||
|
||||
public static function remove(Categorie $categorie) {
|
||||
Functions::connect()->prepare("DELETE FROM categories WHERE id=" . $categorie->getName())->execute();
|
||||
|
||||
}
|
||||
|
||||
public static function update(Categorie $categorie) {
|
||||
$query = ""
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the value of name
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the value of name
|
||||
*
|
||||
* @return self
|
||||
*/
|
||||
public function setName($name)
|
||||
{
|
||||
$this->name = $name;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value of id
|
||||
*/
|
||||
public function getId()
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
}
|
105
assets/php/db/Category.php
Normal file
105
assets/php/db/Category.php
Normal file
@ -0,0 +1,105 @@
|
||||
<?php
|
||||
|
||||
namespace App\DB;
|
||||
|
||||
use App\Functions;
|
||||
use PDO;
|
||||
|
||||
|
||||
class Category {
|
||||
|
||||
private $id;
|
||||
|
||||
private $name;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Get the value of name
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the value of name
|
||||
*
|
||||
* @return self
|
||||
*/
|
||||
public function setName($name)
|
||||
{
|
||||
$this->name = $name;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value of id
|
||||
*/
|
||||
public function getId()
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function setId($id) {
|
||||
$this->id = $id;
|
||||
}
|
||||
|
||||
public function __construct() {}
|
||||
|
||||
public static function fromArray($array) {
|
||||
$cat = new Category();
|
||||
$cat->setId($array["id"]);
|
||||
$cat->setName($array["name"]);
|
||||
return $cat;
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @param boolean $recent sort by most recent of less recent
|
||||
* @param int $limit
|
||||
*
|
||||
* @return Category[]
|
||||
*/
|
||||
public static function list($recent = true, $limit = 100) {
|
||||
$sort = $recent ? "DESC" : "ASC";
|
||||
$query = "SELECT * FROM categories ORDER BY " . $sort . " LIMIT " . $limit;
|
||||
|
||||
$pdo = Functions::connect();
|
||||
$cats = $pdo->query($query)->fetchAll();
|
||||
|
||||
$res = array();
|
||||
|
||||
foreach ($cats as $cat) {
|
||||
$res[] = Category::fromArray($cat);
|
||||
}
|
||||
|
||||
return $res;
|
||||
}
|
||||
|
||||
public static function get(int $id) {
|
||||
return Category::fromArray(Functions::connect()->query("SELECT * FROM categories WHERE id=" . $id)->fetch());
|
||||
}
|
||||
|
||||
public static function add(Category $categorie) {
|
||||
$query = "INSERT INTO categories (id, name)
|
||||
VALUES (NULL, ':name');";
|
||||
|
||||
$pdo = Functions::connect();
|
||||
$prepared = $pdo->prepare($query);
|
||||
$prepared->bindParam(":name", $categorie->getName());
|
||||
$prepared->execute();
|
||||
}
|
||||
|
||||
public static function remove(Category $categorie) {
|
||||
Functions::connect()->prepare("DELETE FROM categories WHERE id=:id")->execute(array(":id" => $categorie->getId()));
|
||||
|
||||
}
|
||||
|
||||
public static function update(Category $categorie) {
|
||||
Functions::connect()->prepare("UPDATE categorie SET name=:name WHERE id=:id")->execute(array(":name" => $categorie->getName(), ":id" => $categorie->getId()));
|
||||
}
|
||||
|
||||
}
|
@ -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()
|
||||
));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,57 +1,102 @@
|
||||
<?php
|
||||
|
||||
namespace App\DB;
|
||||
|
||||
use App\Functions;
|
||||
use PDO;
|
||||
|
||||
|
||||
class Tag {
|
||||
|
||||
private $id;
|
||||
|
||||
private $name;
|
||||
|
||||
public function __construct() {}
|
||||
|
||||
public static function fromArray($array) {
|
||||
$tag = new Tag();
|
||||
$tag->setId($array["id"]);
|
||||
$tag->setName($array["name"]);
|
||||
return $tag;
|
||||
}
|
||||
|
||||
/**
|
||||
* list of posts
|
||||
* Undocumented function
|
||||
*
|
||||
* @param boolean $recent sort by most recent or not
|
||||
* @param integer $limit limit the number of result
|
||||
* @param boolean $recent sort by most recent of less recent
|
||||
* @param int $limit
|
||||
*
|
||||
* @return array(Post)
|
||||
* @return Categorie[]
|
||||
*/
|
||||
public static function list($recent = true, $limit = 100) {
|
||||
$sort = $recent ? "DESC" : "ASC";
|
||||
$query = "SELECT * FROM tag ORDER BY " . $sort . " LIMIT " . $limit;
|
||||
|
||||
$pdo = Functions::connect();
|
||||
$cats = $pdo->query($query)->fetchAll();
|
||||
|
||||
$res = array();
|
||||
|
||||
foreach ($cats as $cat) {
|
||||
$res[] = Tag::fromArray($cat);
|
||||
}
|
||||
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* get a specific Post
|
||||
*
|
||||
* @param integer $id the id
|
||||
*
|
||||
* @return Post
|
||||
*/
|
||||
public static function get(int $id) {
|
||||
return Tag::fromArray(Functions::connect()->query("SELECT * FROM tag WHERE id=" . $id)->fetch());
|
||||
}
|
||||
|
||||
/**
|
||||
* add a post to the db
|
||||
*
|
||||
* @param Post $post
|
||||
*
|
||||
*/
|
||||
public static function add(Post $post) {
|
||||
}
|
||||
public static function add(Tag $tag) {
|
||||
$query = "INSERT INTO tag (id, name)
|
||||
VALUES (NULL, ':name');";
|
||||
|
||||
/**
|
||||
* remove the post
|
||||
*
|
||||
* @param Post $post
|
||||
*
|
||||
*/
|
||||
public static function remove(Post $post) {}
|
||||
|
||||
/**
|
||||
* update the post data on the db
|
||||
*
|
||||
* @param Post $post
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function update(Post $post) {}
|
||||
$pdo = Functions::connect();
|
||||
$prepared = $pdo->prepare($query);
|
||||
$prepared->bindParam(":name", $tag->getName());
|
||||
$prepared->execute();
|
||||
}
|
||||
|
||||
public static function remove(Tag $tag) {
|
||||
Functions::connect()->prepare("DELETE FROM tag WHERE id=:id")->execute(array(":id" => $tag->getId()));
|
||||
|
||||
}
|
||||
|
||||
public static function update(Tag $tag) {
|
||||
Functions::connect()->prepare("UPDATE tag SET name=:name WHERE id=:id")->execute(array(":name" => $tag->getName(), ":id" => $tag->getId()));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the value of name
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the value of name
|
||||
*
|
||||
* @return self
|
||||
*/
|
||||
public function setName($name)
|
||||
{
|
||||
$this->name = $name;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value of id
|
||||
*/
|
||||
public function getId()
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
public function setId($id) {
|
||||
$this->id = $id;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user