-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathForthcomingPlugin.php
More file actions
208 lines (182 loc) · 7.03 KB
/
ForthcomingPlugin.php
File metadata and controls
208 lines (182 loc) · 7.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
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
<?php
/**
* @file ForthcomingPlugin.php
*
* Copyright (c) 2014-2024 Simon Fraser University
* Copyright (c) 2014-2024 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @package plugins.generic.forthcoming
*
* @class ForthcomingPlugin
*
* @brief ForthcomingPlugin main class
*/
namespace APP\plugins\generic\forthcoming;
use APP\core\Application;
use APP\plugins\generic\forthcoming\classes\Handler;
use APP\plugins\generic\forthcoming\classes\SettingsForm;
use PKP\core\JSONMessage;
use PKP\linkAction\LinkAction;
use PKP\linkAction\request\AjaxModal;
use PKP\plugins\GenericPlugin;
use PKP\plugins\Hook;
class ForthcomingPlugin extends GenericPlugin
{
/**
* @copydoc Plugin::getDisplayName()
*/
public function getDisplayName(): string
{
return __('plugins.generic.forthcoming.displayName');
}
/**
* @copydoc Plugin::getDisplayName()
*/
public function getDescription(): string
{
return __('plugins.generic.forthcoming.description');
}
/**
* @copydoc Plugin::register()
*
* @param null|mixed $mainContextId
*/
public function register($category, $path, $mainContextId = null): bool
{
if (!parent::register($category, $path, $mainContextId)) {
return false;
}
if ($this->getEnabled($mainContextId)) {
Hook::add('LoadHandler', [$this, 'loadHandler']);
# Add Forthcoming label to article summary, hide Forthcoming issue from public issue archive and mark in backend archive, redirect calls to issue landing page to custom Forthcoming handler
Hook::add('TemplateManager::display', [$this, 'displayTemplate']);
}
return true;
}
/**
* Handle Forthcoming issue in archive listings
*/
public function displayTemplate(string $hookName, array $params): bool
{
/** @var \APP\template\TemplateManager $templateManager */
[$templateManager, $template] = $params;
$request = Application::get()->getRequest();
$contextId = $request->getContext()?->getId();
$forthcomingIssueId = (int) $this->getSetting($contextId, 'forthcomingIssueId');
if (!$forthcomingIssueId) {
return Hook::CONTINUE;
}
switch ($template) {
// Redirect default issue toc page to Forthcoming page
case 'frontend/pages/issue.tpl':
$issueId = $templateManager->getTemplateVars('issueId');
if ($forthcomingIssueId === (int) $issueId) {
$router = $request->getRouter();
$request->redirectUrl($router->url($request, null, 'forthcoming'));
}
break;
// Article landing page
case 'frontend/pages/article.tpl':
$publication = $templateManager->getTemplateVars('publication');
if ((int) $publication?->getData('issueId') === $forthcomingIssueId) {
$templateManager->registerFilter('output', [$this, 'articleLandingPageFilter']);
}
break;
// Remove Forthcoming issue from the list of issues
case 'frontend/pages/issueArchive.tpl':
$issues = $templateManager->getTemplateVars('issues');
$total = $templateManager->getTemplateVars('total');
foreach ($issues as $key => $issue) {
if ($issue->getId() === (int) $forthcomingIssueId) {
unset($issues[$key]);
--$total;
break;
}
}
$templateManager->assign(['issues' => $issues, 'total' => $total]);
break;
// Backend archive display
case 'manageIssues/issues.tpl':
$forthcomingIssueBackendStyles = "span#cell-{$forthcomingIssueId}-identification:after { font-family: FontAwesome; content: \"\f005\"; }";
$templateManager->addStylesheet(
'forthcomingIssueBackendStyles',
$forthcomingIssueBackendStyles,
['inline' => true, 'contexts' => 'backend']
);
break;
}
return Hook::CONTINUE;
}
/**
* Setup the plugin handler
*/
public function loadHandler($hookName, $args): bool
{
$page = $args[0];
if ($page !== 'forthcoming') {
return Hook::CONTINUE;
}
$request = $this->getRequest();
$forthcomingIssueId = (int) $this->getSetting($request->getContext()->getId(), 'forthcomingIssueId');
if ($forthcomingIssueId) {
define('HANDLER_CLASS', Handler::class);
Handler::setPlugin($this);
Handler::setForthcomingId($forthcomingIssueId);
return Hook::ABORT;
}
$router = $request->getRouter();
$request->redirectUrl($router->url($request, null, 'index'));
return Hook::CONTINUE;
}
/**
* Removed the authorFormFilter and adds a "Forthcoming" label to the article's landing page
*/
public function articleLandingPageFilter($output, $templateMgr): string
{
if (!preg_match('/<h1[^>]+class="page_title"[^>]*>/', $output, $matches, PREG_OFFSET_CAPTURE)) {
return $output;
}
$offset = $matches[0][1];
$newOutput = substr($output, 0, $offset);
$newOutput .= '<div class="forthcomingLabel"><span style="border-radius: 5px; background: #ebebeb; color: #262626; padding: 6px;">' . __('plugins.generic.forthcoming.label') . '</span></div><br />';
$newOutput .= substr($output, $offset);
$output = $newOutput;
$templateMgr->unregisterFilter('output', [$this, 'authorFormFilter']);
return $output;
}
/**
* @copydoc Plugin::getActions()
*/
public function getActions($request, $actionArgs): array
{
$actions = parent::getActions($request, $actionArgs);
if (!$this->getEnabled()) {
return $actions;
}
$router = $request->getRouter();
$url = $router->url($request, null, null, 'manage', null, ['verb' => 'settings', 'plugin' => $this->getName(), 'category' => 'generic']);
array_unshift($actions, new LinkAction('settings', new AjaxModal($url, $this->getDisplayName()), __('manager.plugins.settings')));
return $actions;
}
/**
* @copydoc Plugin::manage()
*/
public function manage($args, $request): JSONMessage
{
if ($request->getUserVar('verb') !== 'settings') {
return parent::manage($args, $request);
}
$form = new SettingsForm($this);
if (!$request->getUserVar('save')) {
$form->initData();
return new JSONMessage(true, $form->fetch($request));
}
$form->readInputData();
if (!$form->validate()) {
return new JSONMessage(true, $form->fetch($request));
}
$form->execute();
return new JSONMessage(true);
}
}