<?php
namespace AdminBundle\Admin\References;
use AdminBundle\Admin\BaseAdmin;
use DcSiteBundle\Entity\Markdown;
use Exception;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Route\RouteCollectionInterface;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class MarkdownAdmin extends BaseAdmin
{
/**
* @param RouteCollectionInterface $collection
*/
protected function configureRoutes(RouteCollectionInterface $collection): void
{
$collection->remove('view');
}
/**
* @param FormMapper $formMapper
* @throws Exception
*/
protected function configureFormFields(FormMapper $formMapper): void
{
$brands = $this->autoRia->getBrands();
$cBrands = [];
foreach ($brands as $item) {
$cBrands[$item['title']] = $item['id'];
}
/** @var Markdown $sub */
$sub = $this->getSubject();
$models = false;
if($sub->getRiaBrandId()) {
$models = $this->autoRia->getModels($sub->getRiaBrandId());
$choices = [];
foreach ($models as $row) {
$choices[$row['title']] = $row['id'];
}
}
$formMapper
->with('Данные авто', ['class' => 'col-lg-6']);
$formMapper->add('ria_brand_id', ChoiceType::class, ['label' => 'Бренд', 'attr' => ['class' => 'riaBrand'], 'choices' => $cBrands]);
if($models) {
$formMapper->add('ria_model_id', ChoiceType::class, ['label' => 'Модель', 'attr' => ['class' => 'riaModel'], 'choices' => $choices, 'required' => false]);
} else {
$formMapper->add('ria_model_id', TextType::class, ['attr' => ['disabled' => 'disabled'], 'label' => 'Для начала нужно указать бренд и нажать кнопку Сохранить и редактировать', 'required' => false]);
}
$formMapper->end()
->with('Уцінка від роздрібної ціни нового автомобіля', ['class' => 'col-lg-6'])
->add('retail_6', null, ['label' => '6 місяців', 'required' => $models != false])
->add('retail_12', null, ['label' => '12 місяців', 'required' => $models != false])
->add('retail_24', null, ['label' => '24 місяців', 'required' => $models != false])
->add('retail_36', null, ['label' => '36 місяців', 'required' => $models != false])
->add('retail_48', null, ['label' => '48 місяців', 'required' => $models != false])
->add('retail_60', null, ['label' => '60 місяців', 'required' => $models != false])
->end();
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults([
'validation_groups' => false,
]);
}
/**
* @param ListMapper $listMapper
*/
protected function configureListFields(ListMapper $listMapper): void
{
$listMapper->addIdentifier('id')
->add('ria_brand_id',null, ['label' => 'Бренд'])
->add('ria_model_id',null, ['label' => 'Модель'])
->add('_action','actions',[
'label' => 'Действия',
'actions' => [
'edit' => [],
'delete' => [],
]
])
;
}
protected function configure(): void
{
parent::configure(); // TODO: Change the autogenerated stub
$this->setTemplate('edit', '@Admin/admin/CRUD/edit-markdown.html.twig');
}
}