2019-04-14 04:05:42 +02:00
|
|
|
<?php
|
2020-10-15 00:19:31 +02:00
|
|
|
|
2021-07-19 07:53:45 +02:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2019-04-14 04:05:42 +02:00
|
|
|
namespace App\Controller\Admin;
|
|
|
|
|
2019-09-10 18:40:31 +02:00
|
|
|
use App\Exception\NotFoundException;
|
2019-04-14 04:05:42 +02:00
|
|
|
use App\Form\EntityForm;
|
2019-08-09 17:00:21 +02:00
|
|
|
use App\Http\ServerRequest;
|
2020-06-26 22:22:53 +02:00
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
2020-09-01 00:32:30 +02:00
|
|
|
use Doctrine\Persistence\ObjectRepository;
|
2019-04-14 04:05:42 +02:00
|
|
|
|
|
|
|
abstract class AbstractAdminCrudController
|
|
|
|
{
|
2020-06-26 22:22:53 +02:00
|
|
|
protected EntityManagerInterface $em;
|
2019-04-14 04:05:42 +02:00
|
|
|
|
2019-12-07 13:19:49 +01:00
|
|
|
protected string $entity_class;
|
2019-04-14 04:05:42 +02:00
|
|
|
|
2020-09-01 00:32:30 +02:00
|
|
|
protected ObjectRepository $record_repo;
|
2019-04-14 04:05:42 +02:00
|
|
|
|
2019-12-07 13:19:49 +01:00
|
|
|
protected string $csrf_namespace;
|
2019-04-14 04:05:42 +02:00
|
|
|
|
2021-04-23 07:24:12 +02:00
|
|
|
public function __construct(
|
|
|
|
protected EntityForm $form
|
|
|
|
) {
|
2019-04-14 04:05:42 +02:00
|
|
|
$this->em = $form->getEntityManager();
|
|
|
|
$this->entity_class = $form->getEntityClass();
|
|
|
|
$this->record_repo = $form->getEntityRepository();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-08-09 17:00:21 +02:00
|
|
|
* @param ServerRequest $request
|
2021-07-19 07:53:45 +02:00
|
|
|
* @param int|string|null $id
|
2019-09-20 18:44:38 +02:00
|
|
|
*
|
2019-04-14 04:05:42 +02:00
|
|
|
*/
|
2021-07-19 07:53:45 +02:00
|
|
|
protected function doEdit(ServerRequest $request, int|string $id = null): object|bool|null
|
2019-04-14 04:05:42 +02:00
|
|
|
{
|
2020-10-15 00:19:31 +02:00
|
|
|
$record = $this->getRecord($id);
|
2019-04-14 04:05:42 +02:00
|
|
|
return $this->form->process($request, $record);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-07-19 07:53:45 +02:00
|
|
|
* @param int|string|null $id
|
2019-04-14 04:05:42 +02:00
|
|
|
*/
|
2021-07-19 07:53:45 +02:00
|
|
|
protected function getRecord(int|string $id = null): ?object
|
2019-04-14 04:05:42 +02:00
|
|
|
{
|
|
|
|
if (null === $id) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-04-14 09:11:59 +02:00
|
|
|
$record = $this->record_repo->find($id);
|
|
|
|
|
|
|
|
if (!$record instanceof $this->entity_class) {
|
2019-09-10 18:40:31 +02:00
|
|
|
throw new NotFoundException(__('Record not found.'));
|
2019-04-14 09:11:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $record;
|
2019-04-14 04:05:42 +02:00
|
|
|
}
|
2019-09-04 20:00:51 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param ServerRequest $request
|
2021-04-24 00:12:47 +02:00
|
|
|
* @param int|string $id
|
2019-09-10 05:45:24 +02:00
|
|
|
* @param string $csrf
|
2019-09-04 20:00:51 +02:00
|
|
|
*/
|
2021-04-24 00:12:47 +02:00
|
|
|
protected function doDelete(ServerRequest $request, int|string $id, string $csrf): void
|
2019-09-04 20:00:51 +02:00
|
|
|
{
|
2019-09-24 04:22:08 +02:00
|
|
|
$request->getCsrf()->verify($csrf, $this->csrf_namespace);
|
2019-09-04 20:00:51 +02:00
|
|
|
|
2020-10-15 00:19:31 +02:00
|
|
|
$record = $this->getRecord($id);
|
2019-09-04 20:00:51 +02:00
|
|
|
|
|
|
|
if ($record instanceof $this->entity_class) {
|
|
|
|
$this->em->remove($record);
|
|
|
|
$this->em->flush();
|
|
|
|
}
|
|
|
|
}
|
2019-04-14 04:05:42 +02:00
|
|
|
}
|