-
Notifications
You must be signed in to change notification settings - Fork 35
/
OldGreggThemePlugin.inc.php
executable file
·392 lines (317 loc) · 12.1 KB
/
OldGreggThemePlugin.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
<?php
/**
* @file plugins/themes/oldGregg/OldGreggThemePlugin.inc.php
*
* Copyright (c) 2017 Vitaliy Bezsheiko, MD
* Distributed under the GNU GPL v3.
*
* @class OldGreggThemePlugin
*
* @brief Old Gregg theme is developed on the basis of bootstrap 4; it has build-in fucntionality of JATS Parser Plugin and browse latest articles plugin
*/
import('lib.pkp.classes.plugins.ThemePlugin');
define('OLDGREGG_CSL_STYLE_DEFAULT', 'vancouver');
define('OLDGREGG_LATEST_ARTICLES_DEFAULT', 20);
define('OLDGREGG_ISSUE_COVER_RELATIVE_URL', 'images/issue_default.jpg');
define('OLDGREGG_LATEST_ISSUES_DEFAULT', 3);
class OldGreggThemePlugin extends ThemePlugin
{
/**
* Initialize the theme's styles, scripts and hooks. This is only run for
* the currently active theme.
*
* @return null
*/
public function init()
{
// Register theme options
$this->addOption('latestArticlesNumber', 'text', array(
'label' => 'plugins.gregg.latest.number',
'description' => 'plugins.gregg.latest.description',
'default' => OLDGREGG_LATEST_ARTICLES_DEFAULT
));
$this->addOption('displayIssuesSlider', 'text', array(
'label' => 'plugins.gregg.if-display.issue-slider',
'description' => 'plugins.gregg.if-display.issue-slider.description',
'default' => OLDGREGG_LATEST_ISSUES_DEFAULT
));
$this->addOption('journalSummary', 'radio', array(
'label' => 'plugins.gregg.journal.summary.display',
'options' => array(
'true' => 'plugins.gregg.journal.summary.display.true',
'false' => 'plugins.gregg.journal.summary.display.false',
),
'default' => false
));
// Number of Categories to display on the front page
$this->addOption("numCategoriesHomepage", "text", array(
'label' => 'plugins.gregg.journal.categories.label',
'description' => 'plugins.gregg.journal.categories.description',
'default' => 4
));
$this->addStyle('bootstrap', 'resources/bootstrap/css/bootstrap.min.css');
$this->addStyle('less', 'resources/less/import.less');
$this->addScript('jquery', 'resources/jquery/jquery.min.js');
$this->addScript('popper', 'resources/bootstrap/js/popper.min.js');
$this->addScript('bootstrap', 'resources/bootstrap/js/bootstrap.min.js');
$this->addScript('fontawesome', 'resources/js/fontawesome-all.min.js');
$this->addScript('main', 'resources/js/main.js');
$this->addStyle(
'my-custom-font1',
'//fonts.googleapis.com/css?family=Lora',
array('baseUrl' => 'https://fonts.googleapis.com/css?family=Lora:400,400i,700,700i&display=swap&subset=cyrillic,vietnamese" rel="stylesheet'));
$this->addStyle(
'my-custom-font2',
'//fonts.googleapis.com/css?family=PT+Serif:400,400i,700,700i&display=swap&subset=cyrillic',
array('baseUrl' => ''));
$this->addStyle(
'my-custom-font6',
'//fonts.googleapis.com/css?family=Source+Sans+Pro:400,600,700,900&display=swap&subset=cyrillic',
array('baseUrl' => ''));
$this->addMenuArea(array('primary', 'user'));
HookRegistry::register('TemplateManager::display', array($this, 'browseLatest'));
HookRegistry::register('TemplateManager::display', array($this, 'browsePopular'));
HookRegistry::register('TemplateManager::display', array($this, 'sitewideData'));
HookRegistry::register('TemplateManager::display', array($this, 'latestIssuesSlider'), HOOK_SEQUENCE_NORMAL);
HookRegistry::register('TemplateManager::display', array($this, 'journalDescription'), HOOK_SEQUENCE_NORMAL);
HookRegistry::register('TemplateManager::display', array($this, 'categoriesJournalIndex'), HOOK_SEQUENCE_NORMAL);
HookRegistry::register('TemplateManager::display', array($this, 'articleSection'));
HookRegistry::register('TemplateManager::display', array($this, 'articleJs'));
}
/**
* Get the display name of this plugin
* @return string
*/
function getDisplayName()
{
return __('plugins.themes.oldGregg.name');
}
/**
* Get the description of this plugin
* @return string
*/
function getDescription()
{
return __('plugins.themes.oldGregg.description');
}
/* For retrieving articles from the database */
public function browseLatest($hookName, $args)
{
$smarty = $args[0];
$template = $args[1];
if ($template != 'frontend/pages/indexJournal.tpl') return false;
/* get number of latest article to display from user input; if there was none - use default */
$latestArticles = $this->getOption("latestArticlesNumber");
if (is_null($latestArticles)) {
$latestArticles = OLDGREGG_LATEST_ARTICLES_DEFAULT;
} else {
$latestArticles = intval($latestArticles);
}
/* retrieve current journal id from the request */
$request = $this->getRequest();
$journal = $request->getJournal();
$journalId = $journal->getId();
/* retrieve latest articles */
$publishedArticleObjects = Services::get("submission")->getMany([
'status' => STATUS_PUBLISHED,
'contextId' => $journalId,
'count' => $latestArticles,
'orderBy' => 'datePublished',
]);
$smarty->assign('publishedArticles', iterator_to_array($publishedArticleObjects));
}
function sitewideData($hookName, $args) {
$smarty = $args[0];
$orcidImagePath = $this->getRequest()->getBaseUrl() . DIRECTORY_SEPARATOR . $this->getTemplatePath() . DIRECTORY_SEPARATOR . "images" . DIRECTORY_SEPARATOR . "orcid.png";
$smarty->assign('orcidImagePath', $orcidImagePath);
}
/**
* @param $hookName string
* @param $args array [TemplateManager, string]
* @brief display most popular articles
* @return void
* Thanks to @ajnyga
*/
function browsePopular($hookName, $args) {
$smarty = $args[0];
$template = $args[1];
if ($template != 'frontend/pages/indexJournal.tpl') return false;
$request = $this->getRequest();
$context = $request->getContext();
$cacheManager = CacheManager::getManager();
$cache = $cacheManager->getCache('oldgregg', $context->getId(), array($this, '_toCache'));
$daysToStale = 1;
if (time() - $cache->getCacheTime() > 60 * 60 * 24 * $daysToStale) {
$cache->flush();
}
$popularArticles = $cache->getContents();
$smarty->assign([
'popularArticles' => $popularArticles,
'locale' => AppLocale::getLocale(),
]);
}
/**
* @param $cache FileCache
*/
function _toCache($cache) {
$request = $this->getRequest();
$context = $request->getContext();
// Find most viewed articles
$filter = array(
STATISTICS_DIMENSION_ASSOC_TYPE => ASSOC_TYPE_SUBMISSION,
);
$filter[STATISTICS_DIMENSION_DAY]['from'] = date('Ymd', mktime(0, 0, 0, date("m")-12, date("d"), date("Y")));
$filter[STATISTICS_DIMENSION_DAY]['to'] = date('Ymd');
$orderBy = array(STATISTICS_METRIC => STATISTICS_ORDER_DESC);
$column = array(
STATISTICS_DIMENSION_SUBMISSION_ID,
);
$latestArticles = $this->getOption("latestArticlesNumber");
if (is_null($latestArticles)) {
$latestArticles = OLDGREGG_LATEST_ARTICLES_DEFAULT;
} else {
$latestArticles = intval($latestArticles);
}
$dbrange = new DBResultRange($latestArticles);
$results = $context->getMetrics(OJS_METRIC_TYPE_COUNTER, $column, $filter, $orderBy, $dbrange);
// Write into cache
$supportedLocales = AppLocale::getSupportedLocales();
$popularArticles = array();
foreach ($results as $result) {
$publishedArticle = Services::get('submission')->get($result['submission_id']);
// The submission likely was unpublished, see Vitaliy-1/oldGregg#132
if (!$publishedArticle) continue;
// Can't cache objects
$popularArticles[$result['submission_id']] = array(
'views' => $result['metric'],
'date_published' => $publishedArticle->getDatePublished()
);
$localizedTitle = array();
foreach ($supportedLocales as $key => $locale) {
$localizedTitle[$key] = $publishedArticle->getFullTitle($key);
}
$popularArticles[$result['submission_id']]['localized_title'] = $localizedTitle;
if (!empty($publishedArticle->getAuthors())) {
$authorsArray = array();
foreach ($publishedArticle->getAuthors() as $author) {
foreach ($supportedLocales as $key => $locale) {
$authorArray[$key] = array(
'family_name' => $author->getFamilyName($key),
'given_name' => $author->getGivenName($key),
);
}
$authorsArray[] = $authorArray;
}
$popularArticles[$result['submission_id']]['authors'] = $authorsArray;
}
}
$cache->setEntireCache($popularArticles);
}
public function latestIssuesSlider($hookName, $args) {
$smarty = $args[0];
$template = $args[1];
if ($template != 'frontend/pages/indexJournal.tpl') return false;
$latestIssuesInput = $this->getOption("displayIssuesSlider");
if (is_null($latestIssuesInput)) {
$latestIssuesInput = OLDGREGG_LATEST_ISSUES_DEFAULT;
} elseif (intval($latestIssuesInput) === 0) {
return false;
} else {
$latestIssuesInput = intval($latestIssuesInput);
}
$request = $this->getRequest();
$journal = $request->getJournal();
$journalId = $journal->getId();
$issuesIterator = Services::get('issue')->getMany([
'count' => $latestIssuesInput,
'contextId' => $journalId
]);
$defaultCoverImageUrl = "/" . $this->getPluginPath() . "/" . OLDGREGG_ISSUE_COVER_RELATIVE_URL;
$latestIssues = iterator_to_array($issuesIterator);
$smarty->assign('latestIssues', $latestIssues);
$smarty->assign('defaultCoverImageUrl', $defaultCoverImageUrl);
}
public function journalDescription ($hookName, $args) {
$smarty = $args[0];
$showSummaryData = $this->getOption("journalSummary");
$showSummary = false;
if (!is_null($showSummaryData) && ($showSummaryData == "true")) {
$showSummary = true;
}
$smarty->assign(array(
'showSummary' => $showSummary,
'journalFilesPath' => $this->getRequest()->getBaseUrl() . '/' . Config::getVar('files', 'public_files_dir') . '/journals/',
));
}
/**
* @param $hookname string
* @param $args array [
* @option TemplateManager
* @option string relative path to the template
* ]
* @brief Add categories to the journal landing page
*/
public function categoriesJournalIndex($hookname, $args) {
$templateMgr = $args[0];
$template = $args[1];
if ($template != "frontend/pages/indexJournal.tpl") return false;
$reguest = $this->getRequest();
$context = $reguest->getContext();
$categoryDao = DAORegistry::getDAO('CategoryDAO');
$categoriesObject = $categoryDao->getByContextId($context->getId());
$numCategoriesHomepage = intval($this->getOption("numCategoriesHomepage"));
$categories = array();
while ($category = $categoriesObject->next()) {
$categories[] = $category;
}
$templateMgr->assign(array(
'categories' => $categories,
'numCategoriesHomepage' => $numCategoriesHomepage
));
}
/**
* @param $hookname string
* @param $args array [
* @option TemplateManager
* @option string relative path to the template
* ]
* @brief Add article Section Title
*/
public function articleSection($hookName, $args) {
$templateMgr = $args[0];
$template = $args[1];
if ($template != "frontend/pages/article.tpl" && $template != "plugins-plugins-generic-jatsParser-generic-jatsParser:articleGalleyView.tpl") return;
$article = $templateMgr->getTemplateVars('article');
$sectionId = $article->getSectionId();
$sectionDao = DAORegistry::getDAO("SectionDAO");
$section = $sectionDao->getById($sectionId);
$sectionTitle = $section->getLocalizedData('title');
$article->setData('title',$sectionTitle);
$publication = $templateMgr->getTemplateVars('publication');
$categoryIds = $publication->getData('categoryIds');
$categoryDao = DAORegistry::getDAO('CategoryDAO');
$categories = [];
foreach ($categoryIds as $categoryId) {
$category = $categoryDao->getById($categoryId);
$categories[] = $category;
}
$templateMgr->assign([
'article' => $article,
'categories' => $categories,
'section' => $section
]);
}
/**
* @param $hookName
* @param $args
* Adds javascript file to the article landing page & (temporary) galley page
*/
public function articleJs($hookName, $args) {
$templateMgr = $args[0];
$template = $args[1];
if ($template != 'frontend/pages/article.tpl' && $template != 'plugins-plugins-generic-jatsParser-generic-jatsParser:articleGalleyView.tpl') return;
$request = $this->getRequest();
$templateMgr->addJavascript('article', $request->getBaseUrl() . '/' . $this->getPluginPath() . '/resources/js/article.js');
}
}
?>