2015-05-22 09:13:52 +00:00
|
|
|
<?php
|
2020-10-14 22:19:31 +00:00
|
|
|
|
2021-07-19 05:53:45 +00:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2018-08-04 22:05:14 +00:00
|
|
|
namespace App\Controller\Admin;
|
2015-05-22 09:13:52 +00:00
|
|
|
|
2018-08-04 22:05:14 +00:00
|
|
|
use App\Entity;
|
2019-09-29 07:50:24 +00:00
|
|
|
use App\Entity\Repository\StationRepository;
|
2019-09-10 16:40:31 +00:00
|
|
|
use App\Exception\NotFoundException;
|
2019-08-07 04:33:55 +00:00
|
|
|
use App\Form;
|
2019-08-09 15:00:21 +00:00
|
|
|
use App\Http\Response;
|
|
|
|
use App\Http\ServerRequest;
|
2020-02-06 02:35:13 +00:00
|
|
|
use App\Session\Flash;
|
2021-07-06 11:36:09 +00:00
|
|
|
use DI\FactoryInterface;
|
2018-11-17 15:30:28 +00:00
|
|
|
use Psr\Http\Message\ResponseInterface;
|
2015-05-22 09:13:52 +00:00
|
|
|
|
2019-04-14 02:05:42 +00:00
|
|
|
class StationsController extends AbstractAdminCrudController
|
2015-05-22 09:13:52 +00:00
|
|
|
{
|
2019-09-29 07:50:24 +00:00
|
|
|
public function __construct(
|
2021-04-23 05:24:12 +00:00
|
|
|
protected StationRepository $stationRepo,
|
2021-07-06 11:36:09 +00:00
|
|
|
protected FactoryInterface $factory
|
2019-09-29 07:50:24 +00:00
|
|
|
) {
|
2021-07-06 11:36:09 +00:00
|
|
|
parent::__construct($factory->make(Form\StationForm::class));
|
2019-04-09 09:06:44 +00:00
|
|
|
|
2019-04-14 02:05:42 +00:00
|
|
|
$this->csrf_namespace = 'admin_stations';
|
2017-08-17 18:28:48 +00:00
|
|
|
}
|
|
|
|
|
2019-08-09 15:00:21 +00:00
|
|
|
public function __invoke(ServerRequest $request, Response $response): ResponseInterface
|
2015-05-22 09:13:52 +00:00
|
|
|
{
|
2019-09-29 07:50:24 +00:00
|
|
|
$stations = $this->stationRepo->fetchArray(false, 'name');
|
2018-02-01 11:49:40 +00:00
|
|
|
|
2019-08-09 15:00:21 +00:00
|
|
|
return $request->getView()->renderToResponse($response, 'admin/stations/index', [
|
2018-02-01 11:49:40 +00:00
|
|
|
'stations' => $stations,
|
2019-09-24 02:22:08 +00:00
|
|
|
'csrf' => $request->getCsrf()->generate($this->csrf_namespace),
|
2018-02-01 11:49:40 +00:00
|
|
|
]);
|
2015-05-22 09:13:52 +00:00
|
|
|
}
|
2017-01-24 00:35:16 +00:00
|
|
|
|
2021-07-19 05:53:45 +00:00
|
|
|
public function editAction(ServerRequest $request, Response $response, int $id = null): ResponseInterface
|
2015-05-22 09:13:52 +00:00
|
|
|
{
|
2020-10-14 22:19:31 +00:00
|
|
|
if (false !== $this->doEdit($request, $id)) {
|
2019-09-24 02:22:08 +00:00
|
|
|
$request->getFlash()->addMessage(($id ? __('Station updated.') : __('Station added.')), Flash::SUCCESS);
|
2021-07-19 05:53:45 +00:00
|
|
|
return $response->withRedirect((string)$request->getRouter()->named('admin:stations:index'));
|
2015-05-22 09:13:52 +00:00
|
|
|
}
|
|
|
|
|
2021-07-19 05:53:45 +00:00
|
|
|
return $request->getView()->renderToResponse(
|
|
|
|
$response,
|
|
|
|
'admin/stations/edit',
|
|
|
|
[
|
|
|
|
'form' => $this->form,
|
|
|
|
'title' => $id ? __('Edit Station') : 'Add Station',
|
|
|
|
]
|
|
|
|
);
|
2015-05-22 09:13:52 +00:00
|
|
|
}
|
2017-01-24 00:35:16 +00:00
|
|
|
|
2021-07-19 05:53:45 +00:00
|
|
|
public function deleteAction(
|
|
|
|
ServerRequest $request,
|
|
|
|
Response $response,
|
|
|
|
int $id,
|
|
|
|
string $csrf
|
|
|
|
): ResponseInterface {
|
2019-09-24 02:22:08 +00:00
|
|
|
$request->getCsrf()->verify($csrf, $this->csrf_namespace);
|
2018-02-07 12:16:29 +00:00
|
|
|
|
2021-07-19 05:53:45 +00:00
|
|
|
$record = $this->record_repo->find($id);
|
2017-08-17 18:28:48 +00:00
|
|
|
if ($record instanceof Entity\Station) {
|
2019-09-29 07:50:24 +00:00
|
|
|
$this->stationRepo->destroy($record);
|
2016-09-27 21:54:19 +00:00
|
|
|
}
|
2017-01-24 00:35:16 +00:00
|
|
|
|
2019-09-24 02:22:08 +00:00
|
|
|
$request->getFlash()->addMessage(__('Station deleted.'), Flash::SUCCESS);
|
2021-07-19 05:53:45 +00:00
|
|
|
return $response->withRedirect((string)$request->getRouter()->named('admin:stations:index'));
|
2015-05-22 09:13:52 +00:00
|
|
|
}
|
2019-04-09 09:06:44 +00:00
|
|
|
|
2021-07-19 05:53:45 +00:00
|
|
|
public function cloneAction(ServerRequest $request, Response $response, int $id): ResponseInterface
|
2019-04-09 09:06:44 +00:00
|
|
|
{
|
2021-07-06 11:36:09 +00:00
|
|
|
$cloneForm = $this->factory->make(Form\StationCloneForm::class);
|
|
|
|
|
2021-07-19 05:53:45 +00:00
|
|
|
$record = $this->record_repo->find($id);
|
2019-04-09 09:06:44 +00:00
|
|
|
if (!($record instanceof Entity\Station)) {
|
2019-09-10 16:40:31 +00:00
|
|
|
throw new NotFoundException(__('Station not found.'));
|
2019-04-09 09:06:44 +00:00
|
|
|
}
|
|
|
|
|
2021-07-06 11:36:09 +00:00
|
|
|
if (false !== $cloneForm->process($request, $record)) {
|
2019-09-24 02:22:08 +00:00
|
|
|
$request->getFlash()->addMessage(__('Changes saved.'), Flash::SUCCESS);
|
2021-07-19 05:53:45 +00:00
|
|
|
return $response->withRedirect((string)$request->getRouter()->named('admin:stations:index'));
|
2019-04-09 09:06:44 +00:00
|
|
|
}
|
|
|
|
|
2021-07-06 11:36:09 +00:00
|
|
|
return $request->getView()->renderToResponse(
|
|
|
|
$response,
|
|
|
|
'system/form_page',
|
|
|
|
[
|
2021-07-19 05:53:45 +00:00
|
|
|
'form' => $cloneForm,
|
|
|
|
'render_mode' => 'edit',
|
|
|
|
'title' => __('Clone Station: %s', $record->getName()),
|
2021-07-06 11:36:09 +00:00
|
|
|
]
|
|
|
|
);
|
2019-04-09 09:06:44 +00:00
|
|
|
}
|
2018-08-04 22:05:14 +00:00
|
|
|
}
|