-
Notifications
You must be signed in to change notification settings - Fork 1
/
Module.php
195 lines (167 loc) · 6.46 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
<?php declare(strict_types=1);
namespace Bibliography;
if (!class_exists(\Common\TraitModule::class)) {
require_once dirname(__DIR__) . '/Common/TraitModule.php';
}
use Common\Stdlib\PsrMessage;
use Common\TraitModule;
use Laminas\EventManager\Event;
use Laminas\EventManager\SharedEventManagerInterface;
use Laminas\ModuleManager\ModuleManager;
use Laminas\Mvc\MvcEvent;
use Omeka\Module\AbstractModule;
/**
* Bibliography
*
* Tools to manage bibliographic items.
*
* @copyright Daniel Berthereau, 2018-2024
* @license http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
*/
class Module extends AbstractModule
{
use TraitModule;
const NAMESPACE = __NAMESPACE__;
public function init(ModuleManager $moduleManager): void
{
require_once __DIR__ . '/vendor/autoload.php';
}
protected function preInstall(): void
{
$services = $this->getServiceLocator();
$translate = $services->get('ControllerPluginManager')->get('translate');
$translator = $services->get('MvcTranslator');
if (!method_exists($this, 'checkModuleActiveVersion') || !$this->checkModuleActiveVersion('Common', '3.4.63')) {
$message = new \Omeka\Stdlib\Message(
$translate('The module %1$s should be upgraded to version %2$s or later.'), // @translate
'Common', '3.4.63'
);
throw new \Omeka\Module\Exception\ModuleCannotInstallException((string) $message);
}
$file = __DIR__ . '/vendor/autoload.php';
if (!file_exists($file)) {
$message = new PsrMessage(
'The libraries should be installed. See module’s installation documentation.' // @translate
);
throw new \Omeka\Module\Exception\ModuleCannotInstallException((string) $message->setTranslator($translator));
}
// Note: The creation of the vocabulary directly failed in previous
// veresion, so there were a pull request https://github.com/omeka/omeka-s/pull/1335 (Omeka < 2.1)
// and a way to import it via sql.
// The fix was done separetly in https://github.com/omeka/omeka-s/commit/8d1476bcc8f2ec51126a44ea7497025ea1dbcb3b.
}
protected function postInstall(): void
{
$this->uninstallModuleCitation();
}
public function onBootstrap(MvcEvent $event): void
{
parent::onBootstrap($event);
$this->getServiceLocator()->get('Omeka\Acl')
->allow(
null,
['Bibliography\Controller\Bibliography'],
['output']
)
;
}
public function attachListeners(SharedEventManagerInterface $sharedEventManager): void
{
// The site is not set yet, so checks are done in method.
$sharedEventManager->attach(
'Omeka\Controller\Site\Item',
'view.show.before',
[$this, 'handleDisplayCitation']
);
$sharedEventManager->attach(
'Omeka\Controller\Site\Item',
'view.show.after',
[$this, 'handleDisplayCitation']
);
$sharedEventManager->attach(
\Omeka\Form\SettingForm::class,
'form.add_elements',
[$this, 'handleMainSettings']
);
// TODO Remove in version of Common 3.4.64.
$sharedEventManager->attach(
\Omeka\Form\SettingForm::class,
'form.add_input_filters',
[$this, 'handleMainSettingsFilters']
);
$sharedEventManager->attach(
\Omeka\Form\SiteSettingsForm::class,
'form.add_elements',
[$this, 'handleSiteSettings']
);
}
public function handleMainSettingsFilters(Event $event): void
{
$inputFilter = $event->getParam('inputFilter');
$inputFilter
->add([
'name' => 'bibliography_crossref_email',
'required' => false,
]);
}
public function handleDisplayCitation(Event $event): void
{
/**
* @var \Omeka\Settings\SiteSettings $siteSettings
* @var \Laminas\View\Renderer\PhpRenderer $view
* @var \Omeka\Api\Representation\AbstractResourceEntityRepresentation $resource
* @see \Bibliography\View\Helper\Citation
*/
$services = $this->getServiceLocator();
$siteSettings = $services->get('Omeka\Settings\Site');
$view = $event->getTarget();
$resource = $view->resource;
$resourceName = $resource->resourceName();
$placements = $siteSettings->get('bibliography_placement_citation', []);
if (in_array('before/' . $resourceName, $placements)
|| in_array('after/' . $resourceName, $placements)
) {
echo $view->citation($resource, ['tag' => 'p']);
}
}
protected function uninstallModuleCitation(): void
{
$services = $this->getServiceLocator();
/** @var \Omeka\Module\Manager $moduleManager */
$moduleManager = $services->get('Omeka\ModuleManager');
$module = $moduleManager->getModule('Citation');
if (!$module) {
return;
}
$state = $module->getState();
if (!in_array($state, [
\Omeka\Module\Manager::STATE_ACTIVE,
\Omeka\Module\Manager::STATE_NOT_ACTIVE,
\Omeka\Module\Manager::STATE_NOT_FOUND,
\Omeka\Module\Manager::STATE_NEEDS_UPGRADE,
\Omeka\Module\Manager::STATE_INVALID_OMEKA_VERSION,
])) {
return;
}
$messenger = $services->get('ControllerPluginManager')->get('messenger');
// Process uninstallation directly: the module has nothing to uninstall.
$entityManager = $services->get('Omeka\EntityManager');
$entity = $entityManager
->getRepository(\Omeka\Entity\Module::class)
->findOneById($module->getId());
if (!$entity) {
$message = new PsrMessage(
'The module Bibliography replaces the module Citation, that cannot be automatically uninstalled.' // @translate
);
$messenger->addWarning($message);
return;
}
$entityManager->remove($entity);
$entityManager->flush();
$message = new PsrMessage(
'The module Bibliography replaces the module Citation, that was automatically uninstalled.' // @translate
);
$messenger->addNotice($message);
$module->setState(\Omeka\Module\Manager::STATE_NOT_INSTALLED);
}
}