Open Monograph Press  3.3.0
ManageSpotlightsGridHandler.inc.php
1 <?php
2 
16 // import grid base classes
17 import('lib.pkp.classes.controllers.grid.GridHandler');
18 
19 
20 // import format grid specific classes
21 import('controllers.grid.content.spotlights.SpotlightsGridCellProvider');
22 import('controllers.grid.content.spotlights.SpotlightsGridRow');
23 import('controllers.grid.content.spotlights.form.SpotlightForm');
24 
25 // import Spotlight class for class constants
26 import('classes.spotlight.Spotlight');
27 
28 // Link action & modal classes
29 import('lib.pkp.classes.linkAction.request.AjaxModal');
30 
32 
36  var $_press;
37 
41  function __construct() {
42  parent::__construct();
43  $this->addRoleAssignment(
44  array(ROLE_ID_MANAGER),
45  array('fetchGrid', 'fetchRow', 'addSpotlight', 'editSpotlight',
46  'updateSpotlight', 'deleteSpotlight', 'itemAutocomplete'));
47  }
48 
49  //
50  // Getters/Setters
51  //
56  function &getPress() {
57  return $this->_press;
58  }
59 
64  function setPress($press) {
65  $this->_press =& $press;
66  }
67 
68  //
69  // Overridden methods from PKPHandler
70  //
77  function authorize($request, &$args, $roleAssignments) {
78  import('lib.pkp.classes.security.authorization.ContextAccessPolicy');
79  $this->addPolicy(new ContextAccessPolicy($request, $roleAssignments));
80  $returner = parent::authorize($request, $args, $roleAssignments);
81 
82  $spotlightId = $request->getUserVar('spotlightId');
83  if ($spotlightId) {
84  $press = $request->getPress();
85  $spotlightDao = DAORegistry::getDAO('SpotlightDAO'); /* @var $spotlightDao SpotlightDAO */
86  $spotlight = $spotlightDao->getById($spotlightId);
87  if ($spotlight == null || $spotlight->getPressId() != $press->getId()) {
88  return false;
89  }
90  }
91 
92  return $returner;
93  }
94 
98  function initialize($request, $args = null) {
99  parent::initialize($request, $args);
100 
101  // Load locale components.
102  AppLocale::requireComponents(LOCALE_COMPONENT_APP_SUBMISSION, LOCALE_COMPONENT_APP_MANAGER);
103 
104  // Basic grid configuration
105  $this->setTitle('spotlight.spotlights');
106 
107  // Set the no items row text
108  $this->setEmptyRowText('spotlight.noneExist');
109 
110  $press = $request->getPress();
111  $this->setPress($press);
112 
113  // Columns
114  import('controllers.grid.content.spotlights.SpotlightsGridCellProvider');
115  $spotlightsGridCellProvider = new SpotlightsGridCellProvider();
116  $this->addColumn(
117  new GridColumn('title',
118  'grid.content.spotlights.form.title',
119  null,
120  null,
121  $spotlightsGridCellProvider,
122  array('width' => 40)
123  )
124  );
125 
126  $this->addColumn(
127  new GridColumn('itemTitle',
128  'grid.content.spotlights.spotlightItemTitle',
129  null,
130  null,
131  $spotlightsGridCellProvider,
132  array('width' => 40)
133  )
134  );
135 
136  $this->addColumn(
137  new GridColumn('type',
138  'common.type',
139  null,
140  null,
141  $spotlightsGridCellProvider
142  )
143  );
144 
145  // Add grid action.
146  $router = $request->getRouter();
147  import('lib.pkp.classes.linkAction.request.AjaxModal');
148  $this->addAction(
149  new LinkAction(
150  'addSpotlight',
151  new AjaxModal(
152  $router->url($request, null, null, 'addSpotlight', null, null),
153  __('grid.action.addSpotlight'),
154  'modal_add_item'
155  ),
156  __('grid.action.addSpotlight'),
157  'add_item')
158  );
159  }
160 
161 
162  //
163  // Overridden methods from GridHandler
164  //
169  function getRowInstance() {
170  return new SpotlightsGridRow($this->getPress());
171  }
172 
176  function loadData($request, $filter = null) {
177 
178  $spotlightDao = DAORegistry::getDAO('SpotlightDAO'); /* @var $spotlightDao SpotlightDAO */
179  $press = $this->getPress();
180  return $spotlightDao->getByPressId($press->getId());
181  }
182 
188  function getRequestArgs() {
189  $press = $this->getPress();
190  return array(
191  'pressId' => $press->getId()
192  );
193  }
194 
195  //
196  // Public Spotlights Grid Actions
197  //
198 
199  function addSpotlight($args, $request) {
200  return $this->editSpotlight($args, $request);
201  }
202 
209  function editSpotlight($args, $request) {
210  $spotlightId = (int)$request->getUserVar('spotlightId');
211  $press = $request->getPress();
212  $pressId = $press->getId();
213 
214  $spotlightForm = new SpotlightForm($pressId, $spotlightId);
215  $spotlightForm->initData();
216 
217  return new JSONMessage(true, $spotlightForm->fetch($request));
218  }
219 
226  function updateSpotlight($args, $request) {
227  // Identify the spotlight entry to be updated
228  $spotlightId = $request->getUserVar('spotlightId');
229 
230  $press = $this->getPress();
231 
232  $spotlightDao = DAORegistry::getDAO('SpotlightDAO'); /* @var $spotlightDao SpotlightDAO */
233  $spotlight = $spotlightDao->getById($spotlightId, $press->getId());
234 
235  // Form handling
236  $spotlightForm = new SpotlightForm($press->getId(), $spotlightId);
237 
238  $spotlightForm->readInputData();
239  if ($spotlightForm->validate()) {
240  $spotlightId = $spotlightForm->execute();
241 
242  if(!isset($spotlight)) {
243  // This is a new entry
244  $spotlight = $spotlightDao->getById($spotlightId, $press->getId());
245  // New added entry action notification content.
246  $notificationContent = __('notification.addedSpotlight');
247  } else {
248  // entry edit action notification content.
249  $notificationContent = __('notification.editedSpotlight');
250  }
251 
252  // Create trivial notification.
253  $currentUser = $request->getUser();
254  $notificationMgr = new NotificationManager();
255  $notificationMgr->createTrivialNotification($currentUser->getId(), NOTIFICATION_TYPE_SUCCESS, array('contents' => $notificationContent));
256 
257  // Prepare the grid row data
258  $row = $this->getRowInstance();
259  $row->setGridId($this->getId());
260  $row->setId($spotlightId);
261  $row->setData($spotlight);
262  $row->initialize($request);
263 
264  // Render the row into a JSON response
265  return DAO::getDataChangedEvent();
266 
267  } else {
268  return new JSONMessage(true, $spotlightForm->fetch($request));
269  }
270  }
271 
278  function deleteSpotlight($args, $request) {
279 
280  // Identify the entry to be deleted
281  $spotlightId = $request->getUserVar('spotlightId');
282 
283  $spotlightDao = DAORegistry::getDAO('SpotlightDAO'); /* @var $spotlightDao SpotlightDAO */
284  $press = $this->getPress();
285  $spotlight = $spotlightDao->getById($spotlightId, $press->getId());
286  if ($spotlight != null) { // authorized
287 
288  $result = $spotlightDao->deleteObject($spotlight);
289 
290  if ($result) {
291  $currentUser = $request->getUser();
292  $notificationMgr = new NotificationManager();
293  $notificationMgr->createTrivialNotification($currentUser->getId(), NOTIFICATION_TYPE_SUCCESS, array('contents' => __('notification.removedSpotlight')));
294  return DAO::getDataChangedEvent();
295  } else {
296  return new JSONMessage(false, __('manager.setup.errorDeletingItem'));
297  }
298  }
299  }
300 
308  function itemAutocomplete($args, $request) {
309  $name = $request->getUserVar('name');
310  $press = $this->getPress();
311  $itemList = array();
312 
313  // get the items that match.
314  $matches = array();
315 
316  import('lib.pkp.classes.submission.PKPSubmission'); // STATUS_PUBLISHED
317  $args = [
318  'status' => STATUS_PUBLISHED,
319  'contextId' => $press->getId(),
320  'count' => 100
321  ];
322 
323  if ($name) {
324  $args['searchPhrase'] = $name;
325  }
326 
327  $submissionsIterator = Services::get('submission')->getMany($args);
328  foreach ($submissionsIterator as $submission) {
329  $matches[] = array('label' => $submission->getLocalizedTitle(), 'value' => $submission->getId() . ':' . SPOTLIGHT_TYPE_BOOK);
330  }
331 
332  if (!empty($matches)) {
333  $itemList[] = array('label' => PKPString::strtoupper(__('submission.monograph')), 'value' => '');
334  $itemList = array_merge($itemList, $matches);
335  }
336 
337  $matches = array();
338 
339  $seriesDao = DAORegistry::getDAO('SeriesDAO'); /* @var $seriesDao SeriesDAO */
340  $allSeries = $seriesDao->getByPressId($press->getId());
341  while ($series = $allSeries->next()) {
342  if ($name == '' || preg_match('/'. preg_quote($name, '/') . '/i', $series->getLocalizedTitle())) {
343  $matches[] = array('label' => $series->getLocalizedTitle(), 'value' => $series->getId() . ':' . SPOTLIGHT_TYPE_SERIES);
344  }
345  }
346 
347  if (!empty($matches)) {
348  $itemList[] = array('label' => PKPString::strtoupper(__('manager.series.book')), 'value' => '');
349  $itemList = array_merge($itemList, $matches);
350  }
351 
352  if (count($itemList) == 0) {
353  return $this->noAutocompleteResults();
354  }
355 
356  return new JSONMessage(true, $itemList);
357  }
358 }
359 
360 
PKPHandler\addRoleAssignment
addRoleAssignment($roleIds, $operations)
Definition: PKPHandler.inc.php:213
GridHandler\setEmptyRowText
setEmptyRowText($emptyRowText)
Definition: GridHandler.inc.php:231
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
ContextAccessPolicy
Class to control access to PKP applications' setup components.
Definition: ContextAccessPolicy.inc.php:17
DAORegistry\getDAO
static & getDAO($name, $dbconn=null)
Definition: DAORegistry.inc.php:57
ManageSpotlightsGridHandler\__construct
__construct()
Definition: ManageSpotlightsGridHandler.inc.php:44
PKPHandler\getId
getId()
Definition: PKPHandler.inc.php:107
ManageSpotlightsGridHandler\authorize
authorize($request, &$args, $roleAssignments)
Definition: ManageSpotlightsGridHandler.inc.php:80
ManageSpotlightsGridHandler\editSpotlight
editSpotlight($args, $request)
Definition: ManageSpotlightsGridHandler.inc.php:212
ManageSpotlightsGridHandler\loadData
loadData($request, $filter=null)
Definition: ManageSpotlightsGridHandler.inc.php:179
ManageSpotlightsGridHandler\initialize
initialize($request, $args=null)
Definition: ManageSpotlightsGridHandler.inc.php:101
SpotlightsGridRow
Spotlights grid row definition.
Definition: SpotlightsGridRow.inc.php:18
ManageSpotlightsGridHandler\getPress
& getPress()
Definition: ManageSpotlightsGridHandler.inc.php:59
GridHandler\addAction
addAction($action, $position=GRID_ACTION_POSITION_ABOVE)
Definition: GridHandler.inc.php:266
GridHandler\addColumn
addColumn($column)
Definition: GridHandler.inc.php:335
ManageSpotlightsGridHandler\getRequestArgs
getRequestArgs()
Definition: ManageSpotlightsGridHandler.inc.php:191
DAO\getDataChangedEvent
static getDataChangedEvent($elementId=null, $parentElementId=null, $content='')
Definition: DAO.inc.php:647
PKPString\strtoupper
static strtoupper($string)
Definition: PKPString.inc.php:178
GridHandler\noAutocompleteResults
noAutocompleteResults()
Definition: GridHandler.inc.php:942
JSONMessage
Class to represent a JSON (Javascript Object Notation) message.
Definition: JSONMessage.inc.php:18
ManageSpotlightsGridHandler\deleteSpotlight
deleteSpotlight($args, $request)
Definition: ManageSpotlightsGridHandler.inc.php:281
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
ManageSpotlightsGridHandler\$_press
$_press
Definition: ManageSpotlightsGridHandler.inc.php:39
SpotlightsGridCellProvider
Base class for a cell provider that can retrieve labels for spotlights.
Definition: SpotlightsGridCellProvider.inc.php:18
ManageSpotlightsGridHandler\updateSpotlight
updateSpotlight($args, $request)
Definition: ManageSpotlightsGridHandler.inc.php:229
GridHandler\setTitle
setTitle($title)
Definition: GridHandler.inc.php:215
ManageSpotlightsGridHandler\getRowInstance
getRowInstance()
Definition: ManageSpotlightsGridHandler.inc.php:172
GridHandler
This class defines basic operations for handling HTML grids. Grids are used to implement a standardiz...
Definition: GridHandler.inc.php:58
ManageSpotlightsGridHandler\addSpotlight
addSpotlight($args, $request)
Definition: ManageSpotlightsGridHandler.inc.php:202
SpotlightForm
Form for reading/creating/editing spotlight items.
Definition: SpotlightForm.inc.php:18
NotificationManager
Definition: NotificationManager.inc.php:19
ManageSpotlightsGridHandler\setPress
setPress($press)
Definition: ManageSpotlightsGridHandler.inc.php:67
PKPHandler\addPolicy
addPolicy($authorizationPolicy, $addToTop=false)
Definition: PKPHandler.inc.php:157
ManageSpotlightsGridHandler\itemAutocomplete
itemAutocomplete($args, $request)
Definition: ManageSpotlightsGridHandler.inc.php:311
ManageSpotlightsGridHandler
Definition: ManageSpotlightsGridHandler.inc.php:31
PKPServices\get
static get($service)
Definition: PKPServices.inc.php:49