Open Journal Systems  3.3.0
NotificationHandler.inc.php
1 <?php
2 
16 import('classes.handler.Handler');
17 import('classes.notification.Notification');
18 
20 
27  function fetchNotification($args, $request) {
28  $this->setupTemplate($request);
29  $user = $request->getUser();
30  $userId = $user?$user->getId():null;
31  $context = $request->getContext();
32  $notificationDao = DAORegistry::getDAO('NotificationDAO'); /* @var $notificationDao NotificationDAO */
33  $notifications = array();
34 
35  // Get the notification options from request.
36  $notificationOptions = $request->getUserVar('requestOptions');
37 
38  if (!$user) {
39  $notifications = array();
40  } elseif (is_array($notificationOptions)) {
41  // Retrieve the notifications.
42  $notifications = $this->_getNotificationsByOptions($notificationOptions, $context->getId(), $userId);
43  } else {
44  // No options, get only TRIVIAL notifications.
45  $notifications = $notificationDao->getByUserId($userId, NOTIFICATION_LEVEL_TRIVIAL);
46  $notifications = $notifications->toArray();
47  }
48 
49  import('lib.pkp.classes.core.JSONMessage');
50  $json = new JSONMessage();
51 
52  if (is_array($notifications) && !empty($notifications)) {
53  $formattedNotificationsData = array();
54  $notificationManager = new NotificationManager();
55 
56  // Format in place notifications.
57  $formattedNotificationsData['inPlace'] = $notificationManager->formatToInPlaceNotification($request, $notifications);
58 
59  // Format general notifications.
60  $formattedNotificationsData['general'] = $notificationManager->formatToGeneralNotification($request, $notifications);
61 
62  // Delete trivial notifications from database.
63  $notificationManager->deleteTrivialNotifications($notifications);
64 
65  $json->setContent($formattedNotificationsData);
66  }
67 
68  return $json;
69  }
70 
78  function _getNotificationsByOptions($notificationOptions, $contextId, $userId = null) {
79  $notificationDao = DAORegistry::getDAO('NotificationDAO'); /* @var $notificationDao NotificationDAO */
80  $notificationsArray = array();
81  $notificationMgr = new NotificationManager();
82 
83  foreach ($notificationOptions as $level => $levelOptions) {
84  if ($levelOptions) {
85  foreach ($levelOptions as $type => $typeOptions) {
86  if ($typeOptions) {
87  $notificationMgr->isVisibleToAllUsers($type, $typeOptions['assocType'], $typeOptions['assocId']) ? $workingUserId = null : $workingUserId = $userId;
88  $notificationsResultFactory = $notificationDao->getByAssoc($typeOptions['assocType'], $typeOptions['assocId'], $workingUserId, $type, $contextId);
89  $notificationsArray = $this->_addNotificationsToArray($notificationsResultFactory, $notificationsArray);
90  } else {
91  if ($userId) {
92  $notificationsResultFactory = $notificationDao->getByUserId($userId, $level, $type, $contextId);
93  $notificationsArray = $this->_addNotificationsToArray($notificationsResultFactory, $notificationsArray);
94  }
95  }
96  }
97  } else {
98  if ($userId) {
99  $notificationsResultFactory = $notificationDao->getByUserId($userId, $level, null, $contextId);
100  $notificationsArray = $this->_addNotificationsToArray($notificationsResultFactory, $notificationsArray);
101  }
102  }
103  $notificationsResultFactory = null;
104  }
105 
106  return $notificationsArray;
107  }
108 
115  function _addNotificationsToArray($resultFactory, $notificationArray) {
116  if (!$resultFactory->wasEmpty()) {
117  $notificationArray = array_merge($notificationArray, $resultFactory->toArray());
118  }
119 
120  return $notificationArray;
121  }
122 
127  function setupTemplate($request) {
128  AppLocale::requireComponents(LOCALE_COMPONENT_PKP_GRID, LOCALE_COMPONENT_PKP_SUBMISSION);
129  parent::setupTemplate($request);
130  }
131 }
132 
133 
AppLocale\requireComponents
static requireComponents()
Definition: env1/MockAppLocale.inc.php:56
DAORegistry\getDAO
static & getDAO($name, $dbconn=null)
Definition: DAORegistry.inc.php:57
NotificationHandler
Handle requests for viewing notifications.
Definition: NotificationHandler.inc.php:19
NotificationHandler\_getNotificationsByOptions
_getNotificationsByOptions($notificationOptions, $contextId, $userId=null)
Definition: NotificationHandler.inc.php:78
JSONMessage
Class to represent a JSON (Javascript Object Notation) message.
Definition: JSONMessage.inc.php:18
NotificationHandler\fetchNotification
fetchNotification($args, $request)
Definition: NotificationHandler.inc.php:27
NotificationHandler\_addNotificationsToArray
_addNotificationsToArray($resultFactory, $notificationArray)
Definition: NotificationHandler.inc.php:115
NotificationManager
Definition: NotificationManager.inc.php:19
NotificationHandler\setupTemplate
setupTemplate($request)
Definition: NotificationHandler.inc.php:127
Handler
Base request handler application class.
Definition: Handler.inc.php:18