-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModule.php
More file actions
300 lines (254 loc) · 11 KB
/
Module.php
File metadata and controls
300 lines (254 loc) · 11 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
<?php
namespace Formularium;
use Composer\Semver\Comparator;
use Omeka\Module\AbstractModule;
use Laminas\EventManager\SharedEventManagerInterface;
use Laminas\EventManager\Event;
use Laminas\Mvc\MvcEvent;
use Laminas\ServiceManager\ServiceLocatorInterface;
class Module extends AbstractModule
{
public function onBootstrap(MvcEvent $event)
{
parent::onBootstrap($event);
$services = $this->getServiceLocator();
$acl = $services->get('Omeka\Acl');
$acl->allow(null, 'Formularium\Controller\Site\Form');
$acl->allow(null, 'Formularium\Api\Adapter\FormAdapter', 'read');
$acl->allow(null, 'Formularium\Api\Adapter\FormSubmissionAdapter', 'create');
$acl->allow(null, 'Formularium\Entity\FormulariumFormSubmission', 'create');
$acl->allow(null, 'Formularium\Entity\FormulariumForm', 'read');
$em = $services->get('Omeka\EntityManager');
$formulariumForms = $em->getRepository('Formularium\Entity\FormulariumForm')->findAll();
if ($formulariumForms) {
$config = $services->get('Config');
$config['resource_page_block_layouts']['factories'] ??= [];
foreach ($formulariumForms as $formulariumForm) {
$name = sprintf('formulariumForm:%d', $formulariumForm->getId());
$factory = Service\Site\ResourcePageBlockLayout\FormulariumFormFactory::class;
$config['resource_page_block_layouts']['factories'][$name] = $factory;
}
$services->get('Omeka\ResourcePageBlockLayoutManager')->configure($config['resource_page_block_layouts']);
}
}
public function install(ServiceLocatorInterface $serviceLocator)
{
$connection = $serviceLocator->get('Omeka\Connection');
$connection->executeStatement(<<<'SQL'
CREATE TABLE formularium_form (
id INT AUTO_INCREMENT NOT NULL,
name VARCHAR(255) NOT NULL,
resource_page_block_title VARCHAR(255) NOT NULL,
components LONGTEXT NOT NULL COMMENT '(DC2Type:json)',
actions LONGTEXT NOT NULL COMMENT '(DC2Type:json)',
PRIMARY KEY(id)
) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB
SQL);
$connection->executeStatement(<<<'SQL'
CREATE TABLE formularium_form_submission (
id INT AUTO_INCREMENT NOT NULL,
form_id INT NOT NULL,
site_id INT DEFAULT NULL,
site_page_id INT DEFAULT NULL,
site_page_block_id INT DEFAULT NULL,
resource_id INT DEFAULT NULL,
submitter_id INT DEFAULT NULL,
handler_id INT DEFAULT NULL,
submitted DATETIME NOT NULL,
handled DATETIME DEFAULT NULL,
submitter_email VARCHAR(255) DEFAULT NULL,
data LONGTEXT NOT NULL COMMENT '(DC2Type:json)',
INDEX IDX_26C0C2F45FF69B7D (form_id),
INDEX IDX_26C0C2F4F6BD1646 (site_id),
INDEX IDX_26C0C2F4F7E2812F (site_page_id),
INDEX IDX_26C0C2F43BD1ED87 (site_page_block_id),
INDEX IDX_26C0C2F4919E5513 (submitter_id),
INDEX IDX_26C0C2F4A6E82043 (handler_id),
PRIMARY KEY(id)
) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB
SQL);
$connection->executeStatement(<<<'SQL'
CREATE TABLE formularium_form_submission_file (
id INT AUTO_INCREMENT NOT NULL,
form_submission_id INT NOT NULL,
name VARCHAR(255) NOT NULL,
media_type VARCHAR(255) NOT NULL,
storage_id VARCHAR(190) NOT NULL,
extension VARCHAR(255) DEFAULT NULL,
UNIQUE INDEX UNIQ_E23B837F5CC5DB90 (storage_id),
INDEX IDX_E23B837F422B0E0C (form_submission_id),
PRIMARY KEY(id)
) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB
SQL);
$connection->executeStatement(<<<'SQL'
ALTER TABLE formularium_form_submission
ADD CONSTRAINT FK_26C0C2F45FF69B7D
FOREIGN KEY (form_id) REFERENCES formularium_form (id)
ON DELETE CASCADE
SQL);
$connection->executeStatement(<<<'SQL'
ALTER TABLE formularium_form_submission
ADD CONSTRAINT FK_26C0C2F4F6BD1646
FOREIGN KEY (site_id) REFERENCES site (id)
ON DELETE SET NULL
SQL);
$connection->executeStatement(<<<'SQL'
ALTER TABLE formularium_form_submission
ADD CONSTRAINT FK_26C0C2F4F7E2812F
FOREIGN KEY (site_page_id) REFERENCES site_page (id)
ON DELETE SET NULL
SQL);
$connection->executeStatement(<<<'SQL'
ALTER TABLE formularium_form_submission
ADD CONSTRAINT FK_26C0C2F43BD1ED87 FOREIGN KEY (site_page_block_id)
REFERENCES site_page_block (id)
ON DELETE SET NULL
SQL);
$connection->executeStatement(<<<'SQL'
ALTER TABLE formularium_form_submission
ADD CONSTRAINT FK_26C0C2F489329D25 FOREIGN KEY (resource_id)
REFERENCES resource (id)
ON DELETE SET NULL
SQL);
$connection->executeStatement(<<<'SQL'
ALTER TABLE formularium_form_submission
ADD CONSTRAINT FK_26C0C2F4919E5513
FOREIGN KEY (submitter_id) REFERENCES user (id)
ON DELETE SET NULL
SQL);
$connection->executeStatement(<<<'SQL'
ALTER TABLE formularium_form_submission
ADD CONSTRAINT FK_26C0C2F4A6E82043
FOREIGN KEY (handler_id) REFERENCES user (id)
ON DELETE SET NULL
SQL);
$connection->executeStatement(<<<'SQL'
ALTER TABLE formularium_form_submission_file
ADD CONSTRAINT FK_E23B837F422B0E0C
FOREIGN KEY (form_submission_id) REFERENCES formularium_form_submission (id)
ON DELETE CASCADE
SQL);
}
public function uninstall(ServiceLocatorInterface $serviceLocator)
{
$connection = $serviceLocator->get('Omeka\Connection');
// TODO Delete files from storage
$connection->executeStatement('DROP TABLE formularium_form_submission_file');
$connection->executeStatement('DROP TABLE formularium_form_submission');
$connection->executeStatement('DROP TABLE formularium_form');
}
public function upgrade($oldVersion, $newVersion, ServiceLocatorInterface $serviceLocator)
{
$connection = $serviceLocator->get('Omeka\Connection');
if (Comparator::lessThan($oldVersion, '0.2.0')) {
$connection->executeStatement(<<<'SQL'
ALTER TABLE formularium_form_submission
ADD submitter_email VARCHAR(255) DEFAULT NULL AFTER handled
SQL);
}
if (Comparator::lessThan($oldVersion, '0.3.0')) {
$connection->executeStatement(<<<'SQL'
ALTER TABLE formularium_form_submission
ADD resource_id INT DEFAULT NULL AFTER site_page_block_id
SQL);
$connection->executeStatement(<<<'SQL'
ALTER TABLE formularium_form_submission
ADD CONSTRAINT FK_26C0C2F489329D25 FOREIGN KEY (resource_id)
REFERENCES resource (id)
ON DELETE SET NULL
SQL);
}
if (Comparator::lessThan($oldVersion, '0.3.1')) {
$connection->executeStatement(<<<'SQL'
ALTER TABLE formularium_form
ADD resource_page_block_title VARCHAR(255) NOT NULL AFTER name
SQL);
}
}
public function attachListeners(SharedEventManagerInterface $sharedEventManager)
{
$sharedEventManager->attach(
'Formularium\Controller\Admin\FormSubmission',
'view.search.filters',
[$this, 'onFormSubmissionViewSearchFilters']
);
$sharedEventManager->attach(
'Formularium\Entity\FormulariumFormSubmissionFile',
'entity.remove.post',
[$this, 'onFormSubmissionFileRemove']
);
$sharedEventManager->attach(
'Omeka\Controller\Admin\Index',
'view.browse.after',
[$this, 'onAdminIndexViewBrowseAfter']
);
}
public function getConfig()
{
return require __DIR__ . '/config/module.config.php';
}
public function onFormSubmissionViewSearchFilters(Event $event)
{
$view = $event->getTarget();
$filters = $event->getParam('filters', []);
$query = $event->getParam('query', []);
if (!empty($query['form_id'])) {
$formulariumForm = $view->api()->read('formularium_forms', $query['form_id'])->getContent();
if ($formulariumForm) {
$filters[$view->translate('Form')][] = $formulariumForm->name();
}
}
if (!empty($query['site_id'])) {
$site = $view->api()->read('sites', $query['site_id'])->getContent();
if ($site) {
$filters[$view->translate('Site')][] = $site->title();
}
}
if (!empty($query['site_page_id'])) {
$sitePage = $view->api()->read('site_pages', $query['site_page_id'])->getContent();
if ($sitePage) {
$filters[$view->translate('Page')][] = $sitePage->title();
}
}
if (!empty($query['site_page_block_id'])) {
$filters[$view->translate('Page block ID')][] = $query['site_page_block_id'];
}
if (!empty($query['resource_id'])) {
$filters[$view->translate('Resource ID')][] = $query['resource_id'];
}
if (isset($query['submitter_id']) && $query['submitter_id']) {
$user = $view->api()->read('users', $query['submitter_id'])->getContent();
if ($user) {
$filters[$view->translate('Submitted by (user)')][] = $user->name();
}
}
if (!empty($query['submitter_email'])) {
$filters[$view->translate('Submitted by (email)')][] = $query['submitter_email'];
}
if (isset($query['handled']) && $query['handled'] !== '') {
$filters[$view->translate('Handled')][] = $query['handled'] ? $view->translate('Yes') : $view->translate('No');
}
if (isset($query['handler_id']) && $query['handler_id']) {
$user = $view->api()->read('users', $query['handler_id'])->getContent();
if ($user) {
$filters[$view->translate('Handled by')][] = $user->name();
}
}
$event->setParam('filters', $filters);
}
public function onFormSubmissionFileRemove(Event $event)
{
$formSubmissionFile = $event->getTarget();
$store = $this->getServiceLocator()->get('Omeka\File\Store');
$storagePath = sprintf('formularium/%s', $formSubmissionFile->getFilename());
$store->delete($storagePath);
}
public function onAdminIndexViewBrowseAfter(Event $event)
{
$view = $event->getTarget();
if (!$view->userIsAllowed('Formularium\Controller\Admin\FormSubmission', 'browse')) {
return;
}
echo $view->partial('formularium/common/dashboard');
}
}