<?php
namespace AdminBundle\Admin\References;
use Exception;
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
use AdminBundle\Admin\BaseAdmin;
use CoreBundle\Model\Vehicles\VehicleType;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Route\RouteCollection;
use Sonata\AdminBundle\Route\RouteCollectionInterface;
use Sonata\MediaBundle\Form\Type\MediaType;
use Symfony\Component\Finder\Exception\AccessDeniedException;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
class VehicleGroupAdmin 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);
parent::preUpdate($object);
}
/**
* @param CarGroup $object
*/
private function prePare($object) {
$user = $this->getUser();
$dealer = $user->getDealer();
if(!$dealer) {
throw new AccessDeniedException();
}
$object->setDealer($dealer);
}
/**
* @param FormMapper $formMapper
* @throws Exception
*/
protected function configureFormFields(FormMapper $formMapper): void
{
$this->checkByRole(['ROLE_SUPER_ADMIN','ROLE_CONTENT_MANAGER','ROLE_DC_MANAGER']);
$User = $this->getUser();
if(!$Dealer = $User->getDealer()) {
throw new AccessDeniedException('User without dealer');
}
$formMapper
->tab('Основная информация')
->with('Контент', ['class' => 'col-lg-6'])
->add('position', null, ['label' => 'Порядок вывода'])
->add('preview', MediaType::class, [
'label' => 'Превью',
'provider' => 'sonata.media.provider.image',
'context' => 'dc_site'
])
->add('vehicle_type', ChoiceType::class, [
'label' => 'Тип техники',
'choices' => array_flip(VehicleType::getTypes())
])
->add('inner_name', TextType::class, ['label' => 'Техническое название'], [])
->add('title_ru', TextType::class, ['label' => 'Название RU'], [])
->add('title_ua', TextType::class, ['label' => 'Название UA'], [])
->add('url', TextType::class, ['label' => 'url'], [])
->end()
->end();
}
/**
* @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 ListMapper $listMapper
*/
protected function configureListFields(ListMapper $listMapper): void
{
$this->checkByRole(['ROLE_SUPER_ADMIN','ROLE_CONTENT_MANAGER','ROLE_DC_MANAGER']);
$listMapper->addIdentifier('id')
->add('inner_name',null, ['label' => 'Техническое название'])
->add('title_ru',null, ['label' => 'Название'])
->add('vehicle_type','choice', ['label' => 'Тип техники', 'editable' => false, 'choices' => VehicleType::getTypes()])
->add('_action','actions',[
'label' => 'Действия',
'actions' => [
'edit' => [],
'delete' => [],
]
])
;
}
}