Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- UserSpice 4
- An Open Source PHP User Management System
- by the UserSpice Team at http://UserSpice.com
- Feb 02 2016 - Ported US3.2.1 top-nav
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- class Notification
- {
- private $db, $user_id, $error, $unread, $day_limit;
- private $notifications = array();
- public function __construct($user_id, $day_limit = 7) {
- $this->db = DB::getInstance();
- $this->user_id = $user_id;
- $this->day_limit = $day_limit;
- $this->removeOldNotifications($user_id);
- $this->getAllNotifications();
- }
- private function getAllNotifications() {
- try {
- $this->notifications = $this->db->query('SELECT * FROM notifications WHERE user_id = ? ORDER BY date_created DESC', array($this->user_id))->results();
- foreach ($this->notifications as $row) {
- if ($row->is_read == 0) $this->unread++;
- }
- return true;
- } catch (\Exception $e) {
- $this->error = $e->getMessage();
- }
- return false;
- }
- public function removeOldNotifications($user_id) {
- try {
- $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));
- return true;
- } catch (\Exception $e) {
- $this->error = $e->getMessage();
- }
- return false;
- }
- public function addNotification($message, $user_id = -1) {
- if ($user_id == -1) $user_id = $this->user_id;
- try {
- if ($results = $this->db->query('INSERT INTO notifications (user_id, message) VALUES (?, ?)', array($user_id, $message))->results()) {
- $this->notifications[] = $results;
- return true;
- }
- else $this->error = 'Unable to query the database.';
- } catch (\Exception $e) {
- $this->error = $e->getMessage();
- }
- return false;
- }
- public function setRead($notification_id, $read = true) {
- try {
- if ($this->db->query('UPDATE notifications SET is_read = ? WHERE id = ?, date_read=NOW()', array($read, $notification_id))) {
- $this->getAllNotifications($this->user_id);
- $this->unread--;
- return true;
- }
- else $this->error = 'Unable to query the database.';
- } catch (\Exception $e) {
- $this->error = $e->getMessage();
- }
- return false;
- }
- public function setReadAll($read = true) {
- try {
- if ($this->db->query('UPDATE notifications SET is_read = ?, date_read=NOW() WHERE user_id = ?', array($read, $this->user_id))) {
- $this->getAllNotifications($this->user_id);
- $this->unread = 0;
- return true;
- }
- else $this->error = 'Unable to query the database.';
- } catch (\Exception $e) {
- $this->error = $e->getMessage();
- }
- return false;
- }
- public function getError() {
- echo $this->error;
- return $this->error;
- }
- public function getNotifications() {
- return $this->notifications;
- }
- public function getCount() {
- return count($this->notifications);
- }
- public function getUnreadCount() {
- return $this->unread;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement