-
Notifications
You must be signed in to change notification settings - Fork 1
/
VolumesFormPlugin.inc.php
352 lines (304 loc) · 9.97 KB
/
VolumesFormPlugin.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
<?php
/**
* @file plugins/generic/volumesForm/VolumesFormPlugin.inc.php
*
* @brief Add Volumes to the submission metadata.
*
*/
import('lib.pkp.classes.plugins.GenericPlugin');
use \PKP\components\forms\FieldSelect;
class VolumesFormPlugin extends GenericPlugin
{
/**
* @copydoc Plugin::getName()
*/
function getName()
{
return 'VolumesFormPlugin';
}
/**
* @copydoc Plugin::getDisplayName()
*/
function getDisplayName()
{
return __('plugins.generic.volumesForm.title');
}
/**
* @copydoc Plugin::getDescription()
*/
function getDescription()
{
return __('plugins.generic.volumesForm.description');
}
/**
* @copydoc Plugin::register()
*/
function register($category, $path, $mainContextId = null)
{
$success = parent::register($category, $path, $mainContextId);
if ($success && $this->getEnabled($mainContextId)) {
// Get custom DAO.
import('plugins.generic.volumesForm.classes.VolumeDAO');
$volumeDao = new VolumeDAO();
DAORegistry::registerDAO('VolumeDAO', $volumeDao);
// Show volume grid in publication and submission workflow.
HookRegistry::register('TemplateResource::getFilename', array($this, '_overridePluginTemplates'));
// Load grid handler.
HookRegistry::register('LoadComponentHandler', array($this, 'setupGridHandler'));
// Handler for custom volume page in frontend.
HookRegistry::register('LoadHandler', array($this, 'setupVolumePageHandler'));
// Load JS for grid handler.
HookRegistry::register('TemplateManager::display', array($this, 'addGridhandlerJs'));
// Extend the publication schema to store data.
HookRegistry::register('Schema::get::publication', array($this, 'addToSchema'));
// We need the volume field in three forms: normal submission, metadataform, quicksubmit form.
HookRegistry::register('submissionsubmitstep1form::display', array($this, 'addToStep1'));
HookRegistry::register('quicksubmitform::display', array($this, 'addToStep1'));
HookRegistry::register('quicksubmitform::AdditionalItems', array($this, 'addToQuicksubmit'));
HookRegistry::register('Form::config::before', array($this, 'addToCatalogEntry'));
// Init the new field.
HookRegistry::register('submissionsubmitstep1form::initdata', array($this, 'metadataInitData'));
HookRegistry::register('quicksubmitform::initdata', array($this, 'metadataInitData'));
// Hook for readUserVars: consider the new field entries.
HookRegistry::register('submissionsubmitstep1form::readuservars', array($this, 'metadataReadUserVars'));
HookRegistry::register('quicksubmitform::readuservars', array($this, 'metadataReadUserVars'));
// Hook for execute: consider the new fields in the publication settings.
HookRegistry::register('Publication::add', array($this, 'metadataExecute'));
HookRegistry::register('quicksubmitform::execute', array($this, 'metadataExecute'));
// Load stylesheet for volume pages.
$request = Application::get()->getRequest();
$url = $request->getBaseUrl() . '/' . $this->getPluginPath() . '/styles/less/volumes.less';
$templateMgr = TemplateManager::getManager($request);
$templateMgr->addStyleSheet('volumeStyles', $url, [
'contexts' => 'frontend',
'priority' => STYLE_SEQUENCE_CORE,
]);
}
return $success;
}
/**
* Permit requests to the volume 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.volumesForm.controllers.grid.VolumeGridHandler') {
import($component);
VolumeGridHandler::setPlugin($this);
return true;
}
return false;
}
/**
* Handle frontend view in catalog.
* @param $hookName string The name of the hook being invoked
* @param $args array The parameters to the invoked hook
*/
public function setupVolumePageHandler($hookName, $params) {
$page = $params[0];
$op = $params[1];
if ($page === 'catalog' && $op == 'volume') {
define('HANDLER_CLASS', 'VolumePageHandler');
$this->import('VolumePageHandler');
VolumePageHandler::setPlugin($this);
return true;
}
return false;
}
/**
* Extend the context entity's schema with an the new field values.
* Save values in publication_settings table.
* @param $hookName string
* @param $args array
*/
public function addToSchema($hookName, $args)
{
// Fetch schema.
$schema = $args[0];
// Store in publication_settings table.
$schema->properties->volumeId = (object) [
'type' => 'string',
'apiSummary' => true,
'multilingual' => false,
'validation' => ['nullable']
];
return false;
}
function addToStep1($hookName, $params)
{
// Fetch template manager.
$request = PKPApplication::get()->getRequest();
$templateMgr = TemplateManager::getManager($request);
$contextId = $request->getContext()->getId();
// Get volumes for this context and assign options.
$volumeDao = DAORegistry::getDAO('VolumeDAO');
$volumeOptions = [];
$volumes = $volumeDao->getByContextId($contextId);
$volumeTitlesArray = $volumes->toAssociativeArray();
foreach ($volumeTitlesArray as $volume) {
$volumeOptions[$volume->getId()] = $volume->getLocalizedTitle();
}
$volumeOptions = ['' => __('submission.submit.selectVolume')] + $volumeOptions;
$templateMgr->assign('volumeOptions', $volumeOptions);
}
/**
* Add volume field to quicksubmit form.
* @param $hookName string
* @param $params array
*/
public function addToQuicksubmit($hookName, $params)
{
// Get smarty and template.
$smarty = $params[1];
$template = $params[2];
// Add volume field to quicksubmit form.
$template .= $smarty->display($this->getTemplateResource('/submission/form/volumeField.tpl'));
return false;
}
/**
* Extend catalog entry form in the publication settings.
* with a volume field.
*
* @param $hookName string
* @param $form FormComponent
*/
public function addToCatalogEntry($hookName, $form)
{
// Only modify the catalog entry form.
if (!defined('FORM_CATALOG_ENTRY') || $form->id !== FORM_CATALOG_ENTRY) {
return;
}
// Don't do anything at the site-wide level, only dependent context.
$context = Application::get()->getRequest()->getContext();
if (!$context) {
return;
}
// Get current publication.
$path = parse_url($form->action)['path'];
if (!$path) return;
$args = explode('/', $path);
$publicationId = 0;
if ($key = array_search('publications', $args)) {
if (array_key_exists($key + 1, $args)) {
$publicationId = intval($args[$key + 1]);
}
}
if (!$publicationId) return;
$publication = Services::get('publication')->get($publicationId);
if (!$publication) return;
// Volume options.
$request = PKPApplication::get()->getRequest();
$contextId = $request->getContext()->getId();
$volumeOptions = [['value' => '', 'label' => '']];
$result = DAORegistry::getDAO('VolumeDAO')->getByContextId($contextId);
while (!$result->eof()) {
$volume = $result->next();
$volumeOptions[] = [
'value' => (int) $volume->getId(),
'label' => $volume->getLocalizedTitle(),
];
}
// Add a field to the form.
$form->addField(new FieldSelect('volumeId', [
'label' => __('volume.volume'),
'value' => $publication->getData('volumeId'),
'options' => $volumeOptions,
]), [FIELD_POSITION_AFTER, 'seriesPosition']);
return false;
}
/**
* Initialize data in the metadata forms.
* @param $hookName string
* @param $args array
*/
function metadataInitData($hookName, $args)
{
// Get form.
$form = $args[0];
// Get publication.
$publicationDao = DAORegistry::getDAO('PublicationDAO');
$publicationId = $form->submission->getData('currentPublicationId');
$publication = $publicationDao->getById($publicationId);
// Initialize Data.
$form->setData('volumeId', $publication->getData('volumeId'));
}
/**
* Get user-entered values.
* @param $hookName string
* @param $args array
*/
public function metadataReadUserVars($hookName, $args)
{
// Get user variables.
$userVars = &$args[1];
$userVars[] = 'volumeId';
}
/**
* Save additional fields in the forms upon execution.
* @param $hookName string
* @param $args array
*/
public function metadataExecute($hookName, $args)
{
// Get Daos and context.
$publicationDao = DAORegistry::getDAO('PublicationDAO');
$context = Application::get()->getRequest()->getContext();
// Get current publication depending on form.
if (get_class($args[0]) === 'QuickSubmitForm') {
$submissionsIterator = Services::get('submission')->getMany([
'contextId' => $context->getId(),
]);
foreach ($submissionsIterator as $submission) {
$publication = $submission->getCurrentPublication();
}
// Get volume ID from form.
$volumeId = $args[0]->getData('volumeId');
} else {
// Fetch publication and get volume ID (by reference).
$publication = &$args[0];
$publicationId = $publication->getData('id');
$publication = $publicationDao->getById($publicationId);
$request = &$args[1];
$volumeId = $request->getUserVars()['volumeId'];
}
// Set data.
if ($publication && $volumeId) {
$publication->setData('volumeId', $volumeId);
// Update pub object.
$publicationDao->updateObject($publication);
}
return false;
}
/**
* Add custom gridhandlerJS for backend
*/
function addGridhandlerJs($hookName, $params)
{
$templateMgr = $params[0];
$request = $this->getRequest();
$gridHandlerJs = $this->getJavaScriptURL($request, false) . DIRECTORY_SEPARATOR . 'VolumeGridHandler.js';
$templateMgr->addJavaScript(
'VolumeGridHandlerJs',
$gridHandlerJs,
array('contexts' => 'backend')
);
return false;
}
/**
* @copydoc Plugin::getInstallMigration()
*/
function getInstallMigration()
{
$this->import('VolumeSchemaMigration');
return new VolumeSchemaMigration();
}
/**
* Get the JavaScript URL for this plugin.
*/
function getJavaScriptURL()
{
return Application::get()->getRequest()->getBaseUrl() . DIRECTORY_SEPARATOR . $this->getPluginPath() . DIRECTORY_SEPARATOR . 'js';
}
}