-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathCustomHeaderSettingsForm.php
More file actions
108 lines (95 loc) · 3.57 KB
/
CustomHeaderSettingsForm.php
File metadata and controls
108 lines (95 loc) · 3.57 KB
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
<?php
/**
* @file CustomHeaderSettingsForm.php
*
* Copyright (c) 2013-2025 Simon Fraser University
* Copyright (c) 2003-2025 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file LICENSE.
*
* @brief Form for managers to modify custom header plugin settings
*/
namespace APP\plugins\generic\customHeader;
use DOMDocument;
use APP\core\Application;
use APP\notification\NotificationManager;
use APP\template\TemplateManager;
use PKP\form\Form;
use PKP\form\validation\FormValidator;
use PKP\form\validation\FormValidatorCSRF;
use PKP\form\validation\FormValidatorCustom;
use PKP\form\validation\FormValidatorPost;
class CustomHeaderSettingsForm extends Form
{
public ?int $contextId;
public CustomHeaderPlugin $plugin;
/**
* Constructor
*/
public function __construct(CustomHeaderPlugin $plugin, ?int $contextId)
{
$this->contextId = $contextId;
$this->plugin = $plugin;
parent::__construct($plugin->getTemplateResource('settingsForm.tpl'));
$this->addCheck(new FormValidatorCustom($this, 'backendContent', FormValidator::FORM_VALIDATOR_OPTIONAL_VALUE, 'plugins.generic.customHeader.backendContent.error', function ($backendContent) {
return $this->validateWellFormed($backendContent);
}));
$this->addCheck(new FormValidatorPost($this));
$this->addCheck(new FormValidatorCSRF($this));
}
/**
* Initialize form data.
*/
public function initData(): void
{
$this->_data = array(
'content' => $this->plugin->getSetting($this->contextId, 'content'),
'backendContent' => $this->plugin->getSetting($this->contextId, 'backendContent'),
'footerContent' => $this->plugin->getSetting($this->contextId, 'footerContent')
);
}
/**
* Assign form data to user-submitted data.
*/
public function readInputData(): void
{
$this->readUserVars(array('content', 'backendContent', 'footerContent'));
}
/**
* Fetch the form.
* @copydoc Form::fetch()
*/
public function fetch($request, $template = null, $display = false): null|string
{
$templateMgr = TemplateManager::getManager($request);
$templateMgr->assign('pluginName', $this->plugin->getName());
return parent::fetch($request, $template, $display);
}
/**
* Save settings.
*/
public function execute(...$functionArgs)
{
parent::execute(...$functionArgs);
$request = Application::get()->getRequest();
$this->plugin->updateSetting($this->contextId, 'content', $this->getData('content'), 'string');
$this->plugin->updateSetting($this->contextId, 'backendContent', $this->getData('backendContent'), 'string');
$this->plugin->updateSetting($this->contextId, 'footerContent', $this->getData('footerContent'), 'string');
$notificationManager = new NotificationManager();
$notificationManager->createTrivialNotification($request->getUser()->getId());
}
/**
* Validate that the input is well-formed XML
* We want to avoid breaking the whole HTML page with an unclosed HTML attribute quote or tag
*/
public function validateWellFormed(string $input): bool
{
$libxml_errors_setting = libxml_use_internal_errors();
libxml_use_internal_errors(true);
libxml_clear_errors();
$dom = new DOMDocument();
$dom->loadHTML($input);
$isWellFormed = count(libxml_get_errors()) == 0;
libxml_use_internal_errors($libxml_errors_setting);
return $isWellFormed;
}
}