page de recher plus modifs vers un system de controllers

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

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\DB\Post;
use DOMXPath;
/**
* inputs <tag type="article" column="(voir les collones de la table post)/>
* return text
@ -37,5 +39,11 @@ class Article extends Tag {
$txt = $doc->createTextNode($post->$col());
$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();
$limit = (int) $el->getAttribute("limit");
$parent = $el->parentNode;
if($el->getAttribute("category") !== null) {
$posts = Post::listByCategory(Post::get($_GET["post"])->getCategory()->getId(), true, 6);
$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;
@ -34,9 +39,22 @@ class Loop extends Tag {
}
$parent = $el->parentNode;
//var_dump($parent);
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;

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

View File

@ -2,11 +2,14 @@
error_reporting(E_ALL);
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\Functions;
use App\Tags\Tag;
use App\Controller;
//renvoie vers le fichier css si il est demandé
if(Functions::endsWith($_GET["page"], ".css")) {
@ -50,7 +53,9 @@ if(strlen($_GET['page']) > 1) {
// A ENLEVER LORS DES COMMITS DE FIN
// var_dump($_GET);
if($_GET["page"] == "/test/") {
require_once "test.php";
// $controller = new Controller();
// echo $controller->getContent("/search", $loader);
die;
}
@ -58,14 +63,18 @@ if($_GET["page"] == "/test/") {
* 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
// //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
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)) {
mkdir($uploadFolder, 0660, true);
}
// $id = 1; //post id
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"])));
}
// $uploadFolder = "../../uploads/posts/$id/";
// if(!file_exists($uploadFolder)) {
// mkdir($uploadFolder, 0660, true);
// }
// 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 '
<script defer>
var processForm = () => {
var request = new XMLHttpRequest();
var form = new FormData();
form.append("photo", document.querySelector("#file").files[0]);
request.open("POST", "/test/", true);
request.send(form);
}
// echo '
// <script defer>
// var processForm = () => {
// var request = new XMLHttpRequest();
// var form = new FormData();
// form.append("photo", document.querySelector("#file").files[0]);
// request.open("POST", "/test/", true);
// request.send(form);
// }
document.querySelector("button").addEventListener("click", processForm);
</script>
';
// document.querySelector("button").addEventListener("click", processForm);
// </script>
// ';