|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace AnyCloud; |
|
4
|
|
|
|
|
5
|
|
|
use AnyCloud\Form\ConfigForm; |
|
6
|
|
|
use Laminas\Mvc\Controller\AbstractController; |
|
|
|
|
|
|
7
|
|
|
use Laminas\Mvc\MvcEvent; |
|
|
|
|
|
|
8
|
|
|
use Laminas\ServiceManager\ServiceLocatorInterface; |
|
|
|
|
|
|
9
|
|
|
use Laminas\View\Renderer\PhpRenderer; |
|
|
|
|
|
|
10
|
|
|
use Omeka\Module\AbstractModule; |
|
11
|
|
|
use Omeka\Module\Exception\ModuleCannotInstallException; |
|
12
|
|
|
|
|
13
|
|
|
class Module extends AbstractModule |
|
14
|
|
|
{ |
|
15
|
|
|
public $adapter; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Get config file. |
|
19
|
|
|
* |
|
20
|
|
|
* @return array Config file |
|
21
|
|
|
*/ |
|
22
|
|
|
public function getConfig(): array |
|
23
|
|
|
{ |
|
24
|
|
|
return include __DIR__.'/config/module.config.php'; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Code to run when first using the module. |
|
29
|
|
|
* |
|
30
|
|
|
* @param MvcEvent $event |
|
31
|
|
|
*/ |
|
32
|
|
|
public function onBootstrap(MvcEvent $event): void |
|
33
|
|
|
{ |
|
34
|
|
|
parent::onBootstrap($event); |
|
35
|
|
|
|
|
36
|
|
|
$this->setFileStoreAlias(); |
|
37
|
|
|
require __DIR__.'/vendor/autoload.php'; // Add autoloader for module-specific requirements |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Generate user messages in case of install problems. |
|
42
|
|
|
* |
|
43
|
|
|
* @param ServiceLocatorInterface $serviceLocator |
|
44
|
|
|
*/ |
|
45
|
|
|
public function install(ServiceLocatorInterface $serviceLocator): void |
|
46
|
|
|
{ |
|
47
|
|
|
if (!file_exists(__DIR__.'/vendor/autoload.php')) { |
|
48
|
|
|
throw new ModuleCannotInstallException('The Any Cloud components via composer should be installed. See module’s installation documentation.'); |
|
49
|
|
|
} |
|
50
|
|
|
$settings = $serviceLocator->get('Omeka\Settings'); |
|
51
|
|
|
$this->manageSettings($settings, 'install'); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Uninstall module and settings. |
|
56
|
|
|
* |
|
57
|
|
|
* @param ServiceLocatorInterface $serviceLocator |
|
58
|
|
|
*/ |
|
59
|
|
|
public function uninstall(ServiceLocatorInterface $serviceLocator): void |
|
60
|
|
|
{ |
|
61
|
|
|
$settings = $serviceLocator->get('Omeka\Settings'); |
|
62
|
|
|
$this->manageSettings($settings, 'uninstall'); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Script to run when upgrading module. |
|
67
|
|
|
* |
|
68
|
|
|
* @param string $oldVersion |
|
69
|
|
|
* @param string $newVersion |
|
70
|
|
|
* @param ServiceLocatorInterface $serviceLocator |
|
71
|
|
|
*/ |
|
72
|
|
|
public function upgrade($oldVersion, $newVersion, ServiceLocatorInterface $serviceLocator): void |
|
73
|
|
|
{ |
|
74
|
|
|
require_once 'data/scripts/upgrade.php'; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Get the configuration form. |
|
79
|
|
|
* |
|
80
|
|
|
* @param PhpRenderer $renderer Render the form |
|
81
|
|
|
* |
|
82
|
|
|
* @return string HTML string of configuration form for module |
|
83
|
|
|
*/ |
|
84
|
|
|
public function getConfigForm(PhpRenderer $renderer): string |
|
85
|
|
|
{ |
|
86
|
|
|
$services = $this->getServiceLocator(); |
|
87
|
|
|
$config = $services->get('Config'); |
|
88
|
|
|
$settings = $services->get('Omeka\Settings'); |
|
89
|
|
|
$form = $services->get('FormElementManager')->get(ConfigForm::class); |
|
90
|
|
|
$data = []; |
|
91
|
|
|
$defaultSettings = $config[strtolower(__NAMESPACE__)]['config']; |
|
92
|
|
|
foreach ($defaultSettings as $name => $value) { |
|
93
|
|
|
$data[$name] = $settings->get($name, $value); |
|
94
|
|
|
} |
|
95
|
|
|
$form->init(); |
|
96
|
|
|
$form->setData($data); |
|
97
|
|
|
|
|
98
|
|
|
// Disable fieldset if corresponding section exists in local.config.php |
|
99
|
|
|
foreach (['aws', 'azure', 'digital_ocean', 'dropbox', 'google', 'scaleway', 'wasabi'] as $key) { |
|
100
|
|
|
if (isset($config['file_store'][$key])) { |
|
101
|
|
|
$fieldset = $form->get("anycloud_$key"); |
|
102
|
|
|
$fieldset->setLabel($fieldset->getLabel().' (disabled because configuration exists in local.config.php)'); |
|
103
|
|
|
foreach ($fieldset->getElements() as $element) { |
|
104
|
|
|
$element->setAttribute('disabled', true); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
$html = $renderer->render('anycloud/module/config', [ |
|
110
|
|
|
'form' => $form, |
|
111
|
|
|
]); |
|
112
|
|
|
|
|
113
|
|
|
return $html; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Handle the config form. |
|
118
|
|
|
* |
|
119
|
|
|
* @param AbstractController $controller |
|
120
|
|
|
* |
|
121
|
|
|
* @return bool|null |
|
122
|
|
|
*/ |
|
123
|
|
|
public function handleConfigForm(AbstractController $controller): ?bool |
|
124
|
|
|
{ |
|
125
|
|
|
$serviceLocator = $this->getServiceLocator(); |
|
126
|
|
|
$settings = $serviceLocator->get('Omeka\Settings'); |
|
127
|
|
|
$form = $serviceLocator->get('FormElementManager')->get(ConfigForm::class); |
|
128
|
|
|
$params = $controller->getRequest()->getPost(); |
|
129
|
|
|
$form->init(); |
|
130
|
|
|
$form->setData($params); |
|
131
|
|
|
if (!$form->isValid()) { |
|
132
|
|
|
$controller->messenger()->addErrors($form->getMessages()); |
|
133
|
|
|
|
|
134
|
|
|
return false; |
|
135
|
|
|
} |
|
136
|
|
|
$params = $form->getData(); |
|
137
|
|
|
$defaultSettings = $this->getDefaultSettings(); |
|
138
|
|
|
$params = array_intersect_key($params, $defaultSettings); |
|
139
|
|
|
foreach ($params as $name => $value) { |
|
140
|
|
|
$settings->set($name, $value); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
return null; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Manage module settings. |
|
148
|
|
|
* |
|
149
|
|
|
* @param ServiceLocatorInterface $settings Object containing Omeka settings |
|
150
|
|
|
* @param string $process Process used to manage setting (`install` or `uninstall`) |
|
151
|
|
|
* @param string $key Name of $settings key to manage |
|
152
|
|
|
*/ |
|
153
|
|
|
protected function manageSettings($settings, $process, $key = 'config'): void |
|
154
|
|
|
{ |
|
155
|
|
|
$config = require __DIR__.'/config/module.config.php'; |
|
156
|
|
|
$defaultSettings = $config[strtolower(__NAMESPACE__)][$key]; |
|
157
|
|
|
foreach ($defaultSettings as $name => $value) { |
|
158
|
|
|
switch ($process) { |
|
159
|
|
|
case 'install': |
|
160
|
|
|
$settings->set($name, $value); |
|
161
|
|
|
break; |
|
162
|
|
|
case 'uninstall': |
|
163
|
|
|
$settings->delete($name); |
|
164
|
|
|
break; |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Override default file store alias to use Any Cloud module for uploads instead. |
|
171
|
|
|
*/ |
|
172
|
|
|
private function setFileStoreAlias(): void |
|
173
|
|
|
{ |
|
174
|
|
|
$serviceLocator = $this->getServiceLocator(); |
|
175
|
|
|
$settings = $serviceLocator->get('Omeka\Settings'); |
|
176
|
|
|
$this->adapter = $settings->get('anycloud_adapter'); |
|
177
|
|
|
if (isset($this->adapter['adapter']) && $this->adapter['adapter'] !== 'default') { |
|
178
|
|
|
$serviceLocator->setAlias('Omeka\File\Store', File\Store\AnyCloud::class); |
|
|
|
|
|
|
179
|
|
|
} |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Get the default settings. |
|
184
|
|
|
* |
|
185
|
|
|
* @param string $key The desired config to grab |
|
186
|
|
|
* |
|
187
|
|
|
* @return mixed |
|
188
|
|
|
*/ |
|
189
|
|
|
private function getDefaultSettings($key = 'config') |
|
190
|
|
|
{ |
|
191
|
|
|
$serviceLocator = $this->getServiceLocator(); |
|
192
|
|
|
// TODO Fix so that configs are actually grabbed and the module can be deleted if desired |
|
193
|
|
|
$config = $serviceLocator->get('Config'); |
|
194
|
|
|
|
|
195
|
|
|
return $config[strtolower(__NAMESPACE__)][$key]; |
|
196
|
|
|
} |
|
197
|
|
|
} |
|
198
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths