/** * Add notification for user * @param string $uri * @param string $msg * @param array|null $vars */ public function add($uri, $msg = self::MSG_DEFAULT, array $vars = null) { // save data into database $record = new UserNotification(); $record->user_id = $this->_targetId; $record->uri = $uri; $record->msg = $msg; if ($vars !== null) { $record->vars = $vars; } $record->save(); }
/** * Cleanup tables as scheduled action */ public static function cleanupTablesSchedule() { // calculate date (now - 1week) for sql query $date = (new \DateTime('now'))->modify('-1 week')->format('Y-m-d'); UserNotification::where('created_at', '<=', $date)->delete(); UserLog::where('created_at', '<=', $date)->delete(); }
/** * Get user p.m and notifications count * @return string * @throws ForbiddenException */ public function actionNotifications() { // check if authed if (!App::$User->isAuth()) { throw new ForbiddenException('Auth required'); } $this->setJsonHeader(); // get user object $user = App::$User->identity(); // get messages count $messagesCount = Message::where('target_id', '=', $user->id)->where('readed', '=', 0)->count(); // get notifications count $notificationsCount = UserNotification::where('user_id', '=', $user->id)->where('readed', '=', 0)->count(); return json_encode(['status' => 1, 'notify' => $notificationsCount, 'messages' => $messagesCount, 'summary' => $notificationsCount + $messagesCount]); }