This repository has been archived on 2024-06-26. You can view files and clone it, but cannot push or open issues or pull requests.
sakura/app/Notification.php

143 lines
3.1 KiB
PHP
Raw Normal View History

2016-03-26 16:36:58 +00:00
<?php
/**
* Notification object.
* @package Sakura
*/
namespace Sakura;
/**
* Notification!
* @package Sakura
* @author Julian van de Groep <me@flash.moe>
*/
class Notification
{
2016-08-05 02:35:37 +00:00
/**
* The identifier.
* @var int
*/
2016-03-26 16:36:58 +00:00
public $id = 0;
2016-08-05 02:35:37 +00:00
/**
* The id of the user this notification is intended for.
* @var int
*/
2016-03-26 16:36:58 +00:00
public $user = 0;
2016-08-05 02:35:37 +00:00
/**
* The timestamp when this notification was created.
* @var int
*/
2016-03-26 16:36:58 +00:00
public $time = 0;
2016-08-05 02:35:37 +00:00
/**
* Whether the user has already read this notification.
* @var bool
*/
2016-03-26 16:36:58 +00:00
public $read = false;
2016-08-05 02:35:37 +00:00
/**
* Title of the notification.
* @var string
*/
2016-03-26 16:36:58 +00:00
public $title = "Notification";
2016-08-05 02:35:37 +00:00
/**
* The rest of the content
* @var string
*/
2016-03-26 16:36:58 +00:00
public $text = "";
2016-08-05 02:35:37 +00:00
/**
* The url this notification should link to when clicked on.
* @var string
*/
2016-03-26 16:36:58 +00:00
public $link = "";
2016-08-05 02:35:37 +00:00
/**
* The image url to display.
* @var string
*/
2016-03-26 16:36:58 +00:00
public $image = "";
2016-08-05 02:35:37 +00:00
/**
* The amount of time this notification should be displayed for
* @var int
*/
2016-03-26 16:36:58 +00:00
public $timeout = 0;
2016-08-05 02:35:37 +00:00
/**
* The constructor.
* @param int $id
*/
2016-03-26 16:36:58 +00:00
public function __construct($id = 0)
{
// Get notification data from the database
$data = DB::table('notifications')
->where('alert_id', $id)
->get();
// Check if anything was returned and assign data
if ($data) {
$data = $data[0];
2016-08-03 17:47:46 +00:00
$this->id = intval($data->alert_id);
$this->user = intval($data->user_id);
$this->time = intval($data->alert_timestamp);
2016-03-26 16:36:58 +00:00
$this->read = intval($data->alert_read) !== 0;
$this->title = $data->alert_title;
$this->text = $data->alert_text;
$this->link = $data->alert_link;
$this->image = $data->alert_img;
2016-08-03 17:47:46 +00:00
$this->timeout = intval($data->alert_timeout);
2016-03-26 16:36:58 +00:00
}
}
2016-08-05 02:35:37 +00:00
/**
* Saving changes to this notification.
*/
2016-03-26 16:36:58 +00:00
public function save()
{
// Create submission data, insert and update take the same format
$data = [
'user_id' => $this->user,
'alert_timestamp' => $this->time,
'alert_read' => $this->read ? 1 : 0,
'alert_title' => $this->title,
'alert_text' => $this->text,
'alert_link' => $this->link,
'alert_img' => $this->image,
'alert_timeout' => $this->timeout,
];
// Update if id isn't 0
if ($this->id) {
DB::table('notifications')
->where('alert_id', $this->id)
->update($data);
} else {
$this->id = DB::table('notifications')
->insertGetId($data);
}
}
2016-08-05 02:35:37 +00:00
/**
* Toggle the read status
*/
2016-03-26 16:36:58 +00:00
public function toggleRead()
{
// Set read to the negative value of itself
$this->read = !$this->read;
}
2016-08-05 02:35:37 +00:00
/**
* Get the user object.
* @return User
*/
public function userData()
{
return User::construct($this->user);
}
2016-03-26 16:36:58 +00:00
}