133 lines
4.9 KiB
PHP

<?php
namespace Chill\ThirdPartyBundle\Controller;
use Chill\MainBundle\CRUD\Controller\AbstractCRUDController;
use Chill\MainBundle\CRUD\Controller\CRUDController;
use Chill\MainBundle\Pagination\PaginatorInterface;
use Chill\MainBundle\Templating\Listing\FilterOrderHelper;
use Chill\ThirdPartyBundle\Repository\ThirdPartyACLAwareRepositoryInterface;
use Chill\ThirdPartyBundle\Repository\ThirdPartyRepository;
use http\Exception\RuntimeException;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\Routing\Annotation\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Component\HttpFoundation\Request;
use Chill\ThirdPartyBundle\Entity\ThirdParty;
use Chill\MainBundle\Security\Authorization\AuthorizationHelper;
use Chill\ThirdPartyBundle\Security\Voter\ThirdPartyVoter;
use Doctrine\Common\Collections\ArrayCollection;
use Chill\ThirdPartyBundle\Form\ThirdPartyType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Component\Security\Core\Role\Role;
use Chill\MainBundle\Pagination\PaginatorFactory;
final class ThirdPartyController extends CRUDController
{
/**
*
* @var AuthorizationHelper
*/
protected $authorizationHelper;
/**
*
* @var TranslatorInterface
*/
protected $translator;
/**
*
* @var PaginatorFactory
*/
protected $paginatorFactory;
protected ThirdPartyACLAwareRepositoryInterface $thirdPartyACLAwareRepository;
protected RequestStack $requestStack;
public function __construct(
AuthorizationHelper $authorizationHelper,
TranslatorInterface $translator,
PaginatorFactory $paginatorFactory,
RequestStack $requestStack,
ThirdPartyACLAwareRepositoryInterface $thirdPartyACLAwareRepository
) {
$this->authorizationHelper = $authorizationHelper;
$this->translator = $translator;
$this->paginatorFactory = $paginatorFactory;
$this->requestStack = $requestStack;
$this->thirdPartyACLAwareRepository = $thirdPartyACLAwareRepository;
}
protected function countEntities(string $action, Request $request, ?FilterOrderHelper $filterOrder = null): int
{
if (NULL === $filterOrder){
throw new \LogicException('filterOrder should not be null');
}
return $this->thirdPartyACLAwareRepository->countThirdParties(ThirdPartyVoter::SHOW,
$filterOrder->getQueryString());
}
protected function getQueryResult(string $action, Request $request, int $totalItems, PaginatorInterface $paginator, ?FilterOrderHelper $filterOrder = null)
{
return $this->thirdPartyACLAwareRepository
->listThirdParties(ThirdPartyVoter::SHOW, $filterOrder->getQueryString(), ['name' => 'ASC'], $paginator->getItemsPerPage(),
$paginator->getCurrentPageFirstItemNumber());
}
protected function onPostCheckACL($action, Request $request, $entity): ?Response
{
if ('edit' === $action || 'view' === $action) {
if ($entity->isChild()) {
throw $this->createAccessDeniedException();
}
}
if ('new' === $action) {
if (!$request->query->has('kind')) {
return $this->render('@ChillThirdParty/ThirdParty/new_pick_kind.html.twig');
} else {
$kind = $request->query->getAlpha('kind', '');
if (!(ThirdParty::KIND_COMPANY === $kind || ThirdParty::KIND_CONTACT === $kind)) {
throw new BadRequestHttpException('This kind is not supported: '.$kind);
}
$entity->setKind($kind);
}
}
return null;
}
protected function createFormFor(string $action, $entity, string $formClass = null, array $formOptions = []): FormInterface
{
if ('new' === $action) {
return parent::createFormFor($action, $entity, $formClass, \array_merge(
$formOptions, [ 'kind' => $this->requestStack->getCurrentRequest()->query->getAlpha('kind')]
));
} elseif ('edit' === $action) {
return parent::createFormFor($action, $entity, $formClass, \array_merge(
$formOptions, [ 'kind' => $entity->getKind()]
));
}
return parent::createFormFor($action, $entity, $formClass, $formOptions);
}
protected function buildFilterOrderHelper(string $action, Request $request): ?FilterOrderHelper
{
return $this->getFilterOrderHelperFactory()
->create(self::class)
->addSearchBox(['name', 'company_name', 'acronym'])
->build();
}
}