-
Notifications
You must be signed in to change notification settings - Fork 18
/
PragmaThemePlugin.inc.php
245 lines (204 loc) · 7.33 KB
/
PragmaThemePlugin.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
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
<?php
/**
* @file plugins/themes/pragma/PragmaThemePlugin.inc.php
*
* Copyright (c) 2014-2022 Simon Fraser University
* Copyright (c) 2003-2022 John Willinsky
* Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
*
* @class PragmaThemePlugin
* @ingroup plugins_themes_pragma
*
* @brief Pragma theme
*/
use APP\facades\Repo;
use APP\issue\Collector;
use APP\services\NavigationMenuService;
use PKP\plugins\ThemePlugin;
class PragmaThemePlugin extends ThemePlugin {
public function init() {
/* Additional theme options */
// Change theme primary color
$this->addOption('baseColour', 'FieldColor', array(
'label' => __('plugins.themes.default.option.colour.label'),
'default' => '#A8DCDD',
));
// Add usage stats display options
$this->addOption('displayStats', 'FieldOptions', [
'type' => 'radio',
'label' => __('plugins.themes.pragma.option.displayStats.label'),
'options' => [
[
'value' => 'none',
'label' => __('plugins.themes.pragma.option.displayStats.none'),
],
[
'value' => 'bar',
'label' => __('plugins.themes.pragma.option.displayStats.bar'),
],
[
'value' => 'line',
'label' => __('plugins.themes.pragma.option.displayStats.line'),
],
],
'default' => 'none',
]);
$baseColour = $this->getOption('baseColour');
$additionalLessVariables = [];
if ($baseColour !== '#A8DCDD') {
$additionalLessVariables[] = '@primary-colour:' . $baseColour . ';';
$additionalLessVariables[] = '@secondary-colour: darken(@primary-colour, 50%);';
}
// Update contrast colour based on primary colour
if ($this->isColourDark($this->getOption('baseColour'))) {
$additionalLessVariables[] = '
@contrast-colour: rgba(255, 255, 255, 0.95);
@secondary-contrast-colour: rgba(255, 255, 255, 0.75);
@tertiary-contrast-colour: rgba(255, 255, 255, 0.65);
';
}
$themeUrl = $this->getPluginPath();
$additionalLessVariables[] = "@themeUrl: '$themeUrl';";
// Add navigation menu areas for this theme
$this->addMenuArea(array('primary', 'user'));
// Adding styles (JQuery UI, Bootstrap, Tag-it)
$this->addStyle('app-css', 'resources/dist/app.min.css');
$this->addStyle('stylesheet', 'resources/less/import.less');
$this->modifyStyle('stylesheet', array('addLessVariables' => join("\n", $additionalLessVariables)));
// Adding scripts (JQuery, Popper, Bootstrap, JQuery UI, Tag-it, Theme's JS)
$this->addScript('app-js', 'resources/dist/app.min.js');
// Styles for HTML galleys
$this->addStyle('htmlGalley', 'resources/less/import.less', array('contexts' => 'htmlGalley'));
$this->modifyStyle('htmlGalley', array('addLessVariables' => join("\n", $additionalLessVariables)));
HookRegistry::add('TemplateManager::display', array($this, 'addSiteWideData'));
HookRegistry::add('TemplateManager::display', array($this, 'addIndexJournalData'));
HookRegistry::add('TemplateManager::display', array($this, 'checkCurrentPage'));
}
/**
* Get the display name of this theme
* @return string
*/
public function getDisplayName() {
return __('plugins.themes.pragma.name');
}
/**
* Get the description of this plugin
* @return string
*/
public function getDescription() {
return __('plugins.themes.pragma.description');
}
/**
* @param $hookname string
* @param $args array
*/
public function addSiteWideData($hookname, $args) {
$templateMgr = $args[0];
$request = $this->getRequest();
$journal = $request->getJournal();
$baseColour = $this->getOption('baseColour');
if (!defined('SESSION_DISABLE_INIT')) {
// Check locales
if ($journal) {
$locales = $journal->getSupportedLocaleNames();
} else {
$locales = $request->getSite()->getSupportedLocaleNames();
}
// Load login form
$loginUrl = $request->url(null, 'login', 'signIn');
if (Config::getVar('security', 'force_login_ssl')) {
$loginUrl = PKPString::regexp_replace('/^http:/', 'https:', $loginUrl);
}
$orcidImageUrl = $this->getPluginPath() . '/templates/images/orcid.png';
if ($request->getContext()) {
$templateMgr->assign('pragmaHomepageImage', $journal->getLocalizedSetting('homepageImage'));
}
$templateMgr->assign(array(
'languageToggleLocales' => $locales,
'loginUrl' => $loginUrl,
'orcidImageUrl' => $orcidImageUrl,
'baseColour' => $baseColour,
));
}
}
/**
* @param $hookname string
* @param $args array
*/
public function addIndexJournalData($hookname, $args) {
$templateMgr = $args[0];
$template = $args[1];
if ($template !== 'frontend/pages/indexJournal.tpl') return false;
$contextId = $this->getRequest()->getContext()->getId();
$recentIssuesWithCurrent = Repo::issue()->getCollector()
->filterByContextIds([$contextId])
->filterByPublished(true)
->limit(4)
->orderBy(Collector::ORDERBY_PUBLISHED_ISSUES)
->getMany();
$currentIssue = Repo::issue()->getCurrent($contextId);
// Exclude the current issue from the list
$recentIssues = [];
if ($currentIssue) {
foreach ($recentIssuesWithCurrent as $issue) {
if ($issue->getId() !== $currentIssue->getId()) {
$recentIssues[] = $issue;
}
}
}
$templateMgr->assign('recentIssues', $recentIssues);
}
/**
* @param $hookname string
* @param $args array
*/
public function checkCurrentPage($hookname, $args) {
$templateMgr = $args[0];
// TODO check the issue with multiple calls of the hook on settings/website
if (!isset($templateMgr->registered_plugins["function"]["pragma_item_active"])) {
$templateMgr->registerPlugin('function', 'pragma_item_active', array($this, 'isActiveItem'));
}
}
/**
* @param $params array
* @param $smarty Smarty_Internal_Template
* @return string
*/
public function isActiveItem($params, $smarty) {
$navigationMenuItem = $params['item'];
$emptyMarker = '';
$activeMarker = ' active';
// Get URL of the current page
$request = $this->getRequest();
$currentUrl = $request->getCompleteUrl();
$currentPage = $request->getRequestedPage();
if (!($navigationMenuItem instanceof NavigationMenuItem && $navigationMenuItem->getIsDisplayed())) {
if (is_string($navigationMenuItem)) {
$navigationMenuItemIndex = preg_replace('/index$/', '', $navigationMenuItem);
$navigationMenuItemSlash = preg_replace('/\/index$/', '', $navigationMenuItem);
if ($navigationMenuItem == $currentUrl || $navigationMenuItemIndex == $currentUrl || $navigationMenuItemSlash == $currentUrl) {
return $activeMarker;
} else {
return $emptyMarker;
}
} else {
return $emptyMarker;
}
}
// Do not add an active marker if it's a dropdown menu
if ($navigationMenuItem->getIsChildVisible()) return $emptyMarker;
// Retrieve URL and its components for a menu item
$itemUrl = $navigationMenuItem->getUrl();
// Check whether menu item points to the current page
$context = $request->getContext();
if ($context) {
$currentIssue = Repo::issue()->getCurrent($context->getId());
if ($navigationMenuItem->getType() === NavigationMenuService::NMI_TYPE_CURRENT) {
$issue = $smarty->getTemplateVars('issue');
if ($issue && ($issue->getId() === $currentIssue->getId()) && $currentPage == "issue") return $activeMarker;
}
}
if ($currentUrl === $itemUrl) return $activeMarker;
return $emptyMarker;
}
}