forked from asmecher/healthCheck
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHealthCheckSettingsForm.php
146 lines (122 loc) · 4.32 KB
/
HealthCheckSettingsForm.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
<?php
/**
* @file HealthCheckSettingsForm.php
*
* Copyright (c) 2017-2023 Simon Fraser University
* Copyright (c) 2017-2023 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class HealthCheckSettingsForm
* @brief Settings form class for the HealthCheck plugin.
*/
namespace APP\plugins\generic\healthCheck;
use APP\core\Application;
use APP\notification\Notification;
use APP\notification\NotificationManager;
use APP\template\TemplateManager;
use PKP\db\DAORegistry;
use PKP\form\Form;
use PKP\form\validation\FormValidatorCSRF;
use PKP\form\validation\FormValidatorPost;
use PKP\plugins\PluginRegistry;
use PKP\config\Config;
class HealthCheckSettingsForm extends Form {
public HealthCheckPlugin $plugin;
/**
* Defines the settings form's template and adds
* validation checks.
*
* Always add POST and CSRF validation to secure
* your form.
*/
public function __construct(HealthCheckPlugin $plugin)
{
parent::__construct($plugin->getTemplateResource('settings.tpl'));
$this->plugin = $plugin;
$this->addCheck(new FormValidatorPost($this));
$this->addCheck(new FormValidatorCSRF($this));
}
/**
* Load settings already saved in the database
*
* Settings are stored by context, so that each journal, press,
* or preprint server can have different settings.
*/
public function initData()
{
$context = Application::get()
->getRequest()
->getContext();
$contextId = $context
? $context->getId()
: Application::CONTEXT_SITE;
parent::initData();
}
/**
* Fetch any additional data needed for your form.
*
* Data assigned to the form using $this->setData() during the
* initData() or readInputData() methods will be passed to the
* template.
*
* In the example below, the plugin name is passed to the
* template so that it can be used in the URL that the form is
* submitted to.
*/
public function fetch($request, $template = null, $display = false)
{
$templateMgr = TemplateManager::getManager($request);
$templateMgr->assign('pluginName', $this->plugin->getName());
// Fetch all installed plugins.
$plugins = PluginRegistry::getAllPlugins();
// Use version DAO to retrieve version.
$versionDao = DAORegistry::getDAO('VersionDAO');
// Begin assembling markup for settings page.
$pluginList = [];
foreach ($plugins as $plugin) {
// Extract version for given plugin from version DAO.
$pluginInfo = explode('/', $plugin->getPluginPath());
$productType = $pluginInfo[0] . '.' . $pluginInfo[1];
$productName = $pluginInfo[2];
$currentVersion = $versionDao->getCurrentVersion($productType, $productName);
// Assemble list item.
$pluginList[] = [
'name' => $plugin->getName(),
'version' => $currentVersion->getVersionString(),
];
}
// Fetch php version and error log location.
$phpVersion = phpversion();
$phpErrorLog = ini_get('error_log');
// Assign variables to template.
$templateMgr->assign('pluginList', $pluginList);
$templateMgr->assign('phpVersion', $phpVersion);
$templateMgr->assign('phpErrorLog', $phpErrorLog);
return parent::fetch($request, $template, $display);
}
/**
* Save the plugin settings and notify the user
* that the save was successful
*/
public function execute(...$functionArgs)
{
$context = Application::get()
->getRequest()
->getContext();
$contextId = $context
? $context->getId()
: Application::CONTEXT_SITE;
$this->plugin->updateSetting(
$contextId,
'publicationStatement',
$this->getData('publicationStatement')
);
$notificationMgr = new NotificationManager();
$notificationMgr->createTrivialNotification(
Application::get()->getRequest()->getUser()->getId(),
Notification::NOTIFICATION_TYPE_SUCCESS,
['contents' => __('common.changesSaved')]
);
return parent::execute();
}
}