src/Form/Forum/SearchForumType.php line 33

Open in your IDE?
  1. <?php
  2. namespace App\Form\Forum;
  3. use App\Entity\Forum;
  4. use App\Entity\ForumCategory;
  5. use App\Entity\Repository\ForumAccessRepository;
  6. use App\EntityManager\ForumAccessManager;
  7. use App\Model\SearchForum;
  8. use Doctrine\ORM\EntityRepository;
  9. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  10. use Symfony\Component\Form\AbstractType;
  11. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  12. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  13. use Symfony\Component\Form\Extension\Core\Type\DateType;
  14. use Symfony\Component\Form\Extension\Core\Type\TextType;
  15. use Symfony\Component\Form\FormBuilderInterface;
  16. use Symfony\Component\OptionsResolver\OptionsResolver;
  17. class SearchForumType extends AbstractType
  18. {
  19.     protected $forumAccessManager;
  20.     public function __construct(ForumAccessManager $forumAccessManager)
  21.     {
  22.         $this->forumAccessManager $forumAccessManager;
  23.     }
  24.     /**
  25.      * @param FormBuilderInterface $builder
  26.      * @param array                $options
  27.      */
  28.     public final function buildForm(FormBuilderInterface $builder, array $options)
  29.     {
  30.         $forumList $this->forumAccessManager->getForumWithReadAccessForCurrentUser();
  31.         // interval default dates
  32.         $dateStart = new \DateTime;
  33.         $dateEnd   = new \DateTime;
  34.         $dateStart->modify('first day of this month');
  35.         $dateEnd->modify('last day of this month');
  36.         $builder
  37.             ->add(
  38.                 'keywords',
  39.                 TextType::class,
  40.                 [
  41.                     'label'    => 'forum.search.label.keywords',
  42.                     'required' => false,
  43.                 ]
  44.             )
  45.             ->add(
  46.                 'searchIn',
  47.                 ChoiceType::class,
  48.                 [
  49.                     'label'    => 'forum.search.label.search_in.label',
  50.                     'expanded' => true,
  51.                     'choices'  => [
  52.                         'forum.search.label.search_in.choices.title'           => SearchForum::SEARCH_IN_TITLE,
  53.                         'forum.search.label.search_in.choices.title_and_topic' => SearchForum::SEARCH_IN_TITLE_AND_TOPIC,
  54.                     ],
  55.                 ]
  56.             )
  57.             ->add(
  58.                 'forum',
  59.                 EntityType::class,
  60.                 [
  61.                     'label'        => 'forum.search.label.forum',
  62.                     'required'     => false,
  63.                     'class'        => Forum::class,
  64.                     'choice_label' => 'name',
  65.                     'choices'      => $forumList,
  66.                     'placeholder'  => 'meta.all',
  67.                     'group_by'     => function ($choice$key$value) {
  68.                         /** @var Forum $choice */
  69.                         return $choice->getCategory() instanceof ForumCategory
  70.                             ?
  71.                             $choice->getCategory()->getName()
  72.                             :
  73.                             'Empty';
  74.                     },
  75.                 ]
  76.             )
  77.             ->add(
  78.                 'author',
  79.                 TextType::class,
  80.                 [
  81.                     'label'    => 'forum.search.label.author',
  82.                     'required' => false,
  83.                 ]
  84.             )
  85.             ->add(
  86.                 'authorExactName',
  87.                 CheckboxType::class,
  88.                 [
  89.                     'label'    => 'forum.search.label.authorExactName',
  90.                     'required' => false,
  91.                 ]
  92.             )
  93.             ->add(
  94.                 'intervalType',
  95.                 ChoiceType::class,
  96.                 [
  97.                     'label'    => 'forum.search.label.intervalType.label',
  98.                     'expanded' => true,
  99.                     'choices'  => [
  100.                         'forum.search.label.intervalType.choices.last_month'    => SearchForum::INTERVAL_LAST_MONTH,
  101.                         'forum.search.label.intervalType.choices.last_semester' => SearchForum::INTERVAL_LAST_SEMESTER,
  102.                         'forum.search.label.intervalType.choices.last_year'     => SearchForum::INTERVAL_LAST_YEAR,
  103.                         'forum.search.label.intervalType.choices.custom'        => SearchForum::INTERVAL_CUSTOM,
  104.                     ],
  105.                 ]
  106.             )
  107.             ->add(
  108.                 'intervalStart',
  109.                 DateType::class,
  110.                 [
  111.                     'label' => 'forum.search.label.intervalStart',
  112.                     'years' => range(2000date('Y')),
  113.                     'data'  => $dateStart,
  114.                 ]
  115.             )
  116.             ->add(
  117.                 'intervalEnd',
  118.                 DateType::class,
  119.                 [
  120.                     'label' => 'forum.search.label.intervalEnd',
  121.                     'years' => range(2000date('Y')),
  122.                     'data'  => $dateEnd,
  123.                 ]
  124.             )
  125.             ->add(
  126.                 'limit',
  127.                 ChoiceType::class,
  128.                 [
  129.                     'label'   => 'forum.search.label.limit',
  130.                     'choices' => [
  131.                         50  => 50,
  132.                         100 => 100,
  133.                         200 => 200,
  134.                     ],
  135.                 ]
  136.             )
  137.             ->add(
  138.                 'displayAs',
  139.                 ChoiceType::class,
  140.                 [
  141.                     'label'    => 'forum.search.label.displayAs.label',
  142.                     'expanded' => true,
  143.                     'choices'  => [
  144.                         'forum.search.label.displayAs.choices.as_topic'   => SearchForum::DISPLAY_AS_TOPIC,
  145.                         'forum.search.label.displayAs.choices.as_message' => SearchForum::DISPLAY_AS_MESSAGE,
  146.                     ],
  147.                 ]
  148.             );
  149.     }
  150.     public function configureOptions(OptionsResolver $resolver)
  151.     {
  152.         $resolver->setDefaults(
  153.             array(
  154.                 'data_class'         => SearchForum::class,
  155.                 'translation_domain' => 'forms',
  156.                 'csrf_protection' => false,
  157.             )
  158.         );
  159.     }
  160.     public function getBlockPrefix()
  161.     {
  162.         return null;
  163.     }
  164. }