<?php
namespace App\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\SerializerInterface;
use Doctrine\Common\Annotations\AnnotationReader;
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\Mime\Address;
use Doctrine\DBAL\DriverManager;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Tools\Pagination\Paginator;
//use OpenAI\OpenAI;
use App\Entity\ConfigGeoServer;
use App\Entity\ConfigGeoLayers;
use App\Entity\FilterRange;
use App\Entity\FilterOptions;
use App\Entity\Layers;
use App\Entity\Styles;
use App\Entity\Account;
use App\Entity\Municipio;
use App\Repository\MunicipioRepository;
use App\Entity\Conversation;
use App\Repository\ConversationRepository;
use App\Entity\Project;
use App\Repository\ProjectRepository;
use App\Entity\PlanProject;
use Symfony\Component\Security\Core\Security;
class BackofficeController extends AbstractController
{
private $params;
public function __construct(ParameterBagInterface $params)
{
$this->params = $params;
}
#[Route(path: '/dashboard', name: 'dashboard')]
public function dashboard() : Response
{
$em = $this->getDoctrine()->getManager();
$data['users'] = $em->getRepository(Account::class)->getUsersWithStats();
$data['guests'] = $em->getRepository(Account::class)->findByRole('ROLE_GUEST');
//Estadísticas
$data['totalUsers'] = $em->createQuery('SELECT COUNT(a.id) FROM App\Entity\Account a')->getSingleScalarResult();
$data['totalPlans'] = $em->createQuery('SELECT COUNT(p.id) FROM App\Entity\Project p')->getSingleScalarResult();
$data['totalProjects'] = $em->getRepository(PlanProject::class)->count([]);
//$data['totalCities'] = $em->getRepository(PlanProject::class)->findAll();
$data['topCities'] = $em->getRepository(Project::class)->getTopCitiesWithProjects(10);
//dump($data);
return $this->render('Backoffice/dashboard.html.twig', $data);
}
#[Route(path: '/users', name: 'users')]
public function users (Request $request, EntityManagerInterface $em) : Response
{
$em = $this->getDoctrine()->getManager();
//$data['users'] = $em->getRepository(Account::class)->findBy([], ['lastConnection' => 'DESC']);
$data['users'] = $em->getRepository(Account::class)->getUsersWithStats();
return $this->render('Backoffice/users.html.twig', $data);
}
#[Route(path: '/users/{id}', name: 'detail-user')]
public function detailUser (Request $request, Account $user) : Response
{
$em = $this->getDoctrine()->getManager();
$data['user'] = $user;
//Get #project
$user_projects = $em->getRepository(Project::class)->findBy(['id_user'=>$user->getId()]);
//Get #plan_project
//Gte #cities
return $this->render('Backoffice/detailUser.html.twig', $data);
}
#[Route(path: '/list-of-guests', name: 'list-guests')]
public function listGuests (Request $request, EntityManagerInterface $em) : Response
{
$em = $this->getDoctrine()->getManager();
$data['users'] = $em->getRepository(Account::class)->findByRole('ROLE_GUEST');
return $this->render('Backoffice/listGuests.html.twig', $data);
}
#[Route(path: '/guest-decision/{id}/{decision}', name: 'guest-decision')]
public function guestDecision (Request $request, EntityManagerInterface $em, MailerInterface $mailer, Account $user, String $decision) : Response
{
$em = $this->getDoctrine()->getManager();
if ($decision == 'accept')
{
//Change rol
$user->setRoles(['ROLE_REGISTERED']);
$em->persist($user);
$em->flush();
}
//Send email
$email = (new TemplatedEmail())
->from(new Address('info@retabit.es', 'Retabit'))
->to($user->getEmail())
->subject('RETABIT Account')
->htmlTemplate('Backoffice/email_decision.html.twig')
->context([
'expiration_date' => new \DateTime('+7 days'),
'name' => $user->getName(),
'decision' => $decision
])
;
$logoPath = $this->getParameter('kernel.project_dir') . '/public/img/logo-menu-white.png';
$cid = $email->embedFromPath($logoPath, 'logo');
$mailer->send($email);
return $this->redirectToRoute('dashboard');
}
}