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) {
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 insertObject($notification) {
132  $this->update(
133  sprintf('INSERT INTO notifications
134  (user_id, level, date_created, context_id, type, assoc_type, assoc_id)
135  VALUES
136  (?, ?, %s, ?, ?, ?, ?)',
138  array(
139  (int) $notification->getUserId(),
140  (int) $notification->getLevel(),
141  (int) $notification->getContextId(),
142  (int) $notification->getType(),
143  (int) $notification->getAssocType(),
144  (int) $notification->getAssocId()
145  )
146  );
147  $notification->setId($this->getInsertId());
148 
149  return $notification->getId();
150  }
151 
162  function build($contextId, $level, $type, $assocType, $assocId, $userId = null) {
163  $params = array(
164  (int) $contextId,
165  (int) $level,
166  (int) $type,
167  (int) $assocType,
168  (int) $assocId
169  );
170 
171  if ($userId) $params[] = (int) $userId;
172 
173  $this->update('DELETE FROM notifications
174  WHERE context_id = ? AND level = ? AND type = ? AND assoc_type = ? AND assoc_id = ?'
175  . ($userId ? ' AND user_id = ?' : ''),
176  $params
177  );
178 
179  $notification = $this->newDataObject();
180  $notification->setContextId($contextId);
181  $notification->setLevel($level);
182  $notification->setType($type);
183  $notification->setAssocType($assocType);
184  $notification->setAssocId($assocId);
185  $notification->setUserId($userId);
186 
187  $notificationId = $this->insertObject($notification);
188  if ($notificationId) {
189  $notification->setId($notificationId);
190  return $notification;
191  } else {
192  return null;
193  }
194  }
195 
202  function deleteById($notificationId, $userId = null) {
203  $params = array((int) $notificationId);
204  if (isset($userId)) $params[] = (int) $userId;
205  $this->update(
206  'DELETE FROM notifications WHERE notification_id = ?' . (isset($userId) ? ' AND user_id = ?' : ''),
207  $params
208  );
209  if ($this->getAffectedRows()) {
210  // If a notification was deleted (possibly validating
211  // $userId in the process) delete associated settings.
212  $notificationSettingsDao = DAORegistry::getDAO('NotificationSettingsDAO'); /* @var $notificationSettingsDaoDao NotificationSettingsDAO */
213  $notificationSettingsDao->deleteSettingsByNotificationId($notificationId);
214  return true;
215  }
216  return false;
217  }
218 
224  function deleteObject($notification) {
225  return $this->deleteById($notification->getId());
226  }
227 
237  function deleteByAssoc($assocType, $assocId, $userId = null, $type = null, $contextId = null) {
238  $notificationsFactory = $this->getByAssoc($assocType, $assocId, $userId, $type, $contextId);
239  while ($notification = $notificationsFactory->next()) {
240  $this->deleteObject($notification);
241  }
242  }
243 
248  function getInsertId() {
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  return $returner;
274  }
275 
281  function transferNotifications($oldUserId, $newUserId) {
282  $this->update(
283  'UPDATE notifications SET user_id = ? WHERE user_id = ?',
284  array((int) $newUserId, (int) $oldUserId)
285  );
286  }
287 
293  function _fromRow($row) {
294  $notification = $this->newDataObject();
295  $notification->setId($row['notification_id']);
296  $notification->setUserId($row['user_id']);
297  $notification->setLevel($row['level']);
298  $notification->setDateCreated($this->datetimeFromDB($row['date_created']));
299  $notification->setDateRead($this->datetimeFromDB($row['date_read']));
300  $notification->setContextId($row['context_id']);
301  $notification->setType($row['type']);
302  $notification->setAssocType($row['assoc_type']);
303  $notification->setAssocId($row['assoc_id']);
304 
305  HookRegistry::call('NotificationDAO::_fromRow', array(&$notification, &$row));
306 
307  return $notification;
308  }
309 }
310 
311 ?>
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