diff --git a/assets/html/includes/head.html b/assets/html/includes/head.html index 31e5d95..339df78 100644 --- a/assets/html/includes/head.html +++ b/assets/html/includes/head.html @@ -5,3 +5,4 @@ + diff --git a/assets/html/post_edit.html b/assets/html/post_edit.html new file mode 100644 index 0000000..e9f9185 --- /dev/null +++ b/assets/html/post_edit.html @@ -0,0 +1,84 @@ + + + + + + + +
+ +
+
+
+
+

+
+ + +
+

+ +

+
+
+

Auteur

+
+ + +
+

+

+
+
+
+
+ +
+

Plus d'articles

+

Catégories

+
dev
+
digi
+
ops
+
devops
+

Tags

+ +
+ +
+
Article précédent
+ +
+ +

Dans le même thème

+
+ + +
+

+ +

+
+ + + + + + +
+
+ +

+
+
+
+
+
+
+ + + + + diff --git a/assets/html/post_new.html b/assets/html/post_new.html new file mode 100644 index 0000000..70a64b0 --- /dev/null +++ b/assets/html/post_new.html @@ -0,0 +1,43 @@ + + + + + + + +
+ +
+
+
+
+

+ +

+
+ + + + +
+

+ +

+
+
+ +
+

Tags

+ +
+
+ + + + + diff --git a/assets/js/script.js b/assets/js/script.js index d5ec9af..dd93587 100644 --- a/assets/js/script.js +++ b/assets/js/script.js @@ -8,4 +8,57 @@ var processForm = () => { request.send(form); } -document.querySelector("button").addEventListener("click", processForm); \ No newline at end of file +// document.querySelector("button").addEventListener("click", processForm); + +var addTag = (element) => { + console.log(this); + console.log(element.target); + /** @var HTMLButtonElement btn */ + var btn = element.target; + var tag = document.createElement("input"); + tag.classList.add("add-tag"); + tag.style.width = "100%"; + var cancelBtn = document.createElement("button"); + cancelBtn.classList.add("cancelBtn"); + + cancelBtn.innerText = "Annuler"; + cancelBtn.addEventListener("click", function(element) { + var input = element.target.parentElement.querySelector(".add-tag"); + input.parentElement.querySelector(".addTag").innerText = "Add Tag"; + input.parentElement.removeChild(input); + element.target.parentElement.removeChild(element.target); + }); + + btn.removeEventListener("click", addTag); + btn.addEventListener("click", addingTag); + + btn.parentElement.insertBefore(tag, btn); + btn.parentElement.insertBefore(cancelBtn, btn); + + btn.innerText = "Ajouter le tag"; + +} + +var addingTag = (element) => { + var input = document.createElement("input"); + var uuid = Math.floor(Math.random() * Math.floor(10000)); + input.setAttribute("type", "checkbox"); + input.setAttribute("id", uuid); + + var label = document.createElement("label"); + label.setAttribute("for", uuid); + var addtag = element.target.parentElement.querySelector(".add-tag"); + label.innerText = addtag.value; + + element.target.parentElement.insertBefore(input, element.target); + element.target.parentElement.insertBefore(label, element.target); + + var cbtn = addtag.parentElement.querySelector(".cancelBtn"); + cbtn.parentElement.removeChild(cbtn); + addtag.parentElement.removeChild(addtag); + + element.target.removeEventListener("click", addingTag); + element.target.addEventListener("click", addTag); +} + +document.querySelector(".addTag").addEventListener("click", addTag); diff --git a/assets/php/Controller.php b/assets/php/Controller.php index 2ff9422..d3b970a 100644 --- a/assets/php/Controller.php +++ b/assets/php/Controller.php @@ -3,22 +3,34 @@ namespace App; use ReflectionClass; +use Composer\Autoload\ClassLoader; class Controller { - public function getContent(String $route, $loader) { - $map = array_filter(CLASSMAP, function($var) { + /** + * get content of controller + * + * @param String $route + * @param ClassLoader $loader + * + * @return String + */ + public function getContent(String $route, ClassLoader $loader) { + $map = array_filter($loader->getClassMap(), function($var) { return strpos($var, "App\Controller\\") === 0; }, ARRAY_FILTER_USE_KEY); foreach ($map as $key => $t) { $loader->loadClass($key); } + /** @var String $class */ foreach (get_declared_classes() as $class) { if(is_subclass_of( $class, 'App\Controller')) { $r = new ReflectionClass($class); foreach ($r->getMethods() as $method) { preg_match_all('#@(.*?)\n#s', $method->getDocComment(), $annotations); + /** @var String $annot */ foreach ($annotations[1] as $annot) { + /** @var String[] $arr */ $arr = preg_split("/ /", $annot); if($arr[0] === "route") { if(preg_match($arr[1], $route)) { diff --git a/assets/php/Controller/HomeController.php b/assets/php/Controller/HomeController.php index 0a0890e..70cebcd 100644 --- a/assets/php/Controller/HomeController.php +++ b/assets/php/Controller/HomeController.php @@ -2,7 +2,9 @@ namespace App\Controller; -class HomeController extends \App\Controller { +use App\Controller; + +class HomeController extends Controller { /** @@ -13,12 +15,30 @@ class HomeController extends \App\Controller { } /** - * @route /^\/post\/[a-z0-9]+\/*$/ + * @route /^\/post\/new\/*$/ + */ + public function postAdd() { + return file_get_contents(DIR."/html/post_new.html"); + } + + /** + * @route /^\/post\/[a-z0-9]+\/$/ */ public function post() { return file_get_contents(DIR."/html/post.html"); } + /** + * @route /^\/post\/[a-z0-9]+\/edit\/$/ + */ + public function postEdit() { + return file_get_contents(DIR."/html/post_edit.html"); + } + + + + + /** * @route /^\/search\// */ diff --git a/assets/php/Functions.php b/assets/php/Functions.php index 2e8daa4..028bdc2 100644 --- a/assets/php/Functions.php +++ b/assets/php/Functions.php @@ -5,14 +5,19 @@ namespace App; use DOMDocument; use DOMNode; use PDO; -use App\Router; +use PDOException; -/** - * @author Avior - * @author Clément Fourrier - */ class Functions { - public static function endsWith($haystack, $needle) { + + /** + * check the end of $needl + * + * @param String $haystack + * @param String $needle + * + * @return bool + */ + public static function endsWith(String $haystack, String $needle): bool { $length = strlen($needle); if ($length == 0) { return true; @@ -21,7 +26,13 @@ class Functions { return (substr($haystack, -$length) === $needle); } - public static function connect() { + /** + * Connect to Database + * + * @return PDO + */ + public static function connect(): PDO { + $host = "127.0.0.1"; $db = "blog"; $user = "blog"; @@ -31,23 +42,19 @@ class Functions { $dsn = "mysql:host=$host;dbname=$db;charset=$charset"; try { $pdo = new PDO($dsn, $user, $pass, null); - } catch (\PDOException $e) { - throw new \PDOException($e->getMessage(), (int)$e->getCode()); + } catch (PDOException $e) { + throw new PDOException($e->getMessage(), (int)$e->getCode()); } return $pdo; } /** - * Undocumented function + * Append HTML to DOMDocument * * @param DOMNode $parent - * @param [type] $source - * - * @var DOMNode $node - * - * @return void + * @param String $source */ - public static function appendHTML(DOMNode $parent, $source) { + public static function appendHTML(DOMNode $parent, String $source) { $tmpDoc = new DOMDocument("1.0", "UTF-8"); $html = ""; $html .= $source; @@ -66,84 +73,4 @@ class Functions { $parent->appendChild($importedNode); } } - - public static function loadRoutes() { - //recupé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 - */ - - $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 = Functions::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); - - } - } diff --git a/assets/php/Router.php b/assets/php/Router.php deleted file mode 100644 index 0a129f2..0000000 --- a/assets/php/Router.php +++ /dev/null @@ -1,49 +0,0 @@ -routeList[$route] = $page; - } - - //fonction de recherche d'une route par rapport a un texte - //return function - public function search($path) { - foreach ($this->routeList as $reg => $page) { - if(preg_match($reg, $path)) { - return $page; - } - } - return function () { - return file_get_contents("../html/404.html"); - }; - } -} diff --git a/assets/php/Tags/Editor.php b/assets/php/Tags/Editor.php new file mode 100644 index 0000000..d2d40f2 --- /dev/null +++ b/assets/php/Tags/Editor.php @@ -0,0 +1,71 @@ +pouet) + $pok = $this->getElement(); + //recuperation du document (necessaire a la création de balises + $doc = $this->getDoc(); + + $type = $pok->getAttribute("element"); + + switch ($type) { + case 'categories': + $option = $doc->createElement("option"); + $text = $doc->createTextNode("Categorie"); + $option->setAttribute("value", "-1"); + $option->setAttribute("disabled", "true"); + $option->setAttribute("selected", "selected"); + $option->appendChild($text); + $pok->parentNode->insertBefore($option, $pok); + foreach (Category::list() as $cat) { + $option = $doc->createElement("option"); + $text = $doc->createTextNode($cat->getName()); + $option->appendChild($text); + $option->setAttribute("value", $cat->getId()); + $pok->parentNode->insertBefore($option, $pok); + } + break; + case 'datetime': + $dt = new DateTime(); + $pok->parentNode->insertBefore($doc->createTextNode($dt->format('d/m/Y H:i:s')), $pok); + break; + case 'content': + $tarea = $doc->createElement("textarea"); + $tarea->setAttribute("style", "width: 100%; min-height: 200px"); + $pok->parentNode->insertBefore($tarea, $pok); + break; + case 'title': + $input = $doc->createElement("input"); + $input->setAttribute("style", "width: 100%"); + $input->setAttribute("placeholder", "titre"); + $pok->parentNode->insertBefore($input, $pok); + break; + case 'tags': + foreach (Tag::list() as $el) { + $tg = $doc->createElement("input"); + $tg->setAttribute("id", $el->getId()); + $tg->setAttribute("type", "checkbox"); + $txt = $doc->createElement("label"); + $txt->appendChild($doc->createTextNode($el->getName())); + $txt->setAttribute("for", $el->getId()); + $pok->parentNode->insertBefore($tg, $pok); + $pok->parentNode->insertBefore($txt, $pok); + } + $btn = $doc->createElement("button"); + $btn->appendChild($doc->createTextNode("Add Tag")); + $btn->setAttribute("class", "addTag"); + $pok->parentNode->insertBefore($btn, $pok); + default: + # code... + break; + } + } +} diff --git a/assets/php/Tags/Search.php b/assets/php/Tags/Search.php deleted file mode 100644 index 6c32b32..0000000 --- a/assets/php/Tags/Search.php +++ /dev/null @@ -1,122 +0,0 @@ -getTags() as $ptag) { - if($tag == $ptag->getId()) { - $tposts[] = $post; - } - } - } - $posts = $tposts; - } - - var_dump($posts); - - } -} - -class Loop extends Tag { - public function render() { - $el = $this->getElement(); - - $doc = $this->getDoc(); - - $limit = (int) $el->getAttribute("limit"); - $parent = $el->parentNode; - - $isRecent = isset($_GET["recent"]) && $_GET["recent"] == "false" ? false : true; - $category = isset($_GET["category"]) && intval($_GET["category"]) ? (int) $_GET["category"] : -1; - $tag = isset($_GET["tag"]) && intval($_GET["tag"]) ? (int) $_GET["tag"] : -1; - - if($el->getAttribute("category") != '') { - $posts = Post::listByCategory(Post::get($_GET["post"])->getCategory()->getId(), $isRecent, 6); - $postsList = array(); - foreach ($posts as $post) { - if($post->getId() != $_GET["post"]) $postsList[] = $post; - } - $posts = $postsList; - } else { - $posts = Post::list(true, 6); - - } - - if($category != -1) { - $posts = Post::listByCategory($category, $isRecent, 20); - } else { - $posts = Post::list($isRecent, 10); - } - if($tag != -1) { - $tposts = array(); - foreach ($posts as $post) { - foreach ($post->getTags() as $ptag) { - if($tag == $ptag->getId()) { - $tposts[] = $post; - } - } - } - $posts = $tposts; - } - - - $limit = $limit > count($posts) ? count($posts) : $limit; - - for ($i=0; $i < $limit; $i++) { - $pok = $el->childNodes->item(0)->cloneNode(true); - - $parent->insertBefore($pok, $el); - - $elements = $pok->getElementsByTagName("loop"); - - foreach ($elements as $ele) { - if($ele->getAttribute("column") == "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 { - $col = 'get' . ucfirst($ele->getAttribute("column")); - $txt = $doc->createTextNode($posts[$i]->$col()); - $ele->parentNode->insertBefore($txt, $ele); - } - } - - $finder = new DomXPath($doc); - $nodes = $finder->query("//*[contains(@class, 'column-cat')]"); - - 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"); - - while ($loop->count() >= 1) { - $loop->item(0)->parentNode->removeChild($loop->item(0)); - } - - - } - } -} diff --git a/assets/php/Tags/Tag.php b/assets/php/Tags/Tag.php index 0ae5db3..dabd963 100644 --- a/assets/php/Tags/Tag.php +++ b/assets/php/Tags/Tag.php @@ -6,11 +6,6 @@ use DOMDocument; use DOMElement; use DOMNode; -/** - * @author Avior - * - * @version 1.0.0 - */ class Tag { /** @var DOMDocument */ diff --git a/assets/php/Tags/Tags.php b/assets/php/Tags/Tags.php index abdc879..eaf9948 100644 --- a/assets/php/Tags/Tags.php +++ b/assets/php/Tags/Tags.php @@ -5,23 +5,18 @@ namespace App\Tags; use App\Tags\Tag; use App\DB\Post; -//ce tag est juste la pour donner les possibilité de mon composant -/** - * input test - * result test - */ class Tags extends Tag { public function render() { - //recuperation de la balise de base (pouet) + $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("href", "/search/?tag=" . $tag->getId()); $res->setAttribute("class", "tag"); $text = $doc->createTextNode($tag->getName()); $res->appendChild($text); diff --git a/assets/php/db/Category.php b/assets/php/db/Category.php index c6e47ae..0244cc5 100644 --- a/assets/php/db/Category.php +++ b/assets/php/db/Category.php @@ -65,7 +65,7 @@ class Category { */ public static function list($recent = true, $limit = 100) { $sort = $recent ? "DESC" : "ASC"; - $query = "SELECT * FROM categories ORDER BY " . $sort . " LIMIT " . $limit; + $query = "SELECT * FROM categories ORDER BY name " . $sort . " LIMIT " . $limit; $pdo = Functions::connect(); $cats = $pdo->query($query)->fetchAll(); diff --git a/assets/php/db/Tag.php b/assets/php/db/Tag.php index d7fcaaa..00e566a 100644 --- a/assets/php/db/Tag.php +++ b/assets/php/db/Tag.php @@ -31,7 +31,7 @@ class Tag { */ public static function list($recent = true, $limit = 100) { $sort = $recent ? "DESC" : "ASC"; - $query = "SELECT * FROM tag ORDER BY " . $sort . " LIMIT " . $limit; + $query = "SELECT * FROM tag ORDER BY name " . $sort . " LIMIT " . $limit; $pdo = Functions::connect(); $cats = $pdo->query($query)->fetchAll(); diff --git a/assets/php/handler.php b/assets/php/handler.php index f614007..b41aa0c 100644 --- a/assets/php/handler.php +++ b/assets/php/handler.php @@ -1,80 +1,72 @@ getClassMap()); -define("CLASSMAP", $loader->getClassMap()); -define("DIR", str_replace("/php", "", __DIR__)); use App\Router; use App\Functions; use App\Tags\Tag; use App\Controller; +error_reporting(E_ALL); +ini_set('display_errors', 'On'); + +/** @var Composer\Autoload\ClassLoader $loader */ +$loader = require "../../vendor/autoload.php"; + +define("DIR", str_replace("/php", "", __DIR__)); + //renvoie vers le fichier css si il est demandé if(Functions::endsWith($_GET["page"], ".css")) { - echo file_get_contents("../css/style.css"); + echo file_get_contents(DIR . "/css/style.css"); die; } -//renvoie vers le fichier js si demand� +//renvoie vers le fichier js si demandé if(Functions::endsWith($_GET["page"], ".js")) { - echo file_get_contents("../js/script.js"); + echo file_get_contents(DIR . "/js/script.js"); die; } -// var_dump(sizeof($_GET)); - // si page non / & finit pas par / at pas de ? -if($_GET["page"] != "" && !Functions::endsWith($_GET["page"], "/") && count($_GET) <= 1) { +if($_GET["page"] != "" && !Functions::endsWith($_GET["page"], "/") && count($_GET) <= 2) { header("Location: /".$_GET["page"]."/"); die; } -//enleve les / du début & fin +/** + * ex: /post/test/ => post/test + * utilité ? pouet simplefier la séparation pour les lignes d'après + */ $_GET['page'] = trim($_GET['page'], '/'); -// si taille supérieur à 1 $_getpost = element +/** + * ex: $_GET['page'] = post/test => $_GET['page'] = post/test & $_GET['post'] = test + */ if(count(explode("/", $_GET["page"])) > 1) { $_GET["post"] = explode("/", $_GET["page"])[1]; } -// $_get[page] = $_get[page][0] +/** + * changer le $_GET["page"] pour rajouter le slash avant + * ex : post/test => /post/test + */ $_GET['page'] = "/" . $_GET['page']; -// si len $_get[page] > 1 (mot ou autre) on rajoute le slash de fin +/** + * si page ne finit pas par / rajouter un / + * ex : /post/test => /post/test/ + * utilité ? afin de faire la recherche des controllers + */ if(strlen($_GET['page']) > 1) { $_GET['page'] = $_GET["page"] . "/"; } -// var_dump($_GET["page"]); - -//page de test pour des functions -// A ENLEVER LORS DES COMMITS DE FIN -// var_dump($_GET); +//debug if($_GET["page"] == "/test/") { - - // $controller = new Controller(); - // echo $controller->getContent("/search", $loader); + require "test.php"; die; } /** - * D�marrage du routage du contenu + * Démarrage du routage du contenu */ - - - -$router = new Router(); - -Functions::loadRoutes(); - $controller = new Controller(); echo Tag::loadTags($controller->getContent($_GET["page"], $loader)); - - -// //chargement des tags contenu sur la page - -// $pokemon = Tag::loadTags($router->search($_GET["page"])(), false); - -// echo $pokemon;