This commit is contained in:
Florian Bouillon 2019-03-03 00:05:08 +01:00
parent 401cb920d2
commit c2e310e5d9
8 changed files with 108 additions and 41 deletions

View File

@ -25,6 +25,9 @@
<p class="post text">
<tag type="editor" element="content"/>
</p>
<button class="bloc-plus submitPost">
Créer
</button>
</div>
</div>

View File

@ -41,15 +41,18 @@ var addTag = (element) => {
var addingTag = (element) => {
var input = document.createElement("input");
var uuid = Math.floor(Math.random() * Math.floor(10000));
var uuid = Math.floor(Math.random() * Math.floor(-1000000));
var addtag = element.target.parentElement.querySelector(".add-tag");
input.setAttribute("type", "checkbox");
input.setAttribute("id", uuid);
input.setAttribute("data-text", addTag.value);
var label = document.createElement("label");
label.setAttribute("for", uuid);
var addtag = element.target.parentElement.querySelector(".add-tag");
label.innerText = addtag.value;
input.setAttribute("data-text", label.innerText);
element.target.parentElement.insertBefore(input, element.target);
element.target.parentElement.insertBefore(label, element.target);
@ -62,3 +65,28 @@ var addingTag = (element) => {
}
document.querySelector(".addTag").addEventListener("click", addTag);
var submit = (el) => {
var major = document.querySelector('.post.text > textarea');
var title = document.querySelector("h2.title > input");
var category = document.querySelector("span.cat > select");
var tags = document.querySelectorAll("input[type='checkbox']:checked");
console.log(title.value);
console.log(category.value);
console.log(major.value);
console.log(tags);
var tglst = "";
tags.forEach(element => {
tglst += "," + element.getAttribute("id") + (element.hasAttribute("data-text") ? ":" + element.getAttribute("data-text") : "");
});
tglst = tglst.substr(1);
window.location.search = "title="+ title.value +"&category=" + category.value + "&content=" + major.value + "&tags=" + tglst;
//?title=$title&category=$category&content=$major&
}
document.querySelector(".submitPost").addEventListener("click", submit);

View File

@ -3,6 +3,8 @@
namespace App\Controller;
use App\Controller;
use App\DB\Post;
use App\DB\Tag;
class HomeController extends Controller {
@ -18,6 +20,34 @@ class HomeController extends Controller {
* @route /^\/post\/new\/*$/
*/
public function postAdd() {
// var_dump($_SESSION["author"]);
// die;
if(isset($_GET["title"]) && isset($_GET["category"]) && isset($_GET["content"]) && isset($_GET["tags"])) {
$post = new Post();
$post->setTitle($_GET["title"]);
$post->setContent($_GET["content"]);
$post->setCategory($_GET["category"]);
// $post->setAuthor();
$tags = explode(",", $_GET["tags"]);
$tgs = array();
foreach ($tags as $tag) {
$new_tag = explode(":", $tag);
if(count($new_tag) > 1) {
$t = new Tag();
$t->setName($new_tag[1]);
$tgs[] = Tag::add($t)->getId();
} else {
$tgs[] = $tag;
}
}
$post->setTags($tgs);
$post->setAuthor($_SESSION["author"]->getId());
Post::add($post);
}
return file_get_contents(DIR."/html/post_new.html");
}

View File

@ -20,7 +20,7 @@ class Editor extends \App\Tags\Tag {
case 'categories':
$option = $doc->createElement("option");
$text = $doc->createTextNode("Categorie");
$option->setAttribute("value", "-1");
$option->setAttribute("value", "0");
$option->setAttribute("disabled", "true");
$option->setAttribute("selected", "selected");
$option->appendChild($text);

View File

@ -70,7 +70,7 @@ class Author {
public static function list($recent = true, $limit = 100) {
$sort = $recent ? "DESC" : "ASC";
$query = "SELECT * FROM author ORDER BY " . $sort . " LIMIT " . $limit;
$query = "SELECT * FROM users ORDER BY id " . $sort . " LIMIT " . $limit;
$pdo = Functions::connect();
$cats = $pdo->query($query)->fetchAll();

View File

@ -3,6 +3,8 @@
namespace App\DB;
use App\Functions;
use DateTime;
use PDO;
class Post {
@ -11,12 +13,8 @@ class Post {
private $title;
private $url;
private $content;
private $short;
private $category;
private $author;
@ -34,19 +32,10 @@ class Post {
$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;
}
@ -75,18 +64,10 @@ class Post {
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);
}
@ -135,9 +116,7 @@ class Post {
$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"]);
@ -240,19 +219,33 @@ 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');";
$query = "INSERT INTO posts (id, title, content, categorie, author, dt)
VALUES (NULL, ':title', ':content', ':category', ':author', ':dt');";
$title = $post->getTitle());
$content = $post->getContent());
$category = $post->getCategory()->getId(), PDO::PARAM_INT);
$author = $post->getAuthor()->getId(), PDO::PARAM_INT);
$dt = (new DateTime())->format("d/m/Y h:i:s"));
$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();
$prepared->bindParam(":category", $post->getCategory()->getId(), PDO::PARAM_INT);
$prepared->bindParam(":author", $post->getAuthor()->getId(), PDO::PARAM_INT);
$prepared->bindParam(":dt", (new DateTime())->format("d/m/Y h:i:s"));
var_dump($prepared->execute(array(
":title" => $post->getTitle(),
":content" => $post->getContent(),
":category" => $post->getCategory()->getId(),
":author" => $post->getAuthor()->getId(),
":dt" => (new DateTime())->format("d/m/Y h:i:s"),
)));
var_dump("t");
}
@ -279,11 +272,9 @@ class 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(
Functions::connect()->prepare("UPDATE posts SET title=':title', content=':content', 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(),

View File

@ -49,14 +49,24 @@ class Tag {
return Tag::fromArray(Functions::connect()->query("SELECT * FROM tag WHERE id=" . $id)->fetch());
}
/**
* Undocumented function
*
* @param Tag $tag
*
* @return Tag
*/
public static function add(Tag $tag) {
$query = "INSERT INTO tag (id, name)
VALUES (NULL, ':name');";
VALUES (NULL, :name);";
// var_dump($tag);
$pdo = Functions::connect();
$prepared = $pdo->prepare($query);
$prepared->bindParam(":name", $tag->getName());
$prepared->execute();
$prepared->execute(array(":name" => $tag->getName()));
return Tag::list(true, 1)[0];
}
public static function remove(Tag $tag) {

View File

@ -4,6 +4,9 @@ use App\Router;
use App\Functions;
use App\Tags\Tag;
use App\Controller;
use App\DB\Author;
session_start();
error_reporting(E_ALL);
ini_set('display_errors', 'On');
@ -11,6 +14,8 @@ ini_set('display_errors', 'On');
/** @var Composer\Autoload\ClassLoader $loader */
$loader = require "../../vendor/autoload.php";
$_SESSION["author"] = Author::list(true, 1)[0];
define("DIR", str_replace("/php", "", __DIR__));
//renvoie vers le fichier css si il est demandé