Advertisement
Guest User

UserSpice 4.3 Dev - Notification.php

a guest
Sep 15th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.15 KB | None | 0 0
  1. <?php
  2. /*
  3. UserSpice 4
  4. An Open Source PHP User Management System
  5. by the UserSpice Team at http://UserSpice.com
  6.  
  7. This program is free software: you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation, either version 3 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program.  If not, see <http://www.gnu.org/licenses/>.
  19. */
  20.  
  21. class Notification
  22. {
  23.     private $db, $user_id, $error, $unread, $archive_day_limit;
  24.     private $notifications = array();
  25.  
  26.     public function __construct($user_id, $all = false, $archive_day_limit = 7) {
  27.         $this->db = DB::getInstance();
  28.         $this->user_id = $user_id;
  29.         $this->archive_day_limit = $archive_day_limit;
  30.         if ($archive_day_limit > 0) $this->archiveOldNotifications($user_id);
  31.         $this->getAllNotifications($all);
  32.     }
  33.  
  34.     private function getAllNotifications($all) {
  35.         if ($all == false) $where = ' AND is_archived=0';
  36.         else $where = '';
  37.         try {
  38.             $this->notifications = $this->db->query('SELECT * FROM notifications WHERE user_id = ? '.$where.' ORDER BY date_created DESC', array($this->user_id))->results();
  39.             foreach ($this->notifications as $row) {
  40.                 if ($row->is_read == 0) $this->unread++;
  41.             }
  42.             return true;
  43.         } catch (\Exception $e) {
  44.             $this->error = $e->getMessage();
  45.         }
  46.         return false;
  47.     }
  48.  
  49.     public function archiveOldNotifications($user_id) {
  50.         try {
  51.             $this->db->query('UPDATE notifications SET is_archived=1 WHERE user_id = ? AND is_read=1 AND date_created < NOW() - INTERVAL ? DAY', array($user_id, $this->archive_day_limit));
  52.             return true;
  53.         } catch (\Exception $e) {
  54.             $this->error = $e->getMessage();
  55.         }
  56.         return false;
  57.     }
  58.  
  59.     public function addNotification($message, $user_id = -1) {
  60.         if ($user_id == -1) $user_id = $this->user_id;
  61.         try {
  62.             if ($results = $this->db->query('INSERT INTO notifications (user_id, message) VALUES (?, ?)', array($user_id, $message))->results()) {
  63.                 $this->notifications[] = $results;
  64.                 return true;
  65.             }
  66.             else $this->error = 'Unable to query the database.';
  67.         } catch (\Exception $e) {
  68.             $this->error = $e->getMessage();
  69.         }
  70.         return false;
  71.     }
  72.  
  73.     public function setRead($notification_id, $read = true) {
  74.         try {
  75.             if ($this->db->query('UPDATE notifications SET is_read = ? WHERE id = ?, date_read=NOW()', array($read, $notification_id))) {
  76.                 $this->getAllNotifications($this->user_id);
  77.                 $this->unread--;
  78.                 return true;
  79.             }
  80.             else $this->error = 'Unable to query the database.';
  81.         } catch (\Exception $e) {
  82.             $this->error = $e->getMessage();
  83.         }
  84.         return false;
  85.     }
  86.  
  87.     public function setReadAll($read = true) {
  88.         try {
  89.             if ($this->db->query('UPDATE notifications SET is_read = ?, date_read=NOW() WHERE user_id = ?', array($read, $this->user_id))) {
  90.                 $this->getAllNotifications($this->user_id);
  91.                 $this->unread = 0;
  92.                 return true;
  93.             }
  94.             else $this->error = 'Unable to query the database.';
  95.         } catch (\Exception $e) {
  96.             $this->error = $e->getMessage();
  97.         }
  98.         return false;
  99.     }
  100.  
  101.     public function getError() {
  102.         echo $this->error;
  103.         return $this->error;
  104.     }
  105.  
  106.     public function getNotifications() {
  107.         return $this->notifications;
  108.     }
  109.  
  110.     public function getCount() {
  111.         return count($this->notifications);
  112.     }
  113.  
  114.     public function getUnreadCount() {
  115.         return $this->unread;
  116.     }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement