forked from TIBHannover/ojsGeo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
OptimetaGeoPlugin.inc.php
executable file
·656 lines (535 loc) · 26.6 KB
/
OptimetaGeoPlugin.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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
<?php
/**
* @file OptimetaGeoPlugin.inc.php
*
* Copyright (c) 2022 OPTIMETA project
* Copyright (c) 2022 Daniel Nüst
* Distributed under the GNU GPL v3. For full terms see the file dLICENSE.
*
* @class OptimetaGeoPlugin
* @brief Plugin class for the OPTIMETA project's geo plugin.
*/
const MAP_URL_PATH = 'map';
// following names are also used in JavaScript files to identify fields
const OPTIMETA_GEO_DB_FIELD_TIME_PERIODS = 'optimetaGeo::timePeriods';
const OPTIMETA_GEO_DB_FIELD_SPATIAL = 'optimetaGeo::spatialProperties';
const OPTIMETA_GEO_DB_FIELD_ADMINUNIT = 'optimetaGeo::administrativeUnit';
const OPTIMETA_GEO_FORM_NAME = 'OptimetaGeo_PublicationForm';
const OPTIMETA_GEO_PLUGIN_PATH = __DIR__;
require_once (OPTIMETA_GEO_PLUGIN_PATH . '/vendor/autoload.php');
import('lib.pkp.classes.plugins.GenericPlugin');
import('plugins.generic.optimetaGeo.classes.Components.Forms.PublicationForm');
import('plugins.generic.optimetaGeo.classes.Components.Forms.SettingsForm');
use Optimeta\Geo\Components\Forms\PublicationForm;
use Optimeta\Geo\Components\Forms\SettingsForm;
class OptimetaGeoPlugin extends GenericPlugin
{
protected $ojsVersion = '3.3.0.0';
protected $versionSpecificNameState = 'state';
protected $templateParameters = [
'pluginStylesheetURL' => '',
'pluginJavaScriptURL' => '',
];
public $dbFields = [
'spatial' => OPTIMETA_GEO_DB_FIELD_SPATIAL,
'temporal' => OPTIMETA_GEO_DB_FIELD_TIME_PERIODS,
'admin' => OPTIMETA_GEO_DB_FIELD_ADMINUNIT,
];
public function register($category, $path, $mainContextId = NULL)
{
// Register the plugin even when it is not enabled
$success = parent::register($category, $path, $mainContextId);
// Current Request / Context
$request = $this->getRequest();
// Fill generic template parameters
$this->templateParameters['pluginStylesheetURL'] = $request->getBaseUrl() . '/' . $this->getPluginPath() . '/css';
$this->templateParameters['pluginJavaScriptURL'] = $request->getBaseUrl() . '/' . $this->getPluginPath() . '/js';
// important to check if plugin is enabled before registering the hook, cause otherwise plugin will always run no matter enabled or disabled!
if ($success && $this->getEnabled()) {
// custom page handler, see https://docs.pkp.sfu.ca/dev/plugin-guide/en/examples-custom-page
HookRegistry::register('LoadHandler', array($this, 'setPageHandler'));
// Hooks for changing the frontent Submit an Article 3. Enter Metadata
HookRegistry::register('Templates::Submission::SubmissionMetadataForm::AdditionalMetadata', array($this, 'extendSubmissionMetadataFormTemplate'));
// Hooks for changing the article page
HookRegistry::register('Templates::Article::Main', array(&$this, 'extendArticleMainTemplate'));
HookRegistry::register('Templates::Article::Details', array(&$this, 'extendArticleDetailsTemplate'));
HookRegistry::register('ArticleHandler::view', array(&$this, 'extendArticleView')); //
// Hooks for changing the issue page
HookRegistry::register('Templates::Issue::TOC::Main', array(&$this, 'extendIssueTocTemplate'));
HookRegistry::register('Templates::Issue::Issue::Article', array(&$this, 'extendIssueTocArticleTemplate'));
// Hook for adding a tab to the publication phase
HookRegistry::register('Template::Workflow::Publication', array($this, 'extendPublicationTab'));
// Hook for creating and setting a new field in the database
HookRegistry::register('Schema::get::publication', array($this, 'addToSchema'));
HookRegistry::register('Publication::edit', array($this, 'editPublication')); // Take care, hook is called twice, first during Submission Workflow and also before Schedule for Publication in the Review Workflow!!!
$request = Application::get()->getRequest();
$templateMgr = TemplateManager::getManager($request);
// jQuery is already loaded via ojs/lib/pkp/classes/template/PKPTemplateManager.inc.php
$urlLeafletCSS = $request->getBaseUrl() . '/' . $this->getPluginPath() . '/js/lib/leaflet/dist/leaflet.css';
$urlLeafletJS = $request->getBaseUrl() . '/' . $this->getPluginPath() . '/js/lib/leaflet/dist/leaflet.js';
$urlLeafletDrawCSS = $request->getBaseUrl() . '/' . $this->getPluginPath() . '/js/lib/leaflet-draw/dist/leaflet.draw.css';
$urlLeafletDrawJS = $request->getBaseUrl() . '/' . $this->getPluginPath() . '/js/lib/leaflet-draw/dist/leaflet.draw.js';
$urlMomentJS = $request->getBaseUrl() . '/' . $this->getPluginPath() . '/js/lib/moment/moment.js';
$urlDaterangepickerJS = $request->getBaseUrl() . '/' . $this->getPluginPath() . '/js/lib/daterangepicker/daterangepicker.js';
$urlDaterangepickerCSS = $request->getBaseUrl() . '/' . $this->getPluginPath() . '/js/lib/daterangepicker/daterangepicker.css';
$urlLeafletControlGeocodeJS = $request->getBaseUrl() . '/' . $this->getPluginPath() . '/js/lib/leaflet-control-geocoder/dist/Control.Geocoder.min.js';
$urlLeafletControlGeocodeCSS = $request->getBaseUrl() . '/' . $this->getPluginPath() . '/js/lib/leaflet-control-geocoder/dist/Control.Geocoder.css';
// loading the leaflet scripts, source: https://leafletjs.com/examples/quick-start/
$templateMgr->addStyleSheet('leafletCSS', $urlLeafletCSS, array('contexts' => array('frontend', 'backend')));
$templateMgr->addJavaScript('leafletJS', $urlLeafletJS, array('contexts' => array('frontend', 'backend')));
// loading the leaflet draw scripts, source: https://www.jsdelivr.com/package/npm/leaflet-draw?path=dist
$templateMgr->addStyleSheet("leafletDrawCSS", $urlLeafletDrawCSS, array('contexts' => array('frontend', 'backend')));
$templateMgr->addJavaScript("leafletDrawJS", $urlLeafletDrawJS, array('contexts' => array('frontend', 'backend')));
// loading the daterangepicker scripts, source: https://www.daterangepicker.com/#example2
$templateMgr->addJavaScript("momentJS", $urlMomentJS, array('contexts' => array('frontend', 'backend')));
$templateMgr->addJavaScript("daterangepickerJS", $urlDaterangepickerJS, array('contexts' => array('frontend', 'backend')));
$templateMgr->addStyleSheet("daterangepickerCSS", $urlDaterangepickerCSS, array('contexts' => array('frontend', 'backend')));
// loading leaflet control geocoder (search), source: https://github.com/perliedman/leaflet-control-geocoder
$templateMgr->addJavaScript("leafletControlGeocodeJS", $urlLeafletControlGeocodeJS, array('contexts' => array('frontend', 'backend')));
$templateMgr->addStyleSheet("leafletControlGeocodeCSS", $urlLeafletControlGeocodeCSS, array('contexts' => array('frontend', 'backend')));
// plugins JS scripts and CSS
$templateMgr->assign('optimetageo_submissionJS', $request->getBaseUrl() . '/' . $this->getPluginPath() . '/js/submission.js');
$templateMgr->assign('optimetageo_article_detailsJS', $request->getBaseUrl() . '/' . $this->getPluginPath() . '/js/article_details.js');
$templateMgr->assign('optimetageo_issueJS', $request->getBaseUrl() . '/' . $this->getPluginPath() . '/js/issue.js');
$templateMgr->assign('optimetageo_markerBaseUrl', $request->getBaseUrl() . '/' . $this->getPluginPath() . '/js/lib/leaflet-color-markers/img/');
$templateMgr->assign('optimetageo_mapUrlPath', MAP_URL_PATH);
$templateMgr->assign('optimetageo_metadataLicense', '<a href="https://creativecommons.org/publicdomain/zero/1.0/">CC-0</a>');
}
return $success;
}
/**
* @param hookName
* @param params
*/
public function setPageHandler($hookName, $params)
{
$page = $params[0];
if ($page === MAP_URL_PATH) {
$this->import('classes/handler/JournalMapHandler');
define('HANDLER_CLASS', 'JournalMapHandler');
return true;
}
return false;
}
/**
* Inject metadata into article HTML head
* @param $hookName string
* @param $args array
* @return boolean
*/
function extendArticleView($hookName, $args)
{
$request = $args[0];
$article = $args[2];
$publication = $article->getCurrentPublication();
$journal = $request->getContext();
$templateMgr = TemplateManager::getManager($request);
$templateMgr->addHeader('dublinCoreTemporal', '<link rel="schema.DC" href="http://purl.org/dc/elements/1.1/" />');
// https://www.dublincore.org/specifications/dublin-core/dcmi-terms/terms/spatial/
if ($spatial = $publication->getData(OPTIMETA_GEO_DB_FIELD_SPATIAL)) {
$templateMgr->addHeader('dublinCoreSpatialCoverage', '<meta name="DC.SpatialCoverage" scheme="GeoJSON" content="' . htmlspecialchars(strip_tags($spatial)) . '" />');
}
if ($administrativeUnit = $publication->getData(OPTIMETA_GEO_DB_FIELD_ADMINUNIT)) {
$administrativeUnitNames = array_map(function ($unit) {
return $unit->name;
}, json_decode($administrativeUnit));
$administrativeUnitNames = implode(', ', $administrativeUnitNames);
$lowestAdministrativeUnitName = null;
$lowestAdministrativeUnitBBox = null;
foreach (json_decode($administrativeUnit) as $unit) {
if ($unit->bbox != 'not available') {
$lowestAdministrativeUnitName = $unit->name;
$lowestAdministrativeUnitBBox = $unit->bbox;
}
}
if ($lowestAdministrativeUnitName) {
// https://dohmaindesigns.com/adding-geo-meta-tags-to-your-website/
$templateMgr->addHeader('geoPlacename', '<meta name="geo.placename" content="' . htmlspecialchars(strip_tags($lowestAdministrativeUnitName)) . '" />');
}
if ($lowestAdministrativeUnitName && $lowestAdministrativeUnitBBox) {
// DCMI Box Encoding Scheme - https://www.dublincore.org/specifications/dublin-core/dcmi-box/
$templateMgr->addHeader('dublincCoreBox', '<meta name="DC.box" content="name=' .
$lowestAdministrativeUnitName .
'; northlimit=' . $lowestAdministrativeUnitBBox->north .
'; southlimit=' . $lowestAdministrativeUnitBBox->south .
'; westlimit=' . $lowestAdministrativeUnitBBox->west .
'; eastlimit=' . $lowestAdministrativeUnitBBox->east .
'; projection=EPSG3857" />');
// ISO 19139 - https://boundingbox.klokantech.com/
$templateMgr->addHeader('isoGeographicBoundingBox', '<meta name="ISO 19139" content="' .
'<gmd:EX_GeographicBoundingBox>' .
'<gmd:westBoundLongitude><gco:Decimal>' . $lowestAdministrativeUnitBBox->west . '</gco:Decimal></gmd:westBoundLongitude>' .
'<gmd:eastBoundLongitude><gco:Decimal>' . $lowestAdministrativeUnitBBox->east . '</gco:Decimal></gmd:eastBoundLongitude>' .
'<gmd:southBoundLatitude><gco:Decimal>' . $lowestAdministrativeUnitBBox->south . '</gco:Decimal></gmd:southBoundLatitude>' .
'<gmd:northBoundLatitude><gco:Decimal>' . $lowestAdministrativeUnitBBox->north . '</gco:Decimal></gmd:northBoundLatitude></gmd:EX_GeographicBoundingBox>" />');
}
}
if ($timePeriods = $publication->getData(OPTIMETA_GEO_DB_FIELD_TIME_PERIODS)) {
// FIXME crazy use of explode makes more sense when we support multiple periods
$begin = explode('..', explode('{', $timePeriods)[1])[0];
$end = explode('}', explode('..', explode('{', $timePeriods)[1])[1])[0];
// / is the ISO8601 time interval separator, see https://en.wikipedia.org/wiki/ISO_8601
$templateMgr->addHeader('dublinCoreTemporal', '<meta name="DC.temporal" scheme="ISO8601" content="' .
$begin. '/' . $end .
'"/>');
$templateMgr->addHeader('dublinCorePeriodOfTime', '<meta name="DC.PeriodOfTime" scheme="ISO8601" content="' .
$begin. '/' . $end .
'"/>');
}
return false;
}
/**
* Function which extends the submissionMetadataFormFields template and adds template variables concerning temporal- and spatial properties
* and the administrative unit if there is already a storage in the database.
* @param hook Templates::Submission::SubmissionMetadataForm::AdditionalMetadata
*/
public function extendSubmissionMetadataFormTemplate($hookName, $params)
{
/*
This way templates are loaded.
Its important that the corresponding hook is activated.
If you want to override a template you need to create a .tpl-file which is in the plug-ins template path which the same
path it got in the regular ojs structure. E.g. if you want to override/add something to this template
'/ojs/lib/pkp/templates/submission/submissionMetadataFormTitleFields.tpl'
you have to store in in the plug-ins template path under this path 'submission/form/submissionMetadataFormFields.tpl'.
Further details can be found here: https://docs.pkp.sfu.ca/dev/plugin-guide/en/templates
Where are templates located: https://docs.pkp.sfu.ca/pkp-theming-guide/en/html-smarty
*/
$templateMgr = &$params[1];
$output = &$params[2];
// example: the arrow is used to access the attribute smarty of the variable smarty
// $templateMgr = $smarty->smarty;
$request = Application::get()->getRequest();
$context = $request->getContext();
/*
Check if the user has entered an username in the plugin settings for the GeoNames API (https://www.geonames.org/login).
The result is passed on accordingly to submission.js as template variable.
*/
$usernameGeonames = $this->getSetting($context->getId(), 'optimetaGeo_geonames_username');
$templateMgr->assign('usernameGeonames', $usernameGeonames);
$baseurlGeonames = $this->getSetting($context->getId(), 'optimetaGeo_geonames_baseurl');
$templateMgr->assign('baseurlGeonames', $baseurlGeonames);
/*
In case the user repeats the step "3. Enter Metadata" in the process 'Submit an Article' and comes back to this step to make changes again,
the already entered data is read from the database, added to the template and displayed for the user.
Data is loaded from the database, passed as template variable to the 'submissionMetadataFormFiels.tpl'
and requested from there in the 'submission.js' to display coordinates in a map, the date and coverage information if available.
*/
$publicationDao = DAORegistry::getDAO('PublicationDAO');
$submissionId = $request->getUserVar('submissionId');
$publication = $publicationDao->getById($submissionId);
$timePeriods = $publication->getData(OPTIMETA_GEO_DB_FIELD_TIME_PERIODS);
$spatialProperties = $publication->getData(OPTIMETA_GEO_DB_FIELD_SPATIAL);
$administrativeUnit = $publication->getData(OPTIMETA_GEO_DB_FIELD_ADMINUNIT);
// for the case that no data is available
if ($timePeriods === null) {
$timePeriods = 'no data';
}
if ($spatialProperties === null || $spatialProperties === '{"type":"FeatureCollection","features":[],"administrativeUnits":{},"temporalProperties":{"timePeriods":[],"provenance":{"description":"not available","id":"not available"}}}') {
$spatialProperties = 'no data';
}
if ($administrativeUnit === null || current($administrativeUnit) === '' || $administrativeUnit === '') {
$administrativeUnit = 'no data';
}
//assign data as variables to the template
$templateMgr->assign(OPTIMETA_GEO_DB_FIELD_TIME_PERIODS, $timePeriods);
$templateMgr->assign(OPTIMETA_GEO_DB_FIELD_SPATIAL, $spatialProperties);
$templateMgr->assign(OPTIMETA_GEO_DB_FIELD_ADMINUNIT, $administrativeUnit);
$templateMgr->assign($this->templateParameters);
// here the original template is extended by the additional template for entering geospatial metadata
$output .= $templateMgr->fetch($this->getTemplateResource('submission/form/submissionMetadataFormFields.tpl'));
return false;
}
/**
* Function which extends ArticleMain Template by geospatial properties if available.
* Data is loaded from the database, passed as template variable to the 'article_details.tpl'
* and requested from there in the 'article_details.js' to display coordinates in a map, the date and coverage information if available.
* @param hook Templates::Article::Main
*/
public function extendArticleMainTemplate($hookName, $params)
{
$templateMgr = &$params[1];
$output = &$params[2];
$publication = $templateMgr->getTemplateVars('publication');
//$journal = Application::get()->getRequest()->getJournal();
// get data from database
$temporalProperties = $publication->getData(OPTIMETA_GEO_DB_FIELD_TIME_PERIODS);
$spatialProperties = $publication->getData(OPTIMETA_GEO_DB_FIELD_SPATIAL);
$administrativeUnit = $publication->getData(OPTIMETA_GEO_DB_FIELD_ADMINUNIT);
//$publication->getLocalizedData('coverage', $journal->getPrimaryLocale());
// for the case that no data is available
if ($temporalProperties === null || $temporalProperties === '') {
$temporalProperties = 'no data';
}
if (($spatialProperties === null || $spatialProperties === '{"type":"FeatureCollection","features":[],"administrativeUnits":{},"temporalProperties":{"timePeriods":[],"provenance":"not available"}}')) {
$spatialProperties = 'no data';
}
if ($administrativeUnit === null || $administrativeUnit === '') {
$administrativeUnit = 'no data';
}
//assign data as variables to the template
$templateMgr->assign(OPTIMETA_GEO_DB_FIELD_TIME_PERIODS, $temporalProperties);
$templateMgr->assign(OPTIMETA_GEO_DB_FIELD_SPATIAL, $spatialProperties);
$templateMgr->assign(OPTIMETA_GEO_DB_FIELD_ADMINUNIT, $administrativeUnit);
$templateMgr->assign($this->templateParameters);
$output .= $templateMgr->fetch($this->getTemplateResource('frontend/objects/article_details.tpl'));
return false;
}
/**
* Function which extends the ArticleMain Template by a download button for the geospatial Metadata as geoJSON.
* @param hook Templates::Article::Details
*/
public function extendArticleDetailsTemplate($hookName, $params)
{
$templateMgr = &$params[1];
$output = &$params[2];
$templateMgr->assign($this->templateParameters);
$output .= $templateMgr->fetch($this->getTemplateResource('frontend/objects/article_details_download.tpl'));
return false;
}
/**
* Function which extends the issue TOC with a timeline and map view - needs the optimetaGeoTheme plugin!
* @param hook Templates::Issue::TOC::Main
*/
public function extendIssueTocTemplate($hookName, $params)
{
$templateMgr = &$params[1];
$output = &$params[2];
$templateMgr->assign($this->templateParameters);
$output .= $templateMgr->fetch($this->getTemplateResource('frontend/objects/issue_map.tpl'));
return false;
}
/**
* Function which extends each article in an issue TOC with hidden fields with geospatial data
* @param hook Templates::Issue::Issue::Article
*/
public function extendIssueTocArticleTemplate($hookName, $params)
{
$templateMgr = &$params[1];
$output = &$params[2];
$templateMgr->assign($this->templateParameters);
$publication = $templateMgr->getTemplateVars('publication');
if ($publication === null) {
// pragma theme
$articlePath = $templateMgr->getTemplateVars('articlePath');
if ($articlePath === null) {
return false;
} else {
$publicationDao = DAORegistry::getDAO('PublicationDAO');
$publication = $publicationDao->getById($articlePath);
}
}
$spatialProperties = $publication->getData(OPTIMETA_GEO_DB_FIELD_SPATIAL);
if (($spatialProperties === null || $spatialProperties === '{"type":"FeatureCollection","features":[],"administrativeUnits":{},"temporalProperties":{"timePeriods":[],"provenance":"not available"}}')) {
$spatialProperties = 'no data';
}
$templateMgr->assign(OPTIMETA_GEO_DB_FIELD_SPATIAL, $spatialProperties);
$templateMgr->assign('journal', Application::get()->getRequest()->getJournal()); // access primary locale
//$temporalProperties = $publication->getData(OPTIMETA_GEO_DB_FIELD_TIME_PERIODS);
//if ($temporalProperties === null || $temporalProperties === '') {
// $temporalProperties = 'no data';
//}
//$templateMgr->assign(OPTIMETA_GEO_DB_FIELD_TIME_PERIODS, $temporalProperties);
//$administrativeUnit = $publication->getLocalizedData('coverage', $journal->getPrimaryLocale());
//if ($administrativeUnit === null || $administrativeUnit === '') {
// $administrativeUnit = 'no data';
//}
//$templateMgr->assign(OPTIMETA_GEO_DB_FIELD_ADMINUNIT, $administrativeUnit);
$output .= $templateMgr->fetch($this->getTemplateResource('frontend/objects/issue_details.tpl'));
return false;
}
/**
* @param string $hookname
* @param array $args [string, TemplateManager]
* @brief Show tab under Publications
*/
public function extendPublicationTab(string $hookName, array $args): void
{
$templateMgr = &$args[1];
$request = $this->getRequest();
$context = $request->getContext();
$submission = $templateMgr->getTemplateVars('submission');
$submissionId = $submission->getId();
$latestPublication = $submission->getLatestPublication();
$dispatcher = $request->getDispatcher();
$apiBaseUrl = $dispatcher->url($request, ROUTE_API, $context->getData('urlPath'), '');
$usernameGeonames = $this->getSetting($context->getId(), 'optimetaGeo_geonames_username');
$templateMgr->assign('usernameGeonames', $usernameGeonames);
$baseurlGeonames = $this->getSetting($context->getId(), 'optimetaGeo_geonames_baseurl');
$templateMgr->assign('baseurlGeonames', $baseurlGeonames);
$form = new PublicationForm(
$apiBaseUrl . 'submissions/' . $submissionId . '/publications/' . $latestPublication->getId(),
$latestPublication,
__('plugins.generic.optimetaGeo.publication.success'));
$state = $templateMgr->getTemplateVars($this->versionSpecificNameState);
$state['components'][OPTIMETA_GEO_FORM_NAME] = $form->getConfig();
$templateMgr->assign($this->versionSpecificNameState, $state);
$templateMgr->assign('submissionId', $submissionId);
$templateMgr->assign($this->templateParameters);
$templateMgr->display($this->getTemplateResource("submission/form/publicationTab.tpl"));
}
/**
* Function which extends the schema of the publication_settings table in the database.
* There are two further rows in the table one for the spatial properties, and one for the timestamp.
* @param hook Schema::get::publication
*/
public function addToSchema($hookName, $params)
{
// possible types: integer, string, text
$schema = $params[0];
// save timestamp as text in a list of ISO8601 time intervals/time periods as described in RFC3339 appendix, https://datatracker.ietf.org/doc/html/rfc3339#appendix-A, see also https://en.wikipedia.org/wiki/ISO_8601#Time_intervals
$timePeriods = '{
"type": "string",
"multilingual": false,
"apiSummary": true,
"validation": [
"nullable"
]
}';
$schema->properties->{OPTIMETA_GEO_DB_FIELD_TIME_PERIODS} = json_decode($timePeriods);
$spatialProperties = '{
"type": "string",
"multilingual": false,
"apiSummary": true,
"validation": [
"nullable"
]
}';
$schema->properties->{OPTIMETA_GEO_DB_FIELD_SPATIAL} = json_decode($spatialProperties);
$administrativeUnits = '{
"type": "string",
"multilingual": false,
"apiSummary": true,
"validation": [
"nullable"
]
}';
$schema->properties->{OPTIMETA_GEO_DB_FIELD_ADMINUNIT} = json_decode($administrativeUnits);
return false;
}
/**
* Function which fills the new fields (created by the function addToSchema) in the schema.
* The data is collected using the 'submission.js', then passed as input to the 'submissionMetadataFormFields.tpl'
* and requested from it in this php script by a POST-method.
* @param hook Publication::edit
*/
function editPublication(string $hookname, array $params)
{
$newPublication = $params[0];
$params = $params[2];
$temporalProperties = $_POST[OPTIMETA_GEO_DB_FIELD_TIME_PERIODS];
$spatialProperties = $_POST[OPTIMETA_GEO_DB_FIELD_SPATIAL];
$administrativeUnit = $_POST[OPTIMETA_GEO_DB_FIELD_ADMINUNIT];
// null if there is no possibility to input data (metadata input before Schedule for Publication)
if ($spatialProperties !== null) {
$newPublication->setData(OPTIMETA_GEO_DB_FIELD_SPATIAL, $spatialProperties);
}
if ($temporalProperties !== null && $temporalProperties !== "") {
$newPublication->setData(OPTIMETA_GEO_DB_FIELD_TIME_PERIODS, $temporalProperties);
}
if ($administrativeUnit !== null) {
$newPublication->setData(OPTIMETA_GEO_DB_FIELD_ADMINUNIT, $administrativeUnit);
// turn admin units into string then save in Coverage field
$administrativeUnitNames = array_map(function ($unit) {
return $unit->name;
}, json_decode($administrativeUnit));
$administrativeUnitNames = implode(', ', $administrativeUnitNames);
$journal = Application::get()->getRequest()->getJournal();
$newPublication->setData('coverage', $administrativeUnitNames, $journal->getPrimaryLocale());
}
}
/**
* @copydoc Plugin::getActions() - https://docs.pkp.sfu.ca/dev/plugin-guide/en/settings
* Function needed for Plugin Settings.
*/
public function getActions($request, $actionArgs)
{
// Get the existing actions
$actions = parent::getActions($request, $actionArgs);
if (!$this->getEnabled()) {
return $actions;
}
// Create a LinkAction that will call the plugin's
// `manage` method with the `settings` verb.
$router = $request->getRouter();
import('lib.pkp.classes.linkAction.request.AjaxModal');
$linkAction = 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
);
// Add the LinkAction to the existing actions.
// Make it the first action to be consistent with
// other plugins.
array_unshift($actions, $linkAction);
return $actions;
}
/**
* @copydoc Plugin::manage() - https://docs.pkp.sfu.ca/dev/plugin-guide/en/settings#the-form-class
* Function needed for Plugin Settings.
*/
public function manage($args, $request)
{
switch ($request->getUserVar('verb')) {
case 'settings':
$form = new SettingsForm($this);
// Fetch the form the first time it loads, before
// the user has tried to save it
if (!$request->getUserVar('save')) {
$form->initData();
return new JSONMessage(true, $form->fetch($request));
}
// Validate and execute the form
$form->readInputData();
if ($form->validate()) {
$form->execute();
return new JSONMessage(true);
}
}
return parent::manage($args, $request);
}
/**
* Provide a name for this plugin (plugin gallery)
*
* The name will appear in the Plugin Gallery where editors can
* install, enable and disable plugins.
*/
public function getDisplayName()
{
return __('plugins.generic.optimetaGeo.name');
}
/**
* Provide a description for this plugin (plugin gallery)
*
* The description will appear in the Plugin Gallery where editors can
* install, enable and disable plugins.
*/
public function getDescription()
{
return __('plugins.generic.optimetaGeo.description');
}
/**
* Get the current context ID or the site-wide context ID (0) if no context
* can be found.
*/
function getCurrentContextId()
{
$context = Application::get()->getRequest()->getContext();
return is_null($context) ? 0 : $context->getId();
}
}