<?php
namespace AdminBundle\Admin\References;
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
use Exception;
use AdminBundle\Admin\BaseAdmin;
use DcSiteBundle\Entity\Engine;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Route\RouteCollection;
use Sonata\AdminBundle\Route\RouteCollectionInterface;
use Symfony\Component\Finder\Exception\AccessDeniedException;
class EngineAdmin extends BaseAdmin
{
/**
* @param RouteCollection $collection
*/
protected function configureRoutes(RouteCollectionInterface $collection): void
{
$collection->remove('view');
}
public function prePersist($object): void
{
$this->prePare($object);
}
public function preUpdate($object): void
{
$this->prePare($object);
}
/**
* @param Engine $object
*/
private function prePare($object) {
$user = $this->getUser();
$dealer = $user->getDealer();
if(!$dealer) {
throw new AccessDeniedException();
}
$object->setDealer($dealer);
}
/**
* @param string $context
* @return ProxyQueryInterface
*/
public function configureQuery($context = 'list'): ProxyQueryInterface
{
$user = $this->getUser();
$query = parent::configureQuery($context);
$dealer = $user->getDealer();
if(!$dealer) {
throw new AccessDeniedException();
}
$query->andWhere(
$query->expr()->eq($query->getRootAliases()[0].'.dealer',':dealer')
);
$query->setParameter('dealer', $dealer->getId());
return $query;
}
/**
* @param FormMapper $formMapper
* @throws Exception
*/
protected function configureFormFields(FormMapper $formMapper): void
{
$User = $this->getUser();
if(!$Dealer = $User->getDealer()) {
throw new AccessDeniedException('User without dealer');
}
$formMapper
->with('Контент', ['class' => 'col-lg-6'])
->add('name', null, ['label' => 'Полное название'])
->add('capacity', null, ['label' => 'Объем двигателя'])
->end();
}
/**
* @param ListMapper $listMapper
*/
protected function configureListFields(ListMapper $listMapper): void
{
$listMapper->addIdentifier('id')
->add('name',null, ['label' => 'Название'])
->add('capacity',null, ['label' => 'Объем'])
->add('_action','actions',[
'label' => 'Действия',
'actions' => [
'edit' => [],
'delete' => [],
]
])
;
}
}