page de recher plus modifs vers un system de controllers

This commit is contained in:
Florian Bouillon 2019-03-01 22:56:39 +01:00
parent b339e09082
commit 91806d4112
11 changed files with 294 additions and 54 deletions

View File

@ -48,7 +48,6 @@
</div> </div>
<div class="text"> <div class="text">
<loop column="content" /> <loop column="content" />
</p>
</div> </div>
</div> </div>
</a> </a>

View File

@ -10,7 +10,7 @@
</header> </header>
<main class="test container"> <main class="test container">
<div class="bloc-post"> <div class="bloc-post">
<div class="container-post post ops"> <div class="container-post post column-cat">
<h2 class="title"><tag type="article" column="title" /></h2> <h2 class="title"><tag type="article" column="title" /></h2>
<div class="etiquettes"> <div class="etiquettes">
<span class="cat"><tag type="article" column="category"/></span> <span class="cat"><tag type="article" column="category"/></span>
@ -51,7 +51,7 @@
<h3 id="titre-between">Dans le même thème</h3> <h3 id="titre-between">Dans le même thème</h3>
<div class="row articles"> <div class="row articles">
<tag type="loop" for="posts" limit="6"> <tag type="loop" for="posts" limit="6" category="true">
<a class="col-12 col-md-6 col-lg-4 column-link"> <a class="col-12 col-md-6 col-lg-4 column-link">
<div class="article column-category"> <div class="article column-category">
<h4 class="title"> <h4 class="title">

View File

@ -19,19 +19,24 @@
</div> </div>
</div> </div>
</div> </div>
<!-- <tag type="search" /> -->
<div class="bloc-post search"> <div class="bloc-post search">
<div class="container-post post ops"> <tag type="loop" limit="20">
<h2 class="title">Mettre en place un serveur web sous linux</h2> <div class="container-post post column-category">
<div class="etiquettes"> <h2 class="title">
<span class="cat">OPS</span> <loop column="title" />
<span class="date">26-06-2018</span> </h2>
<div class="etiquettes">
<span class="cat">
<loop column="category" /></span>
<span class="date">
<loop column="dateTime" /></span>
</div>
<p class="post text">
<loop column="content" />
</p>
</div> </div>
<p class="post text">Dum apud Persas, ut supra narravimus, perfidia regis motus agitat insperatos, </tag>
et in eois tractibus bella rediviva consurgunt, anno sexto decimo et eo diutius post Nepotiani exitium, saeviens
per urbem aeternam urebat cuncta Bellona, ex primordiis
</p>
</div>
</div> </div>
<div class="bloc-filter"> <div class="bloc-filter">

36
assets/php/Controller.php Normal file
View File

@ -0,0 +1,36 @@
<?php
namespace App;
use ReflectionClass;
class Controller {
public function getContent(String $route, $loader) {
$map = array_filter(CLASSMAP, function($var) {
return strpos($var, "App\Controller\\") === 0;
}, ARRAY_FILTER_USE_KEY);
foreach ($map as $key => $t) {
$loader->loadClass($key);
}
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);
foreach ($annotations[1] as $annot) {
$arr = preg_split("/ /", $annot);
if($arr[0] === "route") {
if(preg_match($arr[1], $route)) {
$instance = new $class();
$function = ($method->getName());
return $instance->$function();
}
}
}
}
}
}
}
}

View File

@ -0,0 +1,28 @@
<?php
namespace App\Controller;
class HomeController extends \App\Controller {
/**
* @route /^\/$/
*/
public function home() {
return file_get_contents(DIR."/html/index.html");
}
/**
* @route /^\/post\/[a-z0-9]+\/*$/
*/
public function post() {
return file_get_contents(DIR."/html/post.html");
}
/**
* @route /^\/search\//
*/
public function search() {
return file_get_contents(DIR."/html/search.html");
}
}

View File

@ -6,6 +6,8 @@ use App\Tags\Tag;
use App\Functions; use App\Functions;
use App\DB\Post; use App\DB\Post;
use DOMXPath;
/** /**
* inputs <tag type="article" column="(voir les collones de la table post)/> * inputs <tag type="article" column="(voir les collones de la table post)/>
* return text * return text
@ -37,5 +39,11 @@ class Article extends Tag {
$txt = $doc->createTextNode($post->$col()); $txt = $doc->createTextNode($post->$col());
$pok->parentNode->insertBefore($txt, $pok); $pok->parentNode->insertBefore($txt, $pok);
} }
$finder = new DomXPath($doc);
$nodes = $finder->query("//*[contains(@class, 'column-cat')]");
if(count($nodes) >= 1) $nodes[0]->setAttribute("class", str_replace("column-cat", $post->getCategory()->getName() , $nodes[0]->getAttribute("class")));
} }
} }

View File

@ -21,9 +21,14 @@ class Loop extends Tag {
$doc = $this->getDoc(); $doc = $this->getDoc();
$limit = (int) $el->getAttribute("limit"); $limit = (int) $el->getAttribute("limit");
$parent = $el->parentNode;
if($el->getAttribute("category") !== null) { $isRecent = isset($_GET["recent"]) && $_GET["recent"] == "false" ? false : true;
$posts = Post::listByCategory(Post::get($_GET["post"])->getCategory()->getId(), true, 6); $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(); $postsList = array();
foreach ($posts as $post) { foreach ($posts as $post) {
if($post->getId() != $_GET["post"]) $postsList[] = $post; if($post->getId() != $_GET["post"]) $postsList[] = $post;
@ -34,9 +39,22 @@ class Loop extends Tag {
} }
if($category != -1) {
$parent = $el->parentNode; $posts = Post::listByCategory($category, $isRecent, 20);
//var_dump($parent); } 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; $limit = $limit > count($posts) ? count($posts) : $limit;

122
assets/php/Tags/Search.php Normal file
View File

@ -0,0 +1,122 @@
<?php
namespace App\Tags;
use App\Tags\Tag;
use App\DB\Post;
class Search extends Tag {
public function render() {
$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($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;
}
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));
}
}
}
}

View File

@ -99,9 +99,12 @@ class Post {
return $this->dt; return $this->dt;
} }
/**
*
* @return \App\DB\Tag[]
*/
public function getTags() { public function getTags() {
$temp = array(); $temp = array();
if ($this->tags == null) return $temp; if ($this->tags == null) return $temp;
foreach ($this->tags as $tag) { foreach ($this->tags as $tag) {
$temp[] = Tag::get($tag); $temp[] = Tag::get($tag);
@ -160,7 +163,17 @@ class Post {
$res = array(); $res = array();
foreach ($posts as $post) { foreach ($posts as $post) {
$res[] = Post::fromArray($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; return $res;

View File

@ -2,11 +2,14 @@
error_reporting(E_ALL); error_reporting(E_ALL);
ini_set('display_errors', 'On'); ini_set('display_errors', 'On');
require_once "../../vendor/autoload.php"; $loader = require "../../vendor/autoload.php";
// var_dump($loader->getClassMap());
define("CLASSMAP", $loader->getClassMap());
define("DIR", str_replace("/php", "", __DIR__));
use App\Router; use App\Router;
use App\Functions; use App\Functions;
use App\Tags\Tag; use App\Tags\Tag;
use App\Controller;
//renvoie vers le fichier css si il est demandé //renvoie vers le fichier css si il est demandé
if(Functions::endsWith($_GET["page"], ".css")) { if(Functions::endsWith($_GET["page"], ".css")) {
@ -50,7 +53,9 @@ if(strlen($_GET['page']) > 1) {
// A ENLEVER LORS DES COMMITS DE FIN // A ENLEVER LORS DES COMMITS DE FIN
// var_dump($_GET); // var_dump($_GET);
if($_GET["page"] == "/test/") { if($_GET["page"] == "/test/") {
require_once "test.php";
// $controller = new Controller();
// echo $controller->getContent("/search", $loader);
die; die;
} }
@ -58,14 +63,18 @@ if($_GET["page"] == "/test/") {
* D<EFBFBD>marrage du routage du contenu * D<EFBFBD>marrage du routage du contenu
*/ */
$router = new Router(); $router = new Router();
Functions::loadRoutes(); Functions::loadRoutes();
$controller = new Controller();
echo Tag::loadTags($controller->getContent($_GET["page"], $loader));
//chargement des tags contenu sur la page // //chargement des tags contenu sur la page
$pokemon = Tag::loadTags($router->search($_GET["page"])(), false); // $pokemon = Tag::loadTags($router->search($_GET["page"])(), false);
echo $pokemon; // echo $pokemon;

View File

@ -1,38 +1,40 @@
<?php <?php
if(isset($_GET["image"]) && !empty($_GET["image"])) $_POST["image"] = $_GET["image"];
$id = 1; //post id
$uploadFolder = "../../uploads/posts/$id/"; // if(isset($_GET["image"]) && !empty($_GET["image"])) $_POST["image"] = $_GET["image"];
if(!file_exists($uploadFolder)) { // $id = 1; //post id
mkdir($uploadFolder, 0660, true);
}
if(isset($_FILES["photo"]) && !empty($_FILES["photo"])) { // $uploadFolder = "../../uploads/posts/$id/";
var_dump($_FILES["photo"]);
move_uploaded_file($_FILES["photo"]["tmp_name"], $uploadFolder.$_FILES["photo"]["name"]); // if(!file_exists($uploadFolder)) {
// require_once "functions.php"; // mkdir($uploadFolder, 0660, true);
// file_put_contents($uploadFolder."/pouet.jpg", base64_decode($_POST["image"])); // }
// base64_to_jpeg($_POST["image"], $uploadFolder."/pouet.jpg");
// file_put_content($uploadFolder."/pouet.jpg", base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $_POST["image"]))); // if(isset($_FILES["photo"]) && !empty($_FILES["photo"])) {
} // var_dump($_FILES["photo"]);
// move_uploaded_file($_FILES["photo"]["tmp_name"], $uploadFolder.$_FILES["photo"]["name"]);
// // require_once "functions.php";
// // file_put_contents($uploadFolder."/pouet.jpg", base64_decode($_POST["image"]));
// // base64_to_jpeg($_POST["image"], $uploadFolder."/pouet.jpg");
// // file_put_content($uploadFolder."/pouet.jpg", base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $_POST["image"])));
// }
echo '<input id="file" type="file" multiple/><button>Send !</button>'; // echo '<input id="file" type="file" multiple/><button>Send !</button>';
echo ' // echo '
<script defer> // <script defer>
var processForm = () => { // var processForm = () => {
var request = new XMLHttpRequest(); // var request = new XMLHttpRequest();
var form = new FormData(); // var form = new FormData();
form.append("photo", document.querySelector("#file").files[0]); // form.append("photo", document.querySelector("#file").files[0]);
request.open("POST", "/test/", true); // request.open("POST", "/test/", true);
request.send(form); // request.send(form);
} // }
document.querySelector("button").addEventListener("click", processForm); // document.querySelector("button").addEventListener("click", processForm);
</script> // </script>
'; // ';