forked from tsv-fi/dates
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatesPlugin.inc.php
More file actions
209 lines (178 loc) · 4.84 KB
/
DatesPlugin.inc.php
File metadata and controls
209 lines (178 loc) · 4.84 KB
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
<?php
/**
* @file plugins/generic/dates/DatesPlugin.inc.php
*
* Copyright (c) 2014-2026 Simon Fraser University
* Copyright (c) 2003-2026 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* Dates Plugin for OJS 3.5
*
* Adds publication and editorial workflow dates
* to the public article page.
*/
use APP\core\Application;
use APP\decision\Decision;
use APP\facades\Repo;
use PKP\plugins\GenericPlugin;
use PKP\plugins\Hook;
class DatesPlugin extends GenericPlugin
{
/**
* Register the plugin
*/
public function register($category, $path, $mainContextId = null)
{
$success = parent::register($category, $path, $mainContextId);
if ($success && $this->getEnabled($mainContextId)) {
Hook::add(
'Templates::Article::Details',
[$this, 'addDates']
);
}
return $success;
}
/**
* Plugin display name
*/
public function getDisplayName()
{
return __('plugins.generic.dates.displayName');
}
/**
* Plugin description
*/
public function getDescription()
{
return __('plugins.generic.dates.description');
}
/**
* Safe PHP 8.1+ date formatting
*/
protected function formatDate($dateString, $locale = 'en_US')
{
if (!$dateString) {
return null;
}
$timestamp = strtotime($dateString);
if (!$timestamp) {
return null;
}
$formatter = new \IntlDateFormatter(
$locale,
\IntlDateFormatter::MEDIUM,
\IntlDateFormatter::NONE
);
return $formatter->format($timestamp);
}
/**
* Add article dates to template output
*/
public function addDates($hookName, $args)
{
error_log('DATES PLUGIN HOOK FIRED');
/**
* VERY IMPORTANT:
* For frontend template hooks:
*
* $args[1] = TemplateManager
* $args[2] = output (by reference)
*/
$templateMgr = $args[1];
$output =& $args[2];
$article = $templateMgr->getTemplateVars('article');
$publication = $templateMgr->getTemplateVars('publication');
if (!$article || !$publication) {
error_log('DATES PLUGIN: article or publication missing');
return false;
}
$request = Application::get()->getRequest();
if (!$request) {
return false;
}
$context = $request->getContext();
if (!$context) {
return false;
}
$dates = [];
/**
* Better locale handling for OJS
*/
$locale = $context->getPrimaryLocale();
if (!$locale) {
$locale = 'en_US';
}
/**
* Submitted date
*/
$dateSubmitted = $article->getData('dateSubmitted');
if ($dateSubmitted) {
$dates['submitted'] = $this->formatDate(
$dateSubmitted,
$locale
);
}
/**
* Published date
* IMPORTANT:
* This comes from publication, not article
*/
$datePublished = $publication->getData('datePublished');
if ($datePublished) {
$dates['published'] = $this->formatDate(
$datePublished,
$locale
);
}
/**
* Accepted date via editorial decisions
*/
try {
$decisions = Repo::decision()
->getCollector()
->filterBySubmissionIds([$article->getId()])
->getMany();
foreach ($decisions as $decision) {
if (
(int) $decision->getData('decision')
=== (int) Decision::ACCEPT
) {
$dates['accepted'] = $this->formatDate(
$decision->getData('dateDecided'),
$locale
);
break;
}
}
} catch (\Throwable $e) {
/**
* Never break article rendering because of plugin issues
*/
error_log(
'DatesPlugin decision lookup failed: '
. $e->getMessage()
);
}
/**
* Debug log
*/
error_log(
'DATES PLUGIN DATA: ' . print_r($dates, true)
);
/**
* Assign to Smarty
*/
$templateMgr->assign(
'datesPluginDates',
$dates
);
/**
* THIS is the crucial part:
* append rendered template output
*/
$output .= $templateMgr->fetch(
$this->getTemplateResource('dates.tpl')
);
return false;
}
}