-
Notifications
You must be signed in to change notification settings - Fork 0
/
EnhancedMetadataSettingsForm.inc.php
98 lines (91 loc) · 2.8 KB
/
EnhancedMetadataSettingsForm.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
<?php
import('lib.pkp.classes.form.Form');
import('classes.notification.NotificationManager');
class EnhancedMetadataSettingsForm extends Form
{
public $plugin;
private $folderPath;
/**
* EnhancedMetadataSettingsForm constructor.
* @param $plugin
*/
public function __construct($plugin)
{
parent::__construct($plugin->getTemplateResource('settings.tpl'));
$contextId = Application::getRequest()->getContext()->getId();
$this->plugin = $plugin;
$this->addCheck(new FormValidatorPost($this));
$this->addCheck(new FormValidatorCSRF($this));
$this->folderPath = Config::getVar('files', 'public_files_dir') . '/journals/' . $contextId . '/enhancedForms';
}
/**
*
*/
public function initData()
{
$settings = $this->plugin->getSetting(Application::getRequest()->getContext()->getId(), 'settings');
if (isset($settings)) {
$settings = json_decode($settings, true);
$this->setData('emFiles', $settings);
}
parent::initData();
}
/**
*
*/
public function readInputData()
{
$this->readUserVars(['emFile']);
parent::readInputData();
}
/**
* @param PKPRequest $request
* @param null $template
* @param bool $display
* @return string|null
*/
public function fetch($request, $template = null, $display = false)
{
$templateMgr = TemplateManager::getManager($request);
$templateMgr->assign('pluginName', $this->plugin->getName());
return parent::fetch($request, $template, $display);
}
/**
* @param mixed ...$args
* @return mixed|null
*/
public function execute(...$args)
{
$contextId = Application::getRequest()->getContext()->getId();
$notificationMgr = new NotificationManager();
$fileManager = new PublicFileManager();
$jsonString = $this->getData('emFile');
$jsonObj = json_decode($jsonString, true);
if (isset($jsonObj)) {
if (!$fileManager->fileExists($this->folderPath))
$fileManager->mkdir($this->folderPath);
$filepath = $this->folderPath . '/enh_' . $jsonObj['form'] . '_' . $jsonObj['version'] . '.json';
if ($fileManager->fileExists($filepath)) {
$notificationMgr->createTrivialNotification(
Application::getRequest()->getUser()->getId(),
NOTIFICATION_TYPE_ERROR,
['contents' => __('plugins.generic.enhanced.metadata.settings.upload.error')]
);
} else {
$fileManager->writeFile($filepath, $jsonString);
$settings = $this->plugin->getSetting($contextId, 'settings');
if (isset($settings)) {
$settings = json_decode($settings, true);
}
$settings[$jsonObj['form']][$jsonObj['version']] = $filepath;
$this->plugin->updateSetting($contextId, 'settings', json_encode($settings));
$notificationMgr->createTrivialNotification(
Application::getRequest()->getUser()->getId(),
NOTIFICATION_TYPE_SUCCESS,
['contents' => __('common.changesSaved')]
);
}
}
return parent::execute();
}
}