changements

This commit is contained in:
2019-03-01 16:42:42 +01:00
parent 246ae7582a
commit b339e09082
16 changed files with 632 additions and 321 deletions

View File

@ -5,6 +5,7 @@ namespace App;
use DOMDocument;
use DOMNode;
use PDO;
use App\Router;
/**
* @author Avior <florian.bouillon@delta-wings.net>

View File

@ -4,6 +4,7 @@ namespace App\Tags;
use App\Tags\Tag;
use App\Functions;
use App\DB\Post;
/**
* inputs <tag type="article" column="(voir les collones de la table post)/>
@ -12,11 +13,13 @@ use App\Functions;
class Article extends Tag {
public function render() {
$post = array( //testing purpose
'title'=> "test",
'url'=> "pokemongo",
'content'=> "<p>azerthjjhhdsqmlkjhgfd</p>"
);
// $post = array( //testing purpose
// 'title'=> "test",
// 'url'=> "pokemongo",
// 'content'=> "<p>azerthjjhhdsqmlkjhgfd</p>"
// );
$post = Post::get($_GET["post"]);
$pok = $this->getElement();
$attr = $pok->getAttribute("column");
@ -24,9 +27,14 @@ class Article extends Tag {
$doc = $this->getDoc();
if($attr == "content") {
Functions::appendHTML($pok->parentNode, $post[$attr]);
Functions::appendHTML($pok->parentNode, $post->getContent());
} elseif($attr == "category") {
$txt = $doc->createTextNode($post->getCategory()->getName());
$pok->parentNode->insertBefore($txt, $pok);
} else {
$txt = $doc->createTextNode($post[$attr]);
$col = "get" . ucfirst($attr);
$txt = $doc->createTextNode($post->$col());
$pok->parentNode->insertBefore($txt, $pok);
}
}

View File

@ -3,19 +3,16 @@
namespace App\Tags;
use App\Tags\Tag;
use App\DB\Post;
/**
* input <tag type="author" column="(column name)"/>
* return text
*/
class Author extends Tag {
class AuthorTag extends Tag {
public function render() {
$post = array( //testing purpose
'name'=> "test",
'image'=> "pokemon",
'linkedin'=> "pouet"
);
$author = Post::get($_GET["post"])->getAuthor();
@ -24,7 +21,9 @@ class Author extends Tag {
$doc = $this->getDoc();
$txt = $doc->createTextNode($post[$attr]);
$col = "get" . ucfirst($attr);
$txt = $doc->createTextNode($author->$col());
$pok->parentNode->insertBefore($txt, $pok);
}

View File

@ -6,6 +6,7 @@ use App\Tags\Tag;
use DomXPath;
use App\Functions;
use App\DB\Post;
use App\DB\Category;
/**
* input
@ -21,15 +22,18 @@ class Loop extends Tag {
$limit = (int) $el->getAttribute("limit");
$posts = Post::list($limit);
if($el->getAttribute("category") !== null) {
$posts = Post::listByCategory(Post::get($_GET["post"])->getCategory()->getId(), true, 6);
$postsList = array();
foreach ($posts as $post) {
if($post->getId() != $_GET["post"]) $postsList[] = $post;
}
$posts = $postsList;
} else {
$posts = Post::list(true, 6);
}
$pdo = Functions::connect();
$query = $pdo->query("SELECT title, categories.name as categorie, dt as date, short as content
FROM posts
INNER JOIN categories ON categories.id=posts.categorie
ORDER BY date DESC
LIMIT 6;");
$posts = $query->fetchAll();
$parent = $el->parentNode;
//var_dump($parent);
@ -46,9 +50,13 @@ class Loop extends Tag {
foreach ($elements as $ele) {
if($ele->getAttribute("column") == "content") {
Functions::appendHTML($ele->parentNode, $posts[$i]["content"]);
Functions::appendHTML($ele->parentNode, $posts[$i]->getShort());
} elseif($ele->getAttribute("column") == "category") {
$txt = $doc->createTextNode($posts[$i]->getCategory()->getName());
$ele->parentNode->insertBefore($txt, $ele);
} else {
$txt = $doc->createTextNode($posts[$i][$ele->getAttribute("column")]);
$col = 'get' . ucfirst($ele->getAttribute("column"));
$txt = $doc->createTextNode($posts[$i]->$col());
$ele->parentNode->insertBefore($txt, $ele);
}
}
@ -56,7 +64,13 @@ class Loop extends Tag {
$finder = new DomXPath($doc);
$nodes = $finder->query("//*[contains(@class, 'column-cat')]");
if(count($nodes) >= 1) $nodes[0]->setAttribute("class", str_replace("column-categorie", $posts[$i]["categorie"], $nodes[0]->getAttribute("class")));
if(count($nodes) >= 1) $nodes[0]->setAttribute("class", str_replace("column-category", $posts[$i]->getCategory()->getName() , $nodes[0]->getAttribute("class")));
$nodes = $finder->query("//*[contains(@class, 'column-link')]");
if(count($nodes) >= 1) $nodes[0]->setAttribute("href", "/post/".$posts[$i]->getId());
if(count($nodes) >= 1) $nodes[0]->setAttribute("class", str_replace("column-link", "", $nodes[0]->getAttribute("class")));
$loop = $pok->getElementsByTagName("loop");

31
assets/php/Tags/Tags.php Normal file
View File

@ -0,0 +1,31 @@
<?php
namespace App\Tags;
use App\Tags\Tag;
use App\DB\Post;
//ce tag est juste la pour donner les possibilité de mon composant
/**
* input <tag type="bold">test</tag>
* result <span style="font-weight: bold">test</span>
*/
class Tags extends Tag {
public function render() {
//recuperation de la balise de base (<tag type="bold">pouet</tag>)
$pok = $this->getElement();
//recuperation du document (necessaire a la création de balises
$doc = $this->getDoc();
//creation de la balise "span"
$post = Post::get($_GET["post"]);
/** @var \App\DB\Tag $tag */
foreach ($post->getTags() as $tag) {
$res = $doc->createElement("a");
$res->setAttribute("href", "/search?tag=" . $tag->getId());
$res->setAttribute("class", "tag");
$text = $doc->createTextNode($tag->getName());
$res->appendChild($text);
$pok->parentNode->insertBefore($res, $pok);
}
}
}

View File

@ -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()
));
}
}

View File

@ -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
View 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()));
}
}

View File

@ -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()
));
}
}

View File

@ -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;
}
}

View File

@ -8,7 +8,7 @@ use App\Router;
use App\Functions;
use App\Tags\Tag;
//renvoie vers le fichier css si il est demand<EFBFBD>
//renvoie vers le fichier css si il est demandé
if(Functions::endsWith($_GET["page"], ".css")) {
echo file_get_contents("../css/style.css");
die;
@ -28,10 +28,10 @@ if($_GET["page"] != "" && !Functions::endsWith($_GET["page"], "/") && count($_GE
die;
}
//enleve les / du d<EFBFBD>but & fin
//enleve les / du début & fin
$_GET['page'] = trim($_GET['page'], '/');
// si taille sup<EFBFBD>rieur <EFBFBD> 1 $_getpost = element
// si taille supérieur à 1 $_getpost = element
if(count(explode("/", $_GET["page"])) > 1) {
$_GET["post"] = explode("/", $_GET["page"])[1];
}

View File

@ -1,83 +0,0 @@
<?php
// // include_once "router.php";
// use App\Router;
// //recup<75>ration du router
// $router = Router::getRouter();
// $postCharacters = "[a-z0-9-]";
// //page d'accueil
// $home = function () {
// return file_get_contents("../html/index.html");
// };
// $router->addRoute("/^\/$/", $home); // route : "/"
// //page de post
// $post = function () {
// return file_get_contents("../html/post.html");
// };
// $router->addRoute("/^\/post\/" . $postCharacters . "+\/*$/", $post); // route "/post/*"
// //page de recherche
// $search = function () {
// return file_get_contents("../html/search.html");
// };
// $router->addRoute("/^\/search\/$/", $search); // route "/search/*"
// $edit = function() {
// $_POST = array_merge($_POST, $_GET); //debug uniquement
// var_dump($_POST);
// /*
// $_POST should contain
// post :
// id
// title
// content
// category
// author
// UPDATE posts
// SET
// title = title,
// url = strtolower(preg_replace(["/\ /", '/[\'\/~`\!@#\$%\^&\*\(\)\+=\{\}\[\]\|;:"\<\>,\.\?\\\]/'], ["_", ""], title));
// content = content,
// short = substr(content, 0, 253) . "...";
// category = categoryId
// author = authorId
// WHERE id = id
// */
// require_once "functions.php";
// $request = "UPDATE posts SET `title`=:title, `url`=:url, `content`=:content, `short`=:short, `category`=:category, `author`=:author, WHERE `id`=:id";
// $title = $_POST["title"];
// $url = strtolower(preg_replace(["/\ /", '/[\'\/~`\!@#\$%\^&\*\(\)\+=\{\}\[\]\|;:"\<\>,\.\?\\\]/'], ["_", ""], $title));
// $content = $_POST["content"];
// $short = substr($content, 0, 253) . "...";
// $category = intval($_POST["category"]);
// $author = intval($_POST["author"]);
// $id = intval($_POST["id"]);
// $pdo = connect();
// $prepared = $pdo->prepare($request);
// $prepared->bindParam(":title", $title);
// $prepared->bindParam(":url", $url);
// $prepared->bindParam(":content", $content);
// $prepared->bindParam(":short", $short);
// $prepared->bindParam(":category", $category, PDO::PARAM_INT);
// $prepared->bindParam(":author", $author, PDO::PARAM_INT);
// $prepared->bindParam(":id", $id, PDO::PARAM_INT);
// $prepared->execute();
// };
// $router->addRoute("/^\/post\/" . $postCharacters . "+\/edit\/*$/", $edit);