Open Monograph Press  3.3.0
ChapterGridHandler.inc.php
1 <?php
2 
16 // import grid base classes
17 import('lib.pkp.classes.controllers.grid.CategoryGridHandler');
18 import('lib.pkp.classes.controllers.grid.DataObjectGridCellProvider');
19 
20 // import chapter grid specific classes
21 import('lib.pkp.controllers.grid.users.author.PKPAuthorGridCellProvider');
22 import('controllers.grid.users.chapter.ChapterGridCategoryRow');
23 
24 // Link action & modal classes
25 import('lib.pkp.classes.linkAction.request.AjaxModal');
26 
29  var $_readOnly;
30 
34  function __construct() {
35  parent::__construct();
36  $this->addRoleAssignment(
37  array(ROLE_ID_AUTHOR, ROLE_ID_SUB_EDITOR, ROLE_ID_MANAGER, ROLE_ID_ASSISTANT),
38  array(
39  'fetchGrid', 'fetchRow', 'fetchCategory', 'saveSequence',
40  'addChapter', 'editChapter', 'editChapterTab', 'updateChapter', 'deleteChapter',
41  'addAuthor', 'editAuthor', 'updateAuthor', 'deleteAuthor'
42  )
43  );
44  $this->addRoleAssignment(
45  array(ROLE_ID_SUB_EDITOR, ROLE_ID_MANAGER, ROLE_ID_ASSISTANT),
46  array('identifiers', 'updateIdentifiers', 'clearPubId',)
47  );
48  $this->addRoleAssignment(ROLE_ID_REVIEWER, array('fetchGrid', 'fetchRow'));
49  }
50 
51 
52  //
53  // Getters and Setters
54  //
59  function getMonograph() {
60  return $this->getAuthorizedContextObject(ASSOC_TYPE_MONOGRAPH);
61  }
62 
67  function getPublication() {
68  return $this->getAuthorizedContextObject(ASSOC_TYPE_PUBLICATION);
69  }
70 
75  function getReadOnly() {
76  return $this->_readOnly;
77  }
78 
83  function setReadOnly($readOnly) {
84  $this->_readOnly = $readOnly;
85  }
86 
87 
88  //
89  // Implement template methods from PKPHandler
90  //
97  function authorize($request, &$args, $roleAssignments) {
98  import('lib.pkp.classes.security.authorization.PublicationAccessPolicy');
99  $this->addPolicy(new PublicationAccessPolicy($request, $args, $roleAssignments));
100  return parent::authorize($request, $args, $roleAssignments);
101  }
102 
106  function initialize($request, $args = null) {
107  parent::initialize($request, $args);
108 
109  $this->setTitle('submission.chapters');
110 
111  AppLocale::requireComponents(LOCALE_COMPONENT_APP_DEFAULT, LOCALE_COMPONENT_PKP_DEFAULT, LOCALE_COMPONENT_APP_SUBMISSION, LOCALE_COMPONENT_PKP_SUBMISSION);
112 
113  if ($this->getPublication()->getData('status') === STATUS_PUBLISHED) {
114  $this->setReadOnly(true);
115  }
116 
117  if (!$this->getReadOnly()) {
118  // Grid actions
119  $router = $request->getRouter();
120  $actionArgs = $this->getRequestArgs();
121 
122  $this->addAction(
123  new LinkAction(
124  'addChapter',
125  new AjaxModal(
126  $router->url($request, null, null, 'addChapter', null, $actionArgs),
127  __('submission.chapter.addChapter'),
128  'modal_add_item'
129  ),
130  __('submission.chapter.addChapter'),
131  'add_item'
132  )
133  );
134  }
135 
136  // Columns
137  // reuse the cell providers for the AuthorGrid
138  $cellProvider = new PKPAuthorGridCellProvider($this->getPublication());
139  $this->addColumn(
140  new GridColumn(
141  'name',
142  'author.users.contributor.name',
143  null,
144  null,
145  $cellProvider,
146  array('width' => 50, 'alignment' => COLUMN_ALIGNMENT_LEFT)
147  )
148  );
149  $this->addColumn(
150  new GridColumn(
151  'email',
152  'author.users.contributor.email',
153  null,
154  null,
155  $cellProvider
156  )
157  );
158  $this->addColumn(
159  new GridColumn(
160  'role',
161  'author.users.contributor.role',
162  null,
163  null,
164  $cellProvider
165  )
166  );
167  }
168 
172  function initFeatures($request, $args) {
173  if ($this->canAdminister($request->getUser())) {
174  $this->setReadOnly(false);
175  import('lib.pkp.classes.controllers.grid.feature.OrderCategoryGridItemsFeature');
176  return array(new OrderCategoryGridItemsFeature(ORDER_CATEGORY_GRID_CATEGORIES_AND_ROWS, true, $this));
177  } else {
178  $this->setReadOnly(true);
179  return array();
180  }
181  }
182 
186  function getRequestArgs() {
187  return array_merge(
188  parent::getRequestArgs(),
189  array(
190  'submissionId' => $this->getMonograph()->getId(),
191  'publicationId' => $this->getPublication()->getId(),
192  )
193  );
194  }
195 
201  function canAdminister($user) {
202  $submission = $this->getMonograph();
203  $publication = $this->getPublication();
204  $userRoles = $this->getAuthorizedContextObject(ASSOC_TYPE_USER_ROLES);
205 
206  if ($publication->getData('status') === STATUS_PUBLISHED) {
207  return false;
208  }
209 
210  if (in_array(ROLE_ID_SITE_ADMIN, $userRoles)) {
211  return true;
212  }
213 
214  // Incomplete submissions can be edited. (Presumably author.)
215  if ($submission->getDateSubmitted() == null) return true;
216 
217  // The user may not be allowed to edit the metadata
218  if (Services::get('submission')->canEditPublication($submission->getId(), $user->getId())) {
219  return true;
220  }
221 
222  // Default: Read-only.
223  return false;
224  }
225 
229  function getCategoryRowIdParameterName() {
230  return 'chapterId';
231  }
232 
233 
237  function loadData($request, $filter) {
238  return DAORegistry::getDAO('ChapterDAO')
239  ->getByPublicationId($this->getPublication()->getId())
240  ->toAssociativeArray();
241  }
242 
243 
244  //
245  // Extended methods from GridHandler
246  //
250  function getDataElementSequence($gridDataElement) {
251  return $gridDataElement->getSequence();
252  }
253 
257  function setDataElementSequence($request, $chapterId, $chapter, $newSequence) {
258  if (!$this->canAdminister($request->getUser())) return;
259 
260  $chapterDao = DAORegistry::getDAO('ChapterDAO'); /* @var $chapterDao ChapterDAO */
261  $chapter->setSequence($newSequence);
262  $chapterDao->updateObject($chapter);
263  }
264 
265 
266  //
267  // Implement template methods from CategoryGridHandler
268  //
272  function getCategoryRowInstance() {
273  $monograph = $this->getMonograph();
274  $row = new ChapterGridCategoryRow($monograph, $this->getPublication(), $this->getReadOnly());
275  import('controllers.grid.users.chapter.ChapterGridCategoryRowCellProvider');
276  $row->setCellProvider(new ChapterGridCategoryRowCellProvider());
277  return $row;
278  }
279 
283  function loadCategoryData($request, &$chapter, $filter = null) {
284  $authorFactory = $chapter->getAuthors(); /* @var $authorFactory DAOResultFactory */
285  return $authorFactory->toAssociativeArray();
286  }
287 
291  function getDataElementInCategorySequence($categoryId, &$author) {
292  return $author->getSequence();
293  }
294 
298  function setDataElementInCategorySequence($chapterId, &$author, $newSequence) {
299  if (!$this->canAdminister(Application::get()->getRequest()->getUser())) return;
300 
301  $monograph = $this->getMonograph();
302 
303  // Remove the chapter author id.
304  $chapterAuthorDao = DAORegistry::getDAO('ChapterAuthorDAO'); /* @var $chapterAuthorDao ChapterAuthorDAO */
305  $chapterAuthorDao->deleteChapterAuthorById($author->getId(), $chapterId);
306 
307  // Add it again with the correct sequence value.
308  // FIXME: primary authors not set for chapter authors.
309  $chapterAuthorDao->insertChapterAuthor($author->getId(), $chapterId, false, $newSequence);
310  }
311 
312 
313  //
314  // Public Chapter Grid Actions
315  //
322  function identifiers($args, $request) {
323  $chapter = $this->_getChapterFromRequest($request);
324 
325  import('controllers.tab.pubIds.form.PublicIdentifiersForm');
326  $form = new PublicIdentifiersForm($chapter);
327  $form->initData();
328  return new JSONMessage(true, $form->fetch($request));
329  }
330 
337  function updateIdentifiers($args, $request) {
338  if (!$this->canAdminister($request->getUser())) return new JSONMessage(false);
339 
340  $chapter = $this->_getChapterFromRequest($request);
341 
342  import('controllers.tab.pubIds.form.PublicIdentifiersForm');
343  $form = new PublicIdentifiersForm($chapter);
344  $form->readInputData();
345  if ($form->validate()) {
346  $form->execute();
347  return DAO::getDataChangedEvent();
348  } else {
349  return new JSONMessage(true, $form->fetch($request));
350  }
351  }
352 
359  function clearPubId($args, $request) {
360  if (!$request->checkCSRF()) return new JSONMessage(false);
361  if (!$this->canAdminister($request->getUser())) return new JSONMessage(false);
362 
363  $chapter = $this->_getChapterFromRequest($request);
364 
365  import('controllers.tab.pubIds.form.PublicIdentifiersForm');
366  $form = new PublicIdentifiersForm($chapter);
367  $form->clearPubId($request->getUserVar('pubIdPlugIn'));
368  return new JSONMessage(true);
369  }
370 
376  function addChapter($args, $request) {
377  if (!$this->canAdminister($request->getUser())) return new JSONMessage(false);
378  // Calling editChapterTab() with an empty row id will add
379  // a new chapter.
380  return $this->editChapterTab($args, $request);
381  }
382 
389  function editChapter($args, $request) {
390  if (!$this->canAdminister($request->getUser())) return new JSONMessage(false);
391  $chapter = $this->_getChapterFromRequest($request);
392 
393  // Check if this is a remote galley
394  $templateMgr = TemplateManager::getManager($request);
395  $templateMgr->assign(array(
396  'submissionId' => $this->getMonograph()->getId(),
397  'publicationId' => $this->getPublication()->getId(),
398  'chapterId' => $chapter->getId(),
399  ));
400 
401  if (array_intersect(array(ROLE_ID_MANAGER, ROLE_ID_SUB_EDITOR, ROLE_ID_ASSISTANT), $this->getAuthorizedContextObject(ASSOC_TYPE_USER_ROLES))) {
402  $publisherIdEnabled = in_array('chapter', (array) $request->getContext()->getData('enablePublisherId'));
403  $pubIdPlugins = PluginRegistry::getPlugins('pubIds');
404  $pubIdEnabled = false;
405  foreach ($pubIdPlugins as $pubIdPlugin) {
406  if ($pubIdPlugin->isObjectTypeEnabled('Chapter', $request->getContext()->getId())) {
407  $pubIdEnabled = true;
408  break;
409  }
410  }
411  $templateMgr->assign('showIdentifierTab', $publisherIdEnabled || $pubIdEnabled);
412  }
413 
414  return new JSONMessage(true, $templateMgr->fetch('controllers/grid/users/chapter/editChapter.tpl'));
415  }
416 
423  function editChapterTab($args, $request) {
424  if (!$this->canAdminister($request->getUser())) return new JSONMessage(false);
425  $chapter = $this->_getChapterFromRequest($request);
426 
427  // Form handling
428  import('controllers.grid.users.chapter.form.ChapterForm');
429  $chapterForm = new ChapterForm($this->getMonograph(), $this->getPublication(), $chapter);
430  $chapterForm->initData();
431 
432  return new JSONMessage(true, $chapterForm->fetch($request));
433  }
434 
441  function updateChapter($args, $request) {
442  if (!$this->canAdminister($request->getUser())) return new JSONMessage(false);
443  // Identify the chapter to be updated
444  $chapter = $this->_getChapterFromRequest($request);
445 
446  // Form initialization
447  import('controllers.grid.users.chapter.form.ChapterForm');
448  $chapterForm = new ChapterForm($this->getMonograph(), $this->getPublication(), $chapter);
449  $chapterForm->readInputData();
450 
451  // Form validation
452  if ($chapterForm->validate()) {
453  $notificationMgr = new NotificationManager();
454  $notificationMgr->createTrivialNotification($request->getUser()->getId());
455  $chapterForm->execute();
456  return DAO::getDataChangedEvent($chapterForm->getChapter()->getId());
457  } else {
458  // Return an error
459  return new JSONMessage(false);
460  }
461  }
462 
469  function deleteChapter($args, $request) {
470  if (!$this->canAdminister($request->getUser())) return new JSONMessage(false);
471  // Identify the chapter to be deleted
472  $chapter = $this->_getChapterFromRequest($request);
473  $chapterId = $chapter->getId();
474 
475  // remove Authors assigned to this chapter first
476  $chapterAuthorDao = DAORegistry::getDAO('ChapterAuthorDAO'); /* @var $chapterAuthorDao ChapterAuthorDAO */
477  $assignedAuthorIds = $chapterAuthorDao->getAuthorIdsByChapterId($chapterId);
478 
479  foreach ($assignedAuthorIds as $authorId) {
480  $chapterAuthorDao->deleteChapterAuthorById($authorId, $chapterId);
481  }
482 
483  $chapterDao = DAORegistry::getDAO('ChapterDAO'); /* @var $chapterDao ChapterDAO */
484  $chapterDao->deleteById($chapterId);
485  return DAO::getDataChangedEvent();
486  }
487 
491  function _getChapterFromRequest($request) {
492  return DAORegistry::getDAO('ChapterDAO')->getChapter(
493  (int) $request->getUserVar('chapterId'),
494  $this->getPublication()->getId()
495  );
496  }
497 }
498 
499 
PKPHandler\addRoleAssignment
addRoleAssignment($roleIds, $operations)
Definition: PKPHandler.inc.php:213
ChapterGridHandler\editChapter
editChapter($args, $request)
Definition: ChapterGridHandler.inc.php:392
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
PublicationAccessPolicy
Class to control access to a publication.
Definition: PublicationAccessPolicy.inc.php:20
AppLocale\requireComponents
static requireComponents()
Definition: env1/MockAppLocale.inc.php:56
ChapterGridHandler\getReadOnly
getReadOnly()
Definition: ChapterGridHandler.inc.php:78
PluginRegistry\getPlugins
static & getPlugins($category=null)
Definition: PluginRegistry.inc.php:30
PKPAuthorGridCellProvider
Definition: PKPAuthorGridCellProvider.inc.php:18
ChapterGridCategoryRowCellProvider
Chapter grid category rows cell provider.
Definition: ChapterGridCategoryRowCellProvider.inc.php:18
ChapterGridHandler\updateChapter
updateChapter($args, $request)
Definition: ChapterGridHandler.inc.php:444
ChapterForm
Form for adding/editing a chapter stores/retrieves from an associative array.
Definition: ChapterForm.inc.php:19
ChapterGridHandler\getCategoryRowIdParameterName
getCategoryRowIdParameterName()
Definition: ChapterGridHandler.inc.php:232
ChapterGridHandler\editChapterTab
editChapterTab($args, $request)
Definition: ChapterGridHandler.inc.php:426
ChapterGridHandler\setDataElementInCategorySequence
setDataElementInCategorySequence($chapterId, &$author, $newSequence)
Definition: ChapterGridHandler.inc.php:301
DAORegistry\getDAO
static & getDAO($name, $dbconn=null)
Definition: DAORegistry.inc.php:57
ChapterGridHandler\updateIdentifiers
updateIdentifiers($args, $request)
Definition: ChapterGridHandler.inc.php:340
ChapterGridHandler\getRequestArgs
getRequestArgs()
Definition: ChapterGridHandler.inc.php:189
ChapterGridHandler\setDataElementSequence
setDataElementSequence($request, $chapterId, $chapter, $newSequence)
Definition: ChapterGridHandler.inc.php:260
PublicIdentifiersForm
Displays a pub ids form.
Definition: PublicIdentifiersForm.inc.php:18
PKPHandler\getId
getId()
Definition: PKPHandler.inc.php:107
ChapterGridHandler\_getChapterFromRequest
_getChapterFromRequest($request)
Definition: ChapterGridHandler.inc.php:494
ChapterGridHandler\deleteChapter
deleteChapter($args, $request)
Definition: ChapterGridHandler.inc.php:472
GridHandler\addAction
addAction($action, $position=GRID_ACTION_POSITION_ABOVE)
Definition: GridHandler.inc.php:266
GridHandler\addColumn
addColumn($column)
Definition: GridHandler.inc.php:335
ChapterGridHandler\canAdminister
canAdminister($user)
Definition: ChapterGridHandler.inc.php:204
ChapterGridHandler\initFeatures
initFeatures($request, $args)
Definition: ChapterGridHandler.inc.php:175
DAO\getDataChangedEvent
static getDataChangedEvent($elementId=null, $parentElementId=null, $content='')
Definition: DAO.inc.php:647
ChapterGridHandler\getMonograph
getMonograph()
Definition: ChapterGridHandler.inc.php:62
ChapterGridHandler\loadCategoryData
loadCategoryData($request, &$chapter, $filter=null)
Definition: ChapterGridHandler.inc.php:286
JSONMessage
Class to represent a JSON (Javascript Object Notation) message.
Definition: JSONMessage.inc.php:18
AjaxModal
A modal that retrieves its content from via AJAX.
Definition: AjaxModal.inc.php:18
LinkAction
Base class defining an action that can be performed by the user in the user interface.
Definition: LinkAction.inc.php:22
CategoryGridHandler
Class defining basic operations for handling HTML grids with categories.
Definition: CategoryGridHandler.inc.php:23
ChapterGridCategoryRow
Chapter grid category row definition.
Definition: ChapterGridCategoryRow.inc.php:22
ChapterGridHandler
Handle chapter grid requests.
Definition: ChapterGridHandler.inc.php:27
GridHandler\setTitle
setTitle($title)
Definition: GridHandler.inc.php:215
ChapterGridHandler\loadData
loadData($request, $filter)
Definition: ChapterGridHandler.inc.php:240
PKPTemplateManager\getManager
static & getManager($request=null)
Definition: PKPTemplateManager.inc.php:1239
ChapterGridHandler\getDataElementSequence
getDataElementSequence($gridDataElement)
Definition: ChapterGridHandler.inc.php:253
ChapterGridHandler\addChapter
addChapter($args, $request)
Definition: ChapterGridHandler.inc.php:379
ChapterGridHandler\getCategoryRowInstance
getCategoryRowInstance()
Definition: ChapterGridHandler.inc.php:275
ChapterGridHandler\clearPubId
clearPubId($args, $request)
Definition: ChapterGridHandler.inc.php:362
PKPHandler\getAuthorizedContextObject
& getAuthorizedContextObject($assocType)
Definition: PKPHandler.inc.php:174
ChapterGridHandler\getPublication
getPublication()
Definition: ChapterGridHandler.inc.php:70
ChapterGridHandler\setReadOnly
setReadOnly($readOnly)
Definition: ChapterGridHandler.inc.php:86
ChapterGridHandler\__construct
__construct()
Definition: ChapterGridHandler.inc.php:37
ChapterGridHandler\initialize
initialize($request, $args=null)
Definition: ChapterGridHandler.inc.php:109
ChapterGridHandler\$_readOnly
$_readOnly
Definition: ChapterGridHandler.inc.php:32
ChapterGridHandler\identifiers
identifiers($args, $request)
Definition: ChapterGridHandler.inc.php:325
NotificationManager
Definition: NotificationManager.inc.php:19
PKPApplication\get
static get()
Definition: PKPApplication.inc.php:235
PKPHandler\addPolicy
addPolicy($authorizationPolicy, $addToTop=false)
Definition: PKPHandler.inc.php:157
OrderCategoryGridItemsFeature
Implements category grid ordering functionality.
Definition: OrderCategoryGridItemsFeature.inc.php:23
ChapterGridHandler\getDataElementInCategorySequence
getDataElementInCategorySequence($categoryId, &$author)
Definition: ChapterGridHandler.inc.php:294
PKPServices\get
static get($service)
Definition: PKPServices.inc.php:49
ChapterGridHandler\authorize
authorize($request, &$args, $roleAssignments)
Definition: ChapterGridHandler.inc.php:100