Open Monograph Press  1.1
 All Classes Namespaces Functions Variables Groups Pages
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 ChapterGridHandler() {
35  parent::CategoryGridHandler();
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', 'updateChapter', 'deleteChapter',
41  'addAuthor', 'editAuthor', 'updateAuthor', 'deleteAuthor'
42  )
43  );
44  $this->addRoleAssignment(ROLE_ID_REVIEWER, array('fetchGrid', 'fetchRow'));
45  }
46 
47 
48  //
49  // Getters and Setters
50  //
55  function &getMonograph() {
56  return $this->getAuthorizedContextObject(ASSOC_TYPE_MONOGRAPH);
57  }
58 
63  function getReadOnly() {
64  return $this->_readOnly;
65  }
66 
71  function setReadOnly($readOnly) {
72  $this->_readOnly = $readOnly;
73  }
74 
75 
76  //
77  // Implement template methods from PKPHandler
78  //
85  function authorize($request, &$args, $roleAssignments) {
86  import('classes.security.authorization.SubmissionAccessPolicy');
87  $this->addPolicy(new SubmissionAccessPolicy($request, $args, $roleAssignments));
88  return parent::authorize($request, $args, $roleAssignments);
89  }
90 
95  function initialize($request) {
96  parent::initialize($request);
97 
98  $this->setTitle('submission.chapters');
99  $this->setInstructions('submission.chaptersDescription');
100 
101  AppLocale::requireComponents(LOCALE_COMPONENT_APP_DEFAULT, LOCALE_COMPONENT_PKP_DEFAULT, LOCALE_COMPONENT_APP_SUBMISSION);
102 
103  if (!$this->getReadOnly()) {
104  // Grid actions
105  $router = $request->getRouter();
106  $actionArgs = $this->getRequestArgs();
107 
108  $this->addAction(
109  new LinkAction(
110  'addChapter',
111  new AjaxModal(
112  $router->url($request, null, null, 'addChapter', null, $actionArgs),
113  __('submission.chapter.addChapter'),
114  'modal_add_item'
115  ),
116  __('submission.chapter.addChapter'),
117  'add_item'
118  )
119  );
120  }
121 
122  // Columns
123  // reuse the cell providers for the AuthorGrid
124  $cellProvider = new PKPAuthorGridCellProvider();
125  $this->addColumn(
126  new GridColumn(
127  'name',
128  'author.users.contributor.name',
129  null,
130  'controllers/grid/gridCell.tpl',
131  $cellProvider,
132  array('width' => 50, 'alignment' => COLUMN_ALIGNMENT_LEFT)
133  )
134  );
135  $this->addColumn(
136  new GridColumn(
137  'email',
138  'author.users.contributor.email',
139  null,
140  'controllers/grid/gridCell.tpl',
141  $cellProvider
142  )
143  );
144  $this->addColumn(
145  new GridColumn(
146  'role',
147  'author.users.contributor.role',
148  null,
149  'controllers/grid/gridCell.tpl',
150  $cellProvider
151  )
152  );
153  }
154 
158  function initFeatures($request, $args) {
159  $monograph = $this->getMonograph();
160  $userRoles = $this->getAuthorizedContextObject(ASSOC_TYPE_USER_ROLES);
162  if ($monograph->getDateSubmitted() == null || array_intersect(array(ROLE_ID_MANAGER, ROLE_ID_SUB_EDITOR), $userRoles)) {
163  $this->setReadOnly(false);
164  import('lib.pkp.classes.controllers.grid.feature.OrderCategoryGridItemsFeature');
165  return array(new OrderCategoryGridItemsFeature(ORDER_CATEGORY_GRID_CATEGORIES_AND_ROWS));
166  } else {
167  $this->setReadOnly(true);
168  return array();
169  }
170  }
171 
175  function getRequestArgs() {
176  $monograph = $this->getMonograph();
177  return array_merge(
178  parent::getRequestArgs(),
179  array('submissionId' => $monograph->getId())
180  );
181  }
182 
186  function getCategoryRowIdParameterName() {
187  return 'chapterId';
188  }
190 
194  function loadData($request, $filter) {
195  $monograph = $this->getMonograph();
196  $chapterDao = DAORegistry::getDAO('ChapterDAO');
197  $chapters = $chapterDao->getChapters($monograph->getId());
198  return $chapters->toAssociativeArray();
199  }
200 
201 
202  //
203  // Extended methods from GridHandler
204  //
208  function getDataElementSequence($chapter) {
209  return $chapter->getSequence();
210  }
215  function setDataElementSequence($request, $chapterId, $chapter, $newSequence) {
216  $chapterDao = DAORegistry::getDAO('ChapterDAO');
217  $chapter->setSequence($newSequence);
218  $chapterDao->updateObject($chapter);
219  }
220 
221 
222  //
223  // Implement template methods from CategoryGridHandler
224  //
228  function getCategoryRowInstance() {
229  $monograph = $this->getMonograph();
230  $row = new ChapterGridCategoryRow($monograph, $this->getReadOnly());
231  import('controllers.grid.users.chapter.ChapterGridCategoryRowCellProvider');
232  $row->setCellProvider(new ChapterGridCategoryRowCellProvider());
233  return $row;
234  }
235 
239  function getCategoryData(&$chapter) {
240  $authorFactory = $chapter->getAuthors(); /* @var $authorFactory DAOResultFactory */
241  return $authorFactory->toAssociativeArray();
242  }
243 
247  function getDataElementInCategorySequence($categoryId, $author) {
248  return $author->getSequence();
249  }
254  function setDataElementInCategorySequence($chapterId, $author, $newSequence) {
255  $monograph = $this->getMonograph();
256 
257  // Remove the chapter author id.
258  $chapterAuthorDao = DAORegistry::getDAO('ChapterAuthorDAO');
259  $chapterAuthorDao->deleteChapterAuthorById($author->getId(), $chapterId);
260 
261  // Add it again with the correct sequence value.
262  // FIXME: primary authors not set for chapter authors.
263  $chapterAuthorDao->insertChapterAuthor($author->getId(), $chapterId, $monograph->getId(), false, $newSequence);
264  }
265 
266 
267  //
268  // Public Chapter Grid Actions
269  //
275  function addChapter($args, $request) {
276  // Calling editChapter() with an empty row id will add
277  // a new chapter.
278  return $this->editChapter($args, $request);
279  }
280 
287  function editChapter($args, $request) {
288  $chapter = $this->_getChapterFromRequest($request);
289 
290  // Form handling
291  import('controllers.grid.users.chapter.form.ChapterForm');
292  $chapterForm = new ChapterForm($this->getMonograph(), $chapter);
293  $chapterForm->initData();
294 
295  $json = new JSONMessage(true, $chapterForm->fetch($request));
296  return $json->getString();
297  }
298 
305  function updateChapter($args, $request) {
306  // Identify the chapter to be updated
307  $chapter = $this->_getChapterFromRequest($request);
309  // Form initialization
310  import('controllers.grid.users.chapter.form.ChapterForm');
311  $chapterForm = new ChapterForm($this->getMonograph(), $chapter);
312  $chapterForm->readInputData();
313 
314  // Form validation
315  if ($chapterForm->validate()) {
316  $chapterForm->execute();
317 
318  $newChapter = $chapterForm->getChapter();
319 
320  return DAO::getDataChangedEvent($newChapter->getId());
321  } else {
322  // Return an error
323  $json = new JSONMessage(false);
324  }
325 
326  // Return the serialized JSON response
327  return $json->getString();
328  }
329 
336  function deleteChapter($args, $request) {
337  // Identify the chapter to be deleted
338  $chapter = $this->_getChapterFromRequest($request);
339  $chapterId = $chapter->getId();
340 
341  // remove Authors assigned to this chapter first
342  $chapterAuthorDao = DAORegistry::getDAO('ChapterAuthorDAO');
343  $assignedAuthorIds = $chapterAuthorDao->getAuthorIdsByChapterId($chapterId);
344 
345  foreach ($assignedAuthorIds as $authorId) {
346  $chapterAuthorDao->deleteChapterAuthorById($authorId, $chapterId);
347  }
348 
349  $chapterDao = DAORegistry::getDAO('ChapterDAO');
350  $chapterDao->deleteById($chapterId);
351  return DAO::getDataChangedEvent();
352  }
353 
357  function _getChapterFromRequest($request) {
358  $monograph = $this->getMonograph();
359  $chapterDao = DAORegistry::getDAO('ChapterDAO');
360  return $chapterDao->getChapter((int) $request->getUserVar('chapterId'), $monograph->getId());
361  }
362 }
363 
364 ?>
Form for adding/editing a chapter stores/retrieves from an associative array.
static & getDAO($name, $dbconn=null)
addPolicy($authorizationPolicy, $addToTop=false)
Implements category grid ordering functionality.
static requireComponents()
addAction($action, $position=GRID_ACTION_POSITION_ABOVE)
updateChapter($args, $request)
addColumn($column)
initFeatures($request, $args)
Chapter grid category rows cell provider.
addRoleAssignment($roleIds, $operations)
Class to control (write) access to submissions and (read) access to submission details in OMP...
A modal that retrieves its content from via AJAX.
Class to represent a JSON (Javascript Object Notation) message.
deleteChapter($args, $request)
Chapter grid category row definition.
static getDataChangedEvent($elementId=null, $parentElementId=null)
Definition: DAO.inc.php:606
Class defining basic operations for handling HTML grids with categories.
loadData($request, $filter)
setDataElementSequence($request, $chapterId, $chapter, $newSequence)
Handle chapter grid requests.
getDataElementInCategorySequence($categoryId, $author)
authorize($request, &$args, $roleAssignments)
editChapter($args, $request)
setInstructions($instructions)
Represents a column within a grid. It is used to configure the way cells within a column are displaye...
setDataElementInCategorySequence($chapterId, $author, $newSequence)
& getAuthorizedContextObject($assocType)
Base class defining an action that can be performed by the user in the user interface.