finalisation de ma part :)

This commit is contained in:
2019-03-06 09:13:31 +01:00
parent 9df001d565
commit cc8ee929f8
13 changed files with 91 additions and 101 deletions

View File

@ -40,9 +40,9 @@ class Controller {
// return $instance->$function();
}
} elseif ($arr[0] === "editor" && isset($cl) && $cl == $class) {
if(!isset($_SESSION["author"]) || (isset($_SESSION["author"]) && ($_SESSION["author"]->getRole() != "ROLE_EDITOR" && $_SESSION["author"]->getRole() != "ROLE_ADMIN"))) header("Location: /login/?redirect=".$_SERVER["REQUEST_URI"]);
if(!isset($_SESSION["author"]) || (isset($_SESSION["author"]) && ($_SESSION["author"]->getRole() != "ROLE_EDITOR" && $_SESSION["author"]->getRole() != "ROLE_ADMIN"))) header("Location: /login/");
} elseif($arr[0] === "admin" && isset($cl) && $cl ==$class) {
if(!isset($_SESSION["author"]) || (isset($_SESSION["author"]) && $_SESSION["author"]->getRole() != "ROLE_ADMIN")) header("Location: /login/?redirect=".$_SERVER["REQUEST_URI"]);
if(!isset($_SESSION["author"]) || (isset($_SESSION["author"]) && $_SESSION["author"]->getRole() != "ROLE_ADMIN")) header("Location: /login/");
} elseif($arr[0] === "title" && isset($cl) && $cl == $class) {
array_shift($arr);
$_GET['page_title'] = join(" ", $arr);

View File

@ -19,7 +19,6 @@ class LoginController extends Controller {
$user = Author::getByUsername($_POST["username"]);
if($user->checkPassword($_POST["password"])) {
$_SESSION["author"] = $user;
if(isset($_GET["redirect"])) header("Location: " . $_GET["redirect"]);
header("Location: /");
}
else var_dump("login incorreect");

View File

@ -81,6 +81,15 @@ class PostController extends Controller {
return file_get_contents(DIR."/html/post.html");
}
/**
* @route /^\/post\/[0-9]+\/delete\/$/
* @title Article
*/
public function delete() {
Post::remove(Post::get($_GET["post"]));
header("Location: /");
}
/**
* @route /^\/post\/[0-9]+\/upload\/$/
*/

View File

@ -36,13 +36,35 @@ class UserController extends Controller {
}
/**
* @route /\/users\/edit\/[0-9]+\/$/
* @route /\/users\/[0-9]+\/edit\/$/
* @admin
* @title Modifier un utilisateur
*/
public function editUser() {
$_GET['edit_user'] = explode("/", $_GET["page"])[3];
return \file_get_contents(DIR."/html/user_edit.html");
var_dump($_POST);
$_GET['edit_user'] = explode("/", $_GET["page"])[2];
if(isset($_POST["username"]) && isset($_POST["password"]) && isset($_POST["job"]) && isset($_POST["role"])) {
$user = Author::get($_GET["edit_user"]);
$user->setUsername($_POST["username"]);
if($_POST["password"] != '') $user->setPassword($_POST["password"]);
$user->setRole($_POST["role"]);
var_dump($user);
Author::update($user);
// header("Location: /users/");
}
return file_get_contents(DIR."/html/user_edit.html");
}
/**
* @route /\/users\/[0-9]+\/delete\/$/
* @admin
* @title Modifier un utilisateur
*/
public function deleteUser() {
$_GET['edit_user'] = explode("/", $_GET["page"])[2];
Author::remove(Author::get($_GET["edit_user"]));
header("Location: /users/");
}

View File

@ -6,80 +6,41 @@ use App\DB\Category;
use DateTime;
use App\DB\Tag;
use App\DB\Post;
use App\DB\Author;
class UserEditor extends \App\Tags\Tag {
public function render() {
if($_GET["post"] == "new") $_GET["post"] = null;
elseif(isset($_GET["post"])) $post = Post::get($_GET["post"]);
$user = Author::get($_GET["edit_user"]);
//recuperation de la balise de base (<tag type="bold">pouet</tag>)
$pok = $this->getElement();
$el = $this->getElement();
//recuperation du document (necessaire a la création de balises
$doc = $this->getDoc();
$type = $pok->getAttribute("element");
$type = $el->getAttribute("element");
switch ($type) {
case 'role':
foreach (array("Utilisateur" => "ROLE_USER", "Editeur" =>"ROLE_EDITOR", "Admin" => "ROLE_ADMIN") as $key => $value) {
$opt = $doc->createElement("option");
$opt->setAttribute("value", $value);
if($user->getRole() == $value) $opt->setAttribute("selected", "selected");
$opt->appendChild($doc->createTextNode($key));
$el->parentNode->appendChild();
}
$option = $doc->createElement("option");
$text = $doc->createTextNode("Categorie");
$option->setAttribute("value", "1");
$option->setAttribute("disabled", "true");
if(!isset($post)) $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());
if(isset($post) && $post->getCategory()->getId() == $cat->getId()) $option->setAttribute("selected", "selected");
$pok->parentNode->insertBefore($option, $pok);
$el->parentNode->appendChild($opt);
}
break;
case 'datetime':
if(isset($post)) $txt = $post->getDateTime();
else $txt = (new DateTime())->format('d/m/Y H:i:s');
$pok->parentNode->insertBefore($doc->createTextNode($txt), $pok);
break;
case 'content':
$tarea = $doc->createElement("textarea");
if(isset($post)) $tarea->appendChild($doc->createTextNode($post->getContent()));
$tarea->setAttribute("style", "width: 100%; min-height: 200px");
$pok->parentNode->insertBefore($tarea, $pok);
break;
case 'title':
case 'username':
$input = $doc->createElement("input");
$input->setAttribute("style", "width: 100%");
$input->setAttribute("placeholder", "titre");
if(isset($post)) $input->setAttribute("value", $post->getTitle());
$pok->parentNode->insertBefore($input, $pok);
$input->setAttribute("value", $user->getUsername());
$input->setAttribute("name", "username");
$el->parentNode->insertBefore($input, $el);
break;
case 'job':
$input = $doc->createElement("input");
$input->setAttribute("value", $user->getJob());
$input->setAttribute("name", "job");
$el->parentNode->insertBefore($input, $el);
break;
case 'tags':
foreach (Tag::list() as $el) {
$tg = $doc->createElement("input");
$tg->setAttribute("id", $el->getId());
$tg->setAttribute("type", "checkbox");
$tg->setAttribute("data-text", $el->getName());
if(isset($post)) {
if(in_array($el, $post->getTags())) $tg->setAttribute("checked", "checked");
}
$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;

View File

@ -12,6 +12,7 @@ class Users extends Tag {
$parent = $el->parentNode;
foreach (Author::list(true, 1000) as $user) {
$pok = $el->childNodes->item(0)->cloneNode(true);
$pok->setAttribute("onclick", "window.location = window.location + '" . $user->getId() . "/edit/'");
$parent->insertBefore($pok, $el);

View File

@ -12,7 +12,7 @@ class Author {
private $password;
private $job;
private $job = "Aprenant";
private $role = "ROLE_USER";
@ -134,15 +134,16 @@ class Author {
}
public static function remove(Author $author) {
Functions::connect()->prepare("DELETE FROM author WHERE id=:id")->execute(array(":id" => $author->getId()));
Functions::connect()->prepare("DELETE FROM users 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(
Functions::connect()->prepare("UPDATE users SET username=:username, password=:password, job=:job, role=:role WHERE id=:id")->execute(array(
":username" => $author->getUsername(),
":password" => $author->getPassword(),
":job" => $author->getJob(),
":role" => $author->getRole(),
":id" => $author->getId()
));
}

View File

@ -286,7 +286,16 @@ class Post {
*
*/
public static function remove(Post $post) {
Functions::connect()->prepare("DELETE FROM posts WHERE id=:id")->execute(array(":id" => $post->getId()));
$id = $post->getId();
$prepared = Functions::connect()->prepare("DELETE FROM post_tag WHERE post_id=:id");
$prepared->bindValue(":id", $id, PDO::PARAM_INT);
$prepared->execute();
$prepared = Functions::connect()->prepare("DELETE FROM posts WHERE id=:id");
$prepared->bindValue(":id", $id, PDO::PARAM_INT);
$prepared->execute();
}