ocs  2.3.6
About: OCS (Open Conference System) is a Web publishing tool that will create a complete Web presence for your scholarly conference.
  Fossies Dox: ocs-2.3.6.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

NotificationDAO.inc.php
Go to the documentation of this file.
1 <?php
2 
16 // $Id$
17 
18 import('notification.Notification');
19 
20 class NotificationDAO extends DAO {
24  function NotificationDAO() {
25  parent::DAO();
26  }
27 
33  function &getNotificationById($notificationId) {
34  $result =& $this->retrieve(
35  'SELECT * FROM notifications WHERE notification_id = ?', (int) $notificationId
36  );
37 
38  $notification =& $this->_returnNotificationFromRow($result->GetRowAssoc(false));
39 
40  $result->Close();
41  unset($result);
42 
43  return $notification;
44  }
45 
51  function &getByUserId($userId, $level = NOTIFICATION_LEVEL_NORMAL, $rangeInfo = null) {
53  $productName = $application->getName();
54  $context =& Request::getContext();
55  $contextId = $context?$context->getId():0;
56 
57  $notifications = array();
58 
59  $result =& $this->retrieveRange(
60  'SELECT * FROM notifications WHERE user_id = ? AND product = ? AND context = ? AND level = ? ORDER BY date_created DESC',
61  array((int) $userId, $productName, (int) $contextId, (int) $level), $rangeInfo
62  );
63 
64  $returner = new DAOResultFactory($result, $this, '_returnNotificationFromRow');
65 
66  return $returner;
67  }
68 
74  function setDateRead($notificationId) {
75  $returner = $this->update(
76  sprintf('UPDATE notifications
77  SET date_read = %s
78  WHERE notification_id = ?',
79  $this->datetimeToDB(date('Y-m-d H:i:s'))),
80  (int) $notificationId
81  );
82 
83  return $returner;
84  }
85 
91  function &_returnNotificationFromRow($row) {
92  $notification = new Notification();
93  $notification->setId($row['notification_id']);
94  $notification->setUserId($row['user_id']);
95  $notification->setLevel($row['level']);
96  $notification->setDateCreated($row['date_created']);
97  $notification->setDateRead($row['date_read']);
98  $notification->setTitle($row['title']);
99  $notification->setContents($row['contents']);
100  $notification->setParam($row['param']);
101  $notification->setLocation($row['location']);
102  $notification->setIsLocalized($row['is_localized']);
103  $notification->setContext($row['context']);
104  $notification->setAssocType($row['assoc_type']);
105 
106  HookRegistry::call('NotificationDAO::_returnNotificationFromRow', array(&$notification, &$row));
107 
108  return $notification;
109  }
110 
116  function insertNotification(&$notification) {
118  $productName = $application->getName();
119 
120  if ($this->notificationAlreadyExists($notification)) {
121  return 0;
122  }
123 
124  $notificationSettingsDao =& DAORegistry::getDAO('NotificationSettingsDAO');
125  if ($notification->getLevel() != NOTIFICATION_LEVEL_TRIVIAL) {
126  $notificationSettings = $notificationSettingsDao->getNotificationSettings($notification->getUserId());
127  $notificationEmailSettings = $notificationSettingsDao->getNotificationEmailSettings($notification->getUserId());
128 
129  if(in_array($notification->getAssocType(), $notificationEmailSettings)) {
130  $this->sendNotificationEmail($notification);
131  }
132  }
133 
134  if($notification->getLevel() == NOTIFICATION_LEVEL_TRIVIAL || !in_array($notification->getAssocType(), $notificationSettings)) {
135  $this->update(
136  sprintf('INSERT INTO notifications
137  (user_id, level, date_created, title, contents, param, location, is_localized, context, product, assoc_type)
138  VALUES
139  (?, ?, %s, ?, ?, ?, ?, ?, ?, ?, ?)',
140  $this->datetimeToDB(date('Y-m-d H:i:s'))),
141  array(
142  (int) $notification->getUserId(),
143  (int) $notification->getLevel(),
144  $notification->getTitle(),
145  $notification->getContents(),
146  $notification->getParam(),
147  $notification->getLocation(),
148  (int) $notification->getIsLocalized(),
149  (int) $notification->getContext(),
150  $productName,
151  (int) $notification->getAssocType(),
152  )
153  );
154 
155  $notification->setId($this->getInsertNotificationId());
156  return $notification->getId();
157  } else return 0;
158  }
159 
165  function deleteNotificationById($notificationId, $userId = null) {
166  $params = array($notificationId);
167  if (isset($userId)) $params[] = $userId;
168 
169  return $this->update('DELETE FROM notifications WHERE notification_id = ?' . (isset($userId) ? ' AND user_id = ?' : ''),
170  $params
171  );
172  }
173 
181  function notificationAlreadyExists(&$notification) {
183  $productName = $application->getName();
184  $context =& Request::getContext();
185  $contextId = $context?$context->getId():0;
186 
187  $result =& $this->retrieve(
188  'SELECT date_created FROM notifications WHERE user_id = ? AND title = ? AND contents = ? AND param = ? AND product = ? AND assoc_type = ? AND context = ? AND level = ?',
189  array(
190  (int) $notification->getUserId(),
191  $notification->getTitle(),
192  $notification->getContents(),
193  $notification->getParam(),
194  $productName,
195  (int) $notification->getAssocType(),
196  (int) $contextId,
197  (int) $notification->getLevel()
198  )
199  );
200 
201  $date = isset($result->fields[0]) ? $result->fields[0] : 0;
202 
203  if ($date == 0) {
204  return false;
205  } else {
206  $timeDiff = strtotime($date) - time();
207  if ($timeDiff < 3600) { // 1 hour (in seconds)
208  return true;
209  } else return false;
210  }
211  }
212 
218  return $this->getInsertId('notifications', 'notification_id');
219  }
220 
228  $productName = $application->getName();
229  $context =& Request::getContext();
230  $contextId = $context?$context->getId():0;
231 
232  $result =& $this->retrieve(
233  'SELECT count(*) FROM notifications WHERE user_id = ? AND date_read IS NULL AND product = ? AND context = ? AND level = ?',
234  array((int) $userId, $productName, (int) $contextId, (int) $level)
235  );
236 
237  $returner = $result->fields[0];
238 
239  $result->Close();
240  unset($result);
241 
242  return $returner;
243  }
244 
252  $productName = $application->getName();
253  $context =& Request::getContext();
254  $contextId = $context?$context->getId():0;
255 
256  $result =& $this->retrieve(
257  'SELECT count(*) FROM notifications WHERE user_id = ? AND date_read IS NOT NULL AND product = ? AND context = ? AND level = ?',
258  array((int) $userId, $productName, (int) $contextId, (int) $level)
259  );
260 
261  $returner = $result->fields[0];
262 
263  $result->Close();
264  unset($result);
265 
266  return $returner;
267  }
268 
273  function sendNotificationEmail($notification) {
274  $userId = $notification->getUserId();
275  $userDao =& DAORegistry::getDAO('UserDAO');
276  $user = $userDao->getUser($userId);
278 
279  if ($notification->getIsLocalized()) {
280  $params = array('param' => $notification->getParam());
281  $notificationTitle = __($notification->getTitle(), $params);
282  $notificationContents = __($notification->getContents(), $params);
283  } else {
284  $notificationTitle = $notification->getTitle();
285  $notificationContents = $notification->getContents();
286  }
287 
288  import('mail.MailTemplate');
289  $site =& Request::getSite();
290  $mail = new MailTemplate('NOTIFICATION');
291  $mail->setFrom($site->getLocalizedContactEmail(), $site->getLocalizedContactName());
292  $mail->assignParams(array(
293  'notificationTitle' => $notificationTitle,
294  'notificationContents' => $notificationContents,
295  'url' => $notification->getLocation(),
296  'siteTitle' => $site->getLocalizedTitle()
297  ));
298  $mail->addRecipient($user->getEmail(), $user->getFullName());
299  $mail->send();
300  }
301 }
302 
303 ?>
config TEMPLATE inc php
Definition: config.inc.php:4
Operations for retrieving and modifying objects from a database.
Definition: DAO.inc.php:30
Operations for retrieving and modifying Notification objects.
Class for Notification.
& retrieve($sql, $params=false, $callHooks=true)
Definition: DAO.inc.php:62
$application
Definition: index.php:64
& _returnNotificationFromRow($row)
getInsertId($table='', $id='', $callHooks=true)
Definition: DAO.inc.php:225
deleteNotificationById($notificationId, $userId=null)
sendNotificationEmail($notification)
requireComponents($components, $locale=null)
const NOTIFICATION_LEVEL_TRIVIAL
& retrieveRange($sql, $params=false, $dbResultRange=null, $callHooks=true)
Definition: DAO.inc.php:152
notificationAlreadyExists(&$notification)
setDateRead($notificationId)
__($key, $params=array(), $locale=null)
Wrapper around ADORecordSet providing "factory" features for generating objects from DAOs.
insertNotification(&$notification)
& getDAO($name, $dbconn=null)
call($hookName, $args=null)
getReadNotificationCount($userId, $level=NOTIFICATION_LEVEL_NORMAL)
datetimeToDB($dt)
Definition: DAO.inc.php:258
& getNotificationById($notificationId)
update($sql, $params=false, $callHooks=true, $dieOnError=true)
Definition: DAO.inc.php:186
Subclass of PKPMailTemplate for mailing a template email.
const LOCALE_COMPONENT_APPLICATION_COMMON
const NOTIFICATION_LEVEL_NORMAL
& getByUserId($userId, $level=NOTIFICATION_LEVEL_NORMAL, $rangeInfo=null)
getUnreadNotificationCount($userId, $level=NOTIFICATION_LEVEL_NORMAL)