-
Notifications
You must be signed in to change notification settings - Fork 0
/
GalleysAndAbstractStatsPlugin.php
127 lines (109 loc) · 4.09 KB
/
GalleysAndAbstractStatsPlugin.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
<?php
namespace APP\plugins\generic\galleysAndAbstractStats;
use PKP\plugins\GenericPlugin;
use PKP\plugins\Hook;
use APP\core\Application;
use APP\core\Request;
use APP\core\Services;
use PKP\core\JSONMessage;
use APP\facades\Repo;
use APP\template\TemplateManager;
use PKP\linkAction\LinkAction;
use PKP\linkAction\request\AjaxModal;
class GalleysAndAbstractStatsPlugin extends GenericPlugin
{
public function register($category, $path, $mainContextId = null)
{
$success = parent::register($category, $path, $mainContextId);
if ($success && $this->getEnabled($mainContextId)) {
Hook::add('Templates::Article::Details', [$this, 'viewDataStatement']);
Hook::add('TemplateManager::display', function ($hookName, $args) {
$request = Application::get()->getRequest();
$templateMgr = TemplateManager::getManager($request);
$pluginFullPath = $request->getBaseUrl() . '/' . $this->getPluginPath();
$templateMgr->addStyleSheet('GalleysAndAbstractStats', $pluginFullPath . '/styles/index.css');
});
}
return $success;
}
public function getDisplayName()
{
return __('plugins.generic.galleysAndAbstractStats.name');
}
public function getDescription()
{
return __('plugins.generic.galleysAndAbstractStats.description');
}
public function viewDataStatement(string $hookName, array $params): bool
{
$templateMgr = &$params[1];
$output = &$params[2];
$request = Application::get()->getRequest();
$publication = $templateMgr->getTemplateVars('publication');
$submissionId = $publication->getData('submissionId');
$contextId = $request->getContext()->getId();
$statsService = Services::get('publicationStats');
$metricsByType = $statsService->getTotalsByType($submissionId, $contextId, null, null);
$galleys = Repo::galley()->getCollector()
->filterByPublicationIds(['publicationIds' => $publication->getId()])
->getMany();
$galleysViews = [];
foreach ($galleys as $galley) {
$galleysViews[] = ['galleyLabel' => $galley->getGalleyLabel(), 'galleyViews' => $galley->getViews()];
}
$templateMgr->assign([
'abstractViews' => $metricsByType['abstract'],
'galleysViews' => $galleysViews,
'statsFooterText' => $this->getSetting($contextId, 'statsFooterText')
]);
$output .= $templateMgr->fetch($this->getTemplateResource('index.tpl'));
return false;
}
public function getActions($request, $actionArgs)
{
$actions = parent::getActions($request, $actionArgs);
if (!$this->getEnabled()) {
return $actions;
}
$router = $request->getRouter();
$linkAction = 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_unshift($actions, $linkAction);
return $actions;
}
public function manage($args, $request)
{
switch ($request->getUserVar('verb')) {
case 'settings':
$form = new GalleysAndAbstractStatsForm($this);
if (!$request->getUserVar('save')) {
$form->initData();
return new JSONMessage(true, $form->fetch($request));
}
$form->readInputData();
if ($form->validate()) {
$form->execute();
return new JSONMessage(true);
}
}
return parent::manage($args, $request);
}
}