<?php
namespace App\Controller;
use App\Entity\Notification;
use App\Services\ConfigurationService;
use App\Services\NotificationService;
use App\Services\PersonService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\Translation\TranslatorInterface;
class IndexController extends AbstractController
{
/**
* @Route(path="/", name="dashboard", methods={"GET"})
* @param PersonService $personService
* @param ConfigurationService $configurationService
* @return Response
*/
public function index(PersonService $personService, ConfigurationService $configurationService): Response
{
$countPersons = $personService->countPersonTypes();
$goalNumbers = $configurationService->getGoalNumbers();
return $this->render('dashboard.html.twig', [
// 'coordinators' => $countPersons['coordinators'],
// 'operators' => $countPersons['operators'],
'promoters' => $countPersons['promoters'],
'promoted' => $countPersons['promoted'],
'total' => $countPersons['promoters'] + $countPersons['promoted'],
// 'goalCoordinators' => $goalNumbers['coordinators'],
// 'goalOperators' => $goalNumbers['operators'],
'goalPromoters' => $goalNumbers['promoters'],
'goalPromoted' => $goalNumbers['promoted']
]);
}
/**
* @Route("/chart/person/suburb", methods={"GET"}, name="app_dashboard_person_suburb")
* @param PersonService $personService
* @return Response
*/
public function countPeopleBySuburbs(PersonService $personService): Response
{
$result = $personService->countPeopleBySuburb();
return $this->json(['data' => $result, 'status' => true]);
}
/**
* @Route("/chart/person/program", methods={"GET"}, name="app_dashboard_person_program")
* @param PersonService $personService
* @return Response
*/
public function countPeopleByProgram(PersonService $personService): Response
{
$result = $personService->countPeopleByHelpPrograms();
return $this->json(['data' => $result, 'status' => true]);
}
/**
* @Route("/chart/person/database", methods={"GET"}, name="app_dashboard_person_database")
* @param PersonService $personService
* @return Response
*/
public function countPeopleByExternalDatabase(PersonService $personService): Response
{
$personsByDatabase = $personService->countPersonsByExternalDatabase();
$totalPersons = $personService->countAll();
for ($i = 0; $i < count($personsByDatabase); $i++) {
$percent = ($personsByDatabase[$i]['persons'] * 100) / $totalPersons;
$personsByDatabase[$i]['persons'] = intval($percent);
}
return $this->json(['data' => $personsByDatabase, 'status' => true]);
}
public function sidebar(RequestStack $requestStack): Response
{
$currentRequest = $requestStack->getMainRequest();
$currentPath = $currentRequest->attributes->get('_route');
return $this->render('structure/_sidebar.html.twig', [
'currentPath' => $currentPath
]);
}
public function renderNotificationCount(NotificationService $notificationService): Response
{
return $this->render('structure/_notification_count.html.twig');
// return $this->render('structure/_notification_count.html.twig', [
// 'count' => $notificationService->countByUser($this->getUser()->getId())
// ]);
}
/**
* @Route("/notification/find/user", methods={"GET"}, name="app_notification_find_user")
* @param NotificationService $notificationService
* @param TranslatorInterface $translator
* @return \Symfony\Component\HttpFoundation\JsonResponse
*/
public function findNotificationForUser(NotificationService $notificationService, TranslatorInterface $translator): Response
{
$notifications = $notificationService->findByUser($this->getUser()->getId());
$response = [];
foreach ($notifications as $notification) {
$foundType = false;
for ($i = 0; $i < count($response); $i++) {
if ($notification->getType() == $response[$i]['type']) {
$foundType = true;
$response[$i]['notifications'][] = [
'id' => $notification->getId(),
'generatedAt' => date_format($notification->getGeneratedAt(), 'Y-m-d H:i'),
'generatedBy' => $notification->getGeneratedBy()->getUserIdentifier(),
'extraInfo' => $notification->getExtraInfo()
];
}
}
if (!$foundType) {
$response[] = [
'type' => $notification->getType(),
'text' => $translator->trans($notification->getType()),
'notifications' => [[
'id' => $notification->getId(),
'generatedAt' => date_format($notification->getGeneratedAt(), 'Y-m-d H:i'),
'generatedBy' => $notification->getGeneratedBy()->getUserIdentifier(),
'extraInfo' => $notification->getExtraInfo()
]]
];
}
}
return $this->json(['data' => $response, 'status' => true]);
}
/**
* @Route("/notification/read/{id}", name="app_notification_read")
* @param int $id
* @param NotificationService $notificationService
* @return Response
*/
public function readNotification(int $id, NotificationService $notificationService): Response
{
$notification = $notificationService->wellRead($id);
$path = 'dashboard';
$params = [];
switch ($notification->getType()) {
case Notification::PERSON_CREATE:
case Notification::PERSON_UPDATE:
$path = 'app_person_update';
$params['id'] = $notification->getReference();
break;
case Notification::PERSON_REMOVE:
$path = 'app_person';
break;
case Notification::HELP_PROGRAM_CREATE:
case Notification::HELP_PROGRAM_UPDATE:
$path = 'app_help_program_update';
$params['id'] = $notification->getReference();
break;
case Notification::HELP_PROGRAM_REMOVE:
$path = 'app_help_program_remove';
break;
case Notification::SUBURB_CREATE:
case Notification::SUBURB_UPDATE:
$path = 'app_suburb_update';
break;
case Notification::SUBURB_REMOVE:
$path = 'app_suburb_remove';
break;
case Notification::SECTION_CREATE:
case Notification::SECTION_UPDATE:
$path = 'app_section_update';
$params['id'] = $notification->getReference();
break;
case Notification::SECTION_REMOVE:
$path = 'app_section_remove';
break;
case Notification::EXTERNAL_DB_CREATE:
case Notification::EXTERNAL_DB_UPDATE:
$path = 'app_external_db_update';
$params['id'] = $notification->getReference();
break;
case Notification::EXTERNAL_DB_REMOVE:
$path = 'app_external_db_remove';
break;
case Notification::USER_CREATE:
case Notification::USER_UPDATE:
$path = 'app_user_update';
$params['id'] = $notification->getReference();
break;
case Notification::USER_REMOVE:
$path = 'app_user_remove';
}
return $this->redirectToRoute($path, $params);
}
}