forked from pkp/paperbuzz
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPaperbuzzSettingsForm.php
111 lines (101 loc) · 3.3 KB
/
PaperbuzzSettingsForm.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
<?php
/**
* @file PaperbuzzSettingsForm.inc.php
*
* Copyright (c) 2013-2023 Simon Fraser University
* Copyright (c) 2003-2023 John Willinsky
* Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
*
* @class PaperbuzzSettingsForm
* @brief Form for journal managers to modify Paperbuzz plugin settings
*/
namespace APP\plugins\generic\paperbuzz;
use PKP\form\Form;
use APP\core\Application;
use APP\template\TemplateManager;
use PKP\form\validation\FormValidator;
use PKP\form\validation\FormValidatorPost;
use PKP\form\validation\FormValidatorCSRF;
class PaperbuzzSettingsForm extends Form
{
protected PaperbuzzPlugin $plugin;
/**
* Constructor
* @param PaperbuzzPlugin $plugin
*/
public function __construct($plugin)
{
$this->plugin = $plugin;
parent::__construct($plugin->getTemplateResource('settingsForm.tpl'));
$this->addCheck(new FormValidator($this, 'apiEmail', FormValidator::FORM_VALIDATOR_REQUIRED_VALUE, 'plugins.generic.paperbuzz.settings.apiEmail.required'));
$this->addCheck(new FormValidatorPost($this));
$this->addCheck(new FormValidatorCSRF($this));
}
/**
* @copydoc Form::initData()
*/
public function initData(): void
{
$request = Application::get()->getRequest();
$context = $request->getContext();
if ($context) {
$plugin = $this->plugin;
foreach ($this->getFormFields() as $fieldName => $fieldType) {
$fieldValue = $plugin->getSetting($context->getId(), $fieldName);
if ($fieldName == 'apiEmail' && empty($fieldValue)) {
$fieldValue = $context->getData('supportEmail');
}
$this->setData($fieldName, $fieldValue);
}
}
}
/**
* @copydoc Form::readInputData()
*/
public function readInputData(): void
{
$this->readUserVars(array_keys($this->getFormFields()));
}
/**
* @copydoc Form::fetch()
*/
public function fetch($request, $template = null, $display = false): string
{
$plugin = $this->plugin;
$showMiniOptions = array(
false => __('plugins.generic.paperbuzz.settings.showGraph'),
true => __('plugins.generic.paperbuzz.settings.showMini'),
);
$templateMgr = TemplateManager::getManager($request);
$templateMgr->assign('pluginName', $plugin->getName());
$templateMgr->assign('showMiniOptions', $showMiniOptions);
return parent::fetch($request);
}
/**
* Save settings.
* @copydoc Form::execute()
*/
public function execute(...$functionArgs): void
{
$request = Application::get()->getRequest();
$context = $request->getContext();
if ($context) {
$plugin = $this->plugin;
foreach ($this->getFormFields() as $fieldName => $fieldType) {
$plugin->updateSetting($context->getId(), $fieldName, $this->getData($fieldName), $fieldType);
}
}
}
/**
* Get form fields
* @return array (field name => field type)
*/
public function getFormFields(): array
{
return [
'apiEmail' => 'string',
'hideDownloads' => 'bool',
'showMini' => 'bool'
];
}
}