lib/boab/cms-bundle/src/Manager/AbstractContentType.php line 72

Open in your IDE?
  1. <?php
  2. namespace Boab\CmsBundle\Manager;
  3. use Symfony\Component\Form\FormError;
  4. use Symfony\Component\Form\FormEvent;
  5. use Symfony\Component\Form\FormEvents;
  6. use Symfony\Component\Form\AbstractType;
  7. use Boab\CmsBundle\Util\UtilCommon;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Boab\CmsBundle\Entity\PageInterface;
  10. use Symfony\Component\Form\FormBuilderInterface;
  11. use Boab\CmsBundle\Form\Type\UserEntityType;
  12. use Boab\CmsBundle\Form\Type\CategoryEntityType;
  13. use Symfony\Component\Form\Extension\Core\Type\FileType;
  14. use Symfony\Component\Form\Extension\Core\Type\TextType;
  15. use Symfony\Component\Form\Extension\Core\Type\ButtonType;
  16. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  17. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  18. use Symfony\Component\Validator\Constraints as Constraints;
  19. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  20. use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
  21. use Symfony\Component\Form\Extension\Core\Type\TextareaType;
  22. use Boab\CmsBundle\Repository\ContentRepositoryInterface;
  23. use Boab\CmsBundle\Entity\ContentInterface;
  24. use Boab\CmsBundle\Entity\Term;
  25. use Symfony\Component\Form\Extension\Core\Type\HiddenType;
  26. use Symfony\Component\Validator\Constraints\NotBlank;
  27. abstract class AbstractContentType extends AbstractType implements TypeManagerInterface
  28. {
  29.     protected $repository;
  30.     protected $configs;
  31.     protected $type;
  32.     use UtilCommon;
  33.     use ContentTypeConfigTrait;
  34.     public function setRepository(ContentRepositoryInterface $repository)
  35.     {
  36.         $this->repository $repository;
  37.     }
  38.     public function setConfig(array $configs)
  39.     {
  40.         $this->configs $configs;
  41.     }    
  42.     public function setType(string $type)
  43.     {
  44.         $this->type $type;
  45.     }
  46.     public function getType()
  47.     {
  48.         return $this->type;
  49.     }
  50.     public function getEntityClass()
  51.     {
  52.         return get_class($this->getEntity());
  53.     } 
  54.     public function getCollection(Request $request$page)
  55.     {
  56.         return $this->repository->findContentsByType($this->getEntityClass(), $page);
  57.     } 
  58.     public function getContent(Request $request): ?ContentInterface
  59.     {
  60.         return $this->repository->findContentBySlug($request->get('slug'));
  61.     }
  62.     public function getContentById($contentId): ?ContentInterface
  63.     {
  64.         return $this->repository->findContentById($contentId);
  65.     }
  66.     public function getTypeId()
  67.     {
  68.         return $this->type;
  69.     }       
  70.     public function supportsType($type)
  71.     {
  72.         return $this->supports($type);
  73.     }
  74.     public function supports(string $type)
  75.     {
  76.         return $type == $this->getType();
  77.     }
  78.     public function buildForm(FormBuilderInterface $builder, array $options=[])
  79.     {
  80.         $builder
  81.             ->add('title'TextType::class,[
  82.                 'required'=>true,
  83.                 'attr' => [
  84.                     'class' => 'form-control'
  85.                 ],
  86.                 'constraints' => [
  87.                     new Constraints\NotBlank()
  88.                 ],
  89.             ])
  90.             ->add('summary'TextareaType::class, [
  91.                 'required'=>true,
  92.                 'attr' => [
  93.                     'class' => 'form-control'
  94.                 ],
  95.                 'constraints' => [
  96.                     new Constraints\NotBlank()
  97.                 ],                    
  98.             ])
  99.             ->add('body',TextareaType::class,[
  100.                 'required'=>false,
  101.                 'attr' => [
  102.                     'class' => 'form-control tinymce'
  103.                 ],
  104.                 'constraints' => [
  105.                     new Constraints\NotBlank()
  106.                 ],                    
  107.             ])
  108.             ->add('metaKeywords'TextType::class, [
  109.                 'attr'=>[
  110.                     'class'=>'form-control'
  111.                 ],
  112.                 'required'=>false,
  113.             ]) 
  114.             ->add('metaDescription'TextType::class, [
  115.                 'attr'=>[
  116.                     'class'=>'form-control'
  117.                 ],
  118.                 'required'=>false,
  119.             ])                                   
  120.             ->add('status'ChoiceType::class, [
  121.                 'choices' => ['Published' => 'published''Draft' => 'draft'],
  122.                 'empty_data' => false,
  123.                 'required'=>false,
  124.                 'attr'=>[
  125.                     'class'=>'input-select'
  126.                 ],
  127.                 'placeholder' => 'Choose an option',
  128.                 'constraints' => [
  129.                     new Constraints\NotBlank()
  130.                 ],            
  131.             ])       
  132.             ->add('datePublished'DateTimeType::class, [
  133.                 'widget' => 'single_text',
  134.                 'html5'=> false,
  135.                 'required'=>false,
  136.                 'attr'=>[
  137.                     'class'=>'form-control datetime'
  138.                 ],
  139.                 'empty_data' =>''
  140.             ])      
  141.             ->add("publishOnSocialMedia"CheckboxType::class, [
  142.                 'required' => false,
  143.                 //'data' => true,
  144.             ])
  145.             ->add("isFeatured"CheckboxType::class, [
  146.                 'label'=>'Set as feature content',
  147.                 'required' => false,
  148.                 //'data' => true,
  149.             ])  
  150.             ->add('position'TextType::class, [
  151.                 'label' => 'Position',
  152.                 'attr'=>[
  153.                     'class'=>'form-control'
  154.                 ],
  155.                 'required'=>false,
  156.             ])                           
  157.             ->add("isHome"CheckboxType::class, [
  158.                 'label'=>'Set as home page',
  159.                 'required' => false
  160.             ])
  161.             ->add("user"UserEntityType::class,[
  162.             ])
  163.             ->add('uploadedThumbnail'FileType::class, [
  164.                 'attr'=>[
  165.                     'class'=>'browse-hide page-thumbnail-browse'
  166.                 ],
  167.                 'data_class'=>null,
  168.                 'required'=>false,
  169.             ]);
  170.         $builder->add('cancel'ButtonType::class, [
  171.             'attr' => ['class' => 'btn btn-danger isThemeBtn'],
  172.         ]);
  173.         $builder->add('submit'SubmitType::class, [
  174.             'attr' => [
  175.                 'class' => 'btn btn-success isThemeBtn',
  176.                 'id'=>'submit'
  177.             ],
  178.         ]);        
  179.         if(!$this->getEntity() instanceof PageInterface && isset($this->configs['taxonomy'])){
  180.             $builder->add('term'CategoryEntityType::class, [
  181.                 'vocabulary'=>$this->configs['taxonomy']['vocab_id'],
  182.                 'class'      => Term::class,
  183.             ]);
  184.             $builder->get('term')->addEventListener(FormEvents::SUBMIT, function(FormEvent $event){
  185.                 $content $event->getData();
  186.                 $form $event->getForm();
  187.                 if(!$content){
  188.                     //$form->addError(new FormError('You must choose a category'));
  189.                 }
  190.             });  
  191.         }
  192.         $builder->addEventListener(FormEvents::SUBMIT, function(FormEvent $event){
  193.             $form $event->getForm();
  194.             $content $event->getData();
  195.             $datePublished $form->get('datePublished');
  196.             if(!$datePublished->getData() && $form->get('status')->getData() == ContentInterface::STATUS_PUBLISHED){
  197.                 $datePublished->addError(new FormError('Date must be set if status is published'));
  198.             }
  199.             if($content->getId()){
  200.                 $content->setUpdatedAt(new \DateTimeImmutable('now'));
  201.             }
  202.             $content->setSlug($this->slugify($content->getTitle()));
  203.             $event->setData($content);
  204.         });
  205.    
  206.     }
  207.     public function buildSearchForm(FormBuilderInterface $builder, array $options=[])
  208.     {
  209.         $builder->add'query'TextType::class, [
  210.             'label'=>false,
  211.             'constraints' => [
  212.                     new NotBlank(),
  213.             ],
  214.             'attr'=>[
  215.                 'class'=>'form-control py-2 border-right-0 border',
  216.                 'id'=> 'search-query',
  217.                 'placeholder' => 'Search Courses...'
  218.             ]            
  219.         ])
  220.         ->add 'type'HiddenType::class, [
  221.             'constraints' => [
  222.                     new NotBlank(),
  223.             ],
  224.             'data'=> $options['type'],
  225.         ]);       
  226.     }
  227. }