<?php
namespace App\Form\Forum;
use App\Entity\Forum;
use App\Entity\ForumCategory;
use App\Entity\Repository\ForumAccessRepository;
use App\EntityManager\ForumAccessManager;
use App\Model\SearchForum;
use Doctrine\ORM\EntityRepository;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class SearchForumType extends AbstractType
{
protected $forumAccessManager;
public function __construct(ForumAccessManager $forumAccessManager)
{
$this->forumAccessManager = $forumAccessManager;
}
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public final function buildForm(FormBuilderInterface $builder, array $options)
{
$forumList = $this->forumAccessManager->getForumWithReadAccessForCurrentUser();
// interval default dates
$dateStart = new \DateTime;
$dateEnd = new \DateTime;
$dateStart->modify('first day of this month');
$dateEnd->modify('last day of this month');
$builder
->add(
'keywords',
TextType::class,
[
'label' => 'forum.search.label.keywords',
'required' => false,
]
)
->add(
'searchIn',
ChoiceType::class,
[
'label' => 'forum.search.label.search_in.label',
'expanded' => true,
'choices' => [
'forum.search.label.search_in.choices.title' => SearchForum::SEARCH_IN_TITLE,
'forum.search.label.search_in.choices.title_and_topic' => SearchForum::SEARCH_IN_TITLE_AND_TOPIC,
],
]
)
->add(
'forum',
EntityType::class,
[
'label' => 'forum.search.label.forum',
'required' => false,
'class' => Forum::class,
'choice_label' => 'name',
'choices' => $forumList,
'placeholder' => 'meta.all',
'group_by' => function ($choice, $key, $value) {
/** @var Forum $choice */
return $choice->getCategory() instanceof ForumCategory
?
$choice->getCategory()->getName()
:
'Empty';
},
]
)
->add(
'author',
TextType::class,
[
'label' => 'forum.search.label.author',
'required' => false,
]
)
->add(
'authorExactName',
CheckboxType::class,
[
'label' => 'forum.search.label.authorExactName',
'required' => false,
]
)
->add(
'intervalType',
ChoiceType::class,
[
'label' => 'forum.search.label.intervalType.label',
'expanded' => true,
'choices' => [
'forum.search.label.intervalType.choices.last_month' => SearchForum::INTERVAL_LAST_MONTH,
'forum.search.label.intervalType.choices.last_semester' => SearchForum::INTERVAL_LAST_SEMESTER,
'forum.search.label.intervalType.choices.last_year' => SearchForum::INTERVAL_LAST_YEAR,
'forum.search.label.intervalType.choices.custom' => SearchForum::INTERVAL_CUSTOM,
],
]
)
->add(
'intervalStart',
DateType::class,
[
'label' => 'forum.search.label.intervalStart',
'years' => range(2000, date('Y')),
'data' => $dateStart,
]
)
->add(
'intervalEnd',
DateType::class,
[
'label' => 'forum.search.label.intervalEnd',
'years' => range(2000, date('Y')),
'data' => $dateEnd,
]
)
->add(
'limit',
ChoiceType::class,
[
'label' => 'forum.search.label.limit',
'choices' => [
50 => 50,
100 => 100,
200 => 200,
],
]
)
->add(
'displayAs',
ChoiceType::class,
[
'label' => 'forum.search.label.displayAs.label',
'expanded' => true,
'choices' => [
'forum.search.label.displayAs.choices.as_topic' => SearchForum::DISPLAY_AS_TOPIC,
'forum.search.label.displayAs.choices.as_message' => SearchForum::DISPLAY_AS_MESSAGE,
],
]
);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(
array(
'data_class' => SearchForum::class,
'translation_domain' => 'forms',
'csrf_protection' => false,
)
);
}
public function getBlockPrefix()
{
return null;
}
}