forked from RBoelter/mostViewed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMostViewedPlugin.inc.php
133 lines (126 loc) · 3.5 KB
/
MostViewedPlugin.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
<?php
import('lib.pkp.classes.plugins.GenericPlugin');
class MostViewedPlugin extends GenericPlugin
{
/**
* @return string plugin name
*/
public function getDisplayName()
{
return __('plugins.generic.most.viewed.title');
}
/**
* @return string plugin description
*/
public function getDescription()
{
return __('plugins.generic.most.viewed.desc');
}
/**
* Register the plugin
*
* @param String $category
* @param String $path
* @param null $mainContextId
* @return bool
*/
public function register($category, $path, $mainContextId = NULL)
{
$success = parent::register($category, $path);
HookRegistry::register('AcronPlugin::parseCronTab', array($this, 'callbackParseCronTab'));
if ($success && $this->getEnabled()) {
$request = Application::getRequest();
$templateMgr = TemplateManager::getManager($request);
$templateMgr->addStyleSheet(
'mostViewedArticles',
$request->getBaseUrl() . '/' . $this->getPluginPath() . '/css/mostViewed.css'
);
HookRegistry::register('Templates::Index::journal', array($this, 'mostViewedContent'));
}
return $success;
}
/**
* Append most viewed Content to indexJournal.tpl
* @param $hookName
* @param $args
*/
public function mostViewedContent($hookName, $args)
{
$smarty =& $args[1];
$output =& $args[2];
$request = Application::getRequest();
$contextId = $request->getContext()->getId();
$smarty->assign('mostReadArticles', json_decode($this->getSetting($contextId, 'articles'), true));
$settings = json_decode($this->getSetting($contextId, 'settings'), true);
if ($settings) {
if ($settings['title'])
$smarty->assign('mostReadHeadline', $settings['title']);
if ($settings['position'])
$smarty->assign('mostReadPosition', $settings['position']);
}
$output .= $smarty->fetch($this->getTemplateResource('mostViewed.tpl'));
}
/**
* Add settings button to plugin
* @param $request
* @param array $verb
* @return array
*/
public function getActions($request, $verb)
{
$router = $request->getRouter();
import('lib.pkp.classes.linkAction.request.AjaxModal');
return array_merge(
$this->getEnabled() ? array(
new LinkAction(
'settings',
new AjaxModal(
$router->url($request, null, null, 'manage', null, array('verb' => 'settings', 'plugin' => $this->getName(), 'category' => 'generic')),
$this->getDisplayName()
),
__('manager.plugins.settings'),
null
),
) : array(),
parent::getActions($request, $verb)
);
}
/**
* Manage Settings
* @param array $args
* @param PKPRequest $request
* @return JSONMessage
*/
public function manage($args, $request)
{
switch ($request->getUserVar('verb')) {
case 'settings':
$this->import('MostViewedSettingsForm');
$form = new MostViewedSettingsForm($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);
}
/**
* Acron Plugin Auto Stage: It is needed if server has no configured cron jobs.
* @param $hookName
* @param $args
* @return bool
*/
function callbackParseCronTab($hookName, $args)
{
if ($this->getEnabled() || !Config::getVar('general', 'installed')) {
$taskFilesPath =& $args[0]; // Reference needed.
$taskFilesPath[] = $this->getPluginPath() . DIRECTORY_SEPARATOR . 'scheduledTasksAutoStage.xml';
}
return false;
}
}