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