Open Monograph Press  1.1
 All Classes Namespaces Functions Variables Groups Pages
NotificationHandler.inc.php
1 <?php
2 
16 import('classes.handler.Handler');
17 import('classes.notification.Notification');
18 
23  function NotificationHandler() {
24  parent::Handler();
25  }
26 
32  function index($args, $request) {
33  $this->validate();
34  $this->setupTemplate($request);
35  $templateMgr = TemplateManager::getManager($request);
36  $router = $request->getRouter();
37 
38  $user = $request->getUser();
39  if(isset($user)) {
40  $userId = $user->getId();
41  $templateMgr->assign('isUserLoggedIn', true);
42  } else {
43  $userId = 0;
44 
45  $templateMgr->assign('emailUrl', $router->url($request, null, 'notification', 'subscribeMailList'));
46  $templateMgr->assign('isUserLoggedIn', false);
47  }
48  $context = $request->getContext();
49  $contextId = isset($context)?$context->getId():null;
50 
51  $notificationManager = new NotificationManager();
52  $notificationDao = DAORegistry::getDAO('NotificationDAO');
53 
54  $rangeInfo = $this->getRangeInfo($request, 'notifications');
55 
56  // Construct the formatted notification string to display in the template
57  $formattedNotifications = $notificationManager->getFormattedNotificationsForUser($request, $userId, NOTIFICATION_LEVEL_NORMAL, $contextId, $rangeInfo);
58 
59  // Get the same notifications used for the string so we can paginate
60  $notifications = $notificationDao->getByUserId($userId, NOTIFICATION_LEVEL_NORMAL, null, $contextId, $rangeInfo);
61 
62  $notificationDao = DAORegistry::getDAO('NotificationDAO');
63  $templateMgr->assign('formattedNotifications', $formattedNotifications);
64  $templateMgr->assign('notifications', $notifications);
65  $templateMgr->assign('unread', $notificationDao->getNotificationCount(false, $userId, $contextId));
66  $templateMgr->assign('read', $notificationDao->getNotificationCount(true, $userId, $contextId));
67  $templateMgr->assign('url', $router->url($request, null, 'notification', 'settings'));
68  $templateMgr->display('notification/index.tpl');
69  }
70 
76  function delete($args, $request) {
77  $this->validate();
78 
79  $notificationId = array_shift($args);
80  if (array_shift($args) == 'ajax') {
81  $isAjax = true;
82  } else $isAjax = false;
83 
84  $user = $request->getUser();
85  if(isset($user)) {
86  $userId = (int) $user->getId();
87 
88  $notificationDao = DAORegistry::getDAO('NotificationDAO');
89  $notificationDao->deleteById($notificationId, $userId);
90  }
91 
92  if (!$isAjax) {
93  $router = $request->getRouter();
94  $request->redirectUrl($router->url($request, null, 'notification'));
95  }
96  }
97 
103  function settings($args, $request) {
104  $this->validate();
105  $this->setupTemplate($request);
106 
107 
108  $user = $request->getUser();
109  if(isset($user)) {
110  import('classes.notification.form.NotificationSettingsForm');
111  $notificationSettingsForm = new NotificationSettingsForm();
112  $notificationSettingsForm->display($request);
113  } else {
114  $router = $request->getRouter();
115  $request->redirectUrl($router->url($request, null, 'notification'));
116  }
117  }
118 
124  function saveSettings($args, $request) {
125  $this->validate();
126  $this->setupTemplate($request, true);
127 
128  import('classes.notification.form.NotificationSettingsForm');
129 
130  $notificationSettingsForm = new NotificationSettingsForm();
131  $notificationSettingsForm->readInputData();
132 
133  if ($notificationSettingsForm->validate()) {
134  $notificationSettingsForm->execute($request);
135  $router = $request->getRouter();
136  $request->redirectUrl($router->url($request, null, 'notification', 'settings'));
137  } else {
138  $notificationSettingsForm->display($request);
139  }
140  }
141 
147  function getNotificationFeedUrl($args, $request) {
148  $user = $request->getUser();
149  $router = $request->getRouter();
150  $context = $router->getContext($request);
151 
152  if(isset($user)) {
153  $userId = $user->getId();
154  } else {
155  $userId = 0;
156  }
157 
158  $notificationSubscriptionSettingsDao = DAORegistry::getDAO('NotificationSubscriptionSettingsDAO');
159  $feedType = array_shift($args);
160 
161  $token = $notificationSubscriptionSettingsDao->getRSSTokenByUserId($userId, $context->getId());
162 
163  if ($token) {
164  $request->redirectUrl($router->url($request, null, 'notification', 'notificationFeed', array($feedType, $token)));
165  } else {
166  $token = $notificationSubscriptionSettingsDao->insertNewRSSToken($userId, $context->getId());
167  $request->redirectUrl($router->url($request, null, 'notification', 'notificationFeed', array($feedType, $token)));
168  }
169  }
170 
176  function notificationFeed($args, $request) {
177  $type = array_shift($args);
178  $token = array_shift($args);
179 
180  $this->setupTemplate($request, true);
181 
182  $application = PKPApplication::getApplication();
183  $appName = $application->getNameKey();
184 
185  $site = $request->getSite();
186  $siteTitle = $site->getLocalizedTitle();
187 
188  $notificationSubscriptionSettingsDao = DAORegistry::getDAO('NotificationSubscriptionSettingsDAO');
189  $context = $request->getContext();
190  $userId = $notificationSubscriptionSettingsDao->getUserIdByRSSToken($token, $context->getId());
191 
192  // Make sure the feed type is specified and valid
193  $typeMap = array(
194  'rss' => 'rss.tpl',
195  'rss2' => 'rss2.tpl',
196  'atom' => 'atom.tpl'
197  );
198  $contentTypeMap = array(
199  'rss' => 'rssContent.tpl',
200  'rss2' => 'rss2Content.tpl',
201  'atom' => 'atomContent.tpl'
202  );
203  $mimeTypeMap = array(
204  'rss' => 'application/rdf+xml',
205  'rss2' => 'application/rss+xml',
206  'atom' => 'application/atom+xml'
207  );
208  if (!isset($typeMap[$type])) return false;
209 
210  $notificationManager = new NotificationManager();
211  $notifications = $notificationManager->getFormattedNotificationsForUser($request, $userId, NOTIFICATION_LEVEL_NORMAL, $context->getId(), null, 'notification/' . $contentTypeMap[$type]);
212 
213  $versionDao = DAORegistry::getDAO('VersionDAO');
214  $version = $versionDao->getCurrentVersion();
215 
216  $templateMgr = TemplateManager::getManager($request);
217  $templateMgr->assign('version', $version->getVersionString(false));
218  $templateMgr->assign('selfUrl', $request->getCompleteUrl());
219  $templateMgr->assign('locale', AppLocale::getPrimaryLocale());
220  $templateMgr->assign('appName', $appName);
221  $templateMgr->assign('siteTitle', $siteTitle);
222  $templateMgr->assign('formattedNotifications', $notifications);
223 
224  $templateMgr->display('notification/' . $typeMap[$type], $mimeTypeMap[$type]);
225 
226  return true;
227  }
228 
234  function subscribeMailList($args, $request) {
235  $this->setupTemplate($request);
236 
237  $user = $request->getUser();
238 
239  if(!isset($user)) {
240  import('lib.pkp.classes.notification.form.NotificationMailingListForm');
241  $notificationMailingListForm = new NotificationMailingListForm();
242  $notificationMailingListForm->display($request);
243  } else {
244  $router = $request->getRouter();
245  $request->redirectUrl($router->url($request, null, 'notification'));
246  }
247  }
248 
254  function saveSubscribeMailList($args, $request) {
255  $this->validate();
256  $this->setupTemplate($request, true);
257 
258  import('lib.pkp.classes.notification.form.NotificationMailingListForm');
259 
260  $notificationMailingListForm = new NotificationMailingListForm();
261  $notificationMailingListForm->readInputData();
262 
263  if ($notificationMailingListForm->validate()) {
264  $notificationMailingListForm->execute($request);
265  $router = $request->getRouter();
266  $request->redirectUrl($router->url($request, null, 'notification', 'mailListSubscribed', array('success')));
267  } else {
268  $notificationMailingListForm->display($request);
269  }
270  }
271 
277  function mailListSubscribed($args, $request) {
278  $this->setupTemplate($request);
279  $status = array_shift($args);
280  $templateMgr = TemplateManager::getManager($request);
281 
282  if ($status == 'success') {
283  $templateMgr->assign('status', 'subscribeSuccess');
284  } else {
285  $templateMgr->assign('status', 'subscribeError');
286  $templateMgr->assign('error', true);
287  }
288 
289  $templateMgr->display('notification/maillistSubscribed.tpl');
290  }
291 
297  function confirmMailListSubscription($args, $request) {
298  $this->setupTemplate($request);
299  $userToken = array_shift($args);
300 
301  $templateMgr = TemplateManager::getManager($request);
302  $templateMgr->assign('confirm', true);
303 
304  $context = $request->getContext();
305 
306  $notificationMailListDao = DAORegistry::getDAO('NotificationMailListDAO');
307  $settingId = $notificationMailListDao->getMailListIdByToken($userToken, $context->getId());
308 
309  if($settingId) {
310  $notificationMailListDao->confirmMailListSubscription($settingId);
311  $templateMgr->assign('status', 'confirmSuccess');
312  } else {
313  $templateMgr->assign('status', 'confirmError');
314  $templateMgr->assign('error', true);
315  }
316 
317  $templateMgr->display('notification/maillistSubscribed.tpl');
318  }
319 
325  function unsubscribeMailList($args, $request) {
326  $context = $request->getContext();
327 
328  $this->setupTemplate($request);
329  $templateMgr = TemplateManager::getManager($request);
330 
331  $userToken = array_shift($args);
332 
333  $notificationMailListDao = DAORegistry::getDAO('NotificationMailListDAO');
334  if(isset($userToken)) {
335  if($notificationMailListDao->unsubscribeGuest($userToken, $context->getId())) {
336  $templateMgr->assign('status', "unsubscribeSuccess");
337  $templateMgr->display('notification/maillistSubscribed.tpl');
338  } else {
339  $templateMgr->assign('status', "unsubscribeError");
340  $templateMgr->assign('error', true);
341  $templateMgr->display('notification/maillistSubscribed.tpl');
342  }
343  }
344  }
345 
353  function fetchNotification($args, $request) {
354  $this->setupTemplate($request);
355  $user = $request->getUser();
356  $context = $request->getContext();
357  $notificationDao = DAORegistry::getDAO('NotificationDAO');
358  $notifications = array();
359 
360  // Get the notification options from request.
361  $notificationOptions = $request->getUserVar('requestOptions');
362 
363  if (is_array($notificationOptions)) {
364  // Retrieve the notifications.
365  $notifications = $this->_getNotificationsByOptions($notificationOptions, $context->getId(), $user->getId());
366  } else {
367  // No options, get only TRIVIAL notifications.
368  $notifications = $notificationDao->getByUserId($user->getId(), NOTIFICATION_LEVEL_TRIVIAL);
369  $notifications = $notifications->toArray();
370  }
371 
372  import('lib.pkp.classes.core.JSONMessage');
373  $json = new JSONMessage();
374 
375  if (is_array($notifications) && !empty($notifications)) {
376  $formattedNotificationsData = array();
377  $notificationManager = new NotificationManager();
378 
379  // Format in place notifications.
380  $formattedNotificationsData['inPlace'] = $notificationManager->formatToInPlaceNotification($request, $notifications);
381 
382  // Format general notifications.
383  $formattedNotificationsData['general'] = $notificationManager->formatToGeneralNotification($request, $notifications);
384 
385  // Delete trivial notifications from database.
386  $notificationManager->deleteTrivialNotifications($notifications);
387 
388  $json->setContent($formattedNotificationsData);
389  }
390 
391  return $json->getString();
392  }
393 
401  function _getNotificationsByOptions($notificationOptions, $contextId, $userId = null) {
402  $notificationDao = DAORegistry::getDAO('NotificationDAO');
403  $notificationsArray = array();
404  $notificationMgr = new NotificationManager();
405 
406  foreach ($notificationOptions as $level => $levelOptions) {
407  if ($levelOptions) {
408  foreach ($levelOptions as $type => $typeOptions) {
409  if ($typeOptions) {
410  $notificationMgr->isVisibleToAllUsers($type, $typeOptions['assocType'], $typeOptions['assocId']) ? $workingUserId = null : $workingUserId = $userId;
411  $notificationsResultFactory = $notificationDao->getByAssoc($typeOptions['assocType'], $typeOptions['assocId'], $workingUserId, $type, $contextId);
412  $notificationsArray = $this->_addNotificationsToArray($notificationsResultFactory, $notificationsArray);
413  } else {
414  if ($userId) {
415  $notificationsResultFactory = $notificationDao->getByUserId($userId, $level, $type, $contextId);
416  $notificationsArray = $this->_addNotificationsToArray($notificationsResultFactory, $notificationsArray);
417  }
418  }
419  }
420  } else {
421  if ($userId) {
422  $notificationsResultFactory = $notificationDao->getByUserId($userId, $level, null, $contextId);
423  $notificationsArray = $this->_addNotificationsToArray($notificationsResultFactory, $notificationsArray);
424  }
425  }
426  $notificationsResultFactory = null;
427  }
428 
429  return $notificationsArray;
430  }
431 
438  function _addNotificationsToArray($resultFactory, $notificationArray) {
439  if (!$resultFactory->wasEmpty()) {
440  $notificationArray = array_merge($notificationArray, $resultFactory->toArray());
441  }
442 
443  return $notificationArray;
444  }
445 
450  function setupTemplate($request) {
451  AppLocale::requireComponents(LOCALE_COMPONENT_PKP_GRID, LOCALE_COMPONENT_PKP_SUBMISSION);
452  parent::setupTemplate($request);
453  }
454 }
455 
456 ?>
static & getDAO($name, $dbconn=null)
Form to subscribe to the notification mailing list.
static requireComponents()
static getPrimaryLocale()
confirmMailListSubscription($args, $request)
_getNotificationsByOptions($notificationOptions, $contextId, $userId=null)
mailListSubscribed($args, $request)
Class to represent a JSON (Javascript Object Notation) message.
_addNotificationsToArray($resultFactory, $notificationArray)
unsubscribeMailList($args, $request)
validate($requiredContexts=null, $request=null)
getNotificationFeedUrl($args, $request)
subscribeMailList($args, $request)
Base request handler application class.
Definition: Handler.inc.php:20
Handle requests for viewing notifications.
notificationFeed($args, $request)
fetchNotification($args, $request)
static getRangeInfo($request, $rangeName, $contextData=null)
Form to edit notification settings.
saveSubscribeMailList($args, $request)