|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* YAWIK |
|
4
|
|
|
* |
|
5
|
|
|
* @filesource |
|
6
|
|
|
* @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de) |
|
7
|
|
|
* @license MIT |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
/** */ |
|
11
|
|
|
namespace Core\Form; |
|
12
|
|
|
|
|
13
|
|
|
use Zend\ServiceManager\FactoryInterface; |
|
14
|
|
|
use Zend\ServiceManager\ServiceLocatorInterface; |
|
15
|
|
|
use Core\Entity\Hydrator\EntityHydrator; |
|
16
|
|
|
use Core\Entity\Hydrator\Strategy\FileUploadStrategy; |
|
17
|
|
|
use Auth\Entity\AnonymousUser; |
|
18
|
|
|
use Core\Entity\Hydrator\FileCollectionUploadHydrator; |
|
19
|
|
|
use Zend\Stdlib\AbstractOptions; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Factory for creating file upload formular elements. |
|
23
|
|
|
* |
|
24
|
|
|
* @author Mathias Gelhausen <[email protected]> |
|
25
|
|
|
*/ |
|
26
|
|
|
class FileUploadFactory implements FactoryInterface |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* Service name of the form element used. |
|
30
|
|
|
* |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $fileElement = 'Core/FileUpload'; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Name of the form element. |
|
37
|
|
|
* |
|
38
|
|
|
* @var string |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $fileName = 'file'; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Class name of the file entity to use. |
|
44
|
|
|
* |
|
45
|
|
|
* @var string |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $fileEntityClass = '\Core\Form\FileEntity'; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Should the factored element allow multiple files to be selected? |
|
51
|
|
|
* |
|
52
|
|
|
* @var bool |
|
53
|
|
|
*/ |
|
54
|
|
|
protected $multiple = false; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* The config key in the FileUploadFactory configuration entry. |
|
58
|
|
|
* @var string |
|
59
|
|
|
*/ |
|
60
|
|
|
protected $configKey; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* The configuration stored in the FileUploadFactory configuration entry under {@link $configKey}. |
|
64
|
|
|
* |
|
65
|
|
|
* @var array|null |
|
66
|
|
|
*/ |
|
67
|
|
|
protected $config; |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Optional Module Options. eg. "Jobs/Options" or "Applications/Options" |
|
71
|
|
|
* |
|
72
|
|
|
* @var string|null |
|
73
|
|
|
*/ |
|
74
|
|
|
protected $options; |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* The configuration from the FileUploadFactory configuration |
|
78
|
|
|
* |
|
79
|
|
|
* @param ServiceLocatorInterface $serviceLocator |
|
80
|
|
|
* @return Form |
|
81
|
|
|
*/ |
|
82
|
|
|
public function createService(ServiceLocatorInterface $serviceLocator) |
|
83
|
|
|
{ |
|
84
|
|
|
/* @var $serviceLocator \Zend\Form\FormElementManager */ |
|
85
|
|
|
$service = $serviceLocator->getServiceLocator(); |
|
86
|
|
|
$options=null; |
|
87
|
|
|
if ($this->options) { |
|
|
|
|
|
|
88
|
|
|
$options = $service->get($this->options); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
// Retrieve configuration. |
|
92
|
|
|
if ($this->configKey) { |
|
93
|
|
|
$services = $serviceLocator->getServiceLocator(); |
|
94
|
|
|
$config = $services->get('config'); |
|
95
|
|
|
$config = isset($config['form_elements_config']['file_upload_factories'][$this->configKey]) |
|
96
|
|
|
? $config['form_elements_config']['file_upload_factories'][$this->configKey] |
|
97
|
|
|
: array(); |
|
98
|
|
|
$this->config = $config; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
|
|
102
|
|
|
$form = new Form(); |
|
103
|
|
|
$serviceLocator->injectFactory($form, $serviceLocator); |
|
104
|
|
|
$form->add( |
|
105
|
|
|
array( |
|
106
|
|
|
'type' => $this->fileElement, |
|
107
|
|
|
'name' => $this->fileName, |
|
108
|
|
|
'options' => array( |
|
109
|
|
|
'use_formrow_helper' => false, |
|
110
|
|
|
), |
|
111
|
|
|
'attributes' => array( |
|
112
|
|
|
'class' => 'hide', |
|
113
|
|
|
), |
|
114
|
|
|
) |
|
115
|
|
|
); |
|
116
|
|
|
/* @var $element \Core\Form\Element\FileUpload */ |
|
117
|
|
|
$element = $form->get($this->fileName); |
|
118
|
|
|
$element->setIsMultiple($this->multiple); |
|
119
|
|
|
|
|
120
|
|
|
$user = $serviceLocator->getServiceLocator()->get('AuthenticationService')->getUser(); |
|
121
|
|
|
|
|
122
|
|
|
if (isset($this->config['hydrator']) && $this->config['hydrator']) { |
|
123
|
|
|
/** @noinspection PhpUndefinedVariableInspection */ |
|
124
|
|
|
$hydrator = $services->get('HydratorManager')->get($this->config['hydrator']); |
|
|
|
|
|
|
125
|
|
|
} else { |
|
126
|
|
|
|
|
127
|
|
|
|
|
128
|
|
|
/* @var $fileEntity \Core\Entity\FileInterface */ |
|
129
|
|
|
$fileEntity = new $this->fileEntityClass(); |
|
130
|
|
|
if ($user instanceof AnonymousUser) { |
|
131
|
|
|
$fileEntity->getPermissions()->grant($user, 'all'); |
|
132
|
|
|
} else { |
|
133
|
|
|
$fileEntity->setUser($user); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
$strategy = new FileUploadStrategy($fileEntity); |
|
137
|
|
|
if ($this->multiple) { |
|
138
|
|
|
$hydrator = new FileCollectionUploadHydrator($this->fileName, $strategy); |
|
139
|
|
|
$form->add( |
|
140
|
|
|
array( |
|
141
|
|
|
'type' => 'button', |
|
142
|
|
|
'name' => 'remove', |
|
143
|
|
|
'options' => array( |
|
144
|
|
|
'label' => /*@translate*/ 'Remove all', |
|
145
|
|
|
), |
|
146
|
|
|
'attributes' => array( |
|
147
|
|
|
'class' => 'fu-remove-all btn btn-danger btn-xs pull-right' |
|
148
|
|
|
), |
|
149
|
|
|
) |
|
150
|
|
|
); |
|
151
|
|
|
} else { |
|
152
|
|
|
$hydrator = new EntityHydrator(); |
|
153
|
|
|
$hydrator->addStrategy($this->fileName, $strategy); |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
$form->setHydrator($hydrator); |
|
158
|
|
|
$form->setOptions(array('use_files_array' => true)); |
|
159
|
|
|
|
|
160
|
|
|
$this->configureForm($form, $options); |
|
161
|
|
|
return $form; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Configures the factored form. |
|
166
|
|
|
* |
|
167
|
|
|
* @param \Core\Form\Form $form |
|
168
|
|
|
* @param AbstractOptions $options |
|
169
|
|
|
*/ |
|
170
|
|
|
protected function configureForm($form, AbstractOptions $options) |
|
171
|
|
|
{ |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: