/
Module.php
349 lines (314 loc) · 11.5 KB
/
Module.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
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
<?php declare(strict_types=1);
namespace Cartography;
if (!class_exists(\Generic\AbstractModule::class)) {
require file_exists(dirname(__DIR__) . '/Generic/AbstractModule.php')
? dirname(__DIR__) . '/Generic/AbstractModule.php'
: __DIR__ . '/src/Generic/AbstractModule.php';
}
use Generic\AbstractModule;
use Laminas\EventManager\Event;
use Laminas\EventManager\SharedEventManagerInterface;
use Laminas\Mvc\MvcEvent;
use Laminas\ServiceManager\ServiceLocatorInterface;
/**
* Cartography
*
* Allows to annotate an image or a wms map with the w3c web annotation data
* model and vocabulary.
*
* @copyright Daniel Berthereau, 2018-2023
* @license http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
*/
class Module extends AbstractModule
{
const NAMESPACE = __NAMESPACE__;
protected $dependencies = [
'Annotate',
'DataTypeGeometry',
];
public function onBootstrap(MvcEvent $event): void
{
parent::onBootstrap($event);
if (!$this->areModulesActive($this->dependencies)) {
$this->disableModule(__NAMESPACE__);
return;
}
$this->addAclRules();
}
protected function postInstall(): void
{
$services = $this->getServiceLocator();
$api = $services->get('Omeka\ApiManager');
$settings = $services->get('Omeka\Settings');
// The resource templates are automatically installed during install.
$resourceTemplateSettings = [
'Cartography Describe' => [
'setting' => 'cartography_template_describe',
'data' => [
'oa:motivatedBy' => 'oa:Annotation',
'rdf:value' => 'oa:hasBody',
'oa:hasPurpose' => 'oa:hasBody',
'oa:hasBody' => 'oa:Annotation',
],
],
'Cartography Locate' => [
'setting' => 'cartography_template_locate',
'data' => [
'oa:motivatedBy' => 'oa:Annotation',
'rdf:value' => 'oa:hasBody',
'oa:hasPurpose' => 'oa:hasBody',
'oa:hasBody' => 'oa:Annotation',
],
],
];
$resourceTemplateData = $settings->get('annotate_resource_template_data', []);
foreach ($resourceTemplateSettings as $label => $data) {
$resourceTemplate = $api->read('resource_templates', ['label' => $label])->getContent();
// Add the special resource template settings.
$resourceTemplateData[$resourceTemplate->id()] = $data['data'];
// Set the template as default template.
$settings->set($data['setting'], [$resourceTemplate->id()]);
}
$settings->set('annotate_resource_template_data', $resourceTemplateData);
}
public function uninstall(ServiceLocatorInterface $services): void
{
$this->setServiceLocator($services);
if (!class_exists(\Generic\InstallResources::class)) {
require_once file_exists(dirname(__DIR__) . '/Generic/InstallResources.php')
? dirname(__DIR__) . '/Generic/InstallResources.php'
: __DIR__ . '/src/Generic/InstallResources.php';
}
$installResources = new \Generic\InstallResources($services);
$installResources = $installResources();
foreach (['Cartography Describe', 'Cartography Locate'] as $resourceTemplate) {
$installResources->removeResourceTemplate($resourceTemplate);
}
parent::uninstall($services);
}
/**
* Add ACL rules for this module.
*/
protected function addAclRules(): void
{
/** @var \Omeka\Permissions\Acl $acl */
$acl = $this->getServiceLocator()->get('Omeka\Acl');
$roles = $acl->getRoles();
// TODO Limit rights to access annotate actions too (annotations are already managed).
$acl
->allow(
null,
[Controller\Site\CartographyController::class]
)
->allow(
$roles,
[Controller\Admin\CartographyController::class]
);
}
public function attachListeners(SharedEventManagerInterface $sharedEventManager): void
{
// Events for the admin board.
$sharedEventManager->attach(
'Omeka\Controller\Admin\Item',
'view.show.section_nav',
[$this, 'addTab'],
// The priority should be below Mapping to avoid possible issue.
-1
);
$sharedEventManager->attach(
'Omeka\Controller\Admin\Item',
'view.show.after',
[$this, 'displayTabSection'],
// The priority should be below Mapping to avoid possible issue.
-1
);
// Events for the public front-end.
$controllers = [
'Omeka\Controller\Site\Item',
// 'Omeka\Controller\Site\ItemSet',
// 'Omeka\Controller\Site\Media',
];
foreach ($controllers as $controller) {
// Add the cartography to the resource show public pages.
$sharedEventManager->attach(
$controller,
'view.show.after',
[$this, 'displayPublic']
);
}
$sharedEventManager->attach(
\Omeka\Form\SettingForm::class,
'form.add_elements',
[$this, 'handleMainSettings']
);
$sharedEventManager->attach(
\Omeka\Form\SettingForm::class,
'form.add_input_filters',
[$this, 'handleMainSettingsFilters']
);
$sharedEventManager->attach(
\Omeka\Form\SiteSettingsForm::class,
'form.add_elements',
[$this, 'handleSiteSettings']
);
$sharedEventManager->attach(
\Omeka\Form\SiteSettingsForm::class,
'form.add_input_filters',
[$this, 'handleSiteSettingsFilters']
);
}
public function handleMainSettings(Event $event): void
{
$ckEditorHelper = $this->getServiceLocator()->get('ViewHelperManager')
->get('ckEditor');
$ckEditorHelper();
parent::handleMainSettings($event);
}
public function handleMainSettingsFilters(Event $event): void
{
$inputFilter = version_compare(\Omeka\Module::VERSION, '4', '<')
? $event->getParam('inputFilter')->get('cartography')
: $event->getParam('inputFilter');
$inputFilter
->add([
'name' => 'cartography_display_tab',
'required' => false,
])
->add([
'name' => 'cartography_template_describe',
'required' => false,
])
->add([
'name' => 'cartography_template_describe_empty',
'required' => false,
])
->add([
'name' => 'cartography_template_locate',
'required' => false,
])
->add([
'name' => 'cartography_template_describe_empty',
'required' => false,
]);
}
public function handleSiteSettingsFilters(Event $event): void
{
$inputFilter = version_compare(\Omeka\Module::VERSION, '4', '<')
? $event->getParam('inputFilter')->get('cartography')
: $event->getParam('inputFilter');
$inputFilter
->add([
'name' => 'cartography_append_public',
'required' => false,
])
->add([
'name' => 'cartography_annotate',
'required' => false,
]);
}
/**
* Add a tab to section navigation.
*
* @param Event $event
*/
public function addTab(Event $event): void
{
$services = $this->getServiceLocator();
$acl = $services->get('Omeka\Acl');
$allowed = $acl->userIsAllowed(\Annotate\Entity\Annotation::class, 'read');
if (!$allowed) {
return;
}
$displayTabs = $services->get('Omeka\Settings')->get('cartography_display_tab', []);
if (empty($displayTabs)) {
return;
}
$sectionNav = $event->getParam('section_nav');
if (in_array('describe', $displayTabs)) {
$sectionNav['describe'] = 'Describe'; // @translate
}
if (in_array('locate', $displayTabs)) {
$sectionNav['locate'] = 'Locate'; // @translate
}
$event->setParam('section_nav', $sectionNav);
}
/**
* Display a partial for a resource.
*
* @param Event $event
*/
public function displayTabSection(Event $event): void
{
$services = $this->getServiceLocator();
$settings = $services->get('Omeka\Settings');
$displayTabs = $settings->get('cartography_display_tab', []);
if (empty($displayTabs)) {
return;
}
$acl = $services->get('Omeka\Acl');
$rightRead = $acl->userIsAllowed(\Annotate\Entity\Annotation::class, 'read');
if (!$rightRead) {
return;
}
/** @var \Laminas\View\Renderer\PhpRenderer $view */
$view = $event->getTarget();
$resource = $view->resource;
$displayDescribe = in_array('describe', $displayTabs);
$displayLocate = in_array('locate', $displayTabs);
// This check avoids to load the css and js two times.
$displayAll = $displayDescribe && $displayLocate;
if ($displayDescribe) {
echo $view->cartography($resource, [
'type' => 'describe',
'annotate' => true,
'headers' => true,
'sections' => $displayAll ? ['describe', 'locate'] : ['describe'],
]);
}
if ($displayLocate) {
echo $view->cartography($resource, [
'type' => 'locate',
'annotate' => true,
'headers' => !$displayAll,
'sections' => $displayAll ? ['describe', 'locate'] : ['locate'],
]);
}
}
/**
* Display a partial for a resource in public.
*
* @param Event $event
*/
public function displayPublic(Event $event): void
{
$siteSettings = $this->getServiceLocator()->get('Omeka\Settings\Site');
$displayTabs = $siteSettings->get('cartography_append_public');
if (empty($displayTabs)) {
return;
}
$annotate = (bool) $siteSettings->get('cartography_annotate');
$view = $event->getTarget();
$resource = $view->resource;
$resourceName = $resource->resourceName();
$displayDescribe = in_array('describe_' . $resourceName . '_show', $displayTabs);
$displayLocate = in_array('locate_' . $resourceName . '_show', $displayTabs);
// This check avoids to load the css and js two times.
$displayAll = $displayDescribe && $displayLocate;
if ($displayDescribe) {
echo $view->cartography($resource, [
'type' => 'describe',
'annotate' => $annotate,
'headers' => true,
'sections' => $displayAll ? ['describe', 'locate'] : ['describe'],
]);
}
if ($displayLocate) {
echo $view->cartography($resource, [
'type' => 'locate',
'annotate' => $annotate,
'headers' => !$displayAll,
'sections' => $displayAll ? ['describe', 'locate'] : ['locate'],
]);
}
}
}