Open Journal Systems  3.3.0
PKPNotificationOperationManager.inc.php
1 <?php
2 
20 import('classes.notification.Notification');
21 import('lib.pkp.classes.notification.INotificationInfoProvider');
22 
24 
25  //
26  // Implement INotificationInfoProvider with default values.
27  //
31  function getNotificationUrl($request, $notification) {
32  return null;
33  }
34 
38  function getNotificationMessage($request, $notification) {
39  return null;
40  }
41 
46  function getNotificationContents($request, $notification) {
47  return $this->getNotificationMessage($request, $notification);
48  }
49 
53  function getNotificationTitle($notification) {
54  return __('notification.notification');
55  }
56 
60  function getStyleClass($notification) {
61  return '';
62  }
63 
67  function getIconClass($notification) {
68  return '';
69  }
70 
74  function isVisibleToAllUsers($notificationType, $assocType, $assocId) {
75  return false;
76  }
77 
78 
79  //
80  // Notification manager operations.
81  //
92  public function getFormattedNotificationsForUser($request, $userId, $level = NOTIFICATION_LEVEL_NORMAL, $contextId = null, $rangeInfo = null, $notificationTemplate = 'notification/notification.tpl') {
93  $notificationDao = DAORegistry::getDAO('NotificationDAO'); /* @var $notificationDao NotificationDAO */
94  $notifications = $notificationDao->getByUserId($userId, $level, null, $contextId, $rangeInfo);
95 
96  return $this->formatNotifications($request, $notifications, $notificationTemplate);
97  }
98 
107  public function getParamsForCurrentLocale($params) {
108  $locale = AppLocale::getLocale();
109  $primaryLocale = AppLocale::getPrimaryLocale();
110 
111  $localizedParams = array();
112  foreach ($params as $name => $value) {
113  if (!is_array($value)) {
114  // Non-localized text
115  $localizedParams[$name] = $value;
116  } elseif (isset($value[$locale])) {
117  // Check if the parameter is in the user's current locale
118  $localizedParams[$name] = $value[$locale];
119  } elseif (isset($value[$primaryLocale])) {
120  // Check if the parameter is in the default site locale
121  $localizedParams[$name] = $value[$primaryLocale];
122  } else {
123  // Otherwise, iterate over all supported locales and return the first match
124  $locales = AppLocale::getSupportedLocales();
125  foreach ($locales as $localeKey) {
126  if (isset($value[$localeKey])) {
127  $localizedParams[$name] = $value[$localeKey];
128  }
129  }
130  }
131  }
132 
133  return $localizedParams;
134  }
135 
150  public function createNotification($request, $userId = null, $notificationType, $contextId = null, $assocType = null, $assocId = null, $level = NOTIFICATION_LEVEL_NORMAL, $params = null, $suppressEmail = false, callable $mailConfigurator = null) {
151  $blockedNotifications = $this->getUserBlockedNotifications($userId, $contextId);
152 
153  if (!in_array($notificationType, $blockedNotifications)) {
154  $notificationDao = DAORegistry::getDAO('NotificationDAO');
155  $notification = $notificationDao->newDataObject();
156  $notification->setUserId((int) $userId);
157  $notification->setType((int) $notificationType);
158  $notification->setContextId((int) $contextId);
159  $notification->setAssocType((int) $assocType);
160  $notification->setAssocId((int) $assocId);
161  $notification->setLevel((int) $level);
162 
163  $notificationId = $notificationDao->insertObject($notification);
164 
165  // Send notification emails
166  if ($notification->getLevel() != NOTIFICATION_LEVEL_TRIVIAL && !$suppressEmail) {
167  $notificationEmailSettings = $this->getUserBlockedEmailedNotifications($userId, $contextId);
168 
169  if (!in_array($notificationType, $notificationEmailSettings)) {
170  $this->sendNotificationEmail($request, $notification, $contextId, $mailConfigurator);
171  }
172  }
173 
174  if ($params) {
175  $notificationSettingsDao = DAORegistry::getDAO('NotificationSettingsDAO'); /* @var $notificationSettingsDao NotificationSettingsDAO */
176  foreach ($params as $name => $value) {
177  $notificationSettingsDao->updateNotificationSetting($notificationId, $name, $value);
178  }
179  }
180 
181  return $notification;
182  }
183  }
184 
193  public function createTrivialNotification($userId, $notificationType = NOTIFICATION_TYPE_SUCCESS, $params = null) {
194  $notificationDao = DAORegistry::getDAO('NotificationDAO'); /* @var $notificationDao NotificationDAO */
195  $notification = $notificationDao->newDataObject();
196  $notification->setUserId($userId);
197  $notification->setContextId(CONTEXT_ID_NONE);
198  $notification->setType($notificationType);
199  $notification->setLevel(NOTIFICATION_LEVEL_TRIVIAL);
200 
201  $notificationId = $notificationDao->insertObject($notification);
202 
203  if ($params) {
204  $notificationSettingsDao = DAORegistry::getDAO('NotificationSettingsDAO'); /* @var $notificationSettingsDao NotificationSettingsDAO */
205  foreach ($params as $name => $value) {
206  $notificationSettingsDao->updateNotificationSetting($notificationId, $name, $value);
207  }
208  }
209 
210  return $notification;
211  }
212 
217  public function deleteTrivialNotifications($notifications) {
218  $notificationDao = DAORegistry::getDAO('NotificationDAO'); /* @var $notificationDao NotificationDAO */
219  foreach($notifications as $notification) {
220  // Delete only trivial notifications.
221  if($notification->getLevel() == NOTIFICATION_LEVEL_TRIVIAL) {
222  $notificationDao->deleteById($notification->getId(), $notification->getUserId());
223  }
224  }
225  }
226 
233  public function formatToGeneralNotification($request, $notifications) {
234  $formattedNotificationsData = array();
235  foreach ($notifications as $notification) { /* @var $notification Notification */
236  $formattedNotificationsData[$notification->getLevel()][$notification->getId()] = array(
237  'title' => $this->getNotificationTitle($notification),
238  'text' => $this->getNotificationContents($request, $notification),
239  'addclass' => $this->getStyleClass($notification),
240  'notice_icon' => $this->getIconClass($notification),
241  'styling' => 'jqueryui',
242  );
243  }
244 
245  return $formattedNotificationsData;
246  }
247 
254  public function formatToInPlaceNotification($request, $notifications) {
255  $formattedNotificationsData = null;
256 
257  if (!empty($notifications)) {
258  $templateMgr = TemplateManager::getManager($request);
259  foreach ((array)$notifications as $notification) {
260  $formattedNotificationsData[$notification->getLevel()][$notification->getId()] = $this->formatNotification($request, $notification, 'controllers/notification/inPlaceNotificationContent.tpl');
261  }
262  }
263 
264  return $formattedNotificationsData;
265  }
266 
273  protected function getUserBlockedNotifications($userId, $contextId) {
274  $notificationSubscriptionSettingsDao = DAORegistry::getDAO('NotificationSubscriptionSettingsDAO'); /* @var $notificationSubscriptionSettingsDao NotificationSubscriptionSettingsDAO */
275  return $notificationSubscriptionSettingsDao->getNotificationSubscriptionSettings('blocked_notification', $userId, (int) $contextId);
276  }
277 
282  protected function getUserBlockedEmailedNotifications($userId, $contextId) {
283  $notificationSubscriptionSettingsDao = DAORegistry::getDAO('NotificationSubscriptionSettingsDAO'); /* @var $notificationSubscriptionSettingsDao NotificationSubscriptionSettingsDAO */
284  return $notificationSubscriptionSettingsDao->getNotificationSubscriptionSettings('blocked_emailed_notification', $userId, (int) $contextId);
285  }
286 
293  protected function getMailTemplate($emailKey = null) {
294  import('lib.pkp.classes.mail.MailTemplate');
295  return new MailTemplate($emailKey, null, null, false);
296  }
297 
304  protected function fetchLinkActionNotificationContent($linkAction, $request) {
305  $templateMgr = TemplateManager::getManager($request);
306  $templateMgr->assign('linkAction', $linkAction);
307  return $templateMgr->fetch('controllers/notification/linkActionNotificationContent.tpl');
308  }
309 
310 
311  //
312  // Private helper methods.
313  //
314  /*
315  * Return a string of formatted notifications for display
316  * @param $request PKPRequest
317  * @param $notifications object DAOResultFactory
318  * @param $notificationTemplate string optional Template to use for constructing an individual notification for display
319  * @return string
320  */
321  private function formatNotifications($request, $notifications, $notificationTemplate = 'notification/notification.tpl') {
322  $notificationString = '';
323 
324  // Build out the notifications based on their associated objects and format into a string
325  while($notification = $notifications->next()) {
326  $notificationString .= $this->formatNotification($request, $notification, $notificationTemplate);
327  }
328 
329  return $notificationString;
330  }
331 
338  private function formatNotification($request, $notification, $notificationTemplate = 'notification/notification.tpl') {
339  $templateMgr = TemplateManager::getManager($request);
340 
341  // Set the date read if it isn't already set
342  if (!$notification->getDateRead()) {
343  $notificationDao = DAORegistry::getDAO('NotificationDAO'); /* @var $notificationDao NotificationDAO */
344  $dateRead = $notificationDao->setDateRead($notification->getId(), Core::getCurrentDate());
345  $notification->setDateRead($dateRead);
346  }
347 
348  $user = $request->getUser();
349  $templateMgr->assign(array(
350  'isUserLoggedIn' => $user,
351  'notificationDateCreated' => $notification->getDateCreated(),
352  'notificationId' => $notification->getId(),
353  'notificationContents' => $this->getNotificationContents($request, $notification),
354  'notificationTitle' => $this->getNotificationTitle($notification),
355  'notificationStyleClass' => $this->getStyleClass($notification),
356  'notificationIconClass' => $this->getIconClass($notification),
357  'notificationDateRead' => $notification->getDateRead(),
358  ));
359 
360  if($notification->getLevel() != NOTIFICATION_LEVEL_TRIVIAL) {
361  $templateMgr->assign('notificationUrl', $this->getNotificationUrl($request, $notification));
362  }
363 
364  return $templateMgr->fetch($notificationTemplate);
365  }
366 
374  protected function sendNotificationEmail($request, $notification, ?int $contextId, callable $mailConfigurator = null) {
375  $userId = $notification->getUserId();
376  $userDao = DAORegistry::getDAO('UserDAO'); /* @var $userDao UserDAO */
377  $user = $userDao->getById($userId);
378  if ($user && !$user->getDisabled()) {
379  AppLocale::requireComponents(LOCALE_COMPONENT_APP_COMMON);
380 
381  $context = $request->getContext();
382  if ($contextId && (!$context || $context->getId() != $contextId)) {
383  $contextDao = Application::getContextDAO();
384  $context = $contextDao->getById($contextId);
385  }
386 
387  $site = $request->getSite();
388  $mail = $this->getMailTemplate('NOTIFICATION');
389 
390  if ($context) {
391  $mail->setReplyTo($context->getContactEmail(), $context->getContactName());
392  } else {
393  $mail->setReplyTo($site->getLocalizedContactEmail(), $site->getLocalizedContactName());
394  }
395 
396  $mail->assignParams(array(
397  'notificationContents' => $this->getNotificationContents($request, $notification),
398  'url' => $this->getNotificationUrl($request, $notification),
399  'siteTitle' => $context?$context->getLocalizedName():$site->getLocalizedTitle()
400  ));
401  $mail->addRecipient($user->getEmail(), $user->getFullName());
402  if (is_callable($mailConfigurator)) {
403  $mail = $mailConfigurator($mail);
404  }
405  if (!$mail->send() && $request->getUser()) {
406  import('classes.notification.NotificationManager');
407  $notificationMgr = new NotificationManager();
408  $notificationMgr->createTrivialNotification($request->getUser()->getId(), NOTIFICATION_TYPE_ERROR, array('contents' => __('email.compose.error')));
409  }
410  }
411  }
412 }
413 
414 
Application\getContextDAO
static getContextDAO()
Definition: Application.inc.php:137
PKPNotificationOperationManager\getFormattedNotificationsForUser
getFormattedNotificationsForUser($request, $userId, $level=NOTIFICATION_LEVEL_NORMAL, $contextId=null, $rangeInfo=null, $notificationTemplate='notification/notification.tpl')
Definition: PKPNotificationOperationManager.inc.php:92
PKPNotificationOperationManager\getStyleClass
getStyleClass($notification)
Definition: PKPNotificationOperationManager.inc.php:60
AppLocale\requireComponents
static requireComponents()
Definition: env1/MockAppLocale.inc.php:56
PKPNotificationOperationManager\getUserBlockedEmailedNotifications
getUserBlockedEmailedNotifications($userId, $contextId)
Definition: PKPNotificationOperationManager.inc.php:282
PKPNotificationOperationManager
Base class for notification manager that implements basic notification operations and default notific...
Definition: PKPNotificationOperationManager.inc.php:23
DAORegistry\getDAO
static & getDAO($name, $dbconn=null)
Definition: DAORegistry.inc.php:57
PKPNotificationOperationManager\getNotificationContents
getNotificationContents($request, $notification)
Definition: PKPNotificationOperationManager.inc.php:46
PKPNotificationOperationManager\getNotificationTitle
getNotificationTitle($notification)
Definition: PKPNotificationOperationManager.inc.php:53
PKPNotificationOperationManager\deleteTrivialNotifications
deleteTrivialNotifications($notifications)
Definition: PKPNotificationOperationManager.inc.php:217
INotificationInfoProvider
Interface to retrieve notification presentation information.
Definition: INotificationInfoProvider.inc.php:23
AppLocale\getPrimaryLocale
static getPrimaryLocale()
Definition: env1/MockAppLocale.inc.php:95
MailTemplate
Subclass of Mail for mailing a template email.
Definition: MailTemplate.inc.php:21
PKPNotificationOperationManager\createTrivialNotification
createTrivialNotification($userId, $notificationType=NOTIFICATION_TYPE_SUCCESS, $params=null)
Definition: PKPNotificationOperationManager.inc.php:193
PKPNotificationOperationManager\sendNotificationEmail
sendNotificationEmail($request, $notification, ?int $contextId, callable $mailConfigurator=null)
Definition: PKPNotificationOperationManager.inc.php:374
PKPNotificationOperationManager\getNotificationUrl
getNotificationUrl($request, $notification)
Definition: PKPNotificationOperationManager.inc.php:31
PKPNotificationOperationManager\fetchLinkActionNotificationContent
fetchLinkActionNotificationContent($linkAction, $request)
Definition: PKPNotificationOperationManager.inc.php:304
PKPNotificationOperationManager\isVisibleToAllUsers
isVisibleToAllUsers($notificationType, $assocType, $assocId)
Definition: PKPNotificationOperationManager.inc.php:74
AppLocale\getSupportedLocales
static getSupportedLocales()
Definition: env1/MockAppLocale.inc.php:116
PKPTemplateManager\getManager
static & getManager($request=null)
Definition: PKPTemplateManager.inc.php:1239
PKPNotificationOperationManager\getMailTemplate
getMailTemplate($emailKey=null)
Definition: PKPNotificationOperationManager.inc.php:293
PKPNotificationOperationManager\formatToGeneralNotification
formatToGeneralNotification($request, $notifications)
Definition: PKPNotificationOperationManager.inc.php:233
Core\getCurrentDate
static getCurrentDate($ts=null)
Definition: Core.inc.php:63
PKPNotificationOperationManager\getNotificationMessage
getNotificationMessage($request, $notification)
Definition: PKPNotificationOperationManager.inc.php:38
NotificationManager
Definition: NotificationManager.inc.php:19
PKPNotificationOperationManager\getParamsForCurrentLocale
getParamsForCurrentLocale($params)
Definition: PKPNotificationOperationManager.inc.php:107
PKPNotificationOperationManager\getUserBlockedNotifications
getUserBlockedNotifications($userId, $contextId)
Definition: PKPNotificationOperationManager.inc.php:273
PKPNotificationOperationManager\createNotification
createNotification($request, $userId=null, $notificationType, $contextId=null, $assocType=null, $assocId=null, $level=NOTIFICATION_LEVEL_NORMAL, $params=null, $suppressEmail=false, callable $mailConfigurator=null)
Definition: PKPNotificationOperationManager.inc.php:150
PKPNotificationOperationManager\formatToInPlaceNotification
formatToInPlaceNotification($request, $notifications)
Definition: PKPNotificationOperationManager.inc.php:254
PKPNotificationOperationManager\getIconClass
getIconClass($notification)
Definition: PKPNotificationOperationManager.inc.php:67
AppLocale\getLocale
static getLocale()
Definition: env1/MockAppLocale.inc.php:40