-
Notifications
You must be signed in to change notification settings - Fork 2
/
DataverseSettingsForm.php
118 lines (102 loc) · 4.25 KB
/
DataverseSettingsForm.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
<?php
namespace APP\plugins\generic\dataverse;
use PKP\form\Form;
use PKP\plugins\Plugin;
use APP\core\Application;
use APP\template\TemplateManager;
use PKP\db\DAORegistry;
use PKP\form\validation\FormValidatorUrl;
use PKP\form\validation\FormValidator;
use PKP\form\validation\FormValidatorCustom;
use PKP\form\validation\FormValidatorPost;
use APP\plugins\generic\dataverse\classes\exception\DataverseException;
use APP\plugins\generic\dataverse\classes\dataverseConfiguration\DataverseConfiguration;
use APP\plugins\generic\dataverse\classes\dataverseConfiguration\DataverseConfigurationDAO;
use APP\plugins\generic\dataverse\dataverseAPI\actions\DataverseCollectionActions;
class DataverseSettingsForm extends Form
{
private $plugin;
private $contextId;
public function __construct(Plugin $plugin, int $contextId)
{
parent::__construct($plugin->getTemplateResource('dataverseConfigurationForm.tpl'));
$this->plugin = $plugin;
$this->contextId = $contextId;
$this->addCheck(new FormValidatorUrl(
$this,
'dataverseUrl',
FormValidator::FORM_VALIDATOR_REQUIRED_VALUE,
'plugins.generic.dataverse.settings.dataverseUrlRequired'
));
$this->addCheck(new FormValidator(
$this,
'apiToken',
FormValidator::FORM_VALIDATOR_REQUIRED_VALUE,
'plugins.generic.dataverse.settings.tokenRequired'
));
$this->addCheck(new FormValidatorCustom(
$this,
'termsOfUse',
FormValidator::FORM_VALIDATOR_REQUIRED_VALUE,
'plugins.generic.dataverse.settings.dataverseUrlNotValid',
[$this, 'validateConfiguration']
));
$this->addCheck(new FormValidatorPost($this));
if (Application::get()->getName() == 'ojs2') {
$this->addCheck(new FormValidator(
$this,
'datasetPublish',
FormValidator::FORM_VALIDATOR_REQUIRED_VALUE,
'plugins.generic.dataverse.settings.datasetPublishRequired'
));
}
}
public function initData(): void
{
$configurationDAO = DAORegistry::getDAO('DataverseConfigurationDAO');
$configuration = $configurationDAO->get($this->contextId);
$data = $configuration->getAllData();
foreach ($data as $name => $value) {
$this->setData($name, $value);
}
}
public function readInputData(): void
{
$this->readUserVars(['dataverseUrl', 'apiToken', 'termsOfUse', 'datasetPublish']);
$this->setData('dataverseUrl', $this->normalizeURI($this->getData('dataverseUrl')));
}
private function normalizeURI(string $uri): string
{
return preg_replace("/\/+$/", '', $uri);
}
public function fetch($request, $template = null, $display = false)
{
$configuration = new DataverseConfiguration();
$templateMgr = TemplateManager::getManager($request);
$templateMgr->assign('pluginName', $this->plugin->getName());
$templateMgr->assign('application', Application::get()->getName());
$templateMgr->assign('datasetPublishOptions', $configuration->getDatasetPublishOptions());
return parent::fetch($request);
}
public function execute(...$functionArgs)
{
$this->plugin->updateSetting($this->contextId, 'dataverseUrl', $this->getData('dataverseUrl'), 'string');
$this->plugin->updateSetting($this->contextId, 'apiToken', $this->getData('apiToken'), 'string');
$this->plugin->updateSetting($this->contextId, 'termsOfUse', $this->getData('termsOfUse'));
$this->plugin->updateSetting($this->contextId, 'datasetPublish', (int) $this->getData('datasetPublish'));
parent::execute(...$functionArgs);
}
public function validateConfiguration(): bool
{
$configuration = new DataverseConfiguration();
$configuration->setDataverseUrl($this->getData('dataverseUrl'));
$configuration->setApiToken($this->getData('apiToken'));
$dataverseCollectionActions = new DataverseCollectionActions($configuration);
try {
$dataverseCollectionActions->get();
} catch (DataverseException $e) {
return false;
}
return true;
}
}