Open Journal Systems  3.0.0
 All Data Structures 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 = ?' .
94  ($userId?' AND user_id = ?':'') .
95  ($contextId?' AND context_id = ?':'') .
96  ($type?' AND type = ?':'') .
97  ' ORDER BY date_created DESC',
98  $params
99  );
100 
101  return new DAOResultFactory($result, $this, '_fromRow');
102  }
103 
110  function setDateRead($notificationId, $dateRead) {
111  $this->update(
112  sprintf('UPDATE notifications
113  SET date_read = %s
114  WHERE notification_id = ?',
115  $this->datetimeToDB($dateRead)),
116  (int) $notificationId
117  );
118 
119  return $dateRead;
120  }
121 
126  function newDataObject() {
127  return new Notification();
128  }
129 
135  function insertObject($notification) {
136  $this->update(
137  sprintf('INSERT INTO notifications
138  (user_id, level, date_created, context_id, type, assoc_type, assoc_id)
139  VALUES
140  (?, ?, %s, ?, ?, ?, ?)',
142  array(
143  (int) $notification->getUserId(),
144  (int) $notification->getLevel(),
145  (int) $notification->getContextId(),
146  (int) $notification->getType(),
147  (int) $notification->getAssocType(),
148  (int) $notification->getAssocId()
149  )
150  );
151  $notification->setId($this->getInsertId());
152 
153  return $notification->getId();
154  }
155 
166  function build($contextId, $level, $type, $assocType, $assocId, $userId = null) {
167  $params = array(
168  (int) $contextId,
169  (int) $level,
170  (int) $type,
171  (int) $assocType,
172  (int) $assocId
173  );
174 
175  if ($userId) $params[] = (int) $userId;
176 
177  $this->update('DELETE FROM notifications
178  WHERE context_id = ? AND level = ? AND type = ? AND assoc_type = ? AND assoc_id = ?'
179  . ($userId ? ' AND user_id = ?' : ''),
180  $params
181  );
182 
183  $notification = $this->newDataObject();
184  $notification->setContextId($contextId);
185  $notification->setLevel($level);
186  $notification->setType($type);
187  $notification->setAssocType($assocType);
188  $notification->setAssocId($assocId);
189  $notification->setUserId($userId);
190 
191  $notificationId = $this->insertObject($notification);
192  if ($notificationId) {
193  $notification->setId($notificationId);
194  return $notification;
195  } else {
196  return null;
197  }
198  }
199 
206  function deleteById($notificationId, $userId = null) {
207  $params = array((int) $notificationId);
208  if (isset($userId)) $params[] = (int) $userId;
209  $this->update(
210  'DELETE FROM notifications WHERE notification_id = ?' . (isset($userId) ? ' AND user_id = ?' : ''),
211  $params
212  );
213  if ($this->getAffectedRows()) {
214  // If a notification was deleted (possibly validating
215  // $userId in the process) delete associated settings.
216  $notificationSettingsDao = DAORegistry::getDAO('NotificationSettingsDAO'); /* @var $notificationSettingsDaoDao NotificationSettingsDAO */
217  $notificationSettingsDao->deleteSettingsByNotificationId($notificationId);
218  return true;
219  }
220  return false;
221  }
222 
228  function deleteObject($notification) {
229  return $this->deleteById($notification->getId());
230  }
231 
241  function deleteByAssoc($assocType, $assocId, $userId = null, $type = null, $contextId = null) {
242  $notificationsFactory = $this->getByAssoc($assocType, $assocId, $userId, $type, $contextId);
243  while ($notification = $notificationsFactory->next()) {
244  $this->deleteObject($notification);
245  }
246  }
247 
252  function getInsertId() {
253  return $this->_getInsertId('notifications', 'notification_id');
254  }
255 
264  function getNotificationCount($read = true, $userId, $contextId = null, $level = NOTIFICATION_LEVEL_NORMAL) {
265  $params = array((int) $userId, (int) $level);
266  if ($contextId) $params[] = (int) $contextId;
267 
268  $result = $this->retrieve(
269  'SELECT count(*) FROM notifications WHERE user_id = ? AND date_read IS' . ($read ? ' NOT' : '') . ' NULL AND level = ?'
270  . (isset($contextId) ? ' AND context_id = ?' : ''),
271  $params
272  );
273 
274  $returner = $result->fields[0];
275 
276  $result->Close();
277  return $returner;
278  }
279 
285  function transferNotifications($oldUserId, $newUserId) {
286  $this->update(
287  'UPDATE notifications SET user_id = ? WHERE user_id = ?',
288  array((int) $newUserId, (int) $oldUserId)
289  );
290  }
291 
297  function _fromRow($row) {
298  $notification = $this->newDataObject();
299  $notification->setId($row['notification_id']);
300  $notification->setUserId($row['user_id']);
301  $notification->setLevel($row['level']);
302  $notification->setDateCreated($this->datetimeFromDB($row['date_created']));
303  $notification->setDateRead($this->datetimeFromDB($row['date_read']));
304  $notification->setContextId($row['context_id']);
305  $notification->setType($row['type']);
306  $notification->setAssocType($row['assoc_type']);
307  $notification->setAssocId($row['assoc_id']);
308 
309  HookRegistry::call('NotificationDAO::_fromRow', array(&$notification, &$row));
310 
311  return $notification;
312  }
313 }
314 
315 ?>
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)
setDateRead($notificationId, $dateRead)
& 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...
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