src/AdminBundle/Admin/Insurance/InsuranceConditionCategoryAdmin.php line 14

Open in your IDE?
  1. <?php
  2. namespace AdminBundle\Admin\Insurance;
  3. use AdminBundle\Admin\BaseAdmin;
  4. use AdminBundle\Form\Type\ContentType;
  5. use InsuranceBundle\Entity\InsuranceConditionCategory;
  6. use InsuranceBundle\Entity\InsuranceConditionCategoryContent;
  7. use Sonata\AdminBundle\Datagrid\ListMapper;
  8. use Sonata\AdminBundle\Form\FormMapper;
  9. use Sonata\AdminBundle\Form\Type\ModelType;
  10. use Sonata\AdminBundle\Route\RouteCollectionInterface;
  11. use Sonata\MediaBundle\Form\Type\MediaType;
  12. class InsuranceConditionCategoryAdmin extends BaseAdmin
  13. {
  14.     protected function configureRoutes(RouteCollectionInterface $collection): void
  15.     {
  16.         $collection->remove('view');
  17.         $collection->remove('delete');
  18.     }
  19.     /**
  20.      * @param InsuranceConditionCategory $object
  21.      */
  22.     public function prePersist($object): void
  23.     {
  24.         /** @var InsuranceConditionCategoryContent $content */
  25.         foreach ($object->getContent() as $content) {
  26.             $content->setCategory($object);
  27.         }
  28.         $object->setState(1);
  29.     }
  30.     public function preUpdate($object): void
  31.     {
  32.         parent::preUpdate($object);
  33.     }
  34.     protected function configureFormFields(FormMapper $formMapper): void
  35.     {
  36.         $queryBuilder $this->getModelManager()
  37.             ->getEntityManager(InsuranceConditionCategory::class)
  38.             ->createQueryBuilder('spc')
  39.             ->select('spc')
  40.             ->from(InsuranceConditionCategory::class,'spc')
  41.             ->where('spc.parent_category is null');
  42.         $formMapper
  43.             ->with('Контент', ['class' => 'col-lg-6'])
  44.             ->add('content'ContentType::class, ['label' => false], [
  45.                 'edit' => 'inline',
  46.                 'sortable' => 'position',
  47.                 'admin_code' => 'admin.insurance.insurance_condition_category_content'
  48.             ])
  49.             ->end()
  50.             ->with('Доп. информация', ['class' => 'col-lg-6'])
  51.             ->add('parent_category'ModelType::class, [
  52.                 'label' => 'Родительская категория',
  53.                 'query' => $queryBuilder,
  54.                 'btn_add' => false,
  55.                 'required' => false,
  56.             ])
  57.             ->add('image'MediaType::class, [
  58.                 'provider' => 'sonata.media.provider.image',
  59.                 'context'  => 'dc_site',
  60.                 'label' => 'Изображение',
  61.             ])
  62.             ->end();
  63.     }
  64.     /**
  65.      * @param ListMapper $listMapper
  66.      */
  67.     protected function configureListFields(ListMapper $listMapper): void
  68.     {
  69.         $queryBuilder $this->getModelManager()
  70.             ->getEntityManager(InsuranceConditionCategory::class)
  71.             ->createQueryBuilder('spc')
  72.             ->select('spc')
  73.             ->from(InsuranceConditionCategory::class,'spc')
  74.             ->where('spc.parent_category is null');
  75.         $models = [];
  76.         $models[null] = '';
  77.         /** @var InsuranceConditionCategory $item */
  78.         foreach ($queryBuilder->getQuery()->getResult() as $item) {
  79.             $models[$item->getId()] = $item->getContentByLocale()->getTitle();
  80.         }
  81.         $listMapper->addIdentifier('id')
  82.             ->add('title',null, ['label' => 'Подкатегория'])
  83.             ->add('parent_category',null, ['label' => 'Родительская категория'])
  84.             ->add('_action''actions', [
  85.                 'label' => 'Действия',
  86.                 'actions' => [
  87.                     'edit' => [],
  88.                 ]
  89.             ])
  90.         ;
  91.     }
  92. }