/** * 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; }