forked from pkp/addThis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AddThisPlugin.inc.php
191 lines (172 loc) · 5.56 KB
/
AddThisPlugin.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
<?php
/**
* @file AddThisPlugin.inc.php
*
* Copyright (c) 2014-2020 Simon Fraser University
* Copyright (c) 2003-2020 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file LICENSE.
*
* @class AddThisPlugin
*
* @brief This plugin provides the AddThis social media sharing options for submissions.
*/
import('lib.pkp.classes.plugins.GenericPlugin');
class AddThisPlugin extends GenericPlugin {
/**
* @copydoc Plugin::register()
*/
function register($category, $path, $mainContextId = null) {
if (parent::register($category, $path, $mainContextId)) {
if ($this->getEnabled()) {
HookRegistry::register('Schema::get::context', array($this, 'addToSchema'));
HookRegistry::register('Templates::Catalog::Book::Details', array($this, 'callbackSharingDisplay')); // OMP
HookRegistry::register('Templates::Article::Details', array($this, 'callbackSharingDisplay')); // OJS
// Register the components this plugin implements
HookRegistry::register('LoadComponentHandler', array($this, 'setupGridHandler'));
$this->_registerTemplateResource();
}
return true;
}
return false;
}
/**
* Add properties to the context schema
*
* @param $hookName string `Schema::get::context`
* @param $args [[
* @option object Context schema
* ]]
*/
public function addToSchema($hookName, $args) {
$schema = $args[0];
$prop = '{
"type": "string",
"apiSummary": true,
"validation": [
"nullable"
]
}';
$schema->properties->addThisProfileId = json_decode($prop);
$schema->properties->addThisUsername = json_decode($prop);
$schema->properties->addThisPassword = json_decode($prop);
$schema->properties->addThisDisplayStyle = json_decode($prop);
}
/**
* Permit requests to the statistics grid handler
* @param $hookName string The name of the hook being invoked
* @param $args array The parameters to the invoked hook
*/
function setupGridHandler($hookName, $params) {
$component =& $params[0];
if ($component == 'plugins.generic.addThis.controllers.grid.AddThisStatisticsGridHandler') {
// Allow the static page grid handler to get the plugin object
import($component);
AddThisStatisticsGridHandler::setPlugin($this);
return true;
}
return false;
}
/**
* Get the name of the settings file to be installed on new press
* creation.
* @return string
*/
function getContextSpecificPluginSettingsFile() {
return $this->getPluginPath() . '/settings.xml';
}
/**
* @copydoc PKPPlugin::getDisplayName()
*/
function getDisplayName() {
return __('plugins.generic.addThis.displayName');
}
/**
* @copydoc PKPPlugin::getDescription()
*/
function getDescription() {
return __('plugins.generic.addThis.description');
}
/**
* @copydoc Plugin::getActions()
*/
function getActions($request, $actionArgs) {
$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_merge($actionArgs, array('verb' => 'settings'))),
$this->getDisplayName()
),
__('manager.plugins.settings'),
null
),
):array(),
parent::getActions($request, $actionArgs)
);
}
/**
* @copydoc PKPPlugin::manage()
*/
function manage($args, $request) {
$context = $request->getContext();
$templateMgr = TemplateManager::getManager($request);
switch ($request->getUserVar('verb')) {
case 'showTab':
switch ($request->getUserVar('tab')) {
case 'settings':
$this->import('AddThisSettingsForm');
$form = new AddThisSettingsForm($this, $context);
if ($request->getUserVar('save')) {
$form->readInputData();
if ($form->validate()) {
$form->execute();
return new JSONMessage();
}
} else {
$form->initData();
}
return new JSONMessage(true, $form->fetch($request));
case 'statistics':
return $templateMgr->fetchJson($this->getTemplateResource('statistics.tpl'));
default: assert(false);
}
case 'settings':
$templateMgr->assign('statsConfigured', $this->statsConfigured($context));
$templateMgr->assign('pluginName', $this->getName());
return $templateMgr->fetchJson($this->getTemplateResource('settingsTabs.tpl'));
}
return parent::manage($args, $request);
}
/**
* Hook against Templates::Catalog::Book::BookInfo::Sharing, for including the
* addThis code on submission display.
* @param $hookName string
* @param $params array
*/
function callbackSharingDisplay($hookName, $params) {
$templateMgr = $params[1];
$output =& $params[2];
$request = $this->getRequest();
$context = $request->getContext();
$templateMgr->assign('addThisProfileId', $context->getData('addThisProfileId'));
$templateMgr->assign('addThisUsername', $context->getData('addThisUsername'));
$templateMgr->assign('addThisPassword', $context->getData('addThisPassword'));
$templateMgr->assign('addThisDisplayStyle', $context->getData('addThisDisplayStyle'));
$output .= $templateMgr->fetch($this->getTemplateResource('addThis.tpl'));
return false;
}
/**
* Determines if statistics settings have been enabled for this plugin.
* @param $context Context
* @return boolean
*/
function statsConfigured($context) {
$addThisUsername = $context->getData('addThisUsername');
$addThisPassword = $context->getData('addThisPassword');
$addThisProfileId = $context->getData('addThisProfileId');
return (isset($addThisUsername) && isset($addThisPassword) && isset($addThisProfileId));
}
}