-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathModule.php
1144 lines (1044 loc) · 40.7 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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php declare(strict_types=1);
/*
* Copyright Daniel Berthereau, 2017-2024
*
* This software is governed by the CeCILL license under French law and abiding
* by the rules of distribution of free software. You can use, modify and/ or
* redistribute the software under the terms of the CeCILL license as circulated
* by CEA, CNRS and INRIA at the following URL "http://www.cecill.info".
*
* As a counterpart to the access to the source code and rights to copy, modify
* and redistribute granted by the license, users are provided only with a
* limited warranty and the software's author, the holder of the economic
* rights, and the successive licensors have only limited liability.
*
* In this respect, the user's attention is drawn to the risks associated with
* loading, using, modifying and/or developing or reproducing the software by
* the user in light of its specific status of free software, that may mean that
* it is complicated to manipulate, and that also therefore means that it is
* reserved for developers and experienced professionals having in-depth
* computer knowledge. Users are therefore encouraged to load and test the
* software's suitability as regards their requirements in conditions enabling
* the security of their systems and/or data to be ensured and, more generally,
* to use and operate it in the same conditions as regards security.
*
* The fact that you are presently reading this means that you have had
* knowledge of the CeCILL license and that you accept its terms.
*/
namespace Annotate;
if (!class_exists(\Common\TraitModule::class)) {
require_once dirname(__DIR__) . '/Common/TraitModule.php';
}
use Annotate\Entity\Annotation;
use Annotate\Permissions\Acl;
use Common\Stdlib\PsrMessage;
use Common\TraitModule;
use Laminas\EventManager\Event;
use Laminas\EventManager\SharedEventManagerInterface;
use Laminas\Mvc\MvcEvent;
use Laminas\Permissions\Acl\Acl as LaminasAcl;
use Omeka\Api\Representation\AbstractEntityRepresentation;
use Omeka\Api\Representation\AbstractResourceEntityRepresentation;
use Omeka\Api\Representation\ItemRepresentation;
use Omeka\Api\Representation\ItemSetRepresentation;
use Omeka\Api\Representation\MediaRepresentation;
use Omeka\Api\Representation\UserRepresentation;
use Omeka\Entity\AbstractEntity;
use Omeka\Module\AbstractModule;
/**
* Annotate
*
* @copyright Daniel Berthereau, 2017-2024
* @license http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
*/
class Module extends AbstractModule
{
use TraitModule;
const NAMESPACE = __NAMESPACE__;
protected $dependencies = [
'Common',
'CustomVocab',
];
public function onBootstrap(MvcEvent $event): void
{
parent::onBootstrap($event);
// TODO Add filters (don't display when resource is private, like media?).
// TODO Set Acl public rights to false when the visibility filter will be ready.
// $this->addEntityManagerFilters();
$this->addAclRoleAndRules();
}
protected function preInstall(): void
{
$services = $this->getServiceLocator();
$translate = $services->get('ControllerPluginManager')->get('translate');
if (!method_exists($this, 'checkModuleActiveVersion') || !$this->checkModuleActiveVersion('Common', '3.4.53')) {
$message = new \Omeka\Stdlib\Message(
$translate('The module %1$s should be upgraded to version %2$s or later.'), // @translate
'Common', '3.4.53'
);
throw new \Omeka\Module\Exception\ModuleCannotInstallException((string) $message);
}
}
protected function postInstall(): void
{
$services = $this->getServiceLocator();
$api = $services->get('Omeka\ApiManager');
$settings = $services->get('Omeka\Settings');
$messenger = $services->get('ControllerPluginManager')->get('messenger');
// TODO Replace the resource templates for annotations that are not items.
$resourceTemplateSettings = [
'Annotation' => [
'oa:motivatedBy' => 'oa:Annotation',
'rdf:value' => 'oa:hasBody',
'oa:hasPurpose' => 'oa:hasBody',
'dcterms:language' => 'oa:hasBody',
'oa:hasSource' => 'oa:hasTarget',
'rdf:type' => 'oa:hasTarget',
'dcterms:format' => 'oa:hasTarget',
],
];
$resourceTemplateData = $settings->get('annotate_resource_template_data', []);
foreach ($resourceTemplateSettings as $label => $data) {
try {
$resourceTemplate = $api->read('resource_templates', ['label' => $label])->getContent();
} catch (\Omeka\Api\Exception\NotFoundException $e) {
$message = new PsrMessage(
'The settings to manage the annotation template are not saved. You shoud edit the resource template "Annotation" manually.' // @translate
);
$messenger->addWarning($message);
continue;
}
// Add the special resource template settings.
$resourceTemplateData[$resourceTemplate->id()] = $data;
}
$settings->set('annotate_resource_template_data', $resourceTemplateData);
}
protected function postUninstall(): void
{
$services = $this->getServiceLocator();
require_once dirname(__DIR__) . '/Common/InstallResources.php';
$installResources = new \Common\InstallResources($services);
$installResources = $installResources();
if (!empty($_POST['remove-vocabulary'])) {
$prefix = 'rdf';
$installResources->removeVocabulary($prefix);
$prefix = 'oa';
$installResources->removeVocabulary($prefix);
}
if (!empty($_POST['remove-custom-vocab'])) {
$customVocab = 'Annotation oa:motivatedBy';
$installResources->removeCustomVocab($customVocab);
$customVocab = 'Annotation Body oa:hasPurpose';
$installResources->removeCustomVocab($customVocab);
$customVocab = 'Annotation Target dcterms:format';
$installResources->removeCustomVocab($customVocab);
$customVocab = 'Annotation Target rdf:type';
$installResources->removeCustomVocab($customVocab);
}
if (!empty($_POST['remove-template'])) {
$resourceTemplate = 'Annotation';
$installResources->removeResourceTemplate($resourceTemplate);
}
}
public function warnUninstall(Event $event): void
{
$view = $event->getTarget();
$module = $view->vars()->module;
if ($module->getId() != __NAMESPACE__) {
return;
}
$services = $this->getServiceLocator();
$t = $services->get('MvcTranslator');
$vocabularyLabels = 'RDF Concepts" / "Web Annotation Ontology';
$customVocabs = 'Annotation oa:motivatedBy" / "oa:hasPurpose" / "rdf:type" / "dcterms:format';
$resourceTemplates = 'Annotation';
$html = '<p>';
$html .= '<strong>';
$html .= $t->translate('WARNING'); // @translate
$html .= '</strong>' . ': ';
$html .= '</p>';
$html .= '<p>';
$html .= $t->translate('All the annotations will be removed.'); // @translate
$html .= '</p>';
$html .= '<p>';
$html .= sprintf(
$t->translate('If checked, the values of the vocabularies "%s" will be removed too. The class of the resources that use a class of these vocabularies will be reset.'), // @translate
$vocabularyLabels
);
$html .= '</p>';
$html .= '<label><input name="remove-vocabulary" type="checkbox" form="confirmform">';
$html .= sprintf($t->translate('Remove the vocabularies "%s"'), $vocabularyLabels); // @translate
$html .= '</label>';
$html .= '<p>';
$html .= sprintf(
$t->translate('If checked, the custom vocabs "%s" will be removed too.'), // @translate
$customVocabs
);
$html .= '</p>';
$html .= '<label><input name="remove-custom-vocab" type="checkbox" form="confirmform">';
$html .= sprintf($t->translate('Remove the custom vocabs "%s"'), $customVocabs); // @translate
$html .= '</label>';
$html .= '<p>';
$html .= sprintf(
$t->translate('If checked, the resource templates "%s" will be removed too. The resource template of the resources that use it will be reset.'), // @translate
$resourceTemplates
);
$html .= '</p>';
$html .= '<label><input name="remove-template" type="checkbox" form="confirmform">';
$html .= sprintf($t->translate('Remove the resource templates "%s"'), $resourceTemplates); // @translate
$html .= '</label>';
echo $html;
}
/**
* Add ACL role and rules for this module.
*
* @todo Keep rights for Annotation only (body and target are internal classes).
*/
protected function addAclRoleAndRules(): void
{
/** @var \Omeka\Permissions\Acl $acl */
$services = $this->getServiceLocator();
$acl = $services->get('Omeka\Acl');
// Since Omeka 1.4, modules are ordered, so Guest come after Annotate.
// See \Guest\Module::onBootstrap().
if (!$acl->hasRole('guest')) {
$acl->addRole('guest');
}
if (!$acl->hasRole('guest_private')) {
$acl->addRole('guest_private');
}
$acl
->addRole(Acl::ROLE_ANNOTATOR)
->addRoleLabel(Acl::ROLE_ANNOTATOR, 'Annotator'); // @translate
$settings = $services->get('Omeka\Settings');
// TODO Set rights to false when the visibility filter will be ready.
// TODO Check if public can annotate and flag, and read annotations and own ones.
$publicViewAnnotate = $settings->get('annotate_public_allow_view', true);
if ($publicViewAnnotate) {
$publicAllowAnnotate = $settings->get('annotate_public_allow_annotate', false);
if ($publicAllowAnnotate) {
$this->addRulesForVisitorAnnotators($acl);
} else {
$this->addRulesForVisitors($acl);
}
}
// Identified users can annotate. Reviewer and above can approve. Admins
// can delete.
$this->addRulesForAnnotator($acl);
$this->addRulesForAnnotators($acl);
$this->addRulesForApprobators($acl);
$this->addRulesForAdmins($acl);
}
/**
* Add ACL rules for visitors (read only).
*
* @todo Add rights to update annotation (flag only).
*/
protected function addRulesForVisitors(LaminasAcl $acl): void
{
$acl
->allow(
null,
[Annotation::class],
['read']
)
->allow(
null,
[Api\Adapter\AnnotationAdapter::class],
['search', 'read']
)
->allow(
null,
[Controller\Site\AnnotationController::class],
['index', 'browse', 'show', 'search', 'flag']
);
}
/**
* Add ACL rules for annotator visitors.
*/
protected function addRulesForVisitorAnnotators(LaminasAcl $acl): void
{
$acl
->allow(
null,
[Annotation::class],
['read', 'create']
)
->allow(
null,
[Api\Adapter\AnnotationAdapter::class],
['search', 'read', 'create']
)
->allow(
null,
[Controller\Site\AnnotationController::class],
['index', 'browse', 'show', 'search', 'add', 'flag']
);
}
/**
* Add ACL rules for annotator.
*/
protected function addRulesForAnnotator(LaminasAcl $acl): void
{
// The annotator has less rights than Researcher for core resources, but
// similar rights for annotations that Author has for core resources.
// The rights related to annotation are set with all other annotators.
$acl
->allow(
[\Annotate\Permissions\Acl::ROLE_ANNOTATOR],
[
'Omeka\Controller\Admin\Index',
'Omeka\Controller\Admin\Item',
'Omeka\Controller\Admin\ItemSet',
'Omeka\Controller\Admin\Media',
],
[
'index',
'browse',
'show',
'show-details',
]
)
->allow(
[\Annotate\Permissions\Acl::ROLE_ANNOTATOR],
[
'Omeka\Controller\Admin\Item',
'Omeka\Controller\Admin\ItemSet',
'Omeka\Controller\Admin\Media',
],
[
'search',
'sidebar-select',
]
)
->allow(
[\Annotate\Permissions\Acl::ROLE_ANNOTATOR],
['Omeka\Controller\Admin\User'],
['show', 'edit']
)
->allow(
[\Annotate\Permissions\Acl::ROLE_ANNOTATOR],
['Omeka\Api\Adapter\UserAdapter'],
['read', 'update', 'search']
)
->allow(
[\Annotate\Permissions\Acl::ROLE_ANNOTATOR],
[\Omeka\Entity\User::class],
['read']
)
->allow(
[\Annotate\Permissions\Acl::ROLE_ANNOTATOR],
[\Omeka\Entity\User::class],
['update', 'change-password', 'edit-keys'],
new \Omeka\Permissions\Assertion\IsSelfAssertion
)
// TODO Remove this rule for Omeka >= 1.2.1.
->deny(
[\Annotate\Permissions\Acl::ROLE_ANNOTATOR],
[
'Omeka\Controller\SiteAdmin\Index',
'Omeka\Controller\SiteAdmin\Page',
]
)
->deny(
[\Annotate\Permissions\Acl::ROLE_ANNOTATOR],
['Omeka\Controller\Admin\User'],
['browse']
);
}
/**
* Add ACL rules for annotators (not visitor).
*/
protected function addRulesForAnnotators(LaminasAcl $acl): void
{
$annotators = $acl->getRoles();
$acl
->allow(
$annotators,
[Annotation::class],
['create']
)
->allow(
$annotators,
[Annotation::class],
['update', 'delete'],
new \Omeka\Permissions\Assertion\OwnsEntityAssertion
)
->allow(
$annotators,
[Api\Adapter\AnnotationAdapter::class],
['search', 'read', 'create', 'update', 'delete', 'batch_create', 'batch_update', 'batch_delete']
)
->allow(
$annotators,
[Controller\Site\AnnotationController::class]
)
;
// Unset guest in admin.
$guestId = array_search('guest', $annotators);
unset($annotators[$guestId]);
$acl
->allow(
$annotators,
[Controller\Admin\AnnotationController::class],
['index', 'search', 'browse', 'show', 'show-details', 'add', 'edit', 'delete', 'delete-confirm', 'flag']
);
}
/**
* Add ACL rules for approbators.
*/
protected function addRulesForApprobators(LaminasAcl $acl): void
{
// Admin are approbators too, but rights are set below globally.
$approbators = [
\Omeka\Permissions\Acl::ROLE_REVIEWER,
\Omeka\Permissions\Acl::ROLE_EDITOR,
];
// "view-all" is added via main acl factory for resources.
$acl
->allow(
[\Omeka\Permissions\Acl::ROLE_REVIEWER],
[Annotation::class],
['read', 'create', 'update']
)
->allow(
[\Omeka\Permissions\Acl::ROLE_REVIEWER],
[Annotation::class],
['delete'],
new \Omeka\Permissions\Assertion\OwnsEntityAssertion
)
->allow(
[\Omeka\Permissions\Acl::ROLE_EDITOR],
[Annotation::class],
['read', 'create', 'update', 'delete']
)
->allow(
$approbators,
[Api\Adapter\AnnotationAdapter::class],
['search', 'read', 'create', 'update', 'delete', 'batch_create', 'batch_update', 'batch_delete']
)
->allow(
$approbators,
[Controller\Site\AnnotationController::class]
)
->allow(
$approbators,
Controller\Admin\AnnotationController::class,
[
'index',
'search',
'browse',
'show',
'show-details',
'add',
'edit',
'delete',
'delete-confirm',
'flag',
'batch-approve',
'batch-unapprove',
'batch-flag',
'batch-unflag',
'batch-set-spam',
'batch-set-not-spam',
'toggle-approved',
'toggle-flagged',
'toggle-spam',
'batch-delete',
'batch-delete-all',
'batch-update',
'approve',
'unflag',
'set-spam',
'set-not-spam',
'show-details',
]
);
}
/**
* Add ACL rules for approbators.
*/
protected function addRulesForAdmins(LaminasAcl $acl): void
{
$admins = [
\Omeka\Permissions\Acl::ROLE_GLOBAL_ADMIN,
\Omeka\Permissions\Acl::ROLE_SITE_ADMIN,
];
$acl
->allow(
$admins,
[
Annotation::class,
Api\Adapter\AnnotationAdapter::class,
Controller\Site\AnnotationController::class,
Controller\Admin\AnnotationController::class,
]
);
}
public function attachListeners(SharedEventManagerInterface $sharedEventManager): void
{
// Add the Open Annotation part to the representation.
$representations = [
'users' => UserRepresentation::class,
'item_sets' => ItemSetRepresentation::class,
'items' => ItemRepresentation::class,
'media' => MediaRepresentation::class,
];
foreach ($representations as $representation) {
$sharedEventManager->attach(
$representation,
'rep.resource.json',
[$this, 'filterJsonLd']
);
}
// TODO Add the special data to the resource template.
// Allows to search resource template by resource class.
$sharedEventManager->attach(
\Omeka\Api\Adapter\ResourceTemplateAdapter::class,
'api.search.query',
[$this, 'searchQueryResourceTemplate']
);
// 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 annotations to the resource show public pages.
$sharedEventManager->attach(
$controller,
'view.show.after',
[$this, 'displayPublic']
);
}
// Manage the search query with special fields that are not present in
// default search form.
$sharedEventManager->attach(
\Annotate\Controller\Admin\AnnotationController::class,
'view.advanced_search',
[$this, 'handleViewAdvancedSearch']
);
/* // TODO Include advanced search sidebar query form for annotations.
// Add search fields to the sidebar query form in advanced search pages.
$sharedEventManager->attach(
'Omeka\Controller\Admin\Query',
'view.advanced_search',
[$this, 'handleViewAdvancedSearch']
);
*/
// Filter the search filters for the advanced search pages.
$sharedEventManager->attach(
\Annotate\Controller\Admin\AnnotationController::class,
'view.search.filters',
[$this, 'filterSearchFiltersAnnotation']
);
// Events for the admin board.
$controllers = [
'Omeka\Controller\Admin\Item',
'Omeka\Controller\Admin\ItemSet',
'Omeka\Controller\Admin\Media',
\Annotate\Controller\Admin\AnnotationController::class,
];
foreach ($controllers as $controller) {
$sharedEventManager->attach(
$controller,
'view.show.after',
[$this, 'addHeadersAdmin']
);
$sharedEventManager->attach(
$controller,
'view.add.before',
[$this, 'addHeadersAdmin']
);
$sharedEventManager->attach(
$controller,
'view.edit.before',
[$this, 'addHeadersAdmin']
);
$sharedEventManager->attach(
$controller,
'view.show.section_nav',
[$this, 'addTab']
);
$sharedEventManager->attach(
$controller,
'view.show.after',
[$this, 'displayListAndForm']
);
// Add the details to the resource browse admin pages.
$sharedEventManager->attach(
$controller,
'view.details',
[$this, 'viewDetails']
);
// Add the tab form to the resource edit admin pages.
// Note: it can't be added to the add form, because it has no sense
// to annotate something that does not exist.
$sharedEventManager->attach(
$controller,
'view.edit.section_nav',
[$this, 'addTab']
);
$sharedEventManager->attach(
$controller,
'view.edit.form.after',
[$this, 'displayList']
);
}
$sharedEventManager->attach(
\Annotate\Controller\Admin\AnnotationController::class,
'view.browse.before',
[$this, 'addHeadersAdmin']
);
// Add a tab to the resource template admin pages.
// Can be added to the view of the form too.
$sharedEventManager->attach(
\Omeka\Api\Adapter\ResourceTemplateAdapter::class,
'api.create.post',
[$this, 'handleResourceTemplateCreateOrUpdatePost']
);
$sharedEventManager->attach(
\Omeka\Api\Adapter\ResourceTemplateAdapter::class,
'api.update.post',
[$this, 'handleResourceTemplateCreateOrUpdatePost']
);
$sharedEventManager->attach(
\Omeka\Api\Adapter\ResourceTemplateAdapter::class,
'api.delete.post',
[$this, 'handleResourceTemplateDeletePost']
);
// Display a warn before uninstalling.
$sharedEventManager->attach(
'Omeka\Controller\Admin\Module',
'view.details',
[$this, 'warnUninstall']
);
// Module Csv Import.
$sharedEventManager->attach(
\CSVImport\Form\MappingForm::class,
'form.add_elements',
[$this, 'addCsvImportFormElements']
);
}
/**
* Add the annotation data to the resource JSON-LD.
*/
public function filterJsonLd(Event $event): void
{
if (!$this->userCanRead()) {
return;
}
$resource = $event->getTarget();
$entityColumnName = $this->columnNameOfRepresentation($resource);
$api = $this->getServiceLocator()->get('Omeka\ApiManager');
$annotations = $api
->search('annotations', [$entityColumnName => $resource->id()], ['responseContent' => 'reference'])
->getContent();
if ($annotations) {
$jsonLd = $event->getParam('jsonLd');
// It must be a property, not a class. Cf. iiif too, that uses annotations = iiif_prezi:annotations
// Note: Omeka uses singular for "o:item_set" (array for item), but
// plural for "o:items" (a link for item sets), but singular "o:item"
// for medias. "o:site" uses singular (array for items).
// Anyway, all other terms are singular (dublin core, etc.).
$jsonLd['o:annotation'] = $annotations;
/*
$jsonLd['o:annotations'] = [
'@id' => $this->getServiceLocator()->get('ViewHelperManager')->get('url')
->__invoke('api/default', ['resource' => 'annotations'], ['query' => ['resource_id' => $resource->id()], 'force_canonical' => true]),
];
*/
$event->setParam('jsonLd', $jsonLd);
}
}
/**
* Helper to filter search queries for resource templates.
*/
public function searchQueryResourceTemplate(Event $event): void
{
$query = $event->getParam('request')->getContent();
if (empty($query['resource_class'])) {
return;
}
[$prefix, $localName] = explode(':', $query['resource_class']);
/** @var \Doctrine\ORM\QueryBuilder $qb */
$qb = $event->getParam('queryBuilder');
/** @var \Omeka\Api\Adapter\ResourceTemplateAdapter $adapter */
$adapter = $event->getTarget();
$expr = $qb->expr();
$resourceClassAlias = $adapter->createAlias();
$qb->innerJoin(
'omeka_root.resourceClass',
$resourceClassAlias
);
$vocabularyAlias = $adapter->createAlias();
$qb->innerJoin(
\Omeka\Entity\Vocabulary::class,
$vocabularyAlias,
\Doctrine\ORM\Query\Expr\Join::WITH,
$expr->eq($resourceClassAlias . '.vocabulary', $vocabularyAlias . '.id')
);
$qb->andWhere(
$expr->andX(
$expr->eq(
$vocabularyAlias . '.prefix',
$adapter->createNamedParameter($qb, $prefix)
),
$expr->eq(
$resourceClassAlias . '.localName',
$adapter->createNamedParameter($qb, $localName)
)
)
);
}
/**
* Display the advanced search form for annotations via partial.
*/
public function handleViewAdvancedSearch(Event $event): void
{
$query = $event->getParam('query', []);
$query['datetime'] ??= '';
$partials = $event->getParam('partials', []);
// Remove the resource class field, since it is always "oa:Annotation".
$key = array_search('common/advanced-search/resource-class', $partials);
if ($key !== false) {
unset($partials[$key]);
}
// Replace the resource template field, since the templates are
// restricted to the class "oa:Annotation".
$key = array_search('common/advanced-search/resource-template', $partials);
if ($key === false) {
$partials[] = 'common/advanced-search/resource-template-annotation';
} else {
$partials[$key] = 'common/advanced-search/resource-template-annotation';
}
$partials[] = 'common/advanced-search/date-time-annotation';
// TODO Add a search form on the metadata of the resources.
$event->setParam('query', $query);
$event->setParam('partials', $partials);
}
/**
* Filter search filters of annotations for display.
*/
public function filterSearchFiltersAnnotation(Event $event): void
{
$query = $event->getParam('query', []);
$view = $event->getTarget();
$normalizeDateTimeQuery = $view->plugin('normalizeDateTimeQuery');
if (empty($query['datetime'])) {
$query['datetime'] = [];
} else {
if (!is_array($query['datetime'])) {
$query['datetime'] = [$query['datetime']];
}
foreach ($query['datetime'] as $key => $datetime) {
$datetime = $normalizeDateTimeQuery($datetime);
if ($datetime) {
$query['datetime'][$key] = $datetime;
} else {
unset($query['datetime'][$key]);
}
}
}
if (!empty($query['created'])) {
$datetime = $normalizeDateTimeQuery($query['created'], 'created');
if ($datetime) {
$query['datetime'][] = $datetime;
}
}
if (!empty($query['modified'])) {
$datetime = $normalizeDateTimeQuery($query['modified'], 'modified');
if ($datetime) {
$query['datetime'][] = $datetime;
}
}
if (empty($query['datetime'])) {
return;
}
$filters = $event->getParam('filters');
$translate = $view->plugin('translate');
$queryTypes = [
'>' => $translate('after'),
'>=' => $translate('after or on'),
'=' => $translate('on'),
'<>' => $translate('not on'),
'<=' => $translate('before or on'),
'<' => $translate('before'),
'gte' => $translate('after or on'),
'gt' => $translate('after'),
'eq' => $translate('on'),
'neq' => $translate('not on'),
'lte' => $translate('before or on'),
'lt' => $translate('before'),
'ex' => $translate('has any date / time'),
'nex' => $translate('has no date / time'),
];
$next = false;
foreach ($query['datetime'] as $queryRow) {
$joiner = $queryRow['joiner'];
$field = $queryRow['field'];
$type = $queryRow['type'];
$datetimeValue = $queryRow['value'];
$fieldLabel = $field === 'modified' ? $translate('Modified') : $translate('Created');
$filterLabel = $fieldLabel . ' ' . $queryTypes[$type];
if ($next) {
if ($joiner === 'or') {
$filterLabel = $translate('OR') . ' ' . $filterLabel;
} else {
$filterLabel = $translate('AND') . ' ' . $filterLabel;
}
} else {
$next = true;
}
$filters[$filterLabel][] = $datetimeValue;
}
$event->setParam('filters', $filters);
}
public function handleResourceTemplateCreateOrUpdatePost(Event $event): void
{
// TODO Allow to require a value for body or target via the template.
// The acl are already checked via the api.
$request = $event->getParam('request');
$response = $event->getParam('response');
$services = $this->getServiceLocator();
$api = $services->get('Omeka\ApiManager');
$controllerPlugins = $services->get('ControllerPluginManager');
$annotationPartMapper = $controllerPlugins->get('annotationPartMapper');
$result = [];
$requestContent = $request->getContent();
$requestResourceProperties = $requestContent['o:resource_template_property'] ?? [];
foreach ($requestResourceProperties as $propertyId => $requestResourceProperty) {
if (!isset($requestResourceProperty['data']['annotation_part'])) {
continue;
}
try {
/** @var \Omeka\Api\Representation\PropertyRepresentation $property */
$property = $api->read('properties', $propertyId)->getContent();
} catch (\Omeka\Api\Exception\NotFoundException $e) {
continue;
}
$term = $property->term();
$result[$term] = $annotationPartMapper($term, $requestResourceProperty['data']['annotation_part']);
}
$resourceTemplateId = $response->getContent()->getId();
$settings = $services->get('Omeka\Settings');
$resourceTemplateData = $settings->get('annotate_resource_template_data', []);
$resourceTemplateData[$resourceTemplateId] = $result;
$settings->set('annotate_resource_template_data', $resourceTemplateData);
}
public function handleResourceTemplateDeletePost(Event $event): void
{
// The acl are already checked via the api.
$id = $event->getParam('request')->getId();
$services = $this->getServiceLocator();
$settings = $services->get('Omeka\Settings');
$resourceTemplateData = $settings->get('annotate_resource_template_data', []);
unset($resourceTemplateData[$id]);
$settings->set('annotate_resource_template_data', $resourceTemplateData);
}
public function addCsvImportFormElements(Event $event): void
{
/** @var \CSVImport\Form\MappingForm $form */
$form = $event->getTarget();
$resourceType = $form->getOption('resource_type');
if ($resourceType !== 'annotations') {
return;
}
$services = $this->getServiceLocator();
$acl = $services->get('Omeka\Acl');
if (!$acl->userIsAllowed(Annotation::class, 'create')) {
return;
}
$form->addResourceElements();
if ($acl->userIsAllowed(\Annotate\Entity\Annotation::class, 'change-owner')) {
$form->addOwnerElement();
}
$form->addProcessElements();
$form->addAdvancedElements();
}
/**
* Add the headers for admin management.
*/
public function addHeadersAdmin(Event $event): void
{
// Hacked, because the admin layout doesn't use a partial or a trigger
// for the search engine.
$view = $event->getTarget();
// TODO How to attach all admin events only before 1.3?
if (!$view->params()->fromRoute('__ADMIN__')) {
return;
}
$view->headLink()
->appendStylesheet($view->assetUrl('css/annotate-admin.css', 'Annotate'));
$view->headScript()
->appendFile($view->assetUrl('js/annotate-admin.js', 'Annotate'), 'text/javascript', ['defer' => 'defer']);
}
/**
* Add a tab to section navigation.
*/
public function addTab(Event $event): void
{
$sectionNav = $event->getParam('section_nav');
$sectionNav['annotate'] = 'Annotations'; // @translate
$event->setParam('section_nav', $sectionNav);
}
/**
* Display a partial for a resource.
*/
public function displayListAndForm(Event $event): void
{
$resource = $event->getTarget()->resource;
$acl = $this->getServiceLocator()->get('Omeka\Acl');
$allowed = $acl->userIsAllowed(\Omeka\Entity\Item::class, 'create');
echo '<div id="annotate" class="section annotate">';
$this->displayResourceAnnotations($event, $resource, false);
if ($allowed) {
$this->displayForm($event);
}
echo '</div>';
}
/**
* Display the list for a resource.
*/
public function displayList(Event $event): void
{
$vars = $event->getTarget()->vars();
// Manage add/edit form.
if (isset($vars->resource)) {
$resource = $vars->resource;
} elseif (isset($vars->item)) {
$resource = $vars->item;