|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace AppBundle\Form\Type; |
|
4
|
|
|
|
|
5
|
|
|
use AppBundle\Entity\Meal; |
|
6
|
|
|
use AppBundle\Service\PhotoService; |
|
7
|
|
|
use Symfony\Component\Form\AbstractType; |
|
8
|
|
|
use Symfony\Component\Form\Extension\Core\Type\CheckboxType; |
|
9
|
|
|
use Symfony\Component\Form\Extension\Core\Type\FileType; |
|
10
|
|
|
use Symfony\Component\Form\Extension\Core\Type\NumberType; |
|
11
|
|
|
use Symfony\Component\Form\Extension\Core\Type\SubmitType; |
|
12
|
|
|
use Symfony\Component\Form\Extension\Core\Type\TextType; |
|
13
|
|
|
use Symfony\Component\Form\Extension\Core\Type\TextareaType; |
|
14
|
|
|
use Symfony\Component\Form\FormBuilderInterface; |
|
15
|
|
|
use Symfony\Component\Form\FormEvent; |
|
16
|
|
|
use Symfony\Component\Form\FormEvents; |
|
17
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
18
|
|
|
use Symfony\Component\Validator\Constraints\Image; |
|
19
|
|
|
|
|
20
|
|
|
class MealType extends AbstractType |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var PhotoService |
|
24
|
|
|
*/ |
|
25
|
|
|
private $photoService; |
|
26
|
|
|
|
|
27
|
|
|
public function __construct(PhotoService $photoService) |
|
28
|
|
|
{ |
|
29
|
|
|
$this->photoService = $photoService; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function buildForm(FormBuilderInterface $builder, array $options) |
|
33
|
|
|
{ |
|
34
|
|
|
$builder |
|
35
|
|
|
->add('name', TextType::class, array( |
|
36
|
|
|
'label' => 'Nom', |
|
37
|
|
|
)) |
|
38
|
|
|
->add('price', PriceType::class, array( |
|
39
|
|
|
'label' => 'Prix', |
|
40
|
|
|
)) |
|
41
|
|
|
->add('description', TextareaType::class, array( |
|
42
|
|
|
'label' => 'Description', |
|
43
|
|
|
)) |
|
44
|
|
|
->add('position', NumberType::class, array( |
|
45
|
|
|
'label' => 'Position', |
|
46
|
|
|
)) |
|
47
|
|
|
->add('active', CheckboxType::class, array( |
|
48
|
|
|
'label' => 'Afficher ce plat', |
|
49
|
|
|
)) |
|
50
|
|
|
->add('portuguese', CheckboxType::class, array( |
|
51
|
|
|
'label' => 'Drapeau portugais', |
|
52
|
|
|
)) |
|
53
|
|
|
->add('photo', FileType::class, array( |
|
54
|
|
|
'label' => 'Photo', |
|
55
|
|
|
'mapped' => false, |
|
56
|
|
|
'constraints' => array( |
|
57
|
|
|
new Image() |
|
58
|
|
|
) |
|
59
|
|
|
)) |
|
60
|
|
|
->add('submit', SubmitType::class, array( |
|
61
|
|
|
'label' => 'Enregistrer', |
|
62
|
|
|
'attr' => array('class' => 'btn btn-success') |
|
63
|
|
|
)) |
|
64
|
|
|
; |
|
65
|
|
|
|
|
66
|
|
|
$builder->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) { |
|
67
|
|
|
$form = $event->getForm(); |
|
68
|
|
|
|
|
69
|
|
|
$meal = $form->getData(); |
|
70
|
|
|
$file = $form->get('photo')->getData(); |
|
71
|
|
|
|
|
72
|
|
|
if (!$form->isValid()) { |
|
73
|
|
|
return; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
if (!$file) { |
|
77
|
|
|
return; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
$this->photoService->upload($meal, $file); |
|
81
|
|
|
}); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function configureOptions(OptionsResolver $resolver) |
|
85
|
|
|
{ |
|
86
|
|
|
$resolver->setDefaults(array( |
|
87
|
|
|
'data_class' => Meal::class |
|
88
|
|
|
)); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|