forked from pkp/ojs-markup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SettingsForm.inc.php
227 lines (196 loc) · 7.81 KB
/
SettingsForm.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<?php
/**
* @file plugins/generic/markup/SettingsForm.inc.php
*
* Copyright (c) 2003-2013 John Willinsky
* Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
*
* @class SettingsForm
* @ingroup plugins_generic_markup
*
* @brief Form for Document Markup gateway plugin settings
*/
import('lib.pkp.classes.form.Form');
define('MARKUP_CSL_STYLE_DEFAULT', 'chicago-author-date.csl');
define('MARKUP_CSL_STYLE_NAME_DEFAULT', 'Chicago Manual of Style (author-date)');
define('MARKUP_DOCUMENT_SERVER_URL_DEFAULT', 'http://pkp-udev.lib.sfu.ca/');
class SettingsForm extends Form {
/** @var $journalId int */
var $journalId;
/** @var $plugin object */
var $plugin;
/** @var $settings array */
var $settings;
/**
* Constructor
* @param $plugin mixed Plugin object
* @param $journalId int JournalId
*/
function SettingsForm(&$plugin, $journalId) {
$this->journalId = $journalId;
$this->plugin =& $plugin;
$journal =& Request::getJournal();
parent::Form($plugin->getTemplatePath() . 'settingsForm.tpl');
// Validation checks for this form
$this->settings = array(
'cslStyle' => 'string',
'cslStyleName' => 'string',
'cssHeaderImageName' => 'string',
'markupHostPass' => 'string',
'markupHostURL' => 'string',
'markupHostUser' => 'string',
'reviewVersion' => 'bool',
);
}
/**
* Validate the form
*
* @return bool Whether or not the form validated
*/
function validate() {
$this->addCheck(new FormValidatorPost($this));
$this->addCheck(new FormValidator($this, 'cslStyle', 'required', 'plugins.generic.markup.required.cslStyle'));
$this->addCheck(new FormValidator($this, 'cslStyleName', 'optional', 'plugins.generic.markup.optional.cslStyleName'));
$this->addCheck(new FormValidator($this, 'markupHostPass', 'optional', 'plugins.generic.markup.optional.markupHostPass'));
$this->addCheck(new FormValidator($this, 'markupHostURL', 'required', 'plugins.generic.markup.required.markupHostURL'));
$this->addCheck(new FormValidator($this, 'markupHostUser', 'optional', 'plugins.generic.markup.optional.markupHostUrl'));
$this->addCheck(new FormValidator($this, 'reviewVersion', 'optional', 'plugins.generic.markup.optional.reviewVersion'));
$this->addCheck(new FormValidatorCustom($this, 'cssHeaderImageName', 'optional', 'plugins.generic.markup.error', $this->_validateImage('cssHeaderImage'), true));
return parent::validate();
}
/**
* Initialize plugin settings form
*
* @return void
*/
function initData() {
$journal =& Request::getJournal();
$journalId = $this->journalId;
$plugin =& $this->plugin;
// User must at least load settings page for plugin to work with defaults.
if ($plugin->getSetting($journalId, 'cslStyle') == '') {
$plugin->updateSetting($journalId, 'cslStyle', MARKUP_CSL_STYLE_DEFAULT);
$plugin->updateSetting($journalId, 'cslStyleName', MARKUP_CSL_STYLE_NAME_DEFAULT);
}
if ($plugin->getSetting($journalId, 'markupHostURL') == '') {
$plugin->updateSetting($journalId, 'markupHostURL', MARKUP_DOCUMENT_SERVER_URL_DEFAULT);
}
$this->setData('cslStyle', $plugin->getSetting($journalId, 'cslStyle'));
$this->setData('cslStyleName', $plugin->getSetting($journalId, 'cslStyleName'));
// This field has content only if header image actually exists in the right folder.
$journalFileManager = $this->_getJournalFileManager($journal);
$folderCssImage = glob($journalFileManager->filesDir . 'css/article_header.{jpg,png}', GLOB_BRACE);
if (count($folderCssImage)) {
$this->setData('cssHeaderImageName', basename($folderCssImage[0]));
}
$this->setData('markupHostUser', $plugin->getSetting($journalId, 'markupHostUser'));
$this->setData('reviewVersion', $plugin->getSetting($journalId, 'reviewVersion'));
$this->setData('markupHostURL', $plugin->getSetting($journalId, 'markupHostURL'));
}
/**
* Populate and display settings form
*
* @return void
*/
function display() {
$templateManager =& TemplateManager::getManager();
// Signals indicating plugin compatibility
$templateManager->assign('curlSupport', function_exists('curl_init') ? __('plugins.generic.markup.settings.installed') : __('plugins.generic.markup.settings.notInstalled'));
$templateManager->assign('zipSupport', extension_loaded('zlib') ? __('plugins.generic.markup.settings.installed') : __('plugins.generic.markup.settings.notInstalled'));
$templateManager->assign('php5Support', checkPhpVersion('5.0.0') ? __('plugins.generic.markup.settings.installed') : __('plugins.generic.markup.settings.notInstalled'));
$templateManager->assign('pathInfo', Request::isPathInfoEnabled() ? __('plugins.generic.markup.settings.enabled') : __('plugins.generic.markup.settings.disabled'));
$templateManager->assign('additionalHeadData', '<link rel="stylesheet" type="text/css" href="' . $this->plugin->getCssPath() . 'settingsForm.css" />');
parent::display();
}
/**
* Assign form data to user-submitted data
*
* @return void
*/
function readInputData() {
$this->readUserVars(
array(
'cslStyle',
'cslStyleName',
'cssHeaderImage',
'markupHostPass',
'markupHostURL',
'markupHostUser',
'reviewVersion',
)
);
}
/**
* Save settings
*
* @return void
*/
function execute() {
$plugin =& $this->plugin;
$journalId = $this->journalId;
$plugin->updateSetting($journalId, 'cslStyle', $this->getData('cslStyle'));
$plugin->updateSetting($journalId, 'cslStyleName', $this->getData('cslStyleName'));
$markupHostURL = $this->getData('markupHostURL');
if ($markupHostURL) {
if (!parse_url($markupHostURL, PHP_URL_SCHEME)) {
$markupHostURL = 'http://' . $markupHostURL;
}
if (substr(parse_url($markupHostURL, PHP_URL_PATH), -1) != '/') {
$markupHostURL .= '/';
}
}
$plugin->updateSetting($journalId, 'markupHostURL', $markupHostURL);
$plugin->updateSetting($journalId, 'markupHostUser', $this->getData('markupHostUser'));
$plugin->updateSetting($journalId, 'markupHostPass', $this->getData('markupHostPass'));
$plugin->updateSetting($journalId, 'reviewVersion', $this->getData('reviewVersion'));
// Upload article header image if given. Image suffix already validated above.
$journalFileManager = $this->_getJournalFileManager();
$imageName = 'cssHeaderImage';
if ($journalFileManager->uploadedFileExists($imageName)) {
$extension = $this->_getUploadedImageFileExtension($imageName, $journalFileManager);
$journalFileManager->uploadFile('cssHeaderImage', '/css/article_header' . $extension);
}
}
/**
* Ensure attached header image is a jpg or png
*
* @param $imageName string File name of the uploaded image file
*
* @return bool Whether or not the image file is a jpg or png
*/
function _validateImage($imageName) {
$journalFileManager = $this->_getJournalFileManager();
if ($journalFileManager->uploadedFileExists($imageName)) {
$extension = $this->_getUploadedImageFileExtension($imageName, $journalFileManager);
if ($extension != '.png' && $extension != '.jpg') {
$this->addError('coverPage', __('plugins.generic.markup.optional.cssHeaderImage'));
return false;
}
}
return true;
}
/**
* Returns the file extension of the uploaded image file
*
* @param $fileName mixed Name of the uploaded image file
* @param $journalFileManager mixed Journal file manager object
*
* @return string File extension
*/
function _getUploadedImageFileExtension($fileName, &$journalFileManager) {
$type = $journalFileManager->getUploadedFileType($fileName);
return $journalFileManager->getImageExtension($type);
}
/**
* Returns JournalFileManager instance
*
* @param $journal mixed Journal to get a fiel manager instance for
*
* @return mixed JournalFileManager instance
*/
function _getJournalFileManager($journal = null) {
if (!$journal) { $journal =& Request::getJournal(); }
import('classes.file.JournalFileManager');
return new JournalFileManager($journal);
}
}