-
Notifications
You must be signed in to change notification settings - Fork 445
/
ContextGridHandler.php
311 lines (268 loc) · 8.5 KB
/
ContextGridHandler.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
<?php
/**
* @file controllers/grid/admin/context/ContextGridHandler.php
*
* Copyright (c) 2014-2021 Simon Fraser University
* Copyright (c) 2000-2021 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class ContextGridHandler
*
* @ingroup controllers_grid_admin_context
*
* @brief Handle context grid requests.
*/
namespace PKP\controllers\grid\admin\context;
use APP\core\Application;
use APP\core\Request;
use APP\template\TemplateManager;
use PKP\components\forms\context\PKPContextForm;
use PKP\controllers\grid\feature\OrderGridItemsFeature;
use PKP\controllers\grid\GridColumn;
use PKP\controllers\grid\GridHandler;
use PKP\core\JSONMessage;
use PKP\core\PKPApplication;
use PKP\linkAction\LinkAction;
use PKP\linkAction\request\AjaxModal;
use PKP\security\authorization\PolicySet;
use PKP\security\authorization\RoleBasedHandlerOperationPolicy;
use PKP\security\Role;
class ContextGridHandler extends GridHandler
{
/**
* Constructor
*/
public function __construct()
{
parent::__construct();
$this->addRoleAssignment(
[
Role::ROLE_ID_SITE_ADMIN],
['fetchGrid', 'fetchRow', 'createContext', 'editContext', 'updateContext', 'users',
'deleteContext', 'saveSequence']
);
}
//
// Implement template methods from PKPHandler.
//
/**
* @copydoc PKPHandler::authorize()
*/
public function authorize($request, &$args, $roleAssignments)
{
$rolePolicy = new PolicySet(PolicySet::COMBINING_PERMIT_OVERRIDES);
foreach ($roleAssignments as $role => $operations) {
$rolePolicy->addPolicy(new RoleBasedHandlerOperationPolicy($request, $role, $operations));
}
$this->addPolicy($rolePolicy);
return parent::authorize($request, $args, $roleAssignments);
}
/**
* @copydoc GridHandler::initialize()
*
* @param null|mixed $args
*/
public function initialize($request, $args = null)
{
parent::initialize($request, $args);
$this->setTitle('context.contexts');
// Grid actions.
$router = $request->getRouter();
$this->addAction(
new LinkAction(
'createContext',
new AjaxModal(
$router->url($request, null, null, 'createContext', null, null),
__('admin.contexts.create'),
'modal_add_item',
true,
'context',
['editContext']
),
__('admin.contexts.create'),
'add_item'
)
);
//
// Grid columns.
//
$contextGridCellProvider = new ContextGridCellProvider();
// Context name.
$this->addColumn(
new GridColumn(
'name',
'common.name',
null,
null,
$contextGridCellProvider
)
);
// Context path.
$this->addColumn(
new GridColumn(
'urlPath',
'context.path',
null,
null,
$contextGridCellProvider
)
);
}
//
// Implement methods from GridHandler.
//
/**
* @copydoc GridHandler::getRowInstance()
*
* @return ContextGridRow
*/
protected function getRowInstance()
{
return new ContextGridRow();
}
/**
* @copydoc GridHandler::loadData()
*
* @param null|mixed $filter
*/
protected function loadData($request, $filter = null)
{
// Get all contexts.
$contextDao = Application::getContextDAO();
$contexts = $contextDao->getAll();
return $contexts->toAssociativeArray();
}
/**
* @copydoc GridHandler::setDataElementSequence()
*/
public function setDataElementSequence($request, $rowId, $gridDataElement, $newSequence)
{
$contextDao = Application::getContextDAO();
$gridDataElement->setSequence($newSequence);
$contextDao->updateObject($gridDataElement);
}
/**
* @copydoc GridHandler::getDataElementSequence()
*/
public function getDataElementSequence($gridDataElement)
{
return $gridDataElement->getSequence();
}
/**
* @copydoc GridHandler::addFeatures()
*/
public function initFeatures($request, $args)
{
return [new OrderGridItemsFeature()];
}
/**
* Get the list of "publish data changed" events.
* Used to update the site context switcher upon create/delete.
*
* @return array
*/
public function getPublishChangeEvents()
{
return ['updateHeader'];
}
//
// Public grid actions.
//
/**
* Add a new context.
*
* @param array $args
* @param Request $request
*/
public function createContext($args, $request)
{
// Calling editContext with an empty row id will add a new context.
return $this->editContext($args, $request);
}
/**
* Edit an existing context.
*
* @param array $args
* @param Request $request
*
* @return JSONMessage JSON object
*/
public function editContext($args, $request)
{
$contextService = app()->get('context');
$context = null;
if ($request->getUserVar('rowId')) {
$context = $contextService->get((int) $request->getUserVar('rowId'));
if (!$context) {
return new JSONMessage(false);
}
}
$dispatcher = $request->getDispatcher();
if ($context) {
$apiUrl = $dispatcher->url($request, PKPApplication::ROUTE_API, $context->getPath(), 'contexts/' . $context->getId());
$locales = $context->getSupportedFormLocaleNames();
} else {
$apiUrl = $dispatcher->url($request, PKPApplication::ROUTE_API, Application::SITE_CONTEXT_PATH, 'contexts');
$locales = $request->getSite()->getSupportedLocaleNames();
}
$locales = array_map(fn (string $locale, string $name) => ['key' => $locale, 'label' => $name], array_keys($locales), $locales);
$contextForm = new \APP\components\forms\context\ContextForm($apiUrl, $locales, $request->getBaseUrl(), $context);
$contextFormConfig = $contextForm->getConfig();
// Pass the URL to the context settings wizard so that the AddContextForm
// component can redirect to it when a new context is added.
if (!$context) {
$contextFormConfig['editContextUrl'] = $request->getDispatcher()->url($request, PKPApplication::ROUTE_PAGE, Application::SITE_CONTEXT_PATH, 'admin', 'wizard', ['__id__']);
}
$templateMgr = TemplateManager::getManager($request);
$containerData = [
'components' => [
PKPContextForm::FORM_CONTEXT => $contextFormConfig,
],
'tinyMCE' => [
'skinUrl' => $templateMgr->getTinyMceSkinUrl($request),
],
];
$templateMgr->assign([
'containerData' => $containerData,
'isAddingNewContext' => !$context,
]);
$templateMgr->registerClass(PKPContextForm::class, PKPContextForm::class);
return new JSONMessage(true, $templateMgr->fetch('admin/editContext.tpl'));
}
/**
* Delete a context.
*
* @param array $args
* @param Request $request
*
* @return JSONMessage JSON object
*/
public function deleteContext($args, $request)
{
if (!$request->checkCSRF()) {
return new JSONMessage(false);
}
$contextService = app()->get('context');
$context = $contextService->get((int) $request->getUserVar('rowId'));
if (!$context) {
return new JSONMessage(false);
}
$contextService->delete($context);
return \PKP\db\DAO::getDataChangedEvent($context->getId());
}
/**
* Display users management grid for the given context.
*
* @param array $args
* @param Request $request
*
* @return JSONMessage JSON object
*/
public function users($args, $request)
{
$templateMgr = TemplateManager::getManager($request);
$templateMgr->assign('oldUserId', (int) $request->getUserVar('oldUserId')); // for merging users.
parent::setupTemplate($request);
return $templateMgr->fetchJson('management/accessUsers.tpl');
}
}