<?php
namespace AdminBundle\Admin\Service;
use DateTime;
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
use CoreBundle\Entity\Model;
use DcSiteBundle\Entity\ServiceWorkJob;
use DcSiteBundle\Entity\ServiceWorkPart;
use DcSiteBundle\Entity\ServiceWork;
use Doctrine\ORM\Query\Expr\Join;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Form\Type\ModelType;
use Sonata\AdminBundle\Route\RouteCollectionInterface;
use Sonata\Form\Type\CollectionType;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Finder\Exception\AccessDeniedException;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Security\Core\Security;
class ServiceWorkAdmin extends AbstractAdmin
{
protected Security $security;
public function setContainerServices(Security $security): void
{
$this->security = $security;
}
public function prePersist($object): void
{
$this->prePare($object);
}
public function preUpdate($object): void
{
$this->prePare($object);
parent::preUpdate($object);
}
private function prePare(ServiceWork $object) {
$user = $this->security->getUser();
$dealer = $user->getDealer();
if(!$dealer) {
throw new AccessDeniedException();
}
$object->setDealer($dealer);
/** @var ServiceWorkJob $Job */
foreach ($object->getJobs() as $Job) {
$Job->setRegulation($object);
}
/** @var ServiceWorkPart $Part */
foreach ($object->getParts() as $Part) {
$Part->setRegulation($object);
}
parent::preUpdate($object);
}
/**
* @param RouteCollectionInterface $collection
*/
protected function configureRoutes(RouteCollectionInterface $collection): void
{
$collection->remove('view');
$collection->remove('create');
$collection->remove('delete');
$collection->add('cloneRegulation', $this->getRouterIdParameter().'/clone');
}
/**
* @param FormMapper $formMapper
*/
protected function configureFormFields(FormMapper $formMapper): void
{
$years = [];
$year = new DateTime();
for($i = 1; $i <= 25; $i++) {
$years[$year->format('Y')] = $year->format('Y');
$year->modify('- 1 year');
}
$User = $this->security->getUser();
if(!$Dealer = $User->getDealer()) {
throw new AccessDeniedException('User without dealer');
}
$queryBuilder = $this->getModelManager()
->getEntityManager(Model::class)
->createQueryBuilder('m')
->select('m')
->from('CoreBundle:Model','m')
->innerJoin('m.brand','b', Join::WITH, 'b.id = :brand')
->setParameter('brand', $Dealer->getBrand());
$formMapper
->with(' ', ['class' => 'col-lg-3'])
->add('variation.model', ModelType::class, ['label' => 'Модель', 'required' => true, 'query' => $queryBuilder, 'btn_add' => false])
->add('variation.engine_volume', null, ['label' => 'Объем двигателя'])
->add('mileage', null, ['label' => 'Пробег (км)'])
->end()
->with(' ', ['class' => 'col-lg-3'])
->add('variation.year_from', ChoiceType::class, ['label' => 'Год выпуска от', 'choices' => $years])
->add('variation.year_to', ChoiceType::class, ['label' => 'Год выпуска до', 'choices' => $years, 'required' => false])
->end()
->with(' ', ['class' => 'col-lg-3'])
->add('variation.fuelType', ModelType::class, ['label' => 'Тип топлива', 'btn_add' => false])
->add('variation.driveUnit', ModelType::class, ['label' => 'Привод', 'btn_add' => false])
->end()
->with(' ', ['class' => 'col-lg-3'])
->add('variation.transmissionType', ModelType::class, ['label' => 'КПП', 'btn_add' => false])
->add('variation.transmission_step_count', null, ['label' => 'Кол-во ступеней КПП', 'required' => false])
->end()
->with('Список запасных частей', ['class' => 'col-lg-12'])
->add('parts', CollectionType::class,[
'label' => false,
'by_reference' => false,
],[
'edit' => 'inline',
'inline' => 'table',
'sortable' => 'position',
])
->end()
->with('Список работ', ['class' => 'col-lg-12'])
->add('jobs', CollectionType::class,[
'label' => false,
'by_reference' => false,
],[
'edit' => 'inline',
'inline' => 'table',
'sortable' => 'position',
])
->end()
;
}
protected function configureDatagridFilters(DatagridMapper $datagridMapper): void
{
$datagridMapper->add('id');
$User = $this->security->getUser();
if(!$Dealer = $User->getDealer()) {
throw new AccessDeniedException('User without dealer');
}
$queryBuilder = $this->getModelManager()
->getEntityManager(Model::class)
->createQueryBuilder('m')
->select('m')
->from('CoreBundle:Model','m')
->where('m.brand = :brand')
->setParameter('brand', $User->getDealer()->getBrand());
$datagridMapper->add('variation.model',null, ['label' => 'Модель'], EntityType::class,
['class' => 'CoreBundle:Model', 'query_builder' => $queryBuilder]);
}
/**
* @param string $context
* @return ProxyQueryInterface
*/
public function configureQuery($context = 'list'): ProxyQueryInterface
{
$user = $this->security->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
{
$listMapper->addIdentifier('id')
->add('variation.model',null, ['label' => 'Модель'])
->add('variation.year_from', null, ['label' => 'Год выпуска от'])
->add('variation.year_to', null, ['label' => 'Год выпуска до'])
->add('mileage', null, ['label' => 'Пробег'])
->add('variation.engine_volume', ChoiceType::class, ['label' => 'Объем двигателя'])
->add('variation.transmissionType',null, ['label' => 'КПП'])
->add('variation.fuelType',null, ['label' => 'Тип топлива'])
->add('variation.driveUnit',null, ['label' => 'Привод'])
->add('_action', 'actions', [
'label' => 'Действия',
'actions' => [
'edit' => [],
'cloneRegulation' => [
'template' => '@AdminBundle/CRUD/list__action_clone_r.html.twig'
]
]
])
;
}
}