<?php
namespace AdminBundle\Admin\Shop;
use CoreBundle\Entity\User;
use AdminBundle\Form\Type\ContentType;
use CoreBundle\Entity\Shop\Goods;
use CoreBundle\Entity\Shop\GoodsContent;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Form\Type\ModelType;
use Sonata\AdminBundle\Route\RouteCollectionInterface;
use Sonata\MediaBundle\Form\Type\MediaType;
use Symfony\Component\Finder\Exception\AccessDeniedException;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\NumberType;
use Symfony\Component\Security\Core\Security;
class GoodsAdmin extends AbstractAdmin
{
protected Security $security;
public function setContainerServices(Security $security): void
{
$this->security = $security;
}
/**
* @param RouteCollectionInterface $collection
*/
protected function configureRoutes(RouteCollectionInterface $collection): void
{
$collection->remove('delete');
}
/**
* @param object $object
*/
public function prePersist($object): void
{
/** @var User $User */
$User = $this->security->getUser();
if(!$User->getDealer() && !$object->getDealer()) {
throw new AccessDeniedException('User without dealer');
}
/** @var Goods $object */
$object->setDealer($User->getDealer());
$object->setState((int) $object->getState());
/** @var GoodsContent $content */
foreach ($object->getContent() as $content) {
$content->setGoods($object);
}
}
/**
* @param FormMapper $formMapper
*/
protected function configureFormFields(FormMapper $formMapper): void
{
$formMapper
->tab('Основная информация')
->with(' ', ['class' => 'col-lg-6 without-box-heder'])
->add('state', CheckboxType::class, ['label' => 'Показывать на сайте', 'required' => false])
->add('group', ModelType::class, [
'required' => true,
'label' => 'Группа товаров',
'btn_add' => false,
])
->add('image', MediaType::class, [
'label' => 'Изображение',
'provider' => 'sonata.media.provider.image',
'context' => 'shop'
])
->add('price',NumberType::class, ['label' => 'Цена', 'required' => true])
->add('content', ContentType::class, ['label' => false], [
'edit' => 'inline',
'sortable' => 'position',
])
->end()
->end()
;
}
/**
* @param ListMapper $listMapper
*/
protected function configureListFields(ListMapper $listMapper): void
{
$listMapper->addIdentifier('id')
->add('title',null, ['label' => 'Название'])
->add('price',null, ['label' => 'Цена'])
->add('dealer',null, ['label' => 'Дилер'])
->add('state','choice', ['label' => 'Показывать на сайте', 'editable' => true, 'choices' => [
1 => 'Да',
0 => 'Нет',
]])
->add('_action', 'actions', [
'label' => 'Действия',
'actions' => [
'edit' => [],
]
])
;
}
}