Open Journal Systems  2.4.4
 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 
33  function &getById($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 
57  function &getByUserId($userId, $level = NOTIFICATION_LEVEL_NORMAL, $type = null, $contextId = null, $rangeInfo = null) {
58  $params = array((int) $userId, (int) $level);
59  if ($type) $params[] = (int) $type;
60  if ($contextId) $params[] = (int) $contextId;
61 
62  $result =& $this->retrieveRange(
63  'SELECT * FROM notifications WHERE user_id = ? AND level = ?' . (isset($type) ?' AND type = ?' : '') . (isset($contextId) ?' AND context_id = ?' : '') . ' ORDER BY date_created DESC',
64  $params, $rangeInfo
65  );
66 
67  $returner = new DAOResultFactory($result, $this, '_returnNotificationFromRow');
68 
69  return $returner;
70  }
71 
82  function &getByAssoc($assocType, $assocId, $userId = null, $type = null, $contextId = null) {
83  $params = array((int) $assocType, (int) $assocId);
84  if ($userId) $params[] = (int) $userId;
85  if ($contextId) $params[] = (int) $contextId;
86  if ($type) $params[] = (int) $type;
87 
88  $result =& $this->retrieveRange(
89  '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',
90  $params
91  );
92 
93  $returner = new DAOResultFactory($result, $this, '_returnNotificationFromRow');
94 
95  return $returner;
96  }
97 
104  function setDateRead($notificationId, $dateRead = null) {
105  $dateRead = isset($dateRead) ? $dateRead : Core::getCurrentDate();
106 
107  $this->update(
108  sprintf('UPDATE notifications
109  SET date_read = %s
110  WHERE notification_id = ?',
111  $this->datetimeToDB($dateRead)),
112  (int) $notificationId
113  );
114 
115  return $dateRead;
116  }
117 
122  function newDataObject() {
123  return new Notification();
124  }
125 
131  function &_returnNotificationFromRow($row) {
132  $notification = $this->newDataObject();
133  $notification->setId($row['notification_id']);
134  $notification->setUserId($row['user_id']);
135  $notification->setLevel($row['level']);
136  $notification->setDateCreated($this->datetimeFromDB($row['date_created']));
137  $notification->setDateRead($this->datetimeFromDB($row['date_read']));
138  $notification->setContextId($row['context_id']);
139  $notification->setType($row['type']);
140  $notification->setAssocType($row['assoc_type']);
141  $notification->setAssocId($row['assoc_id']);
142 
143  HookRegistry::call('NotificationDAO::_returnNotificationFromRow', array(&$notification, &$row));
144 
145  return $notification;
146  }
147 
153  function insertObject(&$notification) {
154  $this->update(
155  sprintf('INSERT INTO notifications
156  (user_id, level, date_created, context_id, type, assoc_type, assoc_id)
157  VALUES
158  (?, ?, %s, ?, ?, ?, ?)',
160  array(
161  (int) $notification->getUserId(),
162  (int) $notification->getLevel(),
163  (int) $notification->getContextId(),
164  (int) $notification->getType(),
165  (int) $notification->getAssocType(),
166  (int) $notification->getAssocId()
167  )
168  );
169  $notification->setId($this->getInsertNotificationId());
170 
171  return $notification->getId();
172  }
173 
179  function build(&$notification) {
180  $this->update('DELETE FROM notifications
181  WHERE context_id = ? AND level = ? AND type = ? AND user_id = ?
182  AND assoc_type = ? AND assoc_id = ?',
183  array(
184  (int) $notification->getContextId(),
185  (int) $notification->getLevel(),
186  (int) $notification->getType(),
187  $notification->getUserId(),
188  $notification->getAssocType(),
189  $notification->getAssocId()
190  )
191  );
192  $this->insertObject($notification);
193  }
194 
201  function deleteById($notificationId, $userId = null) {
202  $params = array((int) $notificationId);
203  if (isset($userId)) $params[] = (int) $userId;
204  $this->update(
205  'DELETE FROM notifications WHERE notification_id = ?' . (isset($userId) ? ' AND user_id = ?' : ''),
206  $params
207  );
208  if ($this->getAffectedRows()) {
209  // If a notification was deleted (possibly validating
210  // $userId in the process) delete associated settings.
211  $notificationSettingsDao =& DAORegistry::getDAO('NotificationSettingsDAO'); /* @var $notificationSettingsDaoDao NotificationSettingsDAO */
212  $notificationSettingsDao->deleteSettingsByNotificationId($notificationId);
213  return true;
214  }
215  return false;
216  }
217 
223  function deleteObject($notification) {
224  return $this->deleteById($notification->getId());
225  }
226 
236  function deleteByAssoc($assocType, $assocId, $userId = null, $type = null, $contextId = null) {
237  $notificationsFactory =& $this->getByAssoc($assocType, $assocId, $userId = null, $type = null, $contextId = null);
238  while ($notification =& $notificationsFactory->next()) {
239  $this->deleteObject($notification);
240  unset($notification);
241  }
242  }
243 
249  return $this->getInsertId('notifications', 'notification_id');
250  }
251 
260  function getNotificationCount($read = true, $userId, $contextId = null, $level = NOTIFICATION_LEVEL_NORMAL) {
261  $params = array((int) $userId, (int) $level);
262  if ($contextId) $params[] = (int) $contextId;
263 
264  $result =& $this->retrieve(
265  'SELECT count(*) FROM notifications WHERE user_id = ? AND date_read IS' . ($read ? ' NOT' : '') . ' NULL AND level = ?'
266  . (isset($contextId) ? ' AND context_id = ?' : ''),
267  $params
268  );
269 
270  $returner = $result->fields[0];
271 
272  $result->Close();
273  unset($result);
274 
275  return $returner;
276  }
277 
283  function transferNotifications($oldUserId, $newUserId) {
284  $this->update(
285  'UPDATE notifications SET user_id = ? WHERE user_id = ?',
286  array($newUserId, $oldUserId)
287  );
288  }
289 }
290 
291 ?>
transferNotifications($oldUserId, $newUserId)
Operations for retrieving and modifying objects from a database.
Definition: DAO.inc.php:29
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:83
getInsertId($table= '', $id= '', $callHooks=true)
Definition: DAO.inc.php:252
& getById($notificationId)
& _returnNotificationFromRow($row)
getCurrentDate($ts=null)
Definition: Core.inc.php:94
& getByUserId($userId, $level=NOTIFICATION_LEVEL_NORMAL, $type=null, $contextId=null, $rangeInfo=null)
& retrieveRange($sql, $params=false, $dbResultRange=null, $callHooks=true)
Definition: DAO.inc.php:176
build(&$notification)
deleteByAssoc($assocType, $assocId, $userId=null, $type=null, $contextId=null)
deleteObject($notification)
datetimeFromDB($dt)
Definition: DAO.inc.php:316
getAffectedRows()
Definition: DAO.inc.php:261
Wrapper around ADORecordSet providing &quot;factory&quot; features for generating objects from DAOs...
setDateRead($notificationId, $dateRead=null)
& getDAO($name, $dbconn=null)
deleteById($notificationId, $userId=null)
call($hookName, $args=null)
insertObject(&$notification)
datetimeToDB($dt)
Definition: DAO.inc.php:296
& getByAssoc($assocType, $assocId, $userId=null, $type=null, $contextId=null)
update($sql, $params=false, $callHooks=true, $dieOnError=true)
Definition: DAO.inc.php:211