src/Controller/DefaultController.php line 39

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Form\RegistrationFormType;
  5. use App\Security\AppCustomAuthenticator;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. use Symfony\Component\Security\Http\Authentication\UserAuthenticatorInterface;
  13. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  14. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
  15. use Symfony\Component\HttpFoundation\JsonResponse;
  16. use App\Entity\Diary;
  17. use App\Entity\Photo;
  18. use App\Entity\Testimonial;
  19. use App\Entity\Poll;
  20. use App\Entity\PollQuestion;
  21. use App\Entity\PollQuestionAnswer;
  22. use App\Entity\PollResult;
  23. use App\Entity\Student;
  24. use App\Entity\Page;
  25. class DefaultController extends AbstractController {
  26.     private function getPageOrFail(EntityManagerInterface $em, string $slug): Page {
  27.         $page = $em->getRepository(Page::class)->findOneBySlug($slug);
  28.         if (!$page) {
  29.             throw $this->createNotFoundException("Page not found.");
  30.         }
  31.         return $page;
  32.     }
  33.     #[Route('/', name: 'homepage')]
  34.     public function homepage(EntityManagerInterface $em, Request $request): Response {
  35.         $testimonials = $em->getRepository(Testimonial::class)->findBy(["displayed" => 1]);
  36.         $data = [
  37.             "page" => $this->getPageOrFail($em, 'home'),
  38.             "testimonials" => $testimonials,
  39.         ];
  40.         return $this->render('frontend/homepage.html.twig', $data);
  41.     }
  42.     #[Route('/cheerleading-classes-chorley', name: 'cheerleadingClasses')]
  43.     public function cheerleadingClasses(EntityManagerInterface $em): Response {
  44.         return $this->render('frontend/cheerleading-classes-chorley.html.twig', [
  45.             "page" => $this->getPageOrFail($em, 'cheerleading-classes-chorley'),
  46.         ]);
  47.     }
  48.     #[Route('/dance-classes-chorley', name: 'danceClasses')]
  49.     public function danceClasses(EntityManagerInterface $em): Response {
  50.         return $this->render('frontend/dance-classes-chorley.html.twig', [
  51.             "page" => $this->getPageOrFail($em, 'dance-classes-chorley'),
  52.         ]);
  53.     }
  54.     #[Route('/timetable-fees', name: 'timetableFees')]
  55.     public function timetableFees(EntityManagerInterface $em): Response {
  56.         return $this->render('frontend/timetable-fees.html.twig', [
  57.             "page" => $this->getPageOrFail($em, 'timetable-fees'),
  58.         ]);
  59.     }
  60.     #[Route('/about', name: 'aboutUs')]
  61.     public function aboutUs(EntityManagerInterface $em): Response {
  62.         return $this->render('frontend/about.html.twig', [
  63.             "page" => $this->getPageOrFail($em, 'about'),
  64.         ]);
  65.     }
  66.     #[Route('/contact', name: 'contactUs')]
  67.     public function contactUs(EntityManagerInterface $em): Response {
  68.         return $this->render('frontend/contact.html.twig', [
  69.             "page" => $this->getPageOrFail($em, 'contact'),
  70.         ]);
  71.     }
  72.     #[Route('/members/register', name: 'registerMember')]
  73.     public function registerMember(Request $request, UserPasswordHasherInterface $userPasswordHasher, UserAuthenticatorInterface $userAuthenticator, AppCustomAuthenticator $authenticator, EntityManagerInterface $entityManager): Response {
  74.         $newuser = new User();
  75.         $form = $this->createForm(RegistrationFormType::class, $newuser);
  76.         $form->handleRequest($request);
  77.         if ($form->isSubmitted() && $form->isValid()) {
  78.             // encode the plain password
  79.             $newuser->setPassword(
  80.                     $userPasswordHasher->hashPassword(
  81.                             $newuser,
  82.                             $form->get('plainPassword')->getData()
  83.                     )
  84.             );
  85.             $entityManager->persist($newuser);
  86.             $entityManager->flush();
  87.             // do anything else you need here, like send an email
  88.             $this->addFlash("success", "Member has been successfully added.");
  89.             return $userAuthenticator->authenticateUser(
  90.                             $newuser,
  91.                             $authenticator,
  92.                             $request
  93.             );
  94.         }
  95.         $data = [
  96.             'registrationForm' => $form->createView(),
  97.         ];
  98.         return $this->render('members/register.html.twig', $data);
  99.     }
  100.     #[Route('/members/vote/{slug}', name: 'MemberVote')]
  101.     public function getPolls(Request $request, EntityManagerInterface $em, $slug): Response {
  102.         $ip = $_SERVER['REMOTE_ADDR'];
  103.         $today = new \DateTime();
  104.         $thepoll = $em->getRepository(Poll::class)->getPoll($slug);
  105.         $data["found"] = 0;
  106.         if ($thepoll) {
  107.             foreach ($thepoll as $poll) {
  108.                 foreach ($poll->getPollQuestions() as $questions) {
  109.                     foreach ($questions->getPollQuestionAnswers() as $answers) {
  110.                         if ($answers->getIpaddress() == $ip) {
  111.                             $data["found"] = 1;
  112.                         }
  113.                     }
  114.                 }
  115.             }
  116.             $data["poll"] = $thepoll;
  117.         } else {
  118.             $data["poll"] = "";
  119.         }
  120.         return $this->render('frontend/poll_list.html.twig', $data);
  121.     }
  122.     #[Route('/members/vote-cast/{id}', name: 'MemberVoteCast')]
  123.     public function MemberVoteCast($id, Request $request, EntityManagerInterface $em): Response {
  124.         $question = $em->getRepository(PollQuestion::class)->findOneBy(array("id" => $id));
  125.         if ($question) {
  126.             $ip = $_SERVER['REMOTE_ADDR'];
  127.             $poll = $question->getPoll();
  128.             // If not anonymous, require student verification
  129.             if (!$poll->isAnonymous()) {
  130.                 $surname = $request->request->get('surname');
  131.                 $dob     = $request->request->get('dob');
  132.                 if (!$surname || !$dob) {
  133.                     return new JsonResponse(['status' => 'not_found']);
  134.                 }
  135.                 $student = $em->getRepository(Student::class)->findOneBy([
  136.                     'surname'     => $surname,
  137.                     'dateofbirth' => $dob,
  138.                 ]);
  139.                 if (!$student) {
  140.                     return new JsonResponse(['status' => 'not_found']);
  141.                 }
  142.                 $answer = new PollQuestionAnswer();
  143.                 $answer->setQuestion($question);
  144.                 $answer->setIpaddress($ip);
  145.                 $answer->setSubmitted(new \DateTime());
  146.                 $answer->setStudent($student);
  147.                 $em->persist($answer);
  148.                 $em->flush();
  149.                 return new JsonResponse(['status' => 'success']);
  150.             }
  151.             // Anonymous poll — just record IP
  152.             $answer = new PollQuestionAnswer();
  153.             $answer->setQuestion($question);
  154.             $answer->setIpaddress($ip);
  155.             $answer->setSubmitted(new \DateTime());
  156.             $em->persist($answer);
  157.             $em->flush();
  158.             return new JsonResponse(['status' => 'success']);
  159.         }
  160.         return new JsonResponse(['status' => 'failed']);
  161.     }
  162. }