<?php
namespace AdminBundle\Admin\DCAutoSite;
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
use AdminBundle\Admin\BaseAdmin;
use DcSiteBundle\Entity\SalesContract;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\MediaBundle\Form\Type\MediaType;
use Symfony\Component\Finder\Exception\AccessDeniedException;
use Symfony\Component\Form\Extension\Core\Type\DateType;
/**
* Class SalesContractAdmin
* @package AdminBundle\Admin\DCAutoSite
*/
class ReplacementCarConditionAdmin extends BaseAdmin
{
/**
* @param SalesContract $object
*/
public function prePersist($object): void
{
$user = $this->getUser();
if(!$user->getDealer() && !$object->getDealer()) {
throw new AccessDeniedException('User without dealer');
}
$object->setDealer($user->getDealer());
}
/**
* @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->getRootAliases()[0].'.dealer = :dealer');
$query->setParameter('dealer', $dealer->getId());
return $query;
}
/**
* @param FormMapper $form
*/
public function configureFormFields(FormMapper $form): void
{
$form
->tab('Информация')
->with(' ', ['class' => 'col-lg-6 without-box-heder'])
->add('file', MediaType::class, [
'label' => 'Файл договору',
'provider' => 'sonata.media.provider.file',
'context' => 'dc_file'
])
->add('title_ua', null, [
'label' => 'Название UA',
])
->add('title_ru', null, [
'label' => 'Название RU',
])
->add('begin_date', DateType::class, [
'label' => 'Редакція договору від',
'widget' => 'single_text',
])
->end()
->end()
;
}
/**
* @param ListMapper $list
*/
public function configureListFields(ListMapper $list): void
{
$list->addIdentifier('id')
->add('title_ua', null, ['label' => 'Назва'])
->add('begin_date', null, [
'label' => 'Редакція від',
'format' => 'd.m.Y',
])
->add('_action', 'actions', [
'label' => 'Действия',
'actions' => [
'edit' => [],
]
])
;
}
}