Open Monograph Press  3.3.0
RepresentativesGridHandler.inc.php
1 <?php
2 
16 // import grid base classes
17 import('lib.pkp.classes.controllers.grid.CategoryGridHandler');
18 
19 
20 // import format grid specific classes
21 import('controllers.grid.catalogEntry.RepresentativesGridCellProvider');
22 import('controllers.grid.catalogEntry.RepresentativesGridCategoryRow');
23 import('controllers.grid.catalogEntry.RepresentativesGridRow');
24 
25 // Link action & modal classes
26 import('lib.pkp.classes.linkAction.request.AjaxModal');
27 
30  var $_monograph;
31 
35  function __construct() {
36  parent::__construct();
37  $this->addRoleAssignment(
38  array(ROLE_ID_MANAGER, ROLE_ID_SUB_EDITOR, ROLE_ID_ASSISTANT),
39  array('fetchGrid', 'fetchCategory', 'fetchRow', 'addRepresentative', 'editRepresentative',
40  'updateRepresentative', 'deleteRepresentative'));
41  }
42 
43 
44  //
45  // Getters/Setters
46  //
51  function getMonograph() {
52  return $this->_monograph;
53  }
54 
59  function setMonograph($monograph) {
60  $this->_monograph = $monograph;
61  }
62 
63 
64  //
65  // Overridden methods from PKPHandler
66  //
73  function authorize($request, &$args, $roleAssignments) {
74  import('lib.pkp.classes.security.authorization.SubmissionAccessPolicy');
75  $this->addPolicy(new SubmissionAccessPolicy($request, $args, $roleAssignments));
76  return parent::authorize($request, $args, $roleAssignments);
77  }
78 
79  /*
80  * @copydoc CategoryGridHandler::initialize
81  */
82  function initialize($request, $args = null) {
83  parent::initialize($request, $args);
84 
85  // Retrieve the authorized monograph.
86  $this->setMonograph($this->getAuthorizedContextObject(ASSOC_TYPE_MONOGRAPH));
87 
88  $representativeId = (int) $request->getUserVar('representativeId'); // set if editing or deleting a representative entry
89 
90  if ($representativeId != '') {
91  $representativeDao = DAORegistry::getDAO('RepresentativeDAO'); /* @var $representativeDao RepresentativeDAO */
92  $representative = $representativeDao->getById($representativeId, $this->getMonograph()->getId());
93  if (!isset($representative)) {
94  fatalError('Representative referenced outside of authorized monograph context!');
95  }
96  }
97 
98  // Load submission-specific translations
100  LOCALE_COMPONENT_APP_SUBMISSION,
101  LOCALE_COMPONENT_PKP_SUBMISSION,
102  LOCALE_COMPONENT_PKP_USER,
103  LOCALE_COMPONENT_APP_DEFAULT,
104  LOCALE_COMPONENT_PKP_DEFAULT
105  );
106 
107  // Basic grid configuration
108  $this->setTitle('grid.catalogEntry.representatives');
109 
110  // Grid actions
111  $router = $request->getRouter();
112  $actionArgs = $this->getRequestArgs();
113  $this->addAction(
114  new LinkAction(
115  'addRepresentative',
116  new AjaxModal(
117  $router->url($request, null, null, 'addRepresentative', null, $actionArgs),
118  __('grid.action.addRepresentative'),
119  'modal_add_item'
120  ),
121  __('grid.action.addRepresentative'),
122  'add_item'
123  )
124  );
125 
126  // Columns
127  $cellProvider = new RepresentativesGridCellProvider();
128  $this->addColumn(
129  new GridColumn(
130  'name',
131  'grid.catalogEntry.representativeName',
132  null,
133  null,
134  $cellProvider
135  )
136  );
137  $this->addColumn(
138  new GridColumn(
139  'role',
140  'grid.catalogEntry.representativeRole',
141  null,
142  null,
143  $cellProvider
144  )
145  );
146  }
147 
148 
149  //
150  // Overridden methods from GridHandler
151  //
156  function getRowInstance() {
157  return new RepresentativesGridRow($this->getMonograph());
158  }
159 
164  function getCategoryRowInstance() {
165  return new RepresentativesGridCategoryRow();
166  }
167 
171  function loadCategoryData($request, &$category, $filter = null) {
172  $representativeDao = DAORegistry::getDAO('RepresentativeDAO'); /* @var $representativeDao RepresentativeDAO */
173  if ($category['isSupplier']) {
174  $representatives = $representativeDao->getSuppliersByMonographId($this->getMonograph()->getId());
175  } else {
176  $representatives = $representativeDao->getAgentsByMonographId($this->getMonograph()->getId());
177  }
178  return $representatives->toAssociativeArray();
179  }
180 
184  function getCategoryRowIdParameterName() {
185  return 'representativeCategoryId';
186  }
187 
191  function getRequestArgs() {
192  $monograph = $this->getMonograph();
193  return array_merge(
194  parent::getRequestArgs(),
195  array('submissionId' => $monograph->getId())
196  );
197  }
198 
202  function loadData($request, $filter = null) {
203  // set our labels for the two Representative categories
204  $categories = array(
205  array('name' => 'grid.catalogEntry.agentsCategory', 'isSupplier' => false),
206  array('name' => 'grid.catalogEntry.suppliersCategory', 'isSupplier' => true)
207  );
208 
209  return $categories;
210  }
211 
212 
213  //
214  // Public Representatives Grid Actions
215  //
216 
217  function addRepresentative($args, $request) {
218  return $this->editRepresentative($args, $request);
219  }
220 
227  function editRepresentative($args, $request) {
228  // Identify the representative entry to be updated
229  $representativeId = (int) $request->getUserVar('representativeId');
230  $monograph = $this->getMonograph();
231 
232  $representativeDao = DAORegistry::getDAO('RepresentativeDAO'); /* @var $representativeDao RepresentativeDAO */
233  $representative = $representativeDao->getById($representativeId, $monograph->getId());
234 
235  // Form handling
236  import('controllers.grid.catalogEntry.form.RepresentativeForm');
237  $representativeForm = new RepresentativeForm($monograph, $representative);
238  $representativeForm->initData();
239 
240  return new JSONMessage(true, $representativeForm->fetch($request));
241  }
242 
249  function updateRepresentative($args, $request) {
250  // Identify the representative entry to be updated
251  $representativeId = $request->getUserVar('representativeId');
252  $monograph = $this->getMonograph();
253 
254  $representativeDao = DAORegistry::getDAO('RepresentativeDAO'); /* @var $representativeDao RepresentativeDAO */
255  $representative = $representativeDao->getById($representativeId, $monograph->getId());
256 
257  // Form handling
258  import('controllers.grid.catalogEntry.form.RepresentativeForm');
259  $representativeForm = new RepresentativeForm($monograph, $representative);
260  $representativeForm->readInputData();
261  if ($representativeForm->validate()) {
262  $representativeId = $representativeForm->execute();
263 
264  if(!isset($representative)) {
265  // This is a new entry
266  $representative = $representativeDao->getById($representativeId, $monograph->getId());
267  // New added entry action notification content.
268  $notificationContent = __('notification.addedRepresentative');
269  } else {
270  // entry edit action notification content.
271  $notificationContent = __('notification.editedRepresentative');
272  }
273 
274  // Create trivial notification.
275  $currentUser = $request->getUser();
276  $notificationMgr = new NotificationManager();
277  $notificationMgr->createTrivialNotification($currentUser->getId(), NOTIFICATION_TYPE_SUCCESS, array('contents' => $notificationContent));
278 
279  // Prepare the grid row data
280  $row = $this->getRowInstance();
281  $row->setGridId($this->getId());
282  $row->setId($representativeId);
283  $row->setData($representative);
284  $row->initialize($request);
285 
286  // Render the row into a JSON response
287  return DAO::getDataChangedEvent($representativeId, (int) $representative->getIsSupplier());
288 
289  } else {
290  return new JSONMessage(true, $representativeForm->fetch($request));
291  }
292  }
293 
300  function deleteRepresentative($args, $request) {
301  \AppLocale::requireComponents(LOCALE_COMPONENT_PKP_MANAGER, LOCALE_COMPONENT_APP_MANAGER);
302 
303  // Identify the representative entry to be deleted
304  $representativeId = $request->getUserVar('representativeId');
305 
306  $representativeDao = DAORegistry::getDAO('RepresentativeDAO'); /* @var $representativeDao RepresentativeDAO */
307  $representative = $representativeDao->getById($representativeId, $this->getMonograph()->getId());
308 
309  if (!$representative) {
310  return new JSONMessage(false, __('api.404.resourceNotFound'));
311  }
312 
313  // Don't allow a representative to be deleted if they are associated
314  // with a publication format's market metadata
315  $submission = $this->getAuthorizedContextObject(ASSOC_TYPE_SUBMISSION);
316  foreach ($submission->getData('publications') as $publication) {
317  foreach ($publication->getData('publicationFormats') as $publicationFormat) {
318  $markets = DAORegistry::getDAO('MarketDAO')->getByPublicationFormatId($publicationFormat->getId())->toArray();
319  foreach ($markets as $market) {
320  if (in_array($representative->getId(), [$market->getAgentId(), $market->getSupplierId()])) {
321  return new JSONMessage(false, __('manager.representative.inUse'));
322  }
323  }
324  }
325  }
326 
327  $result = $representativeDao->deleteObject($representative);
328 
329  if ($result) {
330  $currentUser = $request->getUser();
331  $notificationMgr = new NotificationManager();
332  $notificationMgr->createTrivialNotification($currentUser->getId(), NOTIFICATION_TYPE_SUCCESS, array('contents' => __('notification.removedRepresentative')));
333  return DAO::getDataChangedEvent($representative->getId(), (int) $representative->getIsSupplier());
334  } else {
335  return new JSONMessage(false, __('manager.setup.errorDeletingItem'));
336  }
337  }
338 }
339 
340 
PKPHandler\addRoleAssignment
addRoleAssignment($roleIds, $operations)
Definition: PKPHandler.inc.php:213
RepresentativeForm
Form for adding/editing a representative entry.
Definition: RepresentativeForm.inc.php:18
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
RepresentativesGridHandler\getMonograph
getMonograph()
Definition: RepresentativesGridHandler.inc.php:54
DAORegistry\getDAO
static & getDAO($name, $dbconn=null)
Definition: DAORegistry.inc.php:57
RepresentativesGridHandler\addRepresentative
addRepresentative($args, $request)
Definition: RepresentativesGridHandler.inc.php:220
RepresentativesGridCategoryRow
Representatives grid category row definition.
Definition: RepresentativesGridCategoryRow.inc.php:21
RepresentativesGridHandler\getCategoryRowInstance
getCategoryRowInstance()
Definition: RepresentativesGridHandler.inc.php:167
PKPHandler\getId
getId()
Definition: PKPHandler.inc.php:107
RepresentativesGridCellProvider
Base class for a cell provider that can retrieve labels for representatives.
Definition: RepresentativesGridCellProvider.inc.php:18
RepresentativesGridHandler\editRepresentative
editRepresentative($args, $request)
Definition: RepresentativesGridHandler.inc.php:230
RepresentativesGridHandler\loadCategoryData
loadCategoryData($request, &$category, $filter=null)
Definition: RepresentativesGridHandler.inc.php:174
GridHandler\addAction
addAction($action, $position=GRID_ACTION_POSITION_ABOVE)
Definition: GridHandler.inc.php:266
GridHandler\addColumn
addColumn($column)
Definition: GridHandler.inc.php:335
RepresentativesGridHandler\deleteRepresentative
deleteRepresentative($args, $request)
Definition: RepresentativesGridHandler.inc.php:303
DAO\getDataChangedEvent
static getDataChangedEvent($elementId=null, $parentElementId=null, $content='')
Definition: DAO.inc.php:647
RepresentativesGridHandler\setMonograph
setMonograph($monograph)
Definition: RepresentativesGridHandler.inc.php:62
RepresentativesGridRow
Representatives grid row definition.
Definition: RepresentativesGridRow.inc.php:18
RepresentativesGridHandler\initialize
initialize($request, $args=null)
Definition: RepresentativesGridHandler.inc.php:85
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
GridHandler\setTitle
setTitle($title)
Definition: GridHandler.inc.php:215
RepresentativesGridHandler\loadData
loadData($request, $filter=null)
Definition: RepresentativesGridHandler.inc.php:205
RepresentativesGridHandler\getRequestArgs
getRequestArgs()
Definition: RepresentativesGridHandler.inc.php:194
PKPHandler\getAuthorizedContextObject
& getAuthorizedContextObject($assocType)
Definition: PKPHandler.inc.php:174
RepresentativesGridHandler
Handle publication format grid requests for representatives.
Definition: RepresentativesGridHandler.inc.php:28
NotificationManager
Definition: NotificationManager.inc.php:19
RepresentativesGridHandler\getRowInstance
getRowInstance()
Definition: RepresentativesGridHandler.inc.php:159
RepresentativesGridHandler\updateRepresentative
updateRepresentative($args, $request)
Definition: RepresentativesGridHandler.inc.php:252
SubmissionAccessPolicy
Base class to control (write) access to submissions and (read) access to submission details in OMP.
Definition: SubmissionAccessPolicy.inc.php:19
PKPHandler\addPolicy
addPolicy($authorizationPolicy, $addToTop=false)
Definition: PKPHandler.inc.php:157
fatalError
if(!function_exists('import')) fatalError($reason)
Definition: functions.inc.php:32
RepresentativesGridHandler\__construct
__construct()
Definition: RepresentativesGridHandler.inc.php:38
RepresentativesGridHandler\$_monograph
$_monograph
Definition: RepresentativesGridHandler.inc.php:33
RepresentativesGridHandler\authorize
authorize($request, &$args, $roleAssignments)
Definition: RepresentativesGridHandler.inc.php:76
RepresentativesGridHandler\getCategoryRowIdParameterName
getCategoryRowIdParameterName()
Definition: RepresentativesGridHandler.inc.php:187