Open Journal Systems  3.3.0
SectionGridHandler.inc.php
1 <?php
2 
16 import('lib.pkp.controllers.grid.settings.SetupGridHandler');
17 import('controllers.grid.settings.sections.SectionGridRow');
18 
23  function __construct() {
24  parent::__construct();
25  $this->addRoleAssignment(
26  array(ROLE_ID_MANAGER),
27  array('fetchGrid', 'fetchRow', 'addSection', 'editSection', 'updateSection', 'deleteSection', 'saveSequence', 'deactivateSection','activateSection')
28  );
29  }
30 
31 
32  //
33  // Overridden template methods
34  //
38  function initialize($request, $args = null) {
39  parent::initialize($request, $args);
40  $journal = $request->getJournal();
41 
42  // FIXME are these all required?
44  LOCALE_COMPONENT_APP_MANAGER,
45  LOCALE_COMPONENT_PKP_COMMON,
46  LOCALE_COMPONENT_PKP_USER,
47  LOCALE_COMPONENT_APP_COMMON
48  );
49 
50  // Set the grid title.
51  $this->setTitle('section.sections');
52 
53  // Elements to be displayed in the grid
54  $sectionDao = DAORegistry::getDAO('SectionDAO'); /* @var $sectionDao SectionDAO */
55  $subEditorsDao = DAORegistry::getDAO('SubEditorsDAO'); /* @var $subEditorsDao SubEditorsDAO */
56  $sectionIterator = $sectionDao->getByJournalId($journal->getId());
57 
58  $gridData = array();
59  while ($section = $sectionIterator->next()) {
60  // Get the section editors data for the row
61  $assignedSubEditors = $subEditorsDao->getBySubmissionGroupId($section->getId(), ASSOC_TYPE_SECTION, $journal->getId());
62  if(empty($assignedSubEditors)) {
63  $editorsString = __('common.none');
64  } else {
65  $editors = array();
66  foreach ($assignedSubEditors as $subEditor) {
67  $editors[] = $subEditor->getFullName();
68  }
69  $editorsString = implode(', ', $editors);
70  }
71 
72  $sectionId = $section->getId();
73  $gridData[$sectionId] = array(
74  'title' => $section->getLocalizedTitle(),
75  'editors' => $editorsString,
76  'inactive' => $section->getIsInactive(),
77  'seq' => $section->getSequence()
78  );
79  }
80  uasort($gridData, function($a,$b) {
81  return $a['seq']-$b['seq'];
82  });
83 
84  $this->setGridDataElements($gridData);
85 
86  // Add grid-level actions
87  $router = $request->getRouter();
88  import('lib.pkp.classes.linkAction.request.AjaxModal');
89  $this->addAction(
90  new LinkAction(
91  'addSection',
92  new AjaxModal(
93  $router->url($request, null, null, 'addSection', null, array('gridId' => $this->getId())),
94  __('manager.sections.create'),
95  'modal_manage'
96  ),
97  __('manager.sections.create'),
98  'add_section'
99  )
100  );
101 
102  //
103  // Grid columns.
104  //
105  import('controllers.grid.settings.sections.SectionGridCellProvider');
106  $sectionGridCellProvider = new SectionGridCellProvider();
107 
108  // Section name
109  $this->addColumn(
110  new GridColumn(
111  'title',
112  'common.title'
113  )
114  );
115  // Section 'editors'
116  $this->addColumn(new GridColumn('editors', 'user.role.editors'));
117  //Section 'inactive'
118  $this->addColumn(
119  new GridColumn(
120  'inactive',
121  'common.inactive',
122  null,
123  'controllers/grid/common/cell/selectStatusCell.tpl',
124  $sectionGridCellProvider,
125  array('alignment' => COLUMN_ALIGNMENT_CENTER,
126  'width' => 20)
127  )
128  );
129  }
130 
131  //
132  // Overridden methods from GridHandler
133  //
137  function initFeatures($request, $args) {
138  import('lib.pkp.classes.controllers.grid.feature.OrderGridItemsFeature');
139  return array(new OrderGridItemsFeature());
140  }
141 
146  protected function getRowInstance() {
147  return new SectionGridRow();
148  }
149 
153  function getDataElementSequence($row) {
154  return $row['seq'];
155  }
156 
160  function setDataElementSequence($request, $rowId, $gridDataElement, $newSequence) {
161  $sectionDao = DAORegistry::getDAO('SectionDAO'); /* @var $sectionDao SectionDAO */
162  $journal = $request->getJournal();
163  $section = $sectionDao->getById($rowId, $journal->getId());
164  $section->setSequence($newSequence);
165  $sectionDao->updateObject($section);
166  }
167 
168  //
169  // Public Section Grid Actions
170  //
176  function addSection($args, $request) {
177  // Calling editSection with an empty ID will add
178  // a new section.
179  return $this->editSection($args, $request);
180  }
181 
189  function editSection($args, $request) {
190  $sectionId = isset($args['sectionId']) ? $args['sectionId'] : null;
191  $this->setupTemplate($request);
192 
193  import('controllers.grid.settings.sections.form.SectionForm');
194  $sectionForm = new SectionForm($request, $sectionId);
195  $sectionForm->initData();
196  return new JSONMessage(true, $sectionForm->fetch($request));
197  }
198 
205  function updateSection($args, $request) {
206  $sectionId = $request->getUserVar('sectionId');
207 
208  import('controllers.grid.settings.sections.form.SectionForm');
209  $sectionForm = new SectionForm($request, $sectionId);
210  $sectionForm->readInputData();
211 
212  if ($sectionForm->validate()) {
213  $sectionForm->execute();
214  $notificationManager = new NotificationManager();
215  $notificationManager->createTrivialNotification($request->getUser()->getId());
216  return DAO::getDataChangedEvent($sectionForm->getSectionId());
217  }
218  return new JSONMessage(false);
219  }
220 
227  function deleteSection($args, $request) {
228  $journal = $request->getJournal();
229 
230  $sectionDao = DAORegistry::getDAO('SectionDAO'); /* @var $sectionDao SectionDAO */
231  $section = $sectionDao->getById(
232  $request->getUserVar('sectionId'),
233  $journal->getId()
234  );
235 
236  if (!$request->checkCSRF()) {
237  return new JSONMessage(false, __('form.csrfInvalid'));
238  }
239 
240  if (!$section) {
241  return new JSONMessage(false, __('manager.setup.errorDeletingItem'));
242  }
243 
244  AppLocale::requireComponents(LOCALE_COMPONENT_PKP_MANAGER);
245  $submissionDao = DAORegistry::getDAO('SubmissionDAO'); /* @var $submissionDao SubmissionDAO */
246  $checkSubmissions = $submissionDao->retrieve('SELECT p.publication_id FROM publications p JOIN submissions s ON (s.submission_id = p.submission_id) WHERE p.section_id = ? AND s.context_id = ?', array((int) $request->getUserVar('sectionId'), (int) $journal->getId()));
247 
248  if ($checkSubmissions->numRows() > 0) {
249  return new JSONMessage(false, __('manager.sections.alertDelete'));
250  }
251 
252  // Validate if it can be deleted
253  $sectionsIterator = $sectionDao->getByContextId($journal->getId(),null,false);
254  $activeSectionsCount = (!$section->getIsInactive()) ? -1 : 0;
255  while ($checkSection = $sectionsIterator->next()) {
256  if (!$checkSection->getIsInactive()) {
257  $activeSectionsCount++;
258  }
259  }
260 
261  if ($activeSectionsCount < 1) {
262  return new JSONMessage(false, __('manager.sections.confirmDeactivateSection.error'));
263  return false;
264  }
265 
266  if ($checkSubmissions->numRows() > 0) {
267  return new JSONMessage(false, __('manager.sections.alertDelete'));
268  }
269 
270  $sectionDao->deleteObject($section);
271  return DAO::getDataChangedEvent($section->getId());
272 
273  }
274 
281  function deactivateSection($args, $request) {
282  // Identify the current section
283  $sectionId = (int) $request->getUserVar('sectionKey');
284 
285  // Identify the context id.
286  $context = $request->getContext();
287 
288  // Get section object
289  $sectionDao = DAORegistry::getDAO('SectionDAO'); /* @var $sectionDao SectionDAO */
290  // Validate if it can be inactive
291  $sectionsIterator = $sectionDao->getByContextId($context->getId(),null,false);
292  $activeSectionsCount = 0;
293  while ($section = $sectionsIterator->next()) {
294  if (!$section->getIsInactive()) {
295  $activeSectionsCount++;
296  }
297  }
298  if ($activeSectionsCount > 1) {
299  $section = $sectionDao->getById($sectionId, $context->getId());
300 
301  if ($request->checkCSRF() && isset($section) && !$section->getIsInactive()) {
302  $section->setIsInactive(1);
303  $sectionDao->updateObject($section);
304 
305  // Create the notification.
306  $notificationMgr = new NotificationManager();
307  $user = $request->getUser();
308  $notificationMgr->createTrivialNotification($user->getId());
309 
310  return DAO::getDataChangedEvent($sectionId);
311  }
312  } else {
313  // Create the notification.
314  $notificationMgr = new NotificationManager();
315  $user = $request->getUser();
316  $notificationMgr->createTrivialNotification($user->getId(), NOTIFICATION_TYPE_ERROR, array('contents' => __('manager.sections.confirmDeactivateSection.error')));
317  return DAO::getDataChangedEvent($sectionId);
318  }
319 
320  return new JSONMessage(false);
321  }
322 
329  function activateSection($args, $request) {
330 
331  // Identify the current section
332  $sectionId = (int) $request->getUserVar('sectionKey');
333 
334  // Identify the context id.
335  $context = $request->getContext();
336 
337  // Get section object
338  $sectionDao = DAORegistry::getDAO('SectionDAO'); /* @var $sectionDao SectionDAO */
339  $section = $sectionDao->getById($sectionId, $context->getId());
340 
341  if ($request->checkCSRF() && isset($section) && $section->getIsInactive()) {
342  $section->setIsInactive(0);
343  $sectionDao->updateObject($section);
344 
345  // Create the notification.
346  $notificationMgr = new NotificationManager();
347  $user = $request->getUser();
348  $notificationMgr->createTrivialNotification($user->getId());
349 
350  return DAO::getDataChangedEvent($sectionId);
351  }
352 
353  return new JSONMessage(false);
354  }
355 
356 }
357 
358 
PKPHandler\addRoleAssignment
addRoleAssignment($roleIds, $operations)
Definition: PKPHandler.inc.php:213
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
SetupGridHandler
Base class for setup grid handlers.
Definition: SetupGridHandler.inc.php:18
AppLocale\requireComponents
static requireComponents()
Definition: env1/MockAppLocale.inc.php:56
SectionGridHandler\getRowInstance
getRowInstance()
Definition: SectionGridHandler.inc.php:146
SectionGridHandler\updateSection
updateSection($args, $request)
Definition: SectionGridHandler.inc.php:205
SectionGridHandler\initFeatures
initFeatures($request, $args)
Definition: SectionGridHandler.inc.php:137
DAORegistry\getDAO
static & getDAO($name, $dbconn=null)
Definition: DAORegistry.inc.php:57
SectionGridCellProvider
Grid cell provider for section grid.
Definition: SectionGridCellProvider.inc.php:17
SectionGridHandler\addSection
addSection($args, $request)
Definition: SectionGridHandler.inc.php:176
SectionForm
Form for adding/editing a section.
Definition: SectionForm.inc.php:18
PKPHandler\getId
getId()
Definition: PKPHandler.inc.php:107
SectionGridHandler\getDataElementSequence
getDataElementSequence($row)
Definition: SectionGridHandler.inc.php:153
SectionGridRow
Handle section grid row requests.
Definition: SectionGridRow.inc.php:18
GridHandler\addAction
addAction($action, $position=GRID_ACTION_POSITION_ABOVE)
Definition: GridHandler.inc.php:266
GridHandler\addColumn
addColumn($column)
Definition: GridHandler.inc.php:335
SectionGridHandler\__construct
__construct()
Definition: SectionGridHandler.inc.php:23
DAO\getDataChangedEvent
static getDataChangedEvent($elementId=null, $parentElementId=null, $content='')
Definition: DAO.inc.php:647
SectionGridHandler
Handle section grid requests.
Definition: SectionGridHandler.inc.php:19
GridHandler\setGridDataElements
setGridDataElements($data)
Definition: GridHandler.inc.php:379
JSONMessage
Class to represent a JSON (Javascript Object Notation) message.
Definition: JSONMessage.inc.php:18
SectionGridHandler\deactivateSection
deactivateSection($args, $request)
Definition: SectionGridHandler.inc.php:281
SectionGridHandler\initialize
initialize($request, $args=null)
Definition: SectionGridHandler.inc.php:38
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
SectionGridHandler\editSection
editSection($args, $request)
Definition: SectionGridHandler.inc.php:189
GridHandler\setTitle
setTitle($title)
Definition: GridHandler.inc.php:215
SectionGridHandler\activateSection
activateSection($args, $request)
Definition: SectionGridHandler.inc.php:329
SectionGridHandler\deleteSection
deleteSection($args, $request)
Definition: SectionGridHandler.inc.php:227
PKPHandler\setupTemplate
setupTemplate($request)
Definition: PKPHandler.inc.php:466
SectionGridHandler\setDataElementSequence
setDataElementSequence($request, $rowId, $gridDataElement, $newSequence)
Definition: SectionGridHandler.inc.php:160
NotificationManager
Definition: NotificationManager.inc.php:19