|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Media\Form\Type; |
|
4
|
|
|
|
|
5
|
|
|
use Media\Entity\MediaInterface; |
|
6
|
|
|
use Symfony\Component\Form\FormEvent; |
|
7
|
|
|
use Symfony\Component\Form\FormEvents; |
|
8
|
|
|
use Media\Form\Type\MediaTypeInterface; |
|
9
|
|
|
use Symfony\Component\Form\AbstractType; |
|
10
|
|
|
use Media\Provider\MediaProviderInterface; |
|
11
|
|
|
use Symfony\Component\Form\FormBuilderInterface; |
|
12
|
|
|
use Symfony\Component\Form\DataTransformerInterface; |
|
13
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
14
|
|
|
use Symfony\Component\Form\Extension\Core\Type\FileType; |
|
15
|
|
|
use Symfony\Component\Form\Extension\Core\Type\FormType; |
|
16
|
|
|
use Symfony\Component\Form\Extension\Core\Type\CheckboxType; |
|
17
|
|
|
|
|
18
|
|
|
class MediaType extends AbstractType implements MediaTypeInterface |
|
19
|
|
|
{ |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* |
|
23
|
|
|
* @var MediaProviderInterface |
|
24
|
|
|
*/ |
|
25
|
|
|
private $provider; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* |
|
29
|
|
|
* @var MediaInterface |
|
30
|
|
|
*/ |
|
31
|
|
|
private $entityClass; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* |
|
35
|
|
|
* @var DataTransformerInterface |
|
36
|
|
|
*/ |
|
37
|
|
|
private $transformer; |
|
38
|
|
|
|
|
39
|
|
|
public function __construct($transformer, MediaProviderInterface $provider, $entityClass) |
|
40
|
|
|
{ |
|
41
|
|
|
$this->provider = $provider; |
|
42
|
|
|
$this->transformer = $transformer; |
|
43
|
|
|
$this->entityClass = $entityClass; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* |
|
48
|
|
|
* @param OptionsResolver $resolver |
|
49
|
|
|
*/ |
|
50
|
|
|
public function configureOptions(OptionsResolver $resolver) |
|
51
|
|
|
{ |
|
52
|
|
|
$resolver->setDefaults([ |
|
53
|
|
|
'context' => null, |
|
54
|
|
|
'new_on_update' => false, |
|
55
|
|
|
'data_class' => $this->entityClass |
|
56
|
|
|
])->setRequired(['context']); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* |
|
61
|
|
|
* @param FormBuilderInterface $builder |
|
62
|
|
|
* |
|
63
|
|
|
* @param array $options |
|
64
|
|
|
*/ |
|
65
|
|
|
public function buildForm(FormBuilderInterface $builder, array $options) |
|
66
|
|
|
{ |
|
67
|
|
|
|
|
68
|
|
|
$builder->addModelTransformer(new $this->transformer($this->provider, $this->entityClass, $options)); |
|
69
|
|
|
|
|
70
|
|
|
$builder->add('fileContent', FileType::class, [ |
|
71
|
|
|
'label' => false, |
|
72
|
|
|
'required' => false, |
|
73
|
|
|
]); |
|
74
|
|
|
|
|
75
|
|
|
$builder->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event) { |
|
76
|
|
|
if ($event->getData() && $event->getData()->getId()) { |
|
77
|
|
|
$event->getForm()->add('fileContentRemove', CheckboxType::class, [ |
|
78
|
|
|
'data' => false, |
|
79
|
|
|
'mapped' => false, |
|
80
|
|
|
'required' => false |
|
81
|
|
|
]); |
|
82
|
|
|
} |
|
83
|
|
|
}); |
|
84
|
|
|
|
|
85
|
|
|
$builder->addEventListener(FormEvents::SUBMIT, function(FormEvent $event) { |
|
86
|
|
|
if ($event->getForm()->has('fileContentRemove') && $event->getForm()->get('fileContentRemove')->getData()) { |
|
87
|
|
|
$event->setData(null); |
|
88
|
|
|
} |
|
89
|
|
|
}); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* |
|
94
|
|
|
* @return string |
|
95
|
|
|
*/ |
|
96
|
|
|
public function getParent() |
|
97
|
|
|
{ |
|
98
|
|
|
return FormType::class; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
} |
|
102
|
|
|
|