forked from omeka-s-modules/UnApi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModule.php
182 lines (161 loc) · 5.73 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
<?php
namespace UnApi;
use UnApi\Form\ConfigForm;
use Omeka\Module\AbstractModule;
use Laminas\EventManager\Event;
use Laminas\EventManager\SharedEventManagerInterface;
use Laminas\Mvc\Controller\AbstractController;
use Laminas\Mvc\MvcEvent;
use Laminas\ServiceManager\ServiceLocatorInterface;
use Laminas\View\Renderer\PhpRenderer;
class Module extends AbstractModule
{
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
public function onBootstrap(MvcEvent $event)
{
parent::onBootstrap($event);
$this->addAclRules();
}
public function install(ServiceLocatorInterface $serviceLocator)
{
$settings = $serviceLocator->get('Omeka\Settings');
$this->manageSettings($settings, 'install');
}
public function uninstall(ServiceLocatorInterface $serviceLocator)
{
$settings = $serviceLocator->get('Omeka\Settings');
$this->manageSettings($settings, 'uninstall');
}
public function upgrade($oldVersion, $newVersion, ServiceLocatorInterface $serviceLocator)
{
if (version_compare($oldVersion, '1.1.0', '<')) {
$config = require __DIR__ . '/config/module.config.php';
$defaultSettings = $config[strtolower(__NAMESPACE__)]['config'];
$settings = $serviceLocator->get('Omeka\Settings');
$settings->set('unapi_public_server',
$defaultSettings['unapi_public_server']);
}
}
protected function manageSettings($settings, $process, $key = 'config')
{
$config = require __DIR__ . '/config/module.config.php';
$defaultSettings = $config[strtolower(__NAMESPACE__)][$key];
foreach ($defaultSettings as $name => $value) {
switch ($process) {
case 'install':
$settings->set($name, $value);
break;
case 'uninstall':
$settings->delete($name);
break;
}
}
}
protected function addAclRules()
{
$services = $this->getServiceLocator();
$settings = $services->get('Omeka\Settings');
$publicServer = $settings->get('unapi_public_server', true);
if ($publicServer) {
$acl = $services->get('Omeka\Acl');
$acl->allow(null, Controller\IndexController::class, ['index']);
}
}
public function attachListeners(SharedEventManagerInterface $sharedEventManager)
{
$settings = $this->getServiceLocator()->get('Omeka\Settings');
if ($settings->get('unapi_public_server')) {
$sharedEventManager->attach(
'Omeka\Controller\Site\Item',
'view.layout',
[$this, 'addUnApiServerLink']
);
$sharedEventManager->attach(
'Omeka\Controller\Site\Item',
'view.browse.after',
[$this, 'appendUnApiIds']
);
$sharedEventManager->attach(
'Omeka\Controller\Site\Item',
'view.show.after',
[$this, 'appendUnApiId']
);
}
$sharedEventManager->attach(
'Omeka\Controller\Admin\Item',
'view.layout',
[$this, 'addUnApiServerLink']
);
$sharedEventManager->attach(
'Omeka\Controller\Admin\Item',
'view.browse.after',
[$this, 'appendUnApiIds']
);
$sharedEventManager->attach(
'Omeka\Controller\Admin\Item',
'view.show.after',
[$this, 'appendUnApiId']
);
}
public function getConfigForm(PhpRenderer $renderer)
{
$services = $this->getServiceLocator();
$config = $services->get('Config');
$settings = $services->get('Omeka\Settings');
$form = $services->get('FormElementManager')->get(ConfigForm::class);
$data = [];
$defaultSettings = $config[strtolower(__NAMESPACE__)]['config'];
foreach ($defaultSettings as $name => $value) {
$value = $settings->get($name);
$data[$name] = $value;
}
$form->init();
$form->setData($data);
$html = $renderer->formCollection($form);
return $html;
}
public function handleConfigForm(AbstractController $controller)
{
$services = $this->getServiceLocator();
$config = $services->get('Config');
$settings = $services->get('Omeka\Settings');
$params = $controller->getRequest()->getPost();
$form = $services->get('FormElementManager')->get(ConfigForm::class);
$form->init();
$form->setData($params);
if (!$form->isValid()) {
$controller->messenger()->addErrors($form->getMessages());
return false;
}
$defaultSettings = $config[strtolower(__NAMESPACE__)]['config'];
foreach ($params as $name => $value) {
if (array_key_exists($name, $defaultSettings)) {
$settings->set($name, $value);
}
}
}
public function addUnApiServerLink(Event $event)
{
$event->getTarget()->headLink([
'rel' => 'unapi-server',
'type' => 'application/xml',
'title' => 'unAPI',
'href' => $event->getTarget()->url('unapi'),
]);
}
public function appendUnApiIds(Event $event)
{
$items = $event->getTarget()->items;
foreach ($items as $item) {
echo sprintf('<abbr class="unapi-id" title="%s"></abbr>', $item->id());
}
}
public function appendUnApiId(Event $event)
{
$item = $event->getTarget()->item;
echo sprintf('<abbr class="unapi-id" title="%s"></abbr>', $item->id());
}
}