src/Controller/BackofficeController.php line 95

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Component\HttpFoundation\Response;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\HttpFoundation\Session\Session;
  6. use Symfony\Component\Routing\Annotation\Route;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
  9. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
  10. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  11. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  12. use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
  13. use Symfony\Component\Serializer\SerializerInterface;
  14. use Doctrine\Common\Annotations\AnnotationReader;
  15. use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
  16. use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
  17. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  18. use Symfony\Component\Mailer\MailerInterface;
  19. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  20. use Symfony\Component\Mime\Address;
  21. use Doctrine\DBAL\DriverManager;
  22. use Doctrine\ORM\EntityManagerInterface;
  23. use Doctrine\Common\Collections\ArrayCollection;
  24. use Doctrine\ORM\Tools\Pagination\Paginator;
  25. //use OpenAI\OpenAI;
  26. use App\Entity\ConfigGeoServer;
  27. use App\Entity\ConfigGeoLayers;
  28. use App\Entity\FilterRange;
  29. use App\Entity\FilterOptions;
  30. use App\Entity\Layers;
  31. use App\Entity\Styles;
  32. use App\Entity\Account;
  33. use App\Entity\Municipio;
  34. use App\Repository\MunicipioRepository;
  35. use App\Entity\Conversation;
  36. use App\Repository\ConversationRepository;
  37. use App\Entity\Project;
  38. use App\Repository\ProjectRepository;
  39. use App\Entity\PlanProject;
  40. use Symfony\Component\Security\Core\Security;
  41. class BackofficeController extends AbstractController
  42. {
  43.     private $params;
  44.     public function __construct(ParameterBagInterface $params)
  45.     {
  46.         $this->params = $params;
  47.     }
  48.     #[Route(path: '/dashboard', name: 'dashboard')]
  49.     public function dashboard() : Response
  50.     {
  51.         $em = $this->getDoctrine()->getManager();
  52.         $data['users'] = $em->getRepository(Account::class)->getUsersWithStats();
  53.         $data['guests'] = $em->getRepository(Account::class)->findByRole('ROLE_GUEST'); 
  54.         //Estadísticas
  55.         $data['totalUsers'] = $em->createQuery('SELECT COUNT(a.id) FROM App\Entity\Account a')->getSingleScalarResult();
  56.         $data['totalPlans'] = $em->createQuery('SELECT COUNT(p.id) FROM App\Entity\Project p')->getSingleScalarResult();
  57.         $data['totalProjects'] = $em->getRepository(PlanProject::class)->count([]);        
  58.         //$data['totalCities'] = $em->getRepository(PlanProject::class)->findAll();
  59.         $data['topCities'] = $em->getRepository(Project::class)->getTopCitiesWithProjects(10);
  60.         //dump($data);
  61.         return $this->render('Backoffice/dashboard.html.twig', $data);
  62.     }
  63.     #[Route(path: '/users', name: 'users')]
  64.     public function users (Request $request, EntityManagerInterface $em) : Response
  65.     {
  66.         $em = $this->getDoctrine()->getManager();
  67.         //$data['users'] = $em->getRepository(Account::class)->findBy([], ['lastConnection' => 'DESC']);
  68.         $data['users'] = $em->getRepository(Account::class)->getUsersWithStats();
  69.         return $this->render('Backoffice/users.html.twig', $data);
  70.     }    
  71.     #[Route(path: '/users/{id}', name: 'detail-user')]
  72.     public function detailUser (Request $request, Account $user) : Response
  73.     {
  74.         $em = $this->getDoctrine()->getManager();
  75.         $data['user'] = $user;
  76.         //Get #project
  77.         $user_projects = $em->getRepository(Project::class)->findBy(['id_user'=>$user->getId()]);
  78.         //Get #plan_project
  79.         //Gte #cities 
  80.         return $this->render('Backoffice/detailUser.html.twig', $data);
  81.     }
  82.     #[Route(path: '/list-of-guests', name: 'list-guests')]
  83.     public function listGuests (Request $request, EntityManagerInterface $em) : Response
  84.     {
  85.         $em = $this->getDoctrine()->getManager();
  86.     
  87.         $data['users'] = $em->getRepository(Account::class)->findByRole('ROLE_GUEST');        
  88.         return $this->render('Backoffice/listGuests.html.twig', $data);
  89.     }
  90.     #[Route(path: '/guest-decision/{id}/{decision}', name: 'guest-decision')]
  91.     public function guestDecision (Request $request, EntityManagerInterface $em, MailerInterface $mailer, Account $user, String $decision) : Response
  92.     {
  93.         $em = $this->getDoctrine()->getManager();
  94.     
  95.         if ($decision == 'accept')
  96.         {
  97.             //Change rol
  98.             $user->setRoles(['ROLE_REGISTERED']); 
  99.             $em->persist($user);
  100.             $em->flush();
  101.         }  
  102.         
  103.         //Send email 
  104.         $email = (new TemplatedEmail())
  105.             ->from(new Address('info@retabit.es', 'Retabit'))
  106.             ->to($user->getEmail())
  107.             ->subject('RETABIT Account')
  108.             ->htmlTemplate('Backoffice/email_decision.html.twig')
  109.             ->context([
  110.                 'expiration_date' => new \DateTime('+7 days'),
  111.                 'name' => $user->getName(),
  112.                 'decision' => $decision
  113.             ])                
  114.         ;
  115.         $logoPath = $this->getParameter('kernel.project_dir') . '/public/img/logo-menu-white.png';
  116.        
  117.         $cid = $email->embedFromPath($logoPath, 'logo');
  118.         $mailer->send($email);
  119.         return $this->redirectToRoute('dashboard');
  120.     }
  121. }