-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #59 from mathias73/dev
PR dev into master #4
- Loading branch information
Showing
22 changed files
with
635 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.