Open Monograph Press  3.3.0
ContextGridHandler.inc.php
1 <?php
2 
16 import('lib.pkp.classes.controllers.grid.GridHandler');
17 import('lib.pkp.controllers.grid.admin.context.ContextGridRow');
18 
23  function __construct() {
24  parent::__construct();
25  $this->addRoleAssignment(array(
26  ROLE_ID_SITE_ADMIN),
27  array('fetchGrid', 'fetchRow', 'createContext', 'editContext', 'updateContext', 'users',
28  'deleteContext', 'saveSequence')
29  );
30  }
31 
32 
33  //
34  // Implement template methods from PKPHandler.
35  //
39  function authorize($request, &$args, $roleAssignments) {
40  import('lib.pkp.classes.security.authorization.PolicySet');
41  $rolePolicy = new PolicySet(COMBINING_PERMIT_OVERRIDES);
42 
43  import('lib.pkp.classes.security.authorization.RoleBasedHandlerOperationPolicy');
44  foreach($roleAssignments as $role => $operations) {
45  $rolePolicy->addPolicy(new RoleBasedHandlerOperationPolicy($request, $role, $operations));
46  }
47  $this->addPolicy($rolePolicy);
48 
49  return parent::authorize($request, $args, $roleAssignments);
50  }
51 
55  function initialize($request, $args = null) {
56  parent::initialize($request, $args);
57 
58  // Load user-related translations.
60  LOCALE_COMPONENT_PKP_USER,
61  LOCALE_COMPONENT_APP_MANAGER,
62  LOCALE_COMPONENT_PKP_MANAGER,
63  LOCALE_COMPONENT_PKP_ADMIN,
64  LOCALE_COMPONENT_APP_ADMIN
65  );
66 
67  $this->setTitle('context.contexts');
68 
69  // Grid actions.
70  $router = $request->getRouter();
71 
72  import('lib.pkp.classes.linkAction.request.AjaxModal');
73  $this->addAction(
74  new LinkAction(
75  'createContext',
76  new AjaxModal(
77  $router->url($request, null, null, 'createContext', null, null),
78  __('admin.contexts.create'),
79  'modal_add_item',
80  true,
81  'context',
82  ['editContext']
83  ),
84  __('admin.contexts.create'),
85  'add_item'
86  )
87  );
88 
89  //
90  // Grid columns.
91  //
92  import('lib.pkp.controllers.grid.admin.context.ContextGridCellProvider');
93  $contextGridCellProvider = new ContextGridCellProvider();
94 
95  // Context name.
96  $this->addColumn(
97  new GridColumn(
98  'name',
99  'common.name',
100  null,
101  null,
102  $contextGridCellProvider
103  )
104  );
105 
106  // Context path.
107  $this->addColumn(
108  new GridColumn(
109  'urlPath',
110  'context.path',
111  null,
112  null,
113  $contextGridCellProvider
114  )
115  );
116  }
117 
118 
119  //
120  // Implement methods from GridHandler.
121  //
126  protected function getRowInstance() {
127  return new ContextGridRow();
128  }
129 
133  protected function loadData($request, $filter = null) {
134  // Get all contexts.
135  $contextDao = Application::getContextDAO();
136  $contexts = $contextDao->getAll();
137 
138  return $contexts->toAssociativeArray();
139  }
140 
144  function setDataElementSequence($request, $rowId, $gridDataElement, $newSequence) {
145  $contextDao = Application::getContextDAO();
146  $gridDataElement->setSequence($newSequence);
147  $contextDao->updateObject($gridDataElement);
148  }
149 
153  function getDataElementSequence($gridDataElement) {
154  return $gridDataElement->getSequence();
155  }
156 
160  function initFeatures($request, $args) {
161  import('lib.pkp.classes.controllers.grid.feature.OrderGridItemsFeature');
162  return array(new OrderGridItemsFeature());
163  }
164 
171  return array('updateHeader');
172  }
173 
174 
175  //
176  // Public grid actions.
177  //
183  function createContext($args, $request) {
184  // Calling editContext with an empty row id will add a new context.
185  return $this->editContext($args, $request);
186  }
187 
194  function editContext($args, $request) {
195  import('classes.core.Services');
196  $contextService = Services::get('context');
197  $context = null;
198 
199  if ($request->getUserVar('rowId')) {
200  $context = $contextService->get((int) $request->getUserVar('rowId'));
201  if (!$context) {
202  return new JSONMessage(false);
203  }
204  }
205 
206  $dispatcher = $request->getDispatcher();
207  if ($context) {
208  $apiUrl = $dispatcher->url($request, ROUTE_API, $context->getPath(), 'contexts/' . $context->getId());
209  $supportedLocales = $context->getSupportedFormLocales();
210  } else {
211  $apiUrl = $dispatcher->url($request, ROUTE_API, CONTEXT_ID_ALL, 'contexts');
212  $supportedLocales = $request->getSite()->getSupportedLocales();
213  }
214 
215  $localeNames = AppLocale::getAllLocales();
216  $locales = array_map(function($localeKey) use ($localeNames) {
217  return ['key' => $localeKey, 'label' => $localeNames[$localeKey]];
218  }, $supportedLocales);
219 
220  $contextForm = new \APP\components\forms\context\ContextForm($apiUrl, $locales, $request->getBaseUrl(), $context);
221  $contextFormConfig = $contextForm->getConfig();
222 
223  // Pass the URL to the context settings wizard so that the AddContextForm
224  // component can redirect to it when a new context is added.
225  if (!$context) {
226  $contextFormConfig['editContextUrl'] = $request->getDispatcher()->url($request, ROUTE_PAGE, 'index', 'admin', 'wizard', '__id__');
227  }
228 
229  $containerData = [
230  'components' => [
231  FORM_CONTEXT => $contextFormConfig,
232  ],
233  ];
234 
235  $templateMgr = TemplateManager::getManager($request);
236  $templateMgr->assign([
237  'containerData' => $containerData,
238  'isAddingNewContext' => !$context,
239  ]);
240 
241  return new JSONMessage(true, $templateMgr->fetch('admin/editContext.tpl'));
242  }
243 
250  function deleteContext($args, $request) {
251 
252  if (!$request->checkCSRF()) {
253  return new JSONMessage(false);
254  }
255 
256  import('classes.core.Services');
257  $contextService = Services::get('context');
258 
259  $context = $contextService->get((int) $request->getUserVar('rowId'));
260 
261  if (!$context) {
262  return new JSONMessage(false);
263  }
264 
265  $contextService->delete($context);
266 
267  return DAO::getDataChangedEvent($context->getId());
268  }
269 
276  function users($args, $request) {
277  $templateMgr = TemplateManager::getManager($request);
278  $templateMgr->assign('oldUserId', (int) $request->getUserVar('oldUserId')); // for merging users.
279  parent::setupTemplate($request);
280  return $templateMgr->fetchJson('management/accessUsers.tpl');
281  }
282 }
PKPHandler\addRoleAssignment
addRoleAssignment($roleIds, $operations)
Definition: PKPHandler.inc.php:213
ContextGridCellProvider
Subclass for a context grid column's cell provider.
Definition: ContextGridCellProvider.inc.php:18
ContextGridHandler\authorize
authorize($request, &$args, $roleAssignments)
Definition: ContextGridHandler.inc.php:39
Application\getContextDAO
static getContextDAO()
Definition: Application.inc.php:145
PKPLocale\getAllLocales
static & getAllLocales()
Definition: PKPLocale.inc.php:537
GridColumn
The GridColumn class represents a column within a grid. It is used to format the data presented in a ...
Definition: GridColumn.inc.php:27
AppLocale\requireComponents
static requireComponents()
Definition: env1/MockAppLocale.inc.php:56
ContextGridHandler\getDataElementSequence
getDataElementSequence($gridDataElement)
Definition: ContextGridHandler.inc.php:153
ContextGridHandler\initialize
initialize($request, $args=null)
Definition: ContextGridHandler.inc.php:55
ContextGridHandler
Handle context grid requests.
Definition: ContextGridHandler.inc.php:19
GridHandler\addAction
addAction($action, $position=GRID_ACTION_POSITION_ABOVE)
Definition: GridHandler.inc.php:266
GridHandler\addColumn
addColumn($column)
Definition: GridHandler.inc.php:335
DAO\getDataChangedEvent
static getDataChangedEvent($elementId=null, $parentElementId=null, $content='')
Definition: DAO.inc.php:647
ContextGridHandler\editContext
editContext($args, $request)
Definition: ContextGridHandler.inc.php:194
ContextGridRow
Context grid row definition.
Definition: ContextGridRow.inc.php:20
JSONMessage
Class to represent a JSON (Javascript Object Notation) message.
Definition: JSONMessage.inc.php:18
ContextGridHandler\createContext
createContext($args, $request)
Definition: ContextGridHandler.inc.php:183
ContextGridHandler\loadData
loadData($request, $filter=null)
Definition: ContextGridHandler.inc.php:133
AjaxModal
A modal that retrieves its content from via AJAX.
Definition: AjaxModal.inc.php:18
OrderGridItemsFeature
Implements grid ordering functionality.
Definition: OrderGridItemsFeature.inc.php:19
LinkAction
Base class defining an action that can be performed by the user in the user interface.
Definition: LinkAction.inc.php:22
GridHandler\setTitle
setTitle($title)
Definition: GridHandler.inc.php:215
PKPTemplateManager\getManager
static & getManager($request=null)
Definition: PKPTemplateManager.inc.php:1239
ContextGridHandler\initFeatures
initFeatures($request, $args)
Definition: ContextGridHandler.inc.php:160
ContextGridHandler\setDataElementSequence
setDataElementSequence($request, $rowId, $gridDataElement, $newSequence)
Definition: ContextGridHandler.inc.php:144
ContextGridHandler\deleteContext
deleteContext($args, $request)
Definition: ContextGridHandler.inc.php:250
GridHandler
This class defines basic operations for handling HTML grids. Grids are used to implement a standardiz...
Definition: GridHandler.inc.php:58
RoleBasedHandlerOperationPolicy
Class to control access to handler operations via role based access control.
Definition: RoleBasedHandlerOperationPolicy.inc.php:18
PKPHandler\addPolicy
addPolicy($authorizationPolicy, $addToTop=false)
Definition: PKPHandler.inc.php:157
ContextGridHandler\getPublishChangeEvents
getPublishChangeEvents()
Definition: ContextGridHandler.inc.php:170
ContextGridHandler\users
users($args, $request)
Definition: ContextGridHandler.inc.php:276
PolicySet
An ordered list of policies. Policy sets can be added to decision managers like policies....
Definition: PolicySet.inc.php:26
ContextGridHandler\getRowInstance
getRowInstance()
Definition: ContextGridHandler.inc.php:126
PKPServices\get
static get($service)
Definition: PKPServices.inc.php:49
ContextGridHandler\__construct
__construct()
Definition: ContextGridHandler.inc.php:23