-
Notifications
You must be signed in to change notification settings - Fork 2
/
ojsErcPluginSettingsForm.inc.php
executable file
·164 lines (135 loc) · 6.7 KB
/
ojsErcPluginSettingsForm.inc.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
import('lib.pkp.classes.form.Form');
/**
* Form for the plugin settings of the geoOJS plugin.
*/
class ojsErcPluginSettingsForm extends Form
{
public $plugin;
public function __construct($plugin)
{
// Define the settings template and store a copy of the plugin object
parent::__construct($plugin->getTemplateResource('settings.tpl'));
$this->plugin = $plugin;
// Always add POST and CSRF validation to secure your form.
#$this->addCheck(new FormValidatorPost($this)); in earlier versions named this way but changed to the one below
#$this->addCheck(new FormValidatorCSRF($this)); in earlier versions named this way but changed to the one below
$this->addCheck(new \PKP\form\validation\FormValidatorPost($this));
$this->addCheck(new \PKP\form\validation\FormValidatorCSRF($this));
}
/**
* Load settings already saved in the database
*
* Settings are stored by context, so that each journal or press
* can have different settings.
*/
public function initData()
{
$contextId = Application::get()->getRequest()->getContext()->getId();
$this->setData('serverURL', $this->plugin->getSetting($contextId, 'serverURL'));
$this->setData('serverCookie', $this->plugin->getSetting($contextId, 'serverCookie'));
$this->setData('ERCGalleyColourFromDb', $this->plugin->getSetting($contextId, 'ERCGalleyColour'));
$this->setData('releaseVersionFromDb', $this->plugin->getSetting($contextId, 'releaseVersion'));
$this->setData('ERCo2ruiGalley', $this->plugin->getSetting($contextId, 'ERCo2ruiGalley'));
$this->setData('ERCHTMLGalley', $this->plugin->getSetting($contextId, 'ERCHTMLGalley'));
parent::initData();
}
/**
* Load data that was submitted with the form
*/
public function readInputData()
{
$this->readUserVars(['serverURL']);
$this->readUserVars(['serverCookie']);
$this->readUserVars(['ERCGalleyColour']);
$this->readUserVars(['releaseVersion']);
$this->readUserVars(['ERCo2ruiGalley']);
$this->readUserVars(['ERCHTMLGalley']);
parent::readInputData();
/*
Update the config.js of the build in terms of the baseURL and colour specified in the plugin settings.
*/
$pathConfigJs = $this->plugin->pluginPath . '/build/config.js';
$rawConfigFile = fopen($pathConfigJs, "r+");
$readConfigFile = fread($rawConfigFile, filesize($pathConfigJs));
$baseUrlSpecifiedInSettings = $this->_data[serverURL];
$ERCGalleyColourSpecifiedInSettings = $this->_data[ERCGalleyColour];
preg_match('/"baseUrl":\s"[^,]*"/', $readConfigFile, $configOld);
$configNew = '"baseUrl": "' . $baseUrlSpecifiedInSettings . '"';
$adaptedConfig = str_replace($configOld[0], $configNew, $readConfigFile);
preg_match('/"ERCGalleyPrimaryColour":\s"[^,]*"/', $adaptedConfig, $configOld);
$configNew = '"ERCGalleyPrimaryColour": "' . $ERCGalleyColourSpecifiedInSettings . '"';
$adaptedConfig = str_replace($configOld[0], $configNew, $adaptedConfig);
file_put_contents($pathConfigJs, $adaptedConfig);
fclose($rawConfigFile);
/*
If the new set release version by the user is different from the currently by the plugin used release version,
the currently used build is deleted and thus the download process for the build with the version specified by the user in the settings is started in the script "ojsErcPlugin.inc.php.
*/
$contextId = Application::get()->getRequest()->getContext()->getId();
// currently used build version is stored in database and gets updated after each setting change by the user in the function execute() below. Here the setting is queried for the comparison.
$releaseVersionFromDb = $this->plugin->getSetting($contextId, 'releaseVersion');
// build version specified by the user
$releaseVersion = $this->_data[releaseVersion];
// path of the currently used build folder
$pathBuildFolder = $this->plugin->pluginPath . '/build';
// Additionally the case must be considered, if no change is made by the user, then the hidden field remains empty, however no empty indication in the data base should be stored, but the old indication should remain.
if ($releaseVersionFromDb !== $releaseVersion && $releaseVersion !== "") {
rrmdir($pathBuildFolder);
}
}
/**
* 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.
*/
public function fetch($request, $template = null, $display = false)
{
// Pass the plugin name to the template so that it can be
// used in the URL that the form is submitted to
$templateMgr = TemplateManager::getManager($request);
$templateMgr->assign('pluginName', $this->plugin->getName());
return parent::fetch($request, $template, $display);
}
/**
* Save the settings
*/
public function execute()
{
$contextId = Application::get()->getRequest()->getContext()->getId();
$this->plugin->updateSetting($contextId, 'serverURL', $this->getData('serverURL'));
$this->plugin->updateSetting($contextId, 'serverCookie', $this->getData('serverCookie'));
$this->plugin->updateSetting($contextId, 'ERCGalleyColour', $this->getData('ERCGalleyColour'));
$this->plugin->updateSetting($contextId, 'ERCo2ruiGalley', $this->getData('ERCo2ruiGalley'));
$this->plugin->updateSetting($contextId, 'ERCHTMLGalley', $this->getData('ERCHTMLGalley'));
if ($this->getData('releaseVersion') !== ""){
$this->plugin->updateSetting($contextId, 'releaseVersion', $this->getData('releaseVersion'));
}
// Tell the user that the save was successful.
import('classes.notification.NotificationManager');
$notificationMgr = new NotificationManager();
$notificationMgr->createTrivialNotification(
Application::get()->getRequest()->getUser()->getId(),
NOTIFICATION_TYPE_SUCCESS,
['contents' => __('common.changesSaved')]
);
return parent::execute();
}
}
/**
* Function to delete a directory and all content (files, directories) in it.
*/
function rrmdir($dir) {
if (is_dir($dir)) {
$objects = scandir($dir);
foreach ($objects as $object) {
if ($object != "." && $object != "..") {
if (filetype($dir."/".$object) == "dir") rrmdir($dir."/".$object); else unlink($dir."/".$object);
}
}
reset($objects);
rmdir($dir);
}
}