-
Notifications
You must be signed in to change notification settings - Fork 4
/
Module.php
executable file
·197 lines (177 loc) · 6.32 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
<?php
namespace AnyCloud;
use AnyCloud\Form\ConfigForm;
use Laminas\Mvc\Controller\AbstractController;
use Laminas\Mvc\MvcEvent;
use Laminas\ServiceManager\ServiceLocatorInterface;
use Laminas\View\Renderer\PhpRenderer;
use Omeka\Module\AbstractModule;
use Omeka\Module\Exception\ModuleCannotInstallException;
class Module extends AbstractModule
{
public $adapter;
/**
* Get config file.
*
* @return array Config file
*/
public function getConfig(): array
{
return include __DIR__.'/config/module.config.php';
}
/**
* Code to run when first using the module.
*
* @param MvcEvent $event
*/
public function onBootstrap(MvcEvent $event): void
{
parent::onBootstrap($event);
$this->setFileStoreAlias();
require __DIR__.'/vendor/autoload.php'; // Add autoloader for module-specific requirements
}
/**
* Generate user messages in case of install problems.
*
* @param ServiceLocatorInterface $serviceLocator
*/
public function install(ServiceLocatorInterface $serviceLocator): void
{
if (!file_exists(__DIR__.'/vendor/autoload.php')) {
throw new ModuleCannotInstallException('The Any Cloud components via composer should be installed. See module’s installation documentation.');
}
$settings = $serviceLocator->get('Omeka\Settings');
$this->manageSettings($settings, 'install');
}
/**
* Uninstall module and settings.
*
* @param ServiceLocatorInterface $serviceLocator
*/
public function uninstall(ServiceLocatorInterface $serviceLocator): void
{
$settings = $serviceLocator->get('Omeka\Settings');
$this->manageSettings($settings, 'uninstall');
}
/**
* Script to run when upgrading module.
*
* @param string $oldVersion
* @param string $newVersion
* @param ServiceLocatorInterface $serviceLocator
*/
public function upgrade($oldVersion, $newVersion, ServiceLocatorInterface $serviceLocator): void
{
require_once 'data/scripts/upgrade.php';
}
/**
* Get the configuration form.
*
* @param PhpRenderer $renderer Render the form
*
* @return string HTML string of configuration form for module
*/
public function getConfigForm(PhpRenderer $renderer): string
{
$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) {
$data[$name] = $settings->get($name, $value);
}
$form->init();
$form->setData($data);
// Disable fieldset if corresponding section exists in local.config.php
foreach (['aws', 'azure', 'digital_ocean', 'dropbox', 'google', 'scaleway', 'wasabi'] as $key) {
if (isset($config['file_store'][$key])) {
$fieldset = $form->get("anycloud_$key");
$fieldset->setLabel($fieldset->getLabel().' (disabled because configuration exists in local.config.php)');
foreach ($fieldset->getElements() as $element) {
$element->setAttribute('disabled', true);
}
}
}
$html = $renderer->render('anycloud/module/config', [
'form' => $form,
]);
return $html;
}
/**
* Handle the config form.
*
* @param AbstractController $controller
*
* @return bool|null
*/
public function handleConfigForm(AbstractController $controller): ?bool
{
$serviceLocator = $this->getServiceLocator();
$settings = $serviceLocator->get('Omeka\Settings');
$form = $serviceLocator->get('FormElementManager')->get(ConfigForm::class);
$params = $controller->getRequest()->getPost();
$form->init();
$form->setData($params);
if (!$form->isValid()) {
$controller->messenger()->addErrors($form->getMessages());
return false;
}
$params = $form->getData();
$defaultSettings = $this->getDefaultSettings();
$params = array_intersect_key($params, $defaultSettings);
foreach ($params as $name => $value) {
$settings->set($name, $value);
}
return null;
}
/**
* Manage module settings.
*
* @param ServiceLocatorInterface $settings Object containing Omeka settings
* @param string $process Process used to manage setting (`install` or `uninstall`)
* @param string $key Name of $settings key to manage
*/
protected function manageSettings($settings, $process, $key = 'config'): void
{
$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;
}
}
}
/**
* Override default file store alias to use Any Cloud module for uploads instead.
*/
private function setFileStoreAlias(): void
{
$serviceLocator = $this->getServiceLocator();
$settings = $serviceLocator->get('Omeka\Settings');
$this->adapter = $settings->get('anycloud_adapter');
if (isset($this->adapter['adapter']) && $this->adapter['adapter'] !== 'default') {
$serviceLocator->setAlias('Omeka\File\Store', File\Store\AnyCloud::class);
}
}
/**
* Get the default settings.
*
* @param string $key The desired config to grab
*
* @return mixed
*/
private function getDefaultSettings($key = 'config')
{
$serviceLocator = $this->getServiceLocator();
// TODO Fix so that configs are actually grabbed and the module can be deleted if desired
$config = $serviceLocator->get('Config');
return $config[strtolower(__NAMESPACE__)][$key];
}
}