<?php
namespace App\Controller\adminController;
use App\Controller\basisController\AllgemeineFunktionenController;
use App\Controller\basisController\UserBasisController;
use App\Entity\CampusOnlineStatistik;
use App\Entity\CampusSeitenStatistik;
use App\Repository\BuchungenRepository;
use App\Repository\CampusOnlineStatistikRepository;
use App\Repository\CampusSeitenStatistikRepository;
use App\Repository\ChatRepository;
use App\Repository\FirmenGruppenRepository;
use App\Repository\FirmenMitgliederanfrageRepository;
use App\Repository\FirmenProjekteAufgabenUserRepository;
use App\Repository\FirmenProjekteAufgabenUserStundenRepository;
use App\Repository\UserFavoritenRepository;
use App\Repository\UserNotificationRepository;
use App\Repository\UserRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class NotificationController extends AbstractController
{
#[Route('/notification/message', name: 'notification_message')]
public function index(UserNotificationRepository $notificationRepository,
FirmenMitgliederanfrageRepository $firmenMitgliederanfrageRepository,
UserBasisController $userBasisController,
BuchungenRepository $buchungenRepository,
UserRepository $userRepository,
EntityManagerInterface $entityManager): Response
{
$userId = $this->getUser()?->getId();
$notifications = null;
$buchungsanfragen = null;
$notificationsNeueMitglieder = null;
if ($userId != null) {
$notifications = $notificationRepository->findBy(['user' => $userId], ['datum' => 'DESC']);
$notificationsNeueMitglieder = $firmenMitgliederanfrageRepository->findBy(['firma' => $this->getUser()->getFirmenProfil()]);
}
if (in_array("ROLE_FIRMA_ADMIN_SCHULUNG_ANFRAGEN", $this->getUser()->getRoles())) {
$buchungsanfragen = $buchungenRepository->findBy(['bezahlstatus' => 9, 'firma' => $this->getUser()->getFirmenProfil()]);
}
$user = $userRepository->findOneBy(['id' => $userId]);
if (!$this->isGranted('IS_IMPERSONATOR')) {
$user->setLetzterLogin(new \DateTime('now', new \DateTimeZone('Europe/Berlin')));
}
$entityManager->persist($user);
$entityManager->flush();
return $this->render('nav/admin_topbar_notification.html.twig', [
'lable' => $userBasisController->getLableInternerBereich(),
'buchungsanfragen' => $buchungsanfragen,
'notifications' => $notifications,
'notificationsNeueMitglieder' => $notificationsNeueMitglieder,
]);
}
#[Route('/notification/message/user', name: 'notification_message_user')]
public function notification_message_user(Request $request,
UserNotificationRepository $notificationRepository,
UserRepository $userRepository,
UserBasisController $userBasisController,
UserFavoritenRepository $userFavoritenRepository,
CampusSeitenStatistikRepository $campusSeitenStatistikRepository,
FirmenProjekteAufgabenUserRepository $firmenProjekteAufgabenUserRepository,
FirmenProjekteAufgabenUserStundenRepository $firmenProjekteAufgabenUserStundenRepository,
EntityManagerInterface $entityManager): Response
{
$datum = new \DateTime('now', new \DateTimeZone('Europe/Berlin'));
$userId = $this->getUser()?->getId();
$firmaId = $this->getUser()?->getFirmenProfil()?->getId();
$notifications = null;
$favoritenanzahl = 0;
$notificationProjekte = [];
$notificationProjekteTimer = null;
if ($userId != null) {
$notifications = $notificationRepository->findBy(['user' => $userId], ['datum' => 'DESC']);
$favoritenanzahl = count($userFavoritenRepository->findBy(['user_id' => $userId]));
$user = $userRepository->findOneBy(['id' => $userId]);
if (!$this->isGranted('IS_IMPERSONATOR')) {
$user->setLetzterLogin($datum);
}
$entityManager->persist($user);
$entityManager->flush();
$url = $request->get('url');
$domain = $_ENV["WEBSITE_URL"];
/*
$domain = 'https://campus.test';
*/
// TODO: Seitenstatistik zur Zeit abgeschalten
if ($userId == 0) {
if (!$this->isGranted('IS_IMPERSONATOR') && !in_array("ROLE_CELSEO_MASTERADMIN", $this->getUser()->getRoles())) {
if (strlen($url) > 0 && str_contains($url, $domain)) {
$speichern = true;
$letzteStatistik = $campusSeitenStatistikRepository->findOneBy(['user_id' => $userId], ['datum' => 'DESC']);
if ($letzteStatistik == null || $letzteStatistik->getUrl() != $url) {
if ($url == $domain . '/' || $url == $domain . '/login') {
$speichern = false;
}
if ($url == $domain . '/user/konfigurator/auswahl' || $url == $domain . '/user/firma/startseite/0/info') {
$speichern = false;
}
if ($url == $domain . '/user/profil/mein_profil/info/bearbeiten' || $url == $domain . '/user/firma/startseite/0/info') {
$speichern = false;
}
if ($speichern) {
$campusStatistik = new CampusSeitenStatistik();
$campusStatistik->setUserId($userId);
$campusStatistik->setFirmaId($firmaId);
$campusStatistik->setUrl($request->get('url'));
$campusStatistik->setDatum($datum);
if ($request->getSession()->get('appUser')) {
$campusStatistik->setOberflaeche('App');
} else {
$campusStatistik->setOberflaeche('Browser');
}
$entityManager->persist($campusStatistik);
$entityManager->flush();
}
}
}
}
}
// Campus Online Statistik
/* $this->session = $requestStack->getSession();
$this->session->start();
if ($this->session->get('online_datum') == null || $this->session->get('online_datum') != $datum->format('Y-m-d') . '-' . $userId) {
$onlineStatistik = $campusOnlineStatistikRepository->findOneBy(['datum' => $datum]);
if ($onlineStatistik == null) {
$onlineStatistik = new CampusOnlineStatistik();
$onlineStatistik->setDatum($datum);
$onlineStatistik->setUsers('[' . $userId . ']');
} else {
$alleUser = json_decode($onlineStatistik->getUsers(),true);
if (!in_array($userId, $alleUser)) {
$alleUser[] = $userId;
}
$onlineStatistik->setUsers(json_encode($alleUser));
}
$entityManager->persist($onlineStatistik);
$entityManager->flush();
$this->session->set('online_datum', $datum->format('Y-m-d') . '-' . $userId);
}*/
//$allgemeineFunktionenController->getOnlineStatistik();
$notificationProjekte = $firmenProjekteAufgabenUserRepository->findAlleUngelesenenStellen($userId, $firmaId);
$notificationProjekteTimer = $firmenProjekteAufgabenUserStundenRepository->findArbeitsstundenOffenerTimer($this->getUser()->getId());
}
return $this->render('nav/user_navigation_notification.html.twig', [
'lable' => $userBasisController->getLableInternerBereich(),
'notifications' => $notifications,
'anzahlNotificationProjekte' => count($notificationProjekte),
'notificationProjekteTimer' => $notificationProjekteTimer,
'favoritenanzahl' => $favoritenanzahl,
]);
}
#[Route('/notification/chat/loeschen', name: 'notification_chat_loeschen')]
public function notificationChatLoeschen(Request $request,
ChatRepository $chatRepository,
UserNotificationRepository $notificationRepository,
EntityManagerInterface $entityManager): Response
{
$sender = $request->get('sender');
$gruppenID = $request->get('gruppenID');
$userId = $this->getUser()?->getId();
$notifications = $notificationRepository->findBy(['user' => $userId, 'type' => 2, 'von_user_id' => $sender, 'link' => $gruppenID]);
foreach ($notifications as $notification) {
$entityManager->remove($notification);
$entityManager->flush();
}
if ($gruppenID == -1) {
$chatMessages = $chatRepository->findBy(['user_id' => $sender, 'chatpartner_id' => $userId, 'gelesen' => 0]);
foreach ($chatMessages as $message) {
$message->setGelesen(1);
$entityManager->persist($message);
$entityManager->flush();
}
}
$returnData['error'] = false;
return new JsonResponse($returnData);
}
#[Route('/notification/chat/abfragen', name: 'notification_chat_abfragen')]
public function notification_chat_abfragen(Request $request,
FirmenGruppenRepository $firmenGruppenRepository,
UserRepository $userRepository,
UserNotificationRepository $notificationRepository,
EntityManagerInterface $entityManager,
UserBasisController $userBasisController,
ChatRepository $chatRepository): Response
{
$ungelesen = false;
$chatPartnerId = $request->get('chatPartner');
$type = $request->get('type');
$userId = $this->getUser()->getId();
$lastChatID = $request->get('lastChatID');
$fehlendesChatHTML= '';
$meinProfilbild = $this->getUser()->getUserProfil()->getProfilbild();
if ($meinProfilbild != null) {
$meinBild = $this->generateUrl('admin_user_profilbild', ['id' => $userId]) . '?v=' . $meinProfilbild;
} else {
$meinBild = $this->generateUrl('admin_user_profilbild', ['id' => 0]);
}
if($type === 'single') {
$notification = $notificationRepository->findOneBy(['user' => $chatPartnerId, 'type' => 2, 'link' => '-1', 'von_user_id' => $userId, 'gelesen' => 0]);
if ($notification != null) {
$ungelesen = true;
}
$meineNotification = $notificationRepository->findOneBy(['user' => $userId, 'type' => 2, 'link' => '-1', 'von_user_id' => $chatPartnerId, 'gelesen' => 0]);
if ($meineNotification != null) {
$entityManager->remove($meineNotification);
$entityManager->flush();
}
$lastChatMessage = $chatRepository->findLetzteChatnachricht($userId, $chatPartnerId);
if (count($lastChatMessage) > 0) {
$neusteChatID = $lastChatMessage[0]->getId();
if($lastChatID < $neusteChatID) {
$chatMessages = $chatRepository->getFehlendeChatForUser($userId, $chatPartnerId, $lastChatID);
$userAnsprechpartner = $userRepository->findOneBy(['id' => $chatPartnerId]);
$ansprechpartnerProfilbild = $userAnsprechpartner->getUserProfil()->getProfilbild();
if ($ansprechpartnerProfilbild != null) {
$ansprechpartnerBild = $this->generateUrl('user_profilbild_laden', ['id' => $chatPartnerId]) . '?v=' . $ansprechpartnerProfilbild;
} else {
$ansprechpartnerBild = $this->generateUrl('user_profilbild_laden', ['id' => 0]);
}
foreach ($chatMessages as $chatMessage) {
$selfMessage = false;
$chatGelesen = -1;
if ($chatMessage->getUserId() == $userId) {
$senderName = $this->getUser()->getUserProfil()->getVorname() . ' ' . $this->getUser()->getUserProfil()->getNachname();
$selfMessage = true;
$profilBildLink = $meinBild;
$chatGelesen = $chatMessage->getGelesen();
} else {
$senderName = $userAnsprechpartner->getUserProfil()->getVorname() . ' ' . $userAnsprechpartner->getUserProfil()->getNachname();
$profilBildLink = $ansprechpartnerBild;
if ($chatMessage->getGelesen() == 0) {
$chatMessage->setGelesen(1);
$entityManager->persist($chatMessage);
$entityManager->flush();
}
}
$nachricht = $chatMessage->getNachricht();
$zeit = $chatMessage->getGesendetAm()->format('H:i');
//$fehlendesChatHTML .= $userBasisController->getUserBC_Chateintrag($senderName, $selfMessage, $profilBildLink, $nachricht, $zeit, null, $chatMessage->getId(), $chatGelesen, $chatMessage->getDateiname());
$fehlendesChatHTML .= $userBasisController->getUserBC_Chateintrag2($chatMessage->getId(), null);
}
}
}
} else {
$firmenGruppe = $firmenGruppenRepository->findOneBy(['id' => $chatPartnerId]);
$lastChatMessage = $chatRepository->findOneBy(['gruppe' => $firmenGruppe], ['gesendet_am' => 'DESC']);
if ($lastChatMessage != null) {
$neusteChatID = $lastChatMessage->getId();
if($lastChatID < $neusteChatID) {
$chatMessages = $chatRepository->getFehlendeChatForGroup($chatPartnerId, $lastChatID);
foreach ($chatMessages as $chatMessage) {
$userAnsprechpartner = $userRepository->findOneBy(['id' => $chatMessage->getUserId()]);
$ansprechpartnerProfilbild = $userAnsprechpartner->getUserProfil()->getProfilbild();
if ($ansprechpartnerProfilbild != null) {
$ansprechpartnerBild = $this->generateUrl('user_profilbild_laden', ['id' => $userAnsprechpartner->getId()]) . '?v=' . $ansprechpartnerProfilbild;
} else {
$ansprechpartnerBild = $this->generateUrl('user_profilbild_laden', ['id' => 0]);
}
$senderName = $userAnsprechpartner->getUserProfil()->getVorname() . ' ' . $userAnsprechpartner->getUserProfil()->getNachname();
$selfMessage = $chatMessage->getUserId() == $userId;
$profilBildLink = $ansprechpartnerBild;
/* $selfMessage = false;
if ($chatMessage->getUserId() == $userId) {
$senderName = $this->getUser()->getUserProfil()->getVorname() . ' ' . $this->getUser()->getUserProfil()->getNachname();
$selfMessage = true;
$profilBildLink = $meinBild;
} else {
$senderName = $userAnsprechpartner->getUserProfil()->getVorname() . ' ' . $userAnsprechpartner->getUserProfil()->getNachname();
$profilBildLink = $ansprechpartnerBild;
}*/
$nachricht = $chatMessage->getNachricht();
$zeit = $chatMessage->getGesendetAm()->format('H:i');
//$fehlendesChatHTML .= $userBasisController->getUserBC_Chateintrag($senderName, $selfMessage, $profilBildLink, $nachricht, $zeit, null, $chatMessage->getId(), -1,$chatMessage->getDateiname());
$fehlendesChatHTML .= $userBasisController->getUserBC_Chateintrag2($chatMessage->getId(), null);
}
}
}
}
$returnData['ungelesen'] = $ungelesen;
$returnData['fehlenderChat'] = $fehlendesChatHTML;
return new JsonResponse($returnData);
}
#[Route('/notification/chat/letzte_chat_id', name: 'notification_chat_letzte_chat_id')]
public function notification_chat_letzte_chat_id(Request $request,
ChatRepository $chatRepository): Response
{
$neueChatId = 0;
$chatPartner = $request->get('chatPartner');
$userId = $this->getUser()?->getId();
$chatMessage = $chatRepository->findOneBy(['user_id' => $userId, 'chatpartner_id' => $chatPartner], ['gesendet_am' => 'DESC']);
if ($chatMessage != null) {
$neueChatId = $chatMessage->getId();
}
$returnData['neueChatId'] = $neueChatId;
return new JsonResponse($returnData);
}
#[Route('/notification/alles/loeschen', name: 'notification_alles_loeschen', methods: ['GET'])]
public function notificationAllesLoeschen(Request $request, UserNotificationRepository $notificationRepository, EntityManagerInterface $entityManager): Response
{
$notifications = $notificationRepository->findBy(['user' => $this->getUser()?->getId()]);
if ($notifications != null) {
foreach ($notifications as $notification) {
$entityManager->remove($notification);
$entityManager->flush();
}
}
$returnData['error'] = false;
return new JsonResponse($returnData);
}
}