-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathCustomHeaderPlugin.php
More file actions
168 lines (153 loc) · 5.03 KB
/
CustomHeaderPlugin.php
File metadata and controls
168 lines (153 loc) · 5.03 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
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
<?php
/**
* @file CustomHeaderPlugin.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 CustomHeader plugin class
*/
namespace APP\plugins\generic\customHeader;
use APP\core\Application;
use Exception;
use PKP\config\Config;
use PKP\core\JSONMessage;
use PKP\core\PKPApplication;
use PKP\linkAction\LinkAction;
use PKP\linkAction\request\AjaxModal;
use PKP\plugins\GenericPlugin;
use PKP\plugins\Hook;
class CustomHeaderPlugin extends GenericPlugin
{
/** Whether the header has been injected */
public bool $injected = false;
/**
* @copydoc Plugin::register()
* @throws Exception
*/
public function register($category, $path, $mainContextId = null): bool
{
$success = parent::register($category, $path, $mainContextId);
if (!Config::getVar('general', 'installed') || defined('RUNNING_UPGRADE')) {
return true;
}
if ($success && $this->getEnabled()) {
// Insert CustomHeader page tag to page header
Hook::add('TemplateManager::display', array($this, 'displayTemplateHook'));
// Insert custom script to the page footer
Hook::add('Templates::Common::Footer::PageFooter', array($this, 'insertFooter'));
}
return $success;
}
/**
* Get the plugin display name.
*/
public function getDisplayName(): string
{
return __('plugins.generic.customHeader.displayName');
}
/**
* Get the plugin description.
*/
public function getDescription(): string
{
return __('plugins.generic.customHeader.description');
}
/**
* @copydoc Plugin::getActions()
*/
public function getActions($request, $verb): array
{
$router = $request->getRouter();
return array_merge(
$this->getEnabled() ? array(
new LinkAction(
'settings',
new AjaxModal(
$router->url($request, null, null, 'manage', null, ['verb' => 'settings', 'plugin' => $this->getName(), 'category' => 'generic']),
$this->getDisplayName()
),
__('manager.plugins.settings'),
null
),
) : array(),
parent::getActions($request, $verb)
);
}
/**
* @copydoc Plugin::manage()
* @throws Exception
*/
public function manage($args, $request)
{
switch ($request->getUserVar('verb')) {
case 'settings':
$context = $request->getContext();
$form = new CustomHeaderSettingsForm(
$this,
$context ? $context->getId() : PKPApplication::SITE_CONTEXT_ID
);
if ($request->getUserVar('save')) {
$form->readInputData();
if ($form->validate()) {
$form->execute();
return new JSONMessage(true);
}
} else {
$form->initData();
}
return new JSONMessage(true, $form->fetch($request));
}
return parent::manage($args, $request);
}
/**
* Register the CustomHeader script tag
*/
public function displayTemplateHook(string $hookName, array $params)
{
if (!$this->injected) {
$this->injected = true;
$templateMgr =& $params[0];
$request = Application::get()->getRequest();
$context = $request->getContext();
$templateMgr->addHeader(
'custom',
$this->getSetting($context ? $context->getId() : PKPApplication::SITE_CONTEXT_ID, 'content')
);
$templateMgr->addHeader(
'custombackend',
$this->getSetting(
$context ? $context->getId() : PKPApplication::SITE_CONTEXT_ID,
'backendContent'
),
['contexts' => ['backend']]
);
}
return false;
}
/**
* Add custom footer to the page
*/
public function insertFooter(string $hookName, array $params)
{
$templateMgr =& $params[0];
$output =& $params[2];
$request = Application::get()->getRequest();
$context = $request->getContext();
$output .= $this->getSetting($context ? $context->getId() : PKPApplication::SITE_CONTEXT_ID, 'footerContent');
return false;
}
/**
* This plugin can be used site-wide or in a specific context. The
* isSitePlugin check is used to grant access to different users, so this
* plugin must return true only if the user is currently in the site-wide
* context.
*
* @see PluginGridRow::_canEdit()
*/
public function isSitePlugin(): bool
{
return !(Application::get()->getRequest()->getContext());
}
}