<?php
declare(strict_types=0);
/*
* WellCommerce Foundation
*
* This file is part of the WellCommerce package.
*
* (c) Adam Piotrowski <adam@wellcommerce.org>, Adrian Potepa <adrian@wellcommerce.org>
*
* For the full copyright and license information,
* please view the LICENSE file that was distributed with this source code.
*/
namespace WellCommerce\Bundle\CoreBundle\Controller\Admin;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use WellCommerce\Bundle\AppBundle\Entity\AuditableInterface;
use WellCommerce\Bundle\CoreBundle\Controller\AbstractController;
use WellCommerce\Bundle\CoreBundle\Entity\EntityInterface;
use WellCommerce\Bundle\CoreBundle\Manager\ManagerInterface;
use WellCommerce\Component\DataGrid\DataGridInterface;
use WellCommerce\Component\Form\FormBuilderInterface;
use WhiteOctober\BreadcrumbsBundle\Model\Breadcrumbs;
/**
* Class AbstractAdminController
*
* @author Adam Piotrowski <adam@wellcommerce.org>
*/
abstract class AbstractAdminController extends AbstractController
{
/**
* @var ManagerInterface
*/
protected $manager;
public function __construct(ManagerInterface $manager)
{
$this->manager = $manager;
}
protected function index(DataGridInterface $dataGrid): Response
{
$this->denyAccessUnlessGranted($this->manager->getAlias().'.index');
$this->createBreadcrumbs();
return $this->displayTemplate('index', [
'datagrid' => $dataGrid,
]);
}
protected function save(FormBuilderInterface $formBuilder, EntityInterface $entity = null): Response
{
$isNew = false;
$resource = $entity;
$breadcrumbs = $this->createBreadcrumbs();
$module = $this->getRouterHelper()->getCurrentModule();
if (!$entity instanceof EntityInterface) {
$entity = $this->manager->initResource();
$isNew = true;
}
if ($isNew) {
$this->denyAccessUnlessGranted($this->manager->getAlias().'.add');
$breadcrumbs->addItem(sprintf('%s.heading.add', $module));
} else {
$this->denyAccessUnlessGranted($this->manager->getAlias().'.edit');
if ($entity instanceof AuditableInterface) {
$breadcrumbs->addItem($entity->getResourceName());
}
}
$form = $formBuilder->createForm($entity, [
'resource' => $resource,
]);
if ($form->handleRequest()->isSubmitted()) {
if ($form->isValid()) {
if ($this->getEntityManager()->contains($entity)) {
$this->manager->updateResource($entity);
} else {
$this->manager->createResource($entity);
}
}
return $this->createFormDefaultJsonResponse($form);
}
return $this->displayTemplate($isNew ? 'add' : 'edit', [
'form' => $form,
'resource' => $entity,
]);
}
protected function delete(EntityInterface $entity): Response
{
$this->denyAccessUnlessGranted($this->manager->getAlias().'.delete');
$result = ['success' => true];
try {
$this->manager->removeResource($entity);
} catch (\Exception $e) {
$result = ['error' => $e->getMessage()];
}
return $this->jsonResponse($result);
}
protected function deleteGroup(Request $request): Response
{
$this->denyAccessUnlessGranted($this->manager->getAlias().'.delete');
$identifiers = $request->request->filter('identifiers');
foreach ($identifiers as $id) {
$resource = $this->manager->getRepository()->findOneBy(['id' => $id]);
if ($resource instanceof EntityInterface) {
$this->manager->removeResource($resource, false);
}
}
$this->getEntityManager()->flush();
return $this->jsonResponse(['success' => true]);
}
protected function createBreadcrumbs(): Breadcrumbs
{
$module = $this->getRouterHelper()->getCurrentModule();
$breadcrumbs = $this->getBreadcrumbs();
$breadcrumbs->clear();
$breadcrumbs->addItem($module . '.heading.index', $this->getRouterHelper()->getRedirectToActionUrl('index'));
return $breadcrumbs;
}
}