-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBackendUiExamplePlugin.php
More file actions
284 lines (242 loc) · 8.94 KB
/
BackendUiExamplePlugin.php
File metadata and controls
284 lines (242 loc) · 8.94 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
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
<?php
/**
* @file BackendUiExamplePlugin.inc.php
*
* Copyright (c) 2023 Simon Fraser University
* Copyright (c) 2023 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @brief Backend Ui Example Plugin
*/
namespace APP\plugins\generic\backendUiExample;
use APP\core\Application;
use PKP\plugins\GenericPlugin;
use APP\template\TemplateManager;
use PKP\plugins\Hook;
use PKP\components\forms\FormComponent;
use PKP\components\forms\FieldText;
use Illuminate\Http\Request as IlluminateRequest;
use Illuminate\Http\Response;
use Illuminate\Http\JsonResponse;
use PKP\core\PKPBaseController;
use PKP\handler\APIHandler;
use PKP\security\Role;
class BackendUiExamplePlugin extends GenericPlugin {
/**
* @copydoc Plugin::register()
*
* @param null|mixed $mainContextId
*/
public function register($category, $path, $mainContextId = null)
{
if (parent::register($category, $path, $mainContextId)) {
if ($this->getEnabled($mainContextId)) {
// To Attach smarty template to the settings template
Hook::add('Template::Settings::website', [$this, 'callbackShowWebsiteSettingsTabs']);
// To Attach data to the page, so it can be passed
Hook::add('TemplateManager::display', [$this, 'callbackTemplateManagerDisplay']);
// Registering build file for JS and CSS to be loaded
$request = Application::get()->getRequest();
$templateMgr = TemplateManager::getManager($request);
$this->addJavaScript($request, $templateMgr);
$templateMgr->addStyleSheet('backendUiExampleStyle',"{$request->getBaseUrl()}/{$this->getPluginPath()}/public/build/build.css", [
'contexts' => ['backend']
] );
// Add steps to submission wizard
Hook::add('TemplateManager::display', $this->addToSubmissionWizardSteps(...));
$this->addRoute();
}
return true;
}
return false;
}
public function addJavaScript($request, $templateMgr)
{
$templateMgr->addJavaScript(
'BackendUiExample',
"{$request->getBaseUrl()}/{$this->getPluginPath()}/public/build/build.iife.js",
[
'inline' => false,
'contexts' => ['backend'],
'priority' => TemplateManager::STYLE_SEQUENCE_LAST
]
);
}
/**
* Extend the website settings tabs to include example plugin
*
* @param string $hookName The name of the invoked hook
* @param array $args Hook parameters
*
* @return bool Hook handling status
*/
public function callbackShowWebsiteSettingsTabs($hookName, $args)
{
$templateMgr = $args[1];
$output = & $args[2];
$output .= $templateMgr->fetch($this->getTemplateResource('settingsTab.tpl'));
// Permit other plugins to continue interacting with this hook
return false;
}
/**
* Create a simple example form using FormComponent
*
* @return FormComponent
*/
public function getExampleForm()
{
$request = Application::get()->getRequest();
$context = $request->getContext();
$dispatcher = $request->getDispatcher();
$site = $request->getSite();
$locales = $site->getSupportedLocaleNames();
$locales = array_map(fn (string $locale, string $name) => ['key' => $locale, 'label' => $name], array_keys($locales), $locales);
// Create the form
$form = new FormComponent(
'examplePluginForm',
'POST',
$dispatcher->url(
$request,
Application::ROUTE_API,
$context->getData('urlPath'),
'examplePlugin/saveSettings'
),
$locales
);
// Add text field
$form->addField(new FieldText('exampleTitle', [
'label' => __('plugins.generic.backendUiExample.form.title'),
'isRequired' => true,
'groupId' => 'exampleSettings',
'value' => '',
]));
return $form;
}
/**
* Add state for your component when needed
*
* @param string $hookName The name of the invoked hook
* @param array $args Hook parameters
*
* @return bool Hook handling status
*/
public function callbackTemplateManagerDisplay($hookName, $args)
{
$templateMgr = $args[0];
$request = Application::get()->getRequest();
$dispatcher = $request->getDispatcher();
$context = $request->getContext();
if($request->getRequestedOp() == 'settings') {
// Get the form configuration
$form = $this->getExampleForm();
$templateMgr->setState([
'backendUiExampleData' => [
'customForm' => $form->getConfig()
]
]);
}
// Permit other plugins to continue interacting with this hook
return false;
}
/**
* Get the display name of this plugin.
* @return String
*/
function getDisplayName() {
return __('plugins.generic.backendUiExample.displayName');
}
/**
* Get a description of the plugin.
*/
function getDescription() {
return __('plugins.generic.backendUiExample.description');
}
public function addRoute(): void
{
Hook::add('APIHandler::endpoints::submissions', function(string $hookName, PKPBaseController &$apiController, APIHandler $apiHandler): bool {
// This allow to add a route on fly without defining a api controller
// Through this allow quick add/modify routes, it's better to use
// controller based appraoch which is more structured and understandable
$apiHandler->addRoute(
'GET',
'ithenticate',
function (IlluminateRequest $request): JsonResponse {
$fileIds = $request->query('fileIds');
$fileStatuses = [];
foreach ($fileIds as $fileId) {
$fileId = trim($fileId);
if (!empty($fileId)) {
// Here you would typically query your database or service
// to get the actual status of each file.
// For this example, we'll use a mock status
$fileStatuses[$fileId] = '75%';
}
}
return response()->json($fileStatuses, Response::HTTP_OK);
},
'test.onfly',
[
Role::ROLE_ID_SITE_ADMIN,
Role::ROLE_ID_MANAGER,
Role::ROLE_ID_SUB_EDITOR,
]
);
return false;
});
}
/**
* Inject a funding section into the submission wizard steps UI.
*
* @param string $hookName
* @param array $params
*
* @return bool Hook return value
*/
function addToSubmissionWizardSteps($hookName, $params) {
error_log('add to submission wizard steps');
$request = Application::get()->getRequest();
if ($request->getRequestedPage() !== 'submission') {
return;
}
if ($request->getRequestedOp() === 'saved') {
return;
}
$submission = $request
->getRouter()
->getHandler()
->getAuthorizedContextObject(Application::ASSOC_TYPE_SUBMISSION);
if (!$submission || !$submission->getData('submissionProgress')) {
return;
}
/** @var TemplateManager $templateMgr */
$templateMgr = $params[0];
$steps = $templateMgr->getState('steps');
$steps = array_map(function($step) use ($submission) {
if ($step['id'] === 'files') {
$step['sections'][] = [
'id' => 'buiCustom',
'name' => 'Custom section',
'description' => 'custom description',
'component' => 'BuiSubmissionWizardExample',
'props' => ['submissionId' => $submission->getId()]
];
}
return $step;
}, $steps);
$templateMgr->setState([
'steps' => $steps,
]);
// Add custom component to reviewSteps
$reviewSteps = $templateMgr->getTemplateVars('reviewSteps') ?: [];
$reviewSteps[] = [
'id' => 'buiReviewCustom',
'component' => 'BuiSubmissionWizardReviewExample',
'props' => [
'submissionId' => $submission->getId(),
'title' => "'Custom Review Component'"
]
];
$templateMgr->assign('reviewSteps', $reviewSteps);
return false;
}
}