-
Notifications
You must be signed in to change notification settings - Fork 6
/
SettingsForm.php
101 lines (86 loc) · 3.27 KB
/
SettingsForm.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
<?php
/**
* @file SettingsForm.php
*
* Copyright (c) 2014-2023 Simon Fraser University
* Copyright (c) 2003-2023 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class SettingsForm
*
* @brief Form for managers to modify web feeds plugin settings
*/
namespace APP\plugins\generic\webFeed;
use APP\core\Application;
use APP\template\TemplateManager;
use PKP\form\Form;
use PKP\form\validation\FormValidator;
use PKP\form\validation\FormValidatorCSRF;
use PKP\form\validation\FormValidatorPost;
class SettingsForm extends Form
{
/**
* Constructor
*/
public function __construct(private WebFeedPlugin $plugin, private int $contextId)
{
parent::__construct($plugin->getTemplateResource('settingsForm.tpl'));
$this->addCheck(new FormValidatorPost($this));
$this->addCheck(new FormValidatorCSRF($this));
}
/**
* @copydoc Form::initData()
*/
public function initData(): void
{
$contextId = $this->contextId;
$plugin = $this->plugin;
$this->setData('displayPage', $plugin->getSetting($contextId, 'displayPage'));
$this->setData('displayItems', $plugin->getSetting($contextId, 'displayItems'));
$this->setData('recentItems', $plugin->getSetting($contextId, 'recentItems'));
$this->setData('includeIdentifiers', $plugin->getSetting($contextId, 'includeIdentifiers'));
parent::initData();
}
/**
* @copydoc Form::readInputData()
*/
public function readInputData(): void
{
$this->readUserVars(['displayPage', 'displayItems', 'recentItems', 'includeIdentifiers']);
// Check that recent items value is a positive integer
if ((int) $this->getData('recentItems') <= 0) {
$this->setData('recentItems', '');
}
// If recent items is selected or if the application doesn't support issues (there's no displayItems option to select), check that we have a value
if (!WebFeedPlugin::hasIssues() || $this->getData('displayItems') === 'recent') {
$this->addCheck(new FormValidator($this, 'recentItems', 'required', 'plugins.generic.webfeed.settings.recentItemsRequired'));
}
}
/**
* @copydoc Form::fetch()
*
* @param null|mixed $template
*/
public function fetch($request, $template = null, $display = false): string
{
$templateMgr = TemplateManager::getManager($request);
$templateMgr->assign([
'pluginName' => $this->plugin->getName(),
'hasIssues' => WebFeedPlugin::hasIssues()
]);
return parent::fetch($request, $template, $display);
}
/**
* @copydoc Form::execute()
*/
public function execute(...$functionArgs)
{
$plugin = $this->plugin;
$contextId = $this->contextId;
$plugin->updateSetting($contextId, 'displayPage', $this->getData('displayPage'), 'string');
$plugin->updateSetting($contextId, 'displayItems', $this->getData('displayItems'), 'string');
$plugin->updateSetting($contextId, 'recentItems', $this->getData('recentItems'), 'int');
$plugin->updateSetting($contextId, 'includeIdentifiers', $this->getData('includeIdentifiers'), 'bool');
parent::execute(...$functionArgs);
}
}