Open Journal Systems  3.3.0
NotificationDAO.inc.php
1 <?php
2 
18 import('classes.notification.Notification');
19 
20 class NotificationDAO extends DAO {
21 
28  function getById($notificationId, $userId = null) {
29  $params = array((int) $notificationId);
30  if ($userId) $params[] = (int) $userId;
31 
32  $result = $this->retrieve(
33  'SELECT *
34  FROM notifications
35  WHERE notification_id = ?
36  ' . ($userId?' AND user_id = ?':''),
37  $params
38  );
39 
40  $notification = $this->_fromRow($result->GetRowAssoc(false));
41  $result->Close();
42  return $notification;
43  }
44 
56  function getByUserId($userId, $level = NOTIFICATION_LEVEL_NORMAL, $type = null, $contextId = null, $rangeInfo = null) {
57  $params = array((int) $userId, (int) $level);
58  if ($type) $params[] = (int) $type;
59  if ($contextId) $params[] = (int) $contextId;
60 
61  $result = $this->retrieveRange(
62  'SELECT * FROM notifications WHERE user_id = ? AND level = ?' . (isset($type) ?' AND type = ?' : '') . (isset($contextId) ?' AND context_id = ?' : '') . ' ORDER BY date_created DESC',
63  $params, $rangeInfo
64  );
65 
66  return new DAOResultFactory($result, $this, '_fromRow');
67  }
68 
80  function getByAssoc($assocType, $assocId, $userId = null, $type = null, $contextId = null) {
81  $params = array((int) $assocType, (int) $assocId);
82  if ($userId) $params[] = (int) $userId;
83  if ($contextId) $params[] = (int) $contextId;
84  if ($type) $params[] = (int) $type;
85 
86  $result = $this->retrieveRange(
87  'SELECT * FROM notifications WHERE assoc_type = ? AND assoc_id = ?' .
88  ($userId?' AND user_id = ?':'') .
89  ($contextId?' AND context_id = ?':'') .
90  ($type?' AND type = ?':'') .
91  ' ORDER BY date_created DESC',
92  $params
93  );
94 
95  return new DAOResultFactory($result, $this, '_fromRow');
96  }
97 
104  function setDateRead($notificationId, $dateRead) {
105  $this->update(
106  sprintf('UPDATE notifications
107  SET date_read = %s
108  WHERE notification_id = ?',
109  $this->datetimeToDB($dateRead)),
110  (int) $notificationId
111  );
112 
113  return $dateRead;
114  }
115 
120  function newDataObject() {
121  return new Notification();
122  }
123 
129  function insertObject($notification) {
130  $this->update(
131  sprintf('INSERT INTO notifications
132  (user_id, level, date_created, context_id, type, assoc_type, assoc_id)
133  VALUES
134  (?, ?, %s, ?, ?, ?, ?)',
136  array(
137  (int) $notification->getUserId(),
138  (int) $notification->getLevel(),
139  (int) $notification->getContextId(),
140  (int) $notification->getType(),
141  (int) $notification->getAssocType(),
142  (int) $notification->getAssocId()
143  )
144  );
145  $notification->setId($this->getInsertId());
146 
147  return $notification->getId();
148  }
149 
160  function build($contextId, $level, $type, $assocType, $assocId, $userId = null) {
161  $params = array(
162  (int) $contextId,
163  (int) $level,
164  (int) $type,
165  (int) $assocType,
166  (int) $assocId
167  );
168 
169  if ($userId) $params[] = (int) $userId;
170 
171  $this->update('DELETE FROM notifications
172  WHERE context_id = ? AND level = ? AND type = ? AND assoc_type = ? AND assoc_id = ?'
173  . ($userId ? ' AND user_id = ?' : ''),
174  $params
175  );
176 
177  $notification = $this->newDataObject();
178  $notification->setContextId($contextId);
179  $notification->setLevel($level);
180  $notification->setType($type);
181  $notification->setAssocType($assocType);
182  $notification->setAssocId($assocId);
183  $notification->setUserId($userId);
184 
185  $notificationId = $this->insertObject($notification);
186  if ($notificationId) {
187  $notification->setId($notificationId);
188  return $notification;
189  } else {
190  return null;
191  }
192  }
193 
200  function deleteById($notificationId, $userId = null) {
201  $params = array((int) $notificationId);
202  if (isset($userId)) $params[] = (int) $userId;
203  $this->update(
204  'DELETE FROM notifications WHERE notification_id = ?' . (isset($userId) ? ' AND user_id = ?' : ''),
205  $params
206  );
207  if ($this->getAffectedRows()) {
208  // If a notification was deleted (possibly validating
209  // $userId in the process) delete associated settings.
210  $notificationSettingsDao = DAORegistry::getDAO('NotificationSettingsDAO'); /* @var $notificationSettingsDaoDao NotificationSettingsDAO */
211  $notificationSettingsDao->deleteSettingsByNotificationId($notificationId);
212  return true;
213  }
214  return false;
215  }
216 
222  function deleteObject($notification) {
223  return $this->deleteById($notification->getId());
224  }
225 
235  function deleteByAssoc($assocType, $assocId, $userId = null, $type = null, $contextId = null) {
236  $notificationsFactory = $this->getByAssoc($assocType, $assocId, $userId, $type, $contextId);
237  while ($notification = $notificationsFactory->next()) {
238  $this->deleteObject($notification);
239  }
240  }
241 
246  function getInsertId() {
247  return $this->_getInsertId('notifications', 'notification_id');
248  }
249 
258  function getNotificationCount($read = true, $userId, $contextId = null, $level = NOTIFICATION_LEVEL_NORMAL) {
259  $params = array((int) $userId, (int) $level);
260  if ($contextId) $params[] = (int) $contextId;
261 
262  $result = $this->retrieve(
263  'SELECT count(*) FROM notifications WHERE user_id = ? AND date_read IS' . ($read ? ' NOT' : '') . ' NULL AND level = ?'
264  . (isset($contextId) ? ' AND context_id = ?' : ''),
265  $params
266  );
267 
268  $returner = $result->fields[0];
269 
270  $result->Close();
271  return $returner;
272  }
273 
279  function transferNotifications($oldUserId, $newUserId) {
280  $this->update(
281  'UPDATE notifications SET user_id = ? WHERE user_id = ?',
282  array((int) $newUserId, (int) $oldUserId)
283  );
284  }
285 
291  function _fromRow($row) {
292  $notification = $this->newDataObject();
293  $notification->setId($row['notification_id']);
294  $notification->setUserId($row['user_id']);
295  $notification->setLevel($row['level']);
296  $notification->setDateCreated($this->datetimeFromDB($row['date_created']));
297  $notification->setDateRead($this->datetimeFromDB($row['date_read']));
298  $notification->setContextId($row['context_id']);
299  $notification->setType($row['type']);
300  $notification->setAssocType($row['assoc_type']);
301  $notification->setAssocId($row['assoc_id']);
302 
303  HookRegistry::call('NotificationDAO::_fromRow', array(&$notification, &$row));
304 
305  return $notification;
306  }
307 }
308 
309 
NotificationDAO\setDateRead
setDateRead($notificationId, $dateRead)
Definition: NotificationDAO.inc.php:104
NotificationDAO\getNotificationCount
getNotificationCount($read=true, $userId, $contextId=null, $level=NOTIFICATION_LEVEL_NORMAL)
Definition: NotificationDAO.inc.php:258
NotificationDAO\transferNotifications
transferNotifications($oldUserId, $newUserId)
Definition: NotificationDAO.inc.php:279
DAOResultFactory
Wrapper around ADORecordSet providing "factory" features for generating objects from DAOs.
Definition: DAOResultFactory.inc.php:21
NotificationDAO\getByAssoc
getByAssoc($assocType, $assocId, $userId=null, $type=null, $contextId=null)
Definition: NotificationDAO.inc.php:80
Notification
Class for Notification.
Definition: Notification.inc.php:34
DAO\retrieveRange
& retrieveRange($sql, $params=false, $dbResultRange=null, $callHooks=true)
Definition: DAO.inc.php:176
NotificationDAO\getByUserId
getByUserId($userId, $level=NOTIFICATION_LEVEL_NORMAL, $type=null, $contextId=null, $rangeInfo=null)
Definition: NotificationDAO.inc.php:56
DAORegistry\getDAO
static & getDAO($name, $dbconn=null)
Definition: DAORegistry.inc.php:57
DAO\getAffectedRows
getAffectedRows()
Definition: DAO.inc.php:264
DAO\retrieve
& retrieve($sql, $params=false, $callHooks=true)
Definition: DAO.inc.php:85
NotificationDAO\insertObject
insertObject($notification)
Definition: NotificationDAO.inc.php:129
NotificationDAO\deleteObject
deleteObject($notification)
Definition: NotificationDAO.inc.php:222
DAO\datetimeFromDB
datetimeFromDB($dt)
Definition: DAO.inc.php:319
NotificationDAO\getInsertId
getInsertId()
Definition: NotificationDAO.inc.php:246
NotificationDAO\deleteById
deleteById($notificationId, $userId=null)
Definition: NotificationDAO.inc.php:200
DAO\update
update($sql, $params=false, $callHooks=true, $dieOnError=true)
Definition: DAO.inc.php:214
NotificationDAO\getById
getById($notificationId, $userId=null)
Definition: NotificationDAO.inc.php:28
DAO\datetimeToDB
datetimeToDB($dt)
Definition: DAO.inc.php:299
DAO\_getInsertId
_getInsertId($table='', $id='')
Definition: DAO.inc.php:255
NotificationDAO\deleteByAssoc
deleteByAssoc($assocType, $assocId, $userId=null, $type=null, $contextId=null)
Definition: NotificationDAO.inc.php:235
NotificationDAO\_fromRow
_fromRow($row)
Definition: NotificationDAO.inc.php:291
Core\getCurrentDate
static getCurrentDate($ts=null)
Definition: Core.inc.php:63
NotificationDAO\newDataObject
newDataObject()
Definition: NotificationDAO.inc.php:120
NotificationDAO
Operations for retrieving and modifying Notification objects.
Definition: NotificationDAO.inc.php:20
HookRegistry\call
static call($hookName, $args=null)
Definition: HookRegistry.inc.php:86
DAO
Operations for retrieving and modifying objects from a database.
Definition: DAO.inc.php:31
NotificationDAO\build
build($contextId, $level, $type, $assocType, $assocId, $userId=null)
Definition: NotificationDAO.inc.php:160