src/Admin/HomePageAdmin.php line 38

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: mario
  5.  * Date: 30/06/2019
  6.  * Time: 22:52
  7.  */
  8. namespace App\Admin;
  9. use App\Entity\Seo;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use Doctrine\ORM\EntityRepository;
  12. use Doctrine\ORM\OptimisticLockException;
  13. use FOS\CKEditorBundle\Form\Type\CKEditorType;
  14. use Sonata\AdminBundle\Admin\AbstractAdmin;
  15. use Sonata\AdminBundle\Datagrid\ListMapper;
  16. use Sonata\AdminBundle\Datagrid\DatagridMapper;
  17. use Sonata\AdminBundle\Form\FormMapper;
  18. use Sonata\AdminBundle\Form\Type\ModelListType;
  19. use Sonata\AdminBundle\Route\RouteCollection;
  20. use Sonata\AdminBundle\Show\ShowMapper;
  21. use Sonata\Form\Type\CollectionType;
  22. use Sonata\Form\Type\DatePickerType;
  23. use Symfony\Component\Form\Extension\Core\Type\TextareaType;
  24. final class HomePageAdmin extends AbstractAdmin
  25. {
  26.     protected $baseRouteName 'home_page';
  27.     protected $baseRoutePattern 'home_page';
  28.     private EntityManagerInterface $entityManager;
  29.     public function __construct(?string $code null, ?string $class null, ?string $baseControllerName nullEntityManagerInterface $entityManager null)
  30.     {
  31.         $this->entityManager $entityManager;
  32.         parent::__construct($code$class$baseControllerName);
  33.     }
  34.     public function configureRoutes(RouteCollection|\Sonata\AdminBundle\Route\RouteCollectionInterface $collection): void
  35.     {
  36. //        $collection->remove('export');
  37. //        $collection->remove('create');
  38. //        $collection->remove('show');
  39. //        $collection->remove('delete');
  40.     }
  41.     protected function configureFormFields(FormMapper $form): void
  42.     {
  43.         $form
  44.             ->tab('General')
  45.             ->with('')
  46.                 ->add('name'null, array('label' => 'Nombre'))
  47.                 ->add('players'null, [
  48.                     'label' => 'Caballos',
  49.                     'query_builder' => static function (EntityRepository $er) {
  50.                         return $er->createQueryBuilder('p')
  51.                             ->andWhere('p.broodmares = :broodmares')
  52.                             ->setParameter('broodmares'false)
  53.                             ->orderBy('p.name''ASC');
  54.                     },
  55.                 ])
  56.                 ->add('mares'null, [
  57.                     'label' => 'Yeguas de cría',
  58.                     'query_builder' => static function (EntityRepository $er) {
  59.                         return $er->createQueryBuilder('p')
  60.                             ->andWhere('p.broodmares = :broodmares')
  61.                             ->setParameter('broodmares'true)
  62.                             ->orderBy('p.name''ASC');
  63.                     },
  64.                 ])
  65.             ->end()
  66.             ->end()
  67.             ->tab('Banner Principal')
  68.             ->with('')
  69.                 ->add('main_banner'ModelListType::class, ['label' => 'Banner'], ['link_parameters' => ['context' => 'default''provider' => 'sonata.media.provider.image']])
  70.                 ->add('main_banner_mobile'ModelListType::class, ['label' => 'Banner Mobile'], ['link_parameters' => ['context' => 'default''provider' => 'sonata.media.provider.image']])
  71.                 ->add('main_banner_url'null, ['label' => 'URL'])
  72.             ->end()
  73.             ->end()
  74.             ->tab('Banner Secundario')
  75.             ->with('')
  76.                 ->add('secondary_banner'ModelListType::class, ['label' => 'Banner'], ['link_parameters' => ['context' => 'default''provider' => 'sonata.media.provider.image']])
  77.                 ->add('secondary_banner_mobile'ModelListType::class, ['label' => 'Banner Mobile'], ['link_parameters' => ['context' => 'default''provider' => 'sonata.media.provider.image']])
  78.                 ->add('secondary_banner_url'null, ['label' => 'URL'])
  79.             ->end()
  80.             ->end()
  81.             ->tab('Nuestra Historia')
  82.             ->with('')
  83.                 ->add('our_story_title'null, ['label' => 'Título'])
  84.                 ->add('our_story_image'ModelListType::class, ['label' => 'Imagen'], ['link_parameters' => ['context' => 'default''provider' => 'sonata.media.provider.image']])
  85.                 ->add('our_story_content'CKEditorType::class, ['label' => 'Contenido''required' => false])
  86.             ->end()
  87.             ->end()
  88.             ;
  89.             if ($this->isCurrentRoute('edit')) {
  90.                 $form
  91.                     ->tab('Seo')
  92.                     ->with('')
  93.                     ->add('seo.title'null, ['label' => 'Titulo'])
  94.                     ->add('seo.keywords'null, ['label' => 'Palabras claves'])
  95.                     ->add('seo.description'TextareaType::class, ['label' => 'Descripción''required' => false])
  96.                     ->add('seo.share_image'ModelListType::class, ['label' => 'Imagen para compartir''required' => false], ['link_parameters' => ['context' => 'default''provider' => 'sonata.media.provider.image',]])
  97.                     ->end()
  98.                     ->end();
  99.             }
  100.         ;
  101.     }
  102.     protected function configureDatagridFilters(DatagridMapper $datagridMapper): void
  103.     {
  104.         $datagridMapper->add('name'null, array('label' => 'Nombre'));
  105.     }
  106.     protected function configureListFields(ListMapper $listMapper): void
  107.     {
  108.         $listMapper
  109.             ->addIdentifier('name'null, array('label' => 'Nombre'))
  110.             ->add(ListMapper::NAME_ACTIONSListMapper::TYPE_ACTIONS, [
  111.                 'translation_domain' => 'SonataAdminBundle',
  112.                 'actions'            => [
  113.                     'edit' => [],
  114.                 ],
  115.             ]);
  116.     }
  117.     protected function configureShowFields(ShowMapper $showMapper): void
  118.     {
  119.         $showMapper
  120.             ->add('name'null, array('label' => 'Nombre'))
  121.             ;
  122.     }
  123.     /**
  124.      * @throws OptimisticLockException
  125.      */
  126.     public function prePersist(object $object): void
  127.     {
  128.         $em $this->entityManager;
  129.         $seo = new Seo();
  130.         $seo->setTitle($object->getName());
  131.         $em->persist($seo);
  132.         $em->flush($seo);
  133.         $object->setSeo($seo);
  134.     }
  135. }