18 import(
'classes.notification.Notification');
28 function getById($notificationId, $userId =
null) {
29 $params = array((
int) $notificationId);
30 if ($userId) $params[] = (int) $userId;
35 WHERE notification_id = ?
36 ' . ($userId?
' AND user_id = ?':
''),
40 $notification = $this->
_fromRow($result->GetRowAssoc(
false));
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;
62 'SELECT * FROM notifications WHERE user_id = ? AND level = ?' . (isset($type) ?
' AND type = ?' :
'') . (isset($contextId) ?
' AND context_id = ?' :
'') .
' ORDER BY date_created DESC',
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;
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',
106 sprintf(
'UPDATE notifications
108 WHERE notification_id = ?',
110 (
int) $notificationId
131 sprintf(
'INSERT INTO notifications
132 (user_id, level, date_created, context_id, type, assoc_type, assoc_id)
134 (?, ?, %s, ?, ?, ?, ?)',
137 (
int) $notification->getUserId(),
138 (
int) $notification->getLevel(),
139 (
int) $notification->getContextId(),
140 (
int) $notification->getType(),
141 (
int) $notification->getAssocType(),
142 (
int) $notification->getAssocId()
147 return $notification->getId();
160 function build($contextId, $level, $type, $assocType, $assocId, $userId =
null) {
169 if ($userId) $params[] = (int) $userId;
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 = ?' :
''),
178 $notification->setContextId($contextId);
179 $notification->setLevel($level);
180 $notification->setType($type);
181 $notification->setAssocType($assocType);
182 $notification->setAssocId($assocId);
183 $notification->setUserId($userId);
186 if ($notificationId) {
187 $notification->setId($notificationId);
188 return $notification;
201 $params = array((
int) $notificationId);
202 if (isset($userId)) $params[] = (int) $userId;
204 'DELETE FROM notifications WHERE notification_id = ?' . (isset($userId) ?
' AND user_id = ?' :
''),
211 $notificationSettingsDao->deleteSettingsByNotificationId($notificationId);
223 return $this->
deleteById($notification->getId());
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()) {
247 return $this->
_getInsertId(
'notifications',
'notification_id');
259 $params = array((
int) $userId, (
int) $level);
260 if ($contextId) $params[] = (int) $contextId;
263 'SELECT count(*) FROM notifications WHERE user_id = ? AND date_read IS' . ($read ?
' NOT' :
'') .
' NULL AND level = ?'
264 . (isset($contextId) ?
' AND context_id = ?' :
''),
268 $returner = $result->fields[0];
281 'UPDATE notifications SET user_id = ? WHERE user_id = ?',
282 array((
int) $newUserId, (
int) $oldUserId)
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']);
305 return $notification;