Open Journal Systems  3.3.0
NotificationsGridHandler.inc.php
1 <?php
2 
16 // Import UI base classes.
17 import('lib.pkp.classes.controllers.grid.GridHandler');
18 
19 // Other classes associated with this grid
20 import('lib.pkp.controllers.grid.notifications.NotificationsGridCellProvider');
21 
25 
26 
30  function initialize($request, $args = null) {
31  parent::initialize($request, $args);
32  AppLocale::requireComponents(LOCALE_COMPONENT_APP_SUBMISSION, LOCALE_COMPONENT_PKP_SUBMISSION);
33 
34  $this->_selectedNotificationIds = (array) $request->getUserVar('selectedNotificationIds');
35 
36  $cellProvider = new NotificationsGridCellProvider();
37  $this->addColumn(
38  new GridColumn(
39  'task',
41  null,
42  null,
43  $cellProvider,
44  array('anyhtml' => true,
45  'alignment' => COLUMN_ALIGNMENT_LEFT)
46  )
47  );
48 
49  // Set the no items row text
50  $this->setEmptyRowText('grid.noItems');
51 
52  import('lib.pkp.classes.linkAction.request.NullAction');
53  $this->addAction(
54  new LinkAction(
55  'markNew',
56  new NullAction(),
57  __('grid.action.markNew'),
58  'edit' // FIXME: Icon
59  ),
60  GRID_ACTION_POSITION_BELOW
61  );
62  $this->addAction(
63  new LinkAction(
64  'markRead',
65  new NullAction(),
66  __('grid.action.markRead'),
67  'edit' // FIXME: Icon
68  ),
69  GRID_ACTION_POSITION_BELOW
70  );
71 
72  $router = $request->getRouter();
73  $this->addAction(
74  new LinkAction(
75  'deleteNotification',
76  new NullAction(),
77  __('grid.action.delete'),
78  'delete'
79  ),
80  GRID_ACTION_POSITION_BELOW
81  );
82  }
83 
84 
85  //
86  // Overridden methods from GridHandler
87  //
91  public function getJSHandler() {
92  return '$.pkp.controllers.grid.notifications.NotificationsGridHandler';
93  }
94 
98  function setUrls($request, $extraUrls = array()) {
99  $router = $request->getRouter();
100  parent::setUrls(
101  $request,
102  array_merge(
103  $extraUrls,
104  array(
105  'markNewUrl' => $router->url($request, null, null, 'markNew', null, $this->getRequestArgs()),
106  'markReadUrl' => $router->url($request, null, null, 'markRead', null, $this->getRequestArgs()),
107  'deleteUrl' => $router->url($request, null, null, 'deleteNotifications', null, $this->getRequestArgs()),
108  )
109  )
110  );
111  }
112 
118  function getPublishChangeEvents() {
119  return array('updateUnreadNotificationsCount');
120  }
121 
125  function initFeatures($request, $args) {
126  import('lib.pkp.classes.controllers.grid.feature.selectableItems.SelectableItemsFeature');
127  import('lib.pkp.classes.controllers.grid.feature.PagingFeature');
128  return array(new SelectableItemsFeature(), new PagingFeature());
129  }
130 
134  function getSelectName() {
135  return 'selectedNotifications';
136  }
137 
141  function isDataElementSelected($gridDataElement) {
142  return in_array($gridDataElement->getId(), $this->_selectedNotificationIds);
143  }
144 
145 
146  //
147  // Protected methods.
148  //
153  protected function getNotificationsColumnTitle() {
154  return 'common.tasks';
155  }
156 
157 
158  //
159  // Public methods
160  //
167  function markNew($args, $request) {
168  $notificationDao = DAORegistry::getDAO('NotificationDAO'); /* @var $notificationDao NotificationDAO */
169  $user = $request->getUser();
170 
171  $selectedElements = (array) $request->getUserVar('selectedElements');
172  foreach ($selectedElements as $notificationId) {
173  if ($notificationDao->getById($notificationId, $user->getId())) {
174  $notificationDao->setDateRead($notificationId, null);
175  }
176  }
177 
178  $json = DAO::getDataChangedEvent(null, null, $selectedElements);
179  $json->setGlobalEvent('update:unread-tasks-count', ['count' => $this->getUnreadNotificationsCount($user)]);
180  return $json;
181  }
182 
189  function markRead($args, $request) {
190  $notificationDao = DAORegistry::getDAO('NotificationDAO'); /* @var $notificationDao NotificationDAO */
191  $user = $request->getUser();
192 
193  $selectedElements = (array) $request->getUserVar('selectedElements');
194  foreach ($selectedElements as $notificationId) {
195  if ($notification = $notificationDao->getById($notificationId, $user->getId())) {
196  $notificationDao->setDateRead($notificationId, Core::getCurrentDate());
197  }
198  }
199  if ($request->getUserVar('redirect')) {
200  // In this case, the user has clicked on a notification
201  // and wants to view it. Mark it read first and redirect
202  $notificationMgr = new NotificationManager();
203  return $request->redirectUrlJson($notificationMgr->getNotificationUrl($request, $notification));
204  } else {
205  // The notification has been marked read explicitly.
206  // Update its status in the grid.
207  $json = DAO::getDataChangedEvent(null, null, $selectedElements);
208  $json->setGlobalEvent('update:unread-tasks-count', ['count' => $this->getUnreadNotificationsCount($user)]);
209  return $json;
210  }
211  }
212 
219  function deleteNotifications($args, $request) {
220  $notificationDao = DAORegistry::getDAO('NotificationDAO'); /* @var $notificationDao NotificationDAO */
221  $user = $request->getUser();
222 
223  $selectedElements = (array) $request->getUserVar('selectedElements');
224  foreach ($selectedElements as $notificationId) {
225  if ($notification = $notificationDao->getById($notificationId, $user->getId())) {
226  $notificationDao->deleteObject($notification);
227  }
228  }
229  $json = DAO::getDataChangedEvent(null, null, $selectedElements);
230  $json->setGlobalEvent('update:unread-tasks-count', ['count' => $this->getUnreadNotificationsCount($user)]);
231  return $json;
232  }
233 
240  function getUnreadNotificationsCount($user) {
241  $notificationDao = DAORegistry::getDAO('NotificationDAO'); /* @var $notificationDao NotificationDAO */
242  return (int) $notificationDao->getNotificationCount(false, $user->getId(), null, NOTIFICATION_LEVEL_TASK);
243  }
244 }
245 
246 
NotificationsGridHandler\deleteNotifications
deleteNotifications($args, $request)
Definition: NotificationsGridHandler.inc.php:222
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
NotificationsGridHandler\initFeatures
initFeatures($request, $args)
Definition: NotificationsGridHandler.inc.php:128
DAORegistry\getDAO
static & getDAO($name, $dbconn=null)
Definition: DAORegistry.inc.php:57
NotificationsGridHandler\getPublishChangeEvents
getPublishChangeEvents()
Definition: NotificationsGridHandler.inc.php:121
NotificationsGridHandler\initialize
initialize($request, $args=null)
Definition: NotificationsGridHandler.inc.php:33
NotificationsGridCellProvider
Class for a cell provider that can retrieve labels from notifications.
Definition: NotificationsGridCellProvider.inc.php:20
NotificationsGridHandler
Handle the display of notifications for a given user.
Definition: NotificationsGridHandler.inc.php:22
NotificationsGridHandler\setUrls
setUrls($request, $extraUrls=array())
Definition: NotificationsGridHandler.inc.php:101
NotificationsGridHandler\getJSHandler
getJSHandler()
Definition: NotificationsGridHandler.inc.php:94
GridHandler\addAction
addAction($action, $position=GRID_ACTION_POSITION_ABOVE)
Definition: GridHandler.inc.php:266
GridHandler\addColumn
addColumn($column)
Definition: GridHandler.inc.php:335
NotificationsGridHandler\$_selectedNotificationIds
$_selectedNotificationIds
Definition: NotificationsGridHandler.inc.php:27
NotificationsGridHandler\markRead
markRead($args, $request)
Definition: NotificationsGridHandler.inc.php:192
DAO\getDataChangedEvent
static getDataChangedEvent($elementId=null, $parentElementId=null, $content='')
Definition: DAO.inc.php:647
NotificationsGridHandler\isDataElementSelected
isDataElementSelected($gridDataElement)
Definition: NotificationsGridHandler.inc.php:144
NullAction
This action does nothing.
Definition: NullAction.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
SelectableItemsFeature
Implements grid widgets selectable items functionality.
Definition: SelectableItemsFeature.inc.php:19
PagingFeature
Add paging functionality to grids.
Definition: PagingFeature.inc.php:19
NotificationsGridHandler\getUnreadNotificationsCount
getUnreadNotificationsCount($user)
Definition: NotificationsGridHandler.inc.php:243
Core\getCurrentDate
static getCurrentDate($ts=null)
Definition: Core.inc.php:63
GridHandler
This class defines basic operations for handling HTML grids. Grids are used to implement a standardiz...
Definition: GridHandler.inc.php:58
NotificationManager
Definition: NotificationManager.inc.php:19
NotificationsGridHandler\getSelectName
getSelectName()
Definition: NotificationsGridHandler.inc.php:137
NotificationsGridHandler\markNew
markNew($args, $request)
Definition: NotificationsGridHandler.inc.php:170
NotificationsGridHandler\getNotificationsColumnTitle
getNotificationsColumnTitle()
Definition: NotificationsGridHandler.inc.php:156