Advertisement
Guest User

UserSpice 4.3 Dev - Notification.php

a guest
Sep 1st, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.99 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. Feb 02 2016 - Ported US3.2.1 top-nav
  8.  
  9. This program is free software: you can redistribute it and/or modify
  10. it under the terms of the GNU General Public License as published by
  11. the Free Software Foundation, either version 3 of the License, or
  12. (at your option) any later version.
  13.  
  14. This program is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. GNU General Public License for more details.
  18.  
  19. You should have received a copy of the GNU General Public License
  20. along with this program.  If not, see <http://www.gnu.org/licenses/>.
  21. */
  22.  
  23. class Notification
  24. {
  25.     private $db, $user_id, $error, $unread, $day_limit;
  26.     private $notifications = array();
  27.  
  28.     public function __construct($user_id, $day_limit = 7) {
  29.         $this->db = DB::getInstance();
  30.         $this->user_id = $user_id;
  31.         $this->day_limit = $day_limit;
  32.         $this->removeOldNotifications($user_id);
  33.         $this->getAllNotifications();
  34.     }
  35.  
  36.     private function getAllNotifications() {
  37.         try {
  38.             $this->notifications = $this->db->query('SELECT * FROM notifications WHERE user_id = ? 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 removeOldNotifications($user_id) {
  50.         try {
  51.             $this->db->query('DELETE FROM notifications WHERE user_id = ? AND is_read=1 AND date_created < NOW() - INTERVAL ? DAY', array($user_id, $this->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