parent
8118cfe188
commit
d0343eb5a9
@ -0,0 +1,35 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Silex\Controller;
|
||||||
|
|
||||||
|
use DateTime;
|
||||||
|
use Silex\DI\DI;
|
||||||
|
use Silex\Http\HttpResponse;
|
||||||
|
use Silex\Model\News;
|
||||||
|
|
||||||
|
class AdminController
|
||||||
|
{
|
||||||
|
public function publish(DI $di): HttpResponse
|
||||||
|
{
|
||||||
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||||
|
$news = new News(-1, $_POST['title'], $_POST['content'], new DateTime(), $di->getSecurity()->getCurrentUserId());
|
||||||
|
$di->getNewsGateway()->insert($news);
|
||||||
|
HttpResponse::redirect($di->getRouter()->url('news/' . $news->getId()));
|
||||||
|
}
|
||||||
|
$news = new News(-1, '', '', new DateTime(), $di->getSecurity()->getCurrentUserId());
|
||||||
|
return HttpResponse::found('edit', ['news' => $news]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function edit(DI $di, array $params): HttpResponse
|
||||||
|
{
|
||||||
|
$news = $di->getNewsGateway()->getById(intval($params['id']));
|
||||||
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||||
|
$news = new News($news->getId(), $_POST['title'], $_POST['content'], $news->getPublicationDate(), $news->getAuthorId());
|
||||||
|
$di->getNewsGateway()->update($news);
|
||||||
|
HttpResponse::redirect($di->getRouter()->url('news/' . $news->getId()));
|
||||||
|
}
|
||||||
|
return HttpResponse::found('edit', ['news' => $news]);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,28 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Silex\Controller;
|
||||||
|
|
||||||
|
use Silex\DI\DI;
|
||||||
|
use Silex\Http\HttpResponse;
|
||||||
|
use Silex\Router\Route;
|
||||||
|
|
||||||
|
class FrontController
|
||||||
|
{
|
||||||
|
private Route $route;
|
||||||
|
|
||||||
|
public function __construct(Route $route)
|
||||||
|
{
|
||||||
|
$this->route = $route;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function run(DI $di): HttpResponse
|
||||||
|
{
|
||||||
|
if ($this->route->getController() instanceof AdminController
|
||||||
|
&& ($di->getSecurity()->getCurrentUser() === null || !$di->getSecurity()->getCurrentUser()->isAdmin())) {
|
||||||
|
HttpResponse::redirect($di->getRouter()->url('login'));
|
||||||
|
}
|
||||||
|
return $this->route->call($di);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,20 @@
|
|||||||
|
<form action="<?= $_SERVER['REQUEST_URI'] ?>" method="post">
|
||||||
|
<div class="field">
|
||||||
|
<label class="label" for="title">Title</label>
|
||||||
|
<div class="control">
|
||||||
|
<input class="input" type="text" id="title" name="title" value="<?= $params['news']->getTitle() ?>">
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="field">
|
||||||
|
<label class="label" for="content">Content</label>
|
||||||
|
<div class="control">
|
||||||
|
<textarea class="textarea" id="content" name="content" rows="10"><?= $params['news']->getContent() ?></textarea>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="field">
|
||||||
|
<div class="control">
|
||||||
|
<button class="button is-link">Submit</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</form>
|
Loading…
Reference in new issue