-
Notifications
You must be signed in to change notification settings - Fork 31
/
SingleUploadSubscriber.php
158 lines (129 loc) · 5.54 KB
/
SingleUploadSubscriber.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
namespace Avocode\FormExtensionsBundle\Form\EventListener;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\ResolvedFormTypeInterface;
/**
* @author Piotr Gołębiewski <loostro@gmail.com>
*/
class SingleUploadSubscriber implements EventSubscriberInterface
{
/**
* @var string Single upload field configs
*/
protected $configs = array();
protected $files = array();
public static function getSubscribedEvents()
{
return array(
FormEvents::PRE_SUBMIT => array('preSubmit', 0),
FormEvents::SUBMIT => array('onSubmit', 0),
FormEvents::POST_SUBMIT => array('postSubmit', 0),
FormEvents::PRE_SET_DATA => array('preSetData', 0),
);
}
public function preSetData(FormEvent $event)
{
$form = $event->getForm();
$obj = $event->getData();
//can be null if prototype in collection
if($obj == null) {
return;
}
foreach ($form->all() as $child) {
if ($this->isFieldSingleUpload($child->getConfig()->getType())) {
$name = $child->getName();
$getterName = 'get'.ucfirst($name);
$this->files[$name] = $obj->$getterName();
}
}
}
public function preSubmit(FormEvent $event)
{
$form = $event->getForm();
$post = $event->getData();
foreach ($form->all() as $child) {
if ($this->isFieldSingleUpload($child->getConfig()->getType())) {
$childPost = $post[$child->getName()];
$options = $child->getConfig()->getOptions();
$this->configs[$child->getName()] = array(
'nameable' => $options['nameable'],
'deleteable' => $options['deleteable'],
);
if ($options['nameable'] && array_key_exists('name', $childPost)) {
// capture name and store it for onSubmit event
$this->configs[$child->getName()]['captured_name'] = $childPost['name'];
}
if ($options['deleteable'] && array_key_exists('delete', $childPost)) {
// capture name and store it for onSubmit event
$this->configs[$child->getName()]['delete'] = $childPost['delete'];
}
// remove additional data to prevent errors
// by overwriting them with posted file
$post[$child->getName()] = $childPost['file'];
}
$event->setData($post);
}
}
public function onSubmit(FormEvent $event)
{
$form = $event->getForm();
$data = $form->getData();
if ($data != null && count($this->configs) > 0) {
foreach ($this->configs as $field => $config) {
if ($config['nameable'] && array_key_exists('captured_name', $config)) {
$getterName = 'get'.ucfirst($config['nameable']);
$setterName = 'set'.ucfirst($config['nameable']);
// save original name for postSubmit event
$config['original_name'] = $data->$getterName();
// set captured name
$data->$setterName($config['captured_name']);
}
if ($config['deleteable'] && array_key_exists('delete', $config) && $config['delete']) {
$getterPath = 'get'.ucfirst($config['deleteable']);
$setterPath = 'set'.ucfirst($config['deleteable']);
// save original file for postSubmit event
$config['original_path'] = $data->$getterPath();
// remove file
$data->$setterPath(null);
}
if(!array_key_exists('delete', $config) || $config['delete'] == false){
$setter = 'set'.ucfirst($field);
$getter = 'get'.ucfirst($field);
if($data->$getter() === null)
$data->$setter($this->files[$field]);
}
}
$event->setData($data);
}
}
public function postSubmit(FormEvent $event)
{
if (count($this->configs) > 0) {
$form = $event->getForm();
$data = $event->getData();
if (!$form->isValid()) {
foreach ($this->configs as $field => $config) {
if ($config['nameable'] && array_key_exists('original_name', $config)) {
// revert to original name
$setterName = 'set'.ucfirst($config['nameable']);
$data->$setterName($config['original_name']);
}
if ($config['deleteable'] && array_key_exists('original_path', $config)) {
$setterPath = 'set'.ucfirst($config['deleteable']);
// revert to original name
$data->$setterPath($config['original_path']);
}
}
$event->setData($data);
}
}
}
private function isFieldSingleUpload(ResolvedFormTypeInterface $formTypeInterface = null)
{
if($formTypeInterface == null) return false;
if($formTypeInterface->getName() == 'afe_single_upload') return true;
return $this->isFieldSingleUpload($formTypeInterface->getParent());
}
}