static function clearAuthorizations($userId) { //Limpa as notificações de autorização que ja foram respondidas $notAuth = Notification::find()->where(['user_id' => $userId, 'type' => Notification::TYPE_AUTHORIZATION, 'viewed' => false])->all(); foreach ($notAuth as $not) { //Confere todas notificações do tipo autorização $auth = ConnectionAuth::findOne($not->info); if ($auth) { $connection = Connection::findOne($auth->connection_id); if ($connection) { $connections = Connection::find()->where(['reservation_id' => $connection->reservation_id])->all(); $answered = true; foreach ($connections as $conn) { //Confere todas conexões da reserva conferindo se alguma ainda esta pendente $auths = ConnectionAuth::find()->where(['domain' => $auth->domain, 'connection_id' => $conn->id])->all(); foreach ($auths as $a) { //Confere as autorizaçòes daquela conexão if ($a->type != ConnectionAuth::TYPE_WORKFLOW && $a->status == Connection::AUTH_STATUS_PENDING) { $answered = false; break; } } if ($answered == false) { break; } } if ($answered) { //Se ja foi respondida modifica notificação para visualizada $not->viewed = true; $not->save(); } } } } }
static function create($connection_id) { $connection = Connection::findOne($connection_id); if (!$connection) { return; } $reservation = Reservation::findOne($connection->reservation_id); if (!$reservation) { return; } if ($reservation->type == Reservation::TYPE_TEST) { return; } $user_id = $reservation->request_user_id; //Confere se já foi feita uma notificação de algum circuito desta reserva, se sim, reutiliza a mesma notificação $not = null; $notifications = Notification::find()->where(['user_id' => $reservation->request_user_id, 'type' => Notification::TYPE_RESERVATION])->all(); foreach ($notifications as $notification) { if ($notification->info == $reservation->id) { $not = $notification; break; } } if ($not) { //Se já existe, atualiza e coloca nova data //Pequena maquipulação do horário para que nunca existam duas notificações com o mesmo horário $date = new \DateTime('now', new \DateTimeZone("UTC")); $dateAux = $date->format("Y-m-d H:i:s"); while (Notification::find()->where(['user_id' => $user_id, 'date' => $dateAux])->one()) { $date->modify('-1 second'); $dateAux = $date->format("Y-m-d H:i:s"); } $not->date = $dateAux; $not->viewed = 0; $not->save(); } else { //Se for nova, cria notificação $not = new Notification(); $not->user_id = $reservation->request_user_id; //Pequena maquipulação do horário para que nunca existam duas notificações com o mesmo horário $date = new \DateTime('now', new \DateTimeZone("UTC")); $dateAux = $date->format("Y-m-d H:i:s"); while (Notification::find()->where(['user_id' => $user_id, 'date' => $dateAux])->one()) { $date->modify('-1 second'); $dateAux = $date->format("Y-m-d H:i:s"); } $not->date = $dateAux; $not->type = Notification::TYPE_RESERVATION; $not->viewed = 0; $not->info = (string) $reservation->id; $not->save(); } }
public function actionIndex() { $dataProvider = new ActiveDataProvider(['query' => Notification::find()->where(['user_id' => Yii::$app->user->getId()])->orderBy(['date' => SORT_DESC]), 'sort' => false, 'pagination' => false]); return $this->render('/index', array('data' => $dataProvider)); }
static function create($user_id, $type, $group, $domain = null, $date = null) { $notice = []; $notice[0] = $type; //Tipo da noticia $notice[1] = $group; if ($domain) { $notice[2] = $domain; } $not = new Notification(); $not->user_id = $user_id; //Pode receber uma data por parametro, neste caso, utiliza essa data como a data da criação da notificação if ($date) { $not->date = $date; } else { //Pequena maquipulação do horário para que nunca existam duas notificações com o mesmo horário $date = new \DateTime('now', new \DateTimeZone("UTC")); $dateAux = $date->format("Y-m-d H:i:s"); while (Notification::find()->where(['user_id' => $user_id, 'date' => $dateAux])->one()) { $date->modify('-1 second'); $dateAux = $date->format("Y-m-d H:i:s"); } $not->date = $dateAux; } $not->type = Notification::TYPE_NOTICE; $not->viewed = 0; $not->info = json_encode($notice); //Armazena todos dados em um JSON $not->save(); }
/** * GET NOTIFICATIONS * @param string $dateParam * @return string * Retorna o html com até 6 notificações, ja formatado para ser exibido. * Quando recebe uma data de entrada, a utiliza como limite, e retorna apenas o que vem depois dela */ public static function getNotifications($dateParam) { $userId = Yii::$app->user->getId(); if (!$dateParam) { AuthorizationNotification::clearAuthorizations($userId); } //Caso seja a primeira solicitação $array = ""; $max = 0; $date = null; //Le todas reservas anteriores a data limite, ou todas reservas, caso não exista uma data limite if ($dateParam) { $notifications = Notification::find()->where(['user_id' => $userId])->andWhere(['<', 'date', $_POST['date']])->orderBy(['date' => SORT_DESC])->all(); } else { $notifications = Notification::find()->where(['user_id' => $userId])->orderBy(['date' => SORT_DESC])->all(); } //Se não contem, gera aviso de que o usuário não possui notificações if (count($notifications) == 0) { $info = []; $info['date'] = null; $info['array'] = "<li style='text-align: center;'><span style='float: none !important;'><h2>" . Yii::t("notify", 'You don`t have notifications yet.') . "</h2></span></li>"; $info['more'] = false; return $info; } //Percorre as notificações gerando o HTML foreach ($notifications as $notification) { if ($max < 6) { $msg = ""; switch ($notification->type) { case self::TYPE_AUTHORIZATION: $msg = AuthorizationNotification::makeHtml($notification); break; case self::TYPE_RESERVATION: $msg = ReservationNotification::makeHtml($notification); $notification->viewed = true; $notification->save(); break; case self::TYPE_NOTICE: $msg = AaaNotification::makeHtml($notification); $notification->viewed = true; $notification->save(); break; case self::TYPE_TOPOLOGY: $msg = TopologyNotification::makeHtml($notification); $notification->viewed = true; $notification->save(); break; } if ($msg == "") { $notification->delete(); } else { $array .= $msg; $date = $notification->date; } } if ($msg != "") { $max++; } if ($max == 7) { break; } } $info = []; $info['date'] = $date; //Data da ultima notificação retornada, utilizada como limite para ler as proximas em leituras futuras $info['array'] = $array; //HTML a ser exibido if ($max == 7) { $info['more'] = true; } else { $info['more'] = false; } return $info; }