Open Journal Systems  3.0.0
 All Classes Namespaces Functions Variables Groups Pages
NotificationDAO.inc.php
1 <?php
2 
18 import('classes.notification.Notification');
19 
20 class NotificationDAO extends DAO {
24  function NotificationDAO() {
25  parent::DAO();
26  }
27 
34  function getById($notificationId, $userId = null) {
35  $params = array((int) $notificationId);
36  if ($userId) $params[] = (int) $userId;
37 
38  $result = $this->retrieve(
39  'SELECT *
40  FROM notifications
41  WHERE notification_id = ?
42  ' . ($userId?' AND user_id = ?':''),
43  $params
44  );
45 
46  $notification = $this->_fromRow($result->GetRowAssoc(false));
47  $result->Close();
48  return $notification;
49  }
50 
62  function getByUserId($userId, $level = NOTIFICATION_LEVEL_NORMAL, $type = null, $contextId = null, $rangeInfo = null) {
63  $params = array((int) $userId, (int) $level);
64  if ($type) $params[] = (int) $type;
65  if ($contextId) $params[] = (int) $contextId;
66 
67  $result = $this->retrieveRange(
68  'SELECT * FROM notifications WHERE user_id = ? AND level = ?' . (isset($type) ?' AND type = ?' : '') . (isset($contextId) ?' AND context_id = ?' : '') . ' ORDER BY date_created DESC',
69  $params, $rangeInfo
70  );
71 
72  return new DAOResultFactory($result, $this, '_fromRow');
73  }
74 
86  function getByAssoc($assocType, $assocId, $userId = null, $type = null, $contextId = null) {
87  $params = array((int) $assocType, (int) $assocId);
88  if ($userId) $params[] = (int) $userId;
89  if ($contextId) $params[] = (int) $contextId;
90  if ($type) $params[] = (int) $type;
91 
92  $result = $this->retrieveRange(
93  'SELECT * FROM notifications WHERE assoc_type = ? AND assoc_id = ?' . (isset($userId) ?' AND user_id = ?' : '') . (isset($contextId) ?' AND context_id = ?' : '') . (isset($type) ?' AND type = ?' : '') . ' ORDER BY date_created DESC',
94  $params
95  );
96 
97  return new DAOResultFactory($result, $this, '_fromRow');
98  }
99 
106  function setDateRead($notificationId, $dateRead = null) {
107  $dateRead = isset($dateRead) ? $dateRead : Core::getCurrentDate();
108 
109  $this->update(
110  sprintf('UPDATE notifications
111  SET date_read = %s
112  WHERE notification_id = ?',
113  $this->datetimeToDB($dateRead)),
114  (int) $notificationId
115  );
116 
117  return $dateRead;
118  }
119 
124  function newDataObject() {
125  return new Notification();
126  }
127 
133  function insertObject($notification) {
134  $this->update(
135  sprintf('INSERT INTO notifications
136  (user_id, level, date_created, context_id, type, assoc_type, assoc_id)
137  VALUES
138  (?, ?, %s, ?, ?, ?, ?)',
140  array(
141  (int) $notification->getUserId(),
142  (int) $notification->getLevel(),
143  (int) $notification->getContextId(),
144  (int) $notification->getType(),
145  (int) $notification->getAssocType(),
146  (int) $notification->getAssocId()
147  )
148  );
149  $notification->setId($this->getInsertId());
150 
151  return $notification->getId();
152  }
153 
164  function build($contextId, $level, $type, $assocType, $assocId, $userId = null) {
165  $params = array(
166  (int) $contextId,
167  (int) $level,
168  (int) $type,
169  (int) $assocType,
170  (int) $assocId
171  );
172 
173  if ($userId) $params[] = (int) $userId;
174 
175  $this->update('DELETE FROM notifications
176  WHERE context_id = ? AND level = ? AND type = ? AND assoc_type = ? AND assoc_id = ?'
177  . ($userId ? ' AND user_id = ?' : ''),
178  $params
179  );
180 
181  $notification = $this->newDataObject();
182  $notification->setContextId($contextId);
183  $notification->setLevel($level);
184  $notification->setType($type);
185  $notification->setAssocType($assocType);
186  $notification->setAssocId($assocId);
187  $notification->setUserId($userId);
188 
189  $notificationId = $this->insertObject($notification);
190  if ($notificationId) {
191  $notification->setId($notificationId);
192  return $notification;
193  } else {
194  return null;
195  }
196  }
197 
204  function deleteById($notificationId, $userId = null) {
205  $params = array((int) $notificationId);
206  if (isset($userId)) $params[] = (int) $userId;
207  $this->update(
208  'DELETE FROM notifications WHERE notification_id = ?' . (isset($userId) ? ' AND user_id = ?' : ''),
209  $params
210  );
211  if ($this->getAffectedRows()) {
212  // If a notification was deleted (possibly validating
213  // $userId in the process) delete associated settings.
214  $notificationSettingsDao = DAORegistry::getDAO('NotificationSettingsDAO'); /* @var $notificationSettingsDaoDao NotificationSettingsDAO */
215  $notificationSettingsDao->deleteSettingsByNotificationId($notificationId);
216  return true;
217  }
218  return false;
219  }
220 
226  function deleteObject($notification) {
227  return $this->deleteById($notification->getId());
228  }
229 
239  function deleteByAssoc($assocType, $assocId, $userId = null, $type = null, $contextId = null) {
240  $notificationsFactory = $this->getByAssoc($assocType, $assocId, $userId, $type, $contextId);
241  while ($notification = $notificationsFactory->next()) {
242  $this->deleteObject($notification);
243  }
244  }
245 
250  function getInsertId() {
251  return $this->_getInsertId('notifications', 'notification_id');
252  }
253 
262  function getNotificationCount($read = true, $userId, $contextId = null, $level = NOTIFICATION_LEVEL_NORMAL) {
263  $params = array((int) $userId, (int) $level);
264  if ($contextId) $params[] = (int) $contextId;
265 
266  $result = $this->retrieve(
267  'SELECT count(*) FROM notifications WHERE user_id = ? AND date_read IS' . ($read ? ' NOT' : '') . ' NULL AND level = ?'
268  . (isset($contextId) ? ' AND context_id = ?' : ''),
269  $params
270  );
271 
272  $returner = $result->fields[0];
273 
274  $result->Close();
275  return $returner;
276  }
277 
283  function transferNotifications($oldUserId, $newUserId) {
284  $this->update(
285  'UPDATE notifications SET user_id = ? WHERE user_id = ?',
286  array((int) $newUserId, (int) $oldUserId)
287  );
288  }
289 
295  function _fromRow($row) {
296  $notification = $this->newDataObject();
297  $notification->setId($row['notification_id']);
298  $notification->setUserId($row['user_id']);
299  $notification->setLevel($row['level']);
300  $notification->setDateCreated($this->datetimeFromDB($row['date_created']));
301  $notification->setDateRead($this->datetimeFromDB($row['date_read']));
302  $notification->setContextId($row['context_id']);
303  $notification->setType($row['type']);
304  $notification->setAssocType($row['assoc_type']);
305  $notification->setAssocId($row['assoc_id']);
306 
307  HookRegistry::call('NotificationDAO::_fromRow', array(&$notification, &$row));
308 
309  return $notification;
310  }
311 }
312 
313 ?>
static & getDAO($name, $dbconn=null)
transferNotifications($oldUserId, $newUserId)
Operations for retrieving and modifying objects from a database.
Definition: DAO.inc.php:30
getNotificationCount($read=true, $userId, $contextId=null, $level=NOTIFICATION_LEVEL_NORMAL)
Operations for retrieving and modifying Notification objects.
Class for Notification.
& retrieve($sql, $params=false, $callHooks=true)
Definition: DAO.inc.php:84
getByUserId($userId, $level=NOTIFICATION_LEVEL_NORMAL, $type=null, $contextId=null, $rangeInfo=null)
& retrieveRange($sql, $params=false, $dbResultRange=null, $callHooks=true)
Definition: DAO.inc.php:174
_getInsertId($table= '', $id= '')
Definition: DAO.inc.php:249
deleteByAssoc($assocType, $assocId, $userId=null, $type=null, $contextId=null)
deleteObject($notification)
datetimeFromDB($dt)
Definition: DAO.inc.php:313
getAffectedRows()
Definition: DAO.inc.php:258
static call($hookName, $args=null)
getById($notificationId, $userId=null)
Wrapper around ADORecordSet providing &quot;factory&quot; features for generating objects from DAOs...
setDateRead($notificationId, $dateRead=null)
deleteById($notificationId, $userId=null)
getByAssoc($assocType, $assocId, $userId=null, $type=null, $contextId=null)
insertObject($notification)
datetimeToDB($dt)
Definition: DAO.inc.php:293
update($sql, $params=false, $callHooks=true, $dieOnError=true)
Definition: DAO.inc.php:208
build($contextId, $level, $type, $assocType, $assocId, $userId=null)
static getCurrentDate($ts=null)
Definition: Core.inc.php:95