Open Monograph Press  1.1
 All Classes Namespaces Functions Variables Groups Pages
MarketsGridHandler.inc.php
1 <?php
2 
16 // import grid base classes
17 import('lib.pkp.classes.controllers.grid.GridHandler');
18 
19 // import format grid specific classes
20 import('controllers.grid.catalogEntry.MarketsGridCellProvider');
21 import('controllers.grid.catalogEntry.MarketsGridRow');
22 
23 // Link action & modal classes
24 import('lib.pkp.classes.linkAction.request.AjaxModal');
25 
28  var $_monograph;
29 
31  var $_publicationFormat;
32 
36  function MarketsGridHandler() {
37  parent::GridHandler();
38  $this->addRoleAssignment(
39  array(ROLE_ID_MANAGER),
40  array('fetchGrid', 'fetchRow', 'addMarket', 'editMarket',
41  'updateMarket', 'deleteMarket'));
42  }
43 
44 
45  //
46  // Getters/Setters
47  //
52  function getMonograph() {
53  return $this->_monograph;
54  }
55 
60  function setMonograph($monograph) {
61  $this->_monograph = $monograph;
62  }
63 
68  function getPublicationFormat() {
69  return $this->_publicationFormat;
70  }
71 
76  function setPublicationFormat($publicationFormat) {
77  $this->_publicationFormat = $publicationFormat;
78  }
79 
80  //
81  // Overridden methods from PKPHandler
82  //
89  function authorize($request, &$args, $roleAssignments) {
90  import('classes.security.authorization.SubmissionAccessPolicy');
91  $this->addPolicy(new SubmissionAccessPolicy($request, $args, $roleAssignments));
92  return parent::authorize($request, $args, $roleAssignments);
93  }
94 
95  /*
96  * Configure the grid
97  * @param $request PKPRequest
98  */
99  function initialize($request) {
100  parent::initialize($request);
101 
102  // Retrieve the authorized monograph.
103  $monograph = $this->getAuthorizedContextObject(ASSOC_TYPE_MONOGRAPH);
104  $this->setMonograph($monograph);
105  $publicationFormatDao = DAORegistry::getDAO('PublicationFormatDAO');
106  $publicationFormatId = null;
107 
108  // Retrieve the associated publication format for this grid.
109  $marketId = (int) $request->getUserVar('marketId'); // set if editing or deleting a market entry
110 
111  if ($marketId != '') {
112  $marketDao = DAORegistry::getDAO('MarketDAO');
113  $market = $marketDao->getById($marketId, $monograph->getId());
114  if ($market) {
115  $publicationFormatId = $market->getPublicationFormatId();
116  }
117  } else { // empty form for new Market
118  $publicationFormatId = (int) $request->getUserVar('publicationFormatId');
119  }
120 
121  $publicationFormat = $publicationFormatDao->getById($publicationFormatId, $monograph->getId());
122 
123  if ($publicationFormat) {
124  $this->setPublicationFormat($publicationFormat);
125  } else {
126  fatalError('The publication format is not assigned to authorized monograph!');
127  }
128 
129  // Load submission-specific translations
131  LOCALE_COMPONENT_APP_SUBMISSION,
132  LOCALE_COMPONENT_PKP_SUBMISSION,
133  LOCALE_COMPONENT_PKP_USER,
134  LOCALE_COMPONENT_APP_DEFAULT,
135  LOCALE_COMPONENT_PKP_DEFAULT
136  );
137 
138  // Basic grid configuration
139  $this->setTitle('grid.catalogEntry.markets');
140 
141  // Grid actions
142  $router = $request->getRouter();
143  $actionArgs = $this->getRequestArgs();
144  $this->addAction(
145  new LinkAction(
146  'addMarket',
147  new AjaxModal(
148  $router->url($request, null, null, 'addMarket', null, $actionArgs),
149  __('grid.action.addMarket'),
150  'modal_add_item'
151  ),
152  __('grid.action.addMarket'),
153  'add_item'
154  )
155  );
156 
157  // Columns
158  $cellProvider = new MarketsGridCellProvider();
159  $this->addColumn(
160  new GridColumn(
161  'territory',
162  'grid.catalogEntry.marketTerritory',
163  null,
164  'controllers/grid/gridCell.tpl',
165  $cellProvider
166  )
167  );
168  $this->addColumn(
169  new GridColumn(
170  'rep',
171  'grid.catalogEntry.representatives',
172  null,
173  'controllers/grid/gridCell.tpl',
174  $cellProvider
175  )
176  );
177  $this->addColumn(
178  new GridColumn(
179  'price',
180  'monograph.publicationFormat.price',
181  null,
182  'controllers/grid/gridCell.tpl',
183  $cellProvider
184  )
185  );
186  }
187 
188 
189  //
190  // Overridden methods from GridHandler
191  //
196  function getRowInstance() {
197  return new MarketsGridRow($this->getMonograph());
198  }
199 
205  function getRequestArgs() {
206  $monograph = $this->getMonograph();
207  $publicationFormat = $this->getPublicationFormat();
208 
209  return array(
210  'submissionId' => $monograph->getId(),
211  'publicationFormatId' => $publicationFormat->getId()
212  );
213  }
214 
218  function loadData($request, $filter = null) {
219  $publicationFormat = $this->getPublicationFormat();
220  $marketDao = DAORegistry::getDAO('MarketDAO');
221  $data = $marketDao->getByPublicationFormatId($publicationFormat->getId());
222  return $data->toArray();
223  }
225 
226  //
227  // Public Market Grid Actions
228  //
234  function addMarket($args, $request) {
235  return $this->editMarket($args, $request);
236  }
237 
244  function editMarket($args, $request) {
245  // Identify the market entry to be updated
246  $marketId = (int) $request->getUserVar('marketId');
247  $monograph = $this->getMonograph();
248 
249  $marketDao = DAORegistry::getDAO('MarketDAO');
250  $market = $marketDao->getById($marketId, $monograph->getId());
251 
252  // Form handling
253  import('controllers.grid.catalogEntry.form.MarketForm');
254  $marketForm = new MarketForm($monograph, $market);
255  $marketForm->initData();
256 
257  $json = new JSONMessage(true, $marketForm->fetch($request));
258  return $json->getString();
259  }
260 
267  function updateMarket($args, $request) {
268  // Identify the market entry to be updated
269  $marketId = $request->getUserVar('marketId');
270  $monograph = $this->getMonograph();
271 
272  $marketDao = DAORegistry::getDAO('MarketDAO');
273  $market = $marketDao->getById($marketId, $monograph->getId());
274 
275  // Form handling
276  import('controllers.grid.catalogEntry.form.MarketForm');
277  $marketForm = new MarketForm($monograph, $market);
278  $marketForm->readInputData();
279  if ($marketForm->validate()) {
280  $marketId = $marketForm->execute();
281 
282  if(!isset($market)) {
283  // This is a new entry
284  $market = $marketDao->getById($marketId, $monograph->getId());
285  // New added entry action notification content.
286  $notificationContent = __('notification.addedMarket');
287  } else {
288  // entry edit action notification content.
289  $notificationContent = __('notification.editedMarket');
290  }
291 
292  // Create trivial notification.
293  $currentUser = $request->getUser();
294  $notificationMgr = new NotificationManager();
295  $notificationMgr->createTrivialNotification($currentUser->getId(), NOTIFICATION_TYPE_SUCCESS, array('contents' => $notificationContent));
296 
297  // Prepare the grid row data
298  $row = $this->getRowInstance();
299  $row->setGridId($this->getId());
300  $row->setId($marketId);
301  $row->setData($market);
302  $row->initialize($request);
303 
304  // Render the row into a JSON response
305  return DAO::getDataChangedEvent();
306 
307  } else {
308  $json = new JSONMessage(true, $marketForm->fetch($request));
309  return $json->getString();
310  }
311  }
312 
319  function deleteMarket($args, $request) {
320 
321  // Identify the markets entry to be deleted
322  $marketId = $request->getUserVar('marketId');
323 
324  $marketDao = DAORegistry::getDAO('MarketDAO');
325  $market = $marketDao->getById($marketId, $this->getMonograph()->getId());
326  if ($market != null) { // authorized
327 
328  $result = $marketDao->deleteObject($market);
329 
330  if ($result) {
331  $currentUser = $request->getUser();
332  $notificationMgr = new NotificationManager();
333  $notificationMgr->createTrivialNotification($currentUser->getId(), NOTIFICATION_TYPE_SUCCESS, array('contents' => __('notification.removedMarket')));
334  return DAO::getDataChangedEvent();
335  } else {
336  $json = new JSONMessage(false, __('manager.setup.errorDeletingItem'));
337  return $json->getString();
338  }
339  }
340  }
341 }
342 
343 ?>
static & getDAO($name, $dbconn=null)
addPolicy($authorizationPolicy, $addToTop=false)
Markets grid row definition.
static requireComponents()
addAction($action, $position=GRID_ACTION_POSITION_ABOVE)
addColumn($column)
Class defining basic operations for handling HTML grids.
Handle publication format grid requests for markets.
setPublicationFormat($publicationFormat)
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.
Form for adding/editing a market region entry.
static getDataChangedEvent($elementId=null, $parentElementId=null)
Definition: DAO.inc.php:606
updateMarket($args, $request)
loadData($request, $filter=null)
Base class for a cell provider that can retrieve labels for market regions.
Represents a column within a grid. It is used to configure the way cells within a column are displaye...
authorize($request, &$args, $roleAssignments)
& getAuthorizedContextObject($assocType)
Base class defining an action that can be performed by the user in the user interface.
deleteMarket($args, $request)