forked from ojsde/vgWort
-
Notifications
You must be signed in to change notification settings - Fork 1
/
VGWortPlugin.inc.php
437 lines (384 loc) · 13.6 KB
/
VGWortPlugin.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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
<?php
/**
* @file VGWortPlugin.inc.php
*
* Author: Božana Bokan, Center for Digital Systems (CeDiS), Freie Universität Berlin
* Last update: September 25, 2015
* Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
*
* @package plugins.generic.vgWort
* @class VGWortPlugin
*
* @brief VG Wort plugin class
*/
import('lib.pkp.classes.plugins.GenericPlugin');
class VGWortPlugin extends GenericPlugin {
/**
* Called as a plugin is registered to the registry
* @param $category String Name of category plugin was registered to
* @return boolean True iff plugin initialized successfully; if false,
* the plugin will not be registered.
*/
function register($category, $path) {
$success = parent::register($category, $path);
if ($success && $this->getEnabled()) {
$this->addLocaleData();
$this->addHelpData();
$this->import('classes.PixelTag');
$this->import('classes.PixelTagDAO');
$pixelTagDao = new PixelTagDAO($this->getName());
$returner = DAORegistry::registerDAO('PixelTagDAO', $pixelTagDao);
// pixel tags operations can be done just by editors specified in the plug-in settings
if ($this->authorize()) {
// Editor link to VG Wort pages
HookRegistry::register('Templates::Editor::Index::AdditionalItems', array($this, 'displayEditorHomeLinks'));
}
// Handler for editor VG Wort pages
HookRegistry::register('LoadHandler', array($this, 'setupEditorHandler'));
// Insert new field into the author metadata submission form (submission step 3) and metadata form
HookRegistry::register('Templates::Author::Submit::Authors', array($this, 'metadataFieldEdit'));
HookRegistry::register('Templates::Submission::MetadataEdit::Authors', array($this, 'metadataFieldEdit'));
// Hook for initData in two forms -- init the new field
HookRegistry::register('metadataform::initdata', array($this, 'metadataInitData'));
HookRegistry::register('authorsubmitstep3form::initdata', array($this, 'metadataInitData'));
// Hook for save in two forms -- execute
HookRegistry::register('Author::Form::Submit::AuthorSubmitStep3Form::Execute', array($this, 'metadataExecute'));
HookRegistry::register('Submission::Form::MetadataForm::Execute', array($this, 'metadataExecute'));
// Hook for save in two forms -- add validation for the new field
HookRegistry::register('authorsubmitstep3form::Constructor', array($this, 'addCheck'));
HookRegistry::register('metadataform::Constructor', array($this, 'addCheck'));
// Add element for AuthorDAO for storage
HookRegistry::register('authordao::getAdditionalFieldNames', array($this, 'authorSubmitGetFieldNames'));
// Hook for article galley view -- add the pixel tag
HookRegistry::register ('TemplateManager::display', array($this, 'handleTemplateDisplay'));
}
return $success;
}
function getDisplayName() {
return __('plugins.generic.vgWort.displayName');
}
function getDescription() {
if ($this->requirementsFulfilled()) return __('plugins.generic.vgWort.description');
return __('plugins.generic.vgWort.descriptionDisabled');
}
/**
* Check whether or not the requirements for this plug-in are fullfilled
* @return boolean
*/
function requirementsFulfilled() {
$isPHPVersion = checkPhpVersion('5.0.1');
$isSoapExtension = in_array('soap', get_loaded_extensions());
$isOpenSSL = in_array('openssl', get_loaded_extensions());
return $isPHPVersion && $isSoapExtension && $isOpenSSL;
}
/**
* Get the path and filename of the ADODB schema for this plugin.
*/
function getInstallSchemaFile() {
return $this->getPluginPath() . '/xml/schema.xml';
}
/**
* Get the path and filename of the email keys for this plugin.
*/
function getInstallEmailTemplatesFile() {
return $this->getPluginPath() . '/xml/emailTemplates.xml';
}
/**
* Get the path and filename of the help mapping file.
*/
function getHelpMappingFilename() {
return $this->getPluginPath() . '/xml/help.xml';
}
/**
* Get the path and filename of the email locale data for this plugin.
*/
function getInstallEmailTemplateDataFile() {
return $this->getPluginPath() . '/locale/{$installedLocale}/emailTemplates.xml';
}
/**
* Get the template path for this plugin.
*/
function getTemplatePath() {
return parent::getTemplatePath() . 'templates/';
}
/**
* Get the handler path for this plugin.
*/
function getHandlerPath() {
return $this->getPluginPath() . '/pages/';
}
/**
* Get the path and stylesheet for this plugin.
*
*/
function getStyleSheet() {
return $this->getPluginPath() . '/styles/vgWort.css';
}
/**
* Display VG Wort management link in editor home.
*/
function displayEditorHomeLinks($hookName, $params) {
$smarty =& $params[1];
$output =& $params[2];
$templateMgr = TemplateManager::getManager();
$output .= '<h3>' . __('plugins.generic.vgWort.editor.vgWort') . '</h3><ul class="plain"><li>» <a href="' . $templateMgr->smartyUrl(array('op'=>'pixelTags'), $smarty) . '">' . __('plugins.generic.vgWort.editor.pixelTags') . '</a></li></ul>';
return false;
}
/**
* Enable editor pixel tags management.
*/
function setupEditorHandler($hookName, $params) {
$page =& $params[0];
if ($page == 'editor') {
$op =& $params[1];
if ($op) {
$editorPages = array(
'pixelTags',
'deletePixelTag',
'assignPixelTag',
'removePixelTag',
'reinsertPixelTag',
'changeTextType',
'pixelStatistics',
'createPixelTag',
'savePixelTag'
);
if (in_array($op, $editorPages)) {
define('HANDLER_CLASS', 'VGWortEditorHandler');
define('VGWORT_PLUGIN_NAME', $this->getName());
AppLocale::requireComponents(array(LOCALE_COMPONENT_APPLICATION_COMMON, LOCALE_COMPONENT_PKP_USER, LOCALE_COMPONENT_OJS_EDITOR));
$handlerFile =& $params[2];
$handlerFile = $this->getHandlerPath() . 'VGWortEditorHandler.inc.php';
}
}
}
}
/*
* Metadata
*/
/**
* Insert VG Wort field cardNo into author submission step 3 and metadata edit form
*/
function metadataFieldEdit($hookName, $params) {
$smarty =& $params[1];
$output =& $params[2];
$output .= $smarty->fetch($this->getTemplatePath() . 'cardNoEdit.tpl');
return false;
}
/**
* Add VG Wort field cardNo to the metadata view
*/
function metadataFieldView($hookName, $params) {
$smarty =& $params[1];
$output =& $params[2];
$output .= $smarty->fetch($this->getTemplatePath() . 'cardNoView.tpl');
return false;
}
/**
* Init VG Wort field cardNo
*/
function metadataInitData($hookName, $params) {
$form =& $params[0];
$article =& $form->article;
$formAuthors = $form->getData('authors');
$articleAuthors = $article->getAuthors();
for ($i=0; $i<count($articleAuthors); $i++) {
$formAuthors[$i]['cardNo'] = $articleAuthors[$i]->getData('cardNo');
}
$form->setData('authors', $formAuthors);
return false;
}
/**
* Set authors' cardNo
*/
function metadataExecute($hookName, $params) {
$author =& $params[0];
$formAuthor =& $params[1];
$author->setData('cardNo', $formAuthor['cardNo']);
return false;
}
/**
* Add the validation check for the cardNo field (2-7 numbers and if one exists then all should exist)
*/
function addCheck($hookName, $params) {
$form =& $params[0];
if (get_class($form) == 'AuthorSubmitStep3Form' || get_class($form) == 'MetadataForm' ) {
$form->addCheck(new FormValidatorArrayCustom($form, 'authors', 'required', 'plugins.generic.vgWort.cardNoValid', create_function('$cardNo, $regExp', 'return empty($cardNo) ? true : String::regexp_match($regExp, $cardNo);'), array('/^\d{2,7}$/'), false, array('cardNo')));
}
return false;
}
/**
* Add cardNo element to the article author
*/
function authorSubmitGetFieldNames($hookName, $params) {
$fields =& $params[1];
$fields[] = 'cardNo';
return false;
}
/**
* Handle article and submission summary view template display.
*/
function handleTemplateDisplay($hookName, $params) {
$smarty =& $params[0];
$template =& $params[1];
switch ($template) {
case 'article/article.tpl':
case 'rt/printerFriendly.tpl':
$smarty->register_outputfilter(array($this, 'insertPixelTag'));
break;
case 'sectionEditor/submission.tpl':
if ($this->authorize()) {
HookRegistry::register ('TemplateManager::include', array($this, 'assignPixelTag'));
}
break;
}
return false;
}
/**
* Enable editors to assign a VG Wort pixel tag to an article in the submission summary view.
*/
function assignPixelTag($hookName, $args) {
$smarty =& $args[0];
$params =& $args[1];
if (!isset($params['smarty_include_tpl_file'])) return false;
switch ($params['smarty_include_tpl_file']) {
case 'submission/metadata/metadata.tpl':
$journal = $smarty->get_template_vars('currentJournal');
$submission = $smarty->get_template_vars('submission');
$pixelTagDao = DAORegistry::getDAO('PixelTagDAO');
$pixelTag = $pixelTagDao->getPixelTagByArticleId($journal->getId(), $submission->getId());
$vgWortTextType = !isset($pixelTag) ? 0 : $pixelTag->getTextType();
$smarty->assign('pixelTag', $pixelTag);
$smarty->assign('vgWortTextType', $vgWortTextType);
$smarty->assign('typeOptions', PixelTag::getTextTypeOptions());
$smarty->fetch($this->getTemplatePath() . 'assignPixelTag.tpl', null, null, true);
break;
}
return false;
}
/**
* Insert the VG Wort pixel tag in the article page.
*/
function insertPixelTag($output, &$smarty) {
$smarty->unregister_outputfilter('insertPixelTag');
$journal = $smarty->get_template_vars('currentJournal');
$articleId = $smarty->get_template_vars('articleId');
$galley = $smarty->get_template_vars('galley');
if (isset($galley)) {
if($galley->isHtmlGalley() || $galley->isPdfGalley()) {
$publishedArticleDao = DAORegistry::getDAO('PublishedArticleDAO');
$publishedArticle = $publishedArticleDao->getPublishedArticleByBestArticleId($journal->getId(), $articleId);
// the article and the issue have to be published
if (isset($publishedArticle)) {
$issueDao = DAORegistry::getDAO('IssueDAO');
$issue = $issueDao->getIssueById($publishedArticle->getIssueId(), $journal->getId());
if ($issue->getPublished()) {
// get the assigned pixel tag
$pixelTagDao = DAORegistry::getDAO('PixelTagDAO');
$pixelTag = $pixelTagDao->getPixelTagByArticleId($journal->getId(), $publishedArticle->getId());
if (isset($pixelTag) && !$pixelTag->getDateRemoved()) {
// insert the pixel tag in the HTML version, just after the element <div id="content">
$src = 'http://' . $pixelTag->getDomain() . '/na/' . $pixelTag->getPublicCode();
$pixelTagImg = '<img src="' .$src .'" width="1" height="1" alt="" />';
$output = str_replace ('<div id="content">', '<div id="content">'.$pixelTagImg, $output);
// consider the pixel tag in the PDF download links, i.e. change the PDF download links
if ($galley->isPdfGalley()) {
$pdfUrl = Request::url(null, 'article', 'download', array($publishedArticle->getBestArticleId($journal), $galley->getBestGalleyId($journal)));
$newPdfLink = 'http://' . $pixelTag->getDomain() . '/na/' . $pixelTag->getPublicCode() . '?l=' . $pdfUrl;
$output = str_replace ('href="'.$pdfUrl, 'href="'.$newPdfLink, $output);
}
}
}
}
}
}
return $output;
}
/**
* Set the breadcrumbs, given the plugin's tree of items to append.
* @param $subclass boolean
*/
function setBreadcrumbs($isSubclass = false) {
$templateMgr = TemplateManager::getManager();
$pageCrumbs = array(
array(
Request::url(null, 'user'),
'navigation.user'
),
array(
Request::url(null, 'manager'),
'user.role.manager'
)
);
if ($isSubclass) $pageCrumbs[] = array(
Request::url(null, 'manager', 'plugins'),
'manager.plugins'
);
$templateMgr->assign('pageHierarchy', $pageCrumbs);
}
/**
* Display verbs for the management interface.
*/
function getManagementVerbs() {
$verbs = array();
if ($this->requirementsFulfilled()) {
if ($this->getEnabled()) {
$verbs[] = array('settings', __('plugins.generic.vgWort.manager.settings'));
}
return parent::getManagementVerbs($verbs);
}
return $verbs;
}
/*
* Execute a management verb on this plugin
* @param $verb string
* @param $args array
* @param $message string Location for the plugin to put a result msg
* @return boolean
*/
function manage($verb, $args, &$message, &$messageParams) {
if (!parent::manage($verb, $args, $message, $messageParams)) return false;
switch ($verb) {
case 'settings':
$templateMgr = TemplateManager::getManager();
$templateMgr->register_function('plugin_url', array($this, 'smartyPluginUrl'));
$journal = Request::getJournal();
$this->import('classes.form.VGWortSettingsForm');
$form = new VGWortSettingsForm($this, $journal->getId());
if (Request::getUserVar('save')) {
$form->readInputData();
if ($form->validate()) {
$form->execute();
Request::redirect(null, 'manager', 'plugin');
return false;
} else {
$this->setBreadCrumbs(true);
$form->display();
}
} else {
$this->setBreadCrumbs(true);
$form->initData();
$form->display();
}
return true;
default:
// Unknown management verb
assert(false);
return false;
}
}
/**
* Ensure that the user is an editor specified in the plugin settings.
* @return boolean
*/
private function authorize() {
$journal = Request::getJournal();
$editors = $this->getSetting($journal->getId(), 'vgWortEditors');
if (empty($editors)) return false;
$sessionManager = SessionManager::getManager();
$session = $sessionManager->getUserSession();
if (!in_array($session->getUserId(), $editors)) return false;
return true;
}
}
?>