exists()) { if (empty($title) || empty($content)) { throw new Exception($Strings->get("invalid parameters", false)); } $timestamp = date("Y-m-d H:i:s"); if (!empty($timestamp)) { $timestamp = date("Y-m-d H:i:s", strtotime($timestamp)); } $database->insert('notifications', ['uid' => $user->getUID(), 'timestamp' => $timestamp, 'title' => $title, 'content' => $content, 'url' => $url, 'seen' => 0, 'sensitive' => $sensitive]); return $database->id() * 1; } throw new Exception($Strings->get("user does not exist", false)); } /** * Fetch all notifications for a user. * @global $database * @param User $user * @param bool $all If false, only returns unseen notifications. * @return array * @throws Exception */ public static function get(User $user, bool $all = true) { global $database, $Strings; if ($user->exists()) { if ($all) { $notifications = $database->select('notifications', ['notificationid (id)', 'timestamp', 'title', 'content', 'url', 'seen', 'sensitive'], ['uid' => $user->getUID(), 'ORDER' => ['seen', 'timestamp' => 'DESC']]); } else { $notifications = $database->select('notifications', ['notificationid (id)', 'timestamp', 'title', 'content', 'url', 'seen', 'sensitive'], ["AND" => ['uid' => $user->getUID(), 'seen' => 0], 'ORDER' => ['timestamp' => 'DESC']]); } for ($i = 0; $i < count($notifications); $i++) { $notifications[$i]['id'] = $notifications[$i]['id'] * 1; $notifications[$i]['seen'] = ($notifications[$i]['seen'] == "1" ? true : false); $notifications[$i]['sensitive'] = ($notifications[$i]['sensitive'] == "1" ? true : false); } return $notifications; } throw new Exception($Strings->get("user does not exist", false)); } /** * Mark the notification identified by $id as read. * @global $database * @global $Strings * @param User $user * @param int $id * @throws Exception */ public static function read(User $user, int $id) { global $database, $Strings; if ($user->exists()) { if ($database->has('notifications', ['AND' => ['uid' => $user->getUID(), 'notificationid' => $id]])) { $database->update('notifications', ['seen' => 1], ['AND' => ['uid' => $user->getUID(), 'notificationid' => $id]]); return true; } throw new Exception($Strings->get("invalid parameters", false)); } throw new Exception($Strings->get("user does not exist", false)); } public static function delete(User $user, int $id) { global $database, $Strings; if ($user->exists()) { if ($database->has('notifications', ['AND' => ['uid' => $user->getUID(), 'notificationid' => $id]])) { $database->delete('notifications', ['AND' => ['uid' => $user->getUID(), 'notificationid' => $id]]); return true; } throw new Exception($Strings->get("invalid parameters", false)); } throw new Exception($Strings->get("user does not exist", false)); } }