Skip to content

Commit

Permalink
Merge pull request #59 from mathias73/dev
Browse files Browse the repository at this point in the history
PR dev into master #4
  • Loading branch information
mathiiii-dev authored Oct 29, 2020
2 parents 67323e0 + 99ace89 commit 78c1905
Show file tree
Hide file tree
Showing 22 changed files with 635 additions and 46 deletions.
2 changes: 1 addition & 1 deletion .htaccess
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/js/$
RewriteCond %{REQUEST_URI} !^/(js|css)/$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
12 changes: 12 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

require 'vendor/autoload.php';

session_start();

define('ROOT_DIR', basename(dirname(__FILE__)));

$router = new Router($_GET['url']);
$router->get('/', 'Home#show');
$router->get('/sign-up', 'Signup#show');
Expand All @@ -12,4 +16,12 @@
$router->post('/signIn', 'Signin#signIn');
$router->post('/sendmail', 'Mail#sendMail');
$router->get('/exit', 'Signin#disconnect');
$router->get('/posts', 'Posts#showAllPosts');
$router->get('/posts/:id', 'Posts#show');
$router->get('/create-post', 'Posts#showCreatePost');
$router->post('/createPost', 'Posts#createPost');
$router->get('/modify-post/:id', 'Posts#showModifyPost');
$router->post('/modifyPost/:id', 'Posts#modifyPost');
$router->get('/delete-post/:id', 'Posts#deletePost');

$router->run();
153 changes: 153 additions & 0 deletions src/Controller/PostsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
<?php

namespace App\Controller;

use App\Model\Post;
use App\Model\PostsManager;
use App\Model\Repository\PostRepository;
use App\Model\Repository\UserRepository;
use App\Model\Twig;
use Composer\Command\ValidateCommand;

class PostsController extends Twig
{
public function show($id, string $filter = null)
{
$post = new PostRepository();
$postInfo = $post->getPostById($id);
$cookie = $_COOKIE['auth'] ?? null;
$cookie = explode('-----', $cookie);
if (!$postInfo) {
http_response_code(404);
return $this->twig('404.html.twig', ['' => '' . $filter . '']);
}
$userName = $post->getUserForAPost($id);
$date = date_create($postInfo['createdAt']);
$dateFormat = date_format($date, 'd/m/Y');
$this->twig('post.html.twig',
[
'erreur' => $filter,
'title' => $postInfo['title'],
'lead' => $postInfo['lead'],
'content' => $postInfo['content'],
'createdAt' => $dateFormat,
'firstname' => $userName['firstname'],
'idPost' => $id,
'idUserSession' => $_SESSION['id'] ?? $cookie[0],
'idUserPost' => $postInfo['idUser']
]);
}

public function showAllPosts(string $filter = null)
{
$this->twig('posts.html.twig', ['' => '' . $filter . '']);
}

public function showCreatePost(string $filter = null)
{
$cookie = $_COOKIE['auth'] ?? null;
$cookie = explode('-----', $cookie);
if (empty($cookie[0]) && empty($_SESSION['id'])) {
http_response_code(500);
return $this->twig('500.html.twig', ['' => '' . $filter . '']);
}
$this->twig('createPost.html.twig', ['erreur' => '' . $filter . '']);
}

public function createPost()
{
$cookie = $_COOKIE['auth'] ?? null;
$cookie = explode('-----', $cookie);
$post = new Post([
'title' => $_POST['title'],
'lead' => $_POST['lead'],
'content' => $_POST['content'],
'createdAt' => date('y-m-d'),
'idUser' => $_SESSION['id'] ?? $cookie[0],
'isValid' => 0
]);
$postManager = new PostsManager();
if (!$postManager->isNotEmpty($post)) {
return $this->showCreatePost('Veuillez remplir tout les champs');
}
if (!$postManager->checkLength(50, $_POST['title'])) {
return $this->showCreatePost('Le titre est trop long');
}
if (!$postManager->checkLength(100, $_POST['lead'])) {
return $this->showCreatePost('Le chapô est trop long');
} else {
$postRepo = new PostRepository();
$postRepo->addPost($post);
header('Location: /Blog/posts');
}

}

public function showModifyPost($id)
{
$post = new PostRepository();
$postInfo = $post->getPostById($id);
$cookie = $_COOKIE['auth'] ?? null;
$cookie = explode('-----', $cookie);
if (empty($cookie[0]) && empty($_SESSION['id']) || $postInfo['idUser'] != $_SESSION['id'] ?? $cookie[0]) {
http_response_code(500);
$this->twig('500.html.twig', ['' => '']);
} else {
$title = $postInfo['title'];
$lead = $postInfo['lead'];
$content = $postInfo['content'];
$this->twig('modifyPost.html.twig', ['title' => '' . $title . '', 'lead' => '' . $lead . '', 'content' => '' . $content . '', 'idPost' => '' . $id . '']);
}
}

public function modifyPost(int $id)
{
$postRepo = new PostRepository();
$infoPost = $postRepo->getPostById($id);
$cookie = $_COOKIE['auth'] ?? null;
$cookie = explode('-----', $cookie);
if ($infoPost['idUser'] != $_SESSION['id'] ?? $cookie[0]) {
http_response_code(500);
$this->twig('500.html.twig', ['' => '']);
} else {
$post = new Post([
'title' => $_POST['title'],
'lead' => $_POST['lead'],
'content' => $_POST['content'],
'updatedAt' => date('y-m-d'),
'idUser' => $infoPost['idUser'],
'createdAt' => $infoPost['createdAt'],
'isValid' => 0
]);
$postManager = new PostsManager();
if (!$postManager->isNotEmpty($post)) {
return $this->showCreatePost('Veuillez remplir tout les champs');
}
if (!$postManager->checkLength(50, $_POST['title'])) {
return $this->showCreatePost('Le titre est trop long');
}
if (!$postManager->checkLength(100, $_POST['lead'])) {
return $this->showCreatePost('Le chapô est trop long');
} else {
$postRepo->modifyPost($id, $post);
header('Location: /Blog/posts');
}
}
}

public function deletePost(int $id)
{
$post = new PostRepository();
$postInfo = $post->getPostById($id);
$cookie = $_COOKIE['auth'] ?? null;
$cookie = explode('-----', $cookie);
if (empty($cookie[0]) && empty($_SESSION['id']) || $postInfo['idUser'] != $_SESSION['id'] ?? $cookie[0]) {
http_response_code(500);
$this->twig('500.html.twig', ['' => '']);
} else {
$postRepo = new PostRepository();
$postRepo->deletePost($id);
header('Location: /Blog/posts');
}
}
}
6 changes: 2 additions & 4 deletions src/Controller/SigninController.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,13 @@ public function signIn() : void
}
else{
$userManager->connectUser($user);
$home = new HomeController();
$home->show();
header('Location: /Blog');
}
}

public function disconnect(){
$userManager = new UserManager();
$userManager->userDisconnect();
$home = new HomeController();
$home->show();
header('Location: /Blog');
}
}
4 changes: 1 addition & 3 deletions src/Controller/SignupController.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ public function signUp() : void
}else{
$userRepo = new UserRepository();
$userRepo->addUser($user);
$signin = new SigninController();
$signin->show();
header('Location: sign-in');
}

}
}
94 changes: 94 additions & 0 deletions src/Model/Post.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

namespace App\Model;

class Post extends Model
{
protected int $id, $idUser, $isValid;
protected string $title, $lead, $content, $createdAt, $updatedAt;

public function __construct(array $data)
{
$this->hydrate($data);
}

public function getId() : int
{
return $this->id;
}

public function setId($id)
{
$this->id = $id;
}

public function getIdUser() : int
{
return $this->idUser;
}

public function setIdUser($idUser)
{
$this->idUser = $idUser;
}

public function getIsValid() : int
{
return $this->isValid;
}

public function setIsValid($isValid)
{
$this->isValid = $isValid;
}

public function getTitle() : string
{
return $this->title;
}

public function setTitle($title)
{
$this->title = $title;
}

public function getLead() : string
{
return $this->lead;
}

public function setLead($lead)
{
$this->lead = $lead;
}

public function getContent() : string
{
return $this->content;
}

public function setContent($content)
{
$this->content = $content;
}

public function getCreatedAt() : string
{
return $this->createdAt;
}

public function setCreatedAt($createdAt)
{
$this->createdAt = $createdAt;
}

public function getUpdateAt() : string
{
return $this->updatedAt;
}

public function setUpdatedAt($updatedAt)
{
$this->updatedAt = $updatedAt;
}
}
30 changes: 30 additions & 0 deletions src/Model/PostsManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace App\Model;

use App\Controller\PostsController;

class PostsManager
{
public function isNotEmpty(Post $post) : bool
{
$title = $post->getTitle();
$lead = $post->getLead();
$content = $post->getContent();
$createdAt = $post->getCreatedAt();
$idUser = $post->getIdUser();
$isValid = $post->getIsValid();
if (empty($title) && empty($lead) && empty($content) && empty($createdAt)) {
return false;
}
return true;
}

public function checkLength(int $length, string $input) : bool
{
if (strlen($input) > $length){
return false;
}
return true;
}
}
Loading

0 comments on commit 78c1905

Please sign in to comment.