-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJournalCategoriesPlugin.php
More file actions
246 lines (211 loc) · 7.84 KB
/
JournalCategoriesPlugin.php
File metadata and controls
246 lines (211 loc) · 7.84 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
<?php
namespace APP\plugins\generic\JournalCategories;
use PKP\plugins\GenericPlugin;
use PKP\plugins\Hook;
use APP\core\Application;
use PKP\core\JSONMessage;
use APP\notification\NotificationManager;
use PKP\notification\PKPNotification;
use PKP\linkAction\LinkAction;
use PKP\linkAction\request\AjaxModal;
class JournalCategoriesPlugin extends GenericPlugin
{
public function register($category, $path, $mainContextId = NULL)
{
// Register the plugin even when it is not enabled
$success = parent::register($category, $path);
if ($success && $this->getEnabled()) {
// Hook into the site index template
Hook::add('TemplateManager::display', [$this, 'handleTemplateDisplay']);
// Add template data hook
Hook::add('TemplateManager::fetch', [$this, 'handleTemplateFetch']);
}
return $success;
}
/**
* Provide a name for this plugin
*
* The name will appear in the Plugin Gallery where editors can
* install, enable and disable plugins.
*/
public function getDisplayName()
{
return 'Journal Categories';
}
/**
* Provide a description for this plugin
*
* The description will appear in the Plugin Gallery where editors can
* install, enable and disable plugins.
*/
public function getDescription()
{
return 'An OJS plugin built for Edinburgh Diamond to add journal categories on the main homepage. Categories are configurable via plugin settings.';
}
/**
* Mark this as a site-wide plugin so it appears in Admin > Site Settings > Plugins
*/
public function isSitePlugin()
{
return true;
}
/**
* Indicate this plugin has a settings form
*/
public function getActions($request, $verb)
{
// Only show settings at the site level, not within individual journal contexts
if ($request->getContext()) {
return parent::getActions($request, $verb);
}
$router = $request->getRouter();
return array_merge(
$this->getEnabled() ? [
new LinkAction(
'settings',
new AjaxModal(
$router->url(
request: $request,
op: 'manage',
params: [
'verb' => 'settings',
'plugin' => $this->getName(),
'category' => 'generic',
]
),
$this->getDisplayName()
),
__('manager.plugins.settings'),
null
),
] : [],
parent::getActions($request, $verb)
);
}
/**
* Handle settings form display and save
*/
public function manage($args, $request)
{
switch ($request->getUserVar('verb')) {
case 'settings':
$form = new JournalCategoriesSettingsForm($this);
if ($request->getUserVar('save')) {
$form->readInputData();
if ($form->validate()) {
$form->execute();
$notificationManager = new NotificationManager();
$notificationManager->createTrivialNotification(
$request->getUser()->getId(),
PKPNotification::NOTIFICATION_TYPE_SUCCESS,
['contents' => __('common.changesSaved')]
);
return new JSONMessage(true);
}
} else {
$form->initData();
}
$form->setData('pluginName', $this->getName());
return new JSONMessage(true, $form->fetch($request));
}
return parent::manage($args, $request);
}
/**
* Get journal categories from plugin settings (falls back to empty array)
*/
private function getJournalCategories(): array
{
$categoriesJson = $this->getSetting(CONTEXT_SITE, 'categories');
if (!$categoriesJson) {
return [];
}
$categories = json_decode($categoriesJson, true);
return is_array($categories) ? $categories : [];
}
/**
* Handle template display
*/
public function handleTemplateDisplay(string $hookName, array $params): bool {
$templateMgr = $params[0];
$template = &$params[1];
// Only modify the site index page
if ($template === 'frontend/pages/indexSite.tpl') {
// Add stylesheet
$request = Application::get()->getRequest();
$baseUrl = $request->getBaseUrl();
$cssUrl = $baseUrl . '/' . $this->getPluginPath() . '/styles/journal_category.css';
$templateMgr->addStyleSheet(
'journalCategories',
$cssUrl,
[
'contexts' => 'frontend',
'priority' => STYLE_SEQUENCE_NORMAL
]
);
// Get all journals
$contextDao = Application::getContextDAO();
$contexts = $contextDao->getAll(true);
// Organize journals by categories
$categorizedJournals = $this->organizeJournalsByCategory($contexts);
// Add categorized journals to template
$templateMgr->assign('categorizedJournals', $categorizedJournals);
$templateMgr->assign('journalCategories', $this->getJournalCategories());
// Use custom template
$template = $this->getTemplateResource('indexSite.tpl');
}
return false;
}
/**
* Handle template fetch to inject custom template
*/
public function handleTemplateFetch(string $hookName, array $params): bool {
$template = &$params[1];
if ($template === 'frontend/pages/indexSite.tpl') {
$template = $this->getTemplateResource('indexSite.tpl');
}
return false;
}
/**
* Organize journals by category
*/
private function organizeJournalsByCategory($contexts): array {
$categories = $this->getJournalCategories();
$categorizedJournals = [];
$uncategorized = [];
// Initialize categories
foreach ($categories as $categoryName => $categoryInfo) {
$categorizedJournals[$categoryName] = [
'description' => $categoryInfo['description'],
'journals' => []
];
}
// Iterate through contexts and categorize
while ($context = $contexts->next()) {
$journalId = $context->getId();
$categorized = false;
foreach ($categories as $categoryName => $categoryInfo) {
// journal_ids stored as comma-separated string or array
$ids = is_array($categoryInfo['journal_ids'])
? $categoryInfo['journal_ids']
: array_map('intval', array_filter(array_map('trim', explode(',', $categoryInfo['journal_ids']))));
if (in_array($journalId, $ids)) {
$categorizedJournals[$categoryName]['journals'][] = $context;
$categorized = true;
break;
}
}
// Add to uncategorized if not in any category
if (!$categorized) {
$uncategorized[] = $context;
}
}
// Add uncategorized journals if any exist
if (!empty($uncategorized)) {
$categorizedJournals['Other Journals'] = [
'description' => 'Additional journals',
'journals' => $uncategorized
];
}
return $categorizedJournals;
}
}