<?php declare(strict_types=1);
namespace App\Form;
use App\Entity\Order;
use App\Entity\Package;
use App\Repository\PackageRepository;
use App\Utils\CommonConstants;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Security\Core\Security;
/**
* Class OrderFormType
* @package App\Form
*/
class OrderFormType extends AbstractType
{
/**
* @var Security
*/
private $security;
public function __construct(Security $security)
{
$this->security = $security;
}
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('package', EntityType::class, [
'class' => Package::class,
'query_builder' => function (PackageRepository $er) {
$isCoach = null == $this->security->getUser() ? false : in_array(CommonConstants::ROLE_ROLE_COACH, $this->security->getUser()->getRoles());
return $er->createQueryBuilder('p')
->where('p.coach = :coach')
->setParameter('coach', $isCoach);
},
'multiple' => false,
'expanded' => true,
]);
if ($options['submit']) {
$builder->add('submit', SubmitType::class, [
'label' => 'Valider',
'attr' => ['class' => 'btn btn-success']
]);
}
}
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Order::class,
'attr' => ['id' => 'orderForm'],
'submit' => null,
]);
}
}