<?php
namespace App\Controller;
use App\Entity\User;
use App\Form\RegistrationFormType;
use App\Security\AppCustomAuthenticator;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\UserAuthenticatorInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Component\HttpFoundation\JsonResponse;
use App\Entity\Diary;
use App\Entity\Photo;
use App\Entity\Testimonial;
use App\Entity\Poll;
use App\Entity\PollQuestion;
use App\Entity\PollQuestionAnswer;
use App\Entity\PollResult;
use App\Entity\Student;
use App\Entity\Page;
class DefaultController extends AbstractController {
private function getPageOrFail(EntityManagerInterface $em, string $slug): Page {
$page = $em->getRepository(Page::class)->findOneBySlug($slug);
if (!$page) {
throw $this->createNotFoundException("Page not found.");
}
return $page;
}
#[Route('/', name: 'homepage')]
public function homepage(EntityManagerInterface $em, Request $request): Response {
$testimonials = $em->getRepository(Testimonial::class)->findBy(["displayed" => 1]);
$data = [
"page" => $this->getPageOrFail($em, 'home'),
"testimonials" => $testimonials,
];
return $this->render('frontend/homepage.html.twig', $data);
}
#[Route('/cheerleading-classes-chorley', name: 'cheerleadingClasses')]
public function cheerleadingClasses(EntityManagerInterface $em): Response {
return $this->render('frontend/cheerleading-classes-chorley.html.twig', [
"page" => $this->getPageOrFail($em, 'cheerleading-classes-chorley'),
]);
}
#[Route('/dance-classes-chorley', name: 'danceClasses')]
public function danceClasses(EntityManagerInterface $em): Response {
return $this->render('frontend/dance-classes-chorley.html.twig', [
"page" => $this->getPageOrFail($em, 'dance-classes-chorley'),
]);
}
#[Route('/timetable-fees', name: 'timetableFees')]
public function timetableFees(EntityManagerInterface $em): Response {
return $this->render('frontend/timetable-fees.html.twig', [
"page" => $this->getPageOrFail($em, 'timetable-fees'),
]);
}
#[Route('/about', name: 'aboutUs')]
public function aboutUs(EntityManagerInterface $em): Response {
return $this->render('frontend/about.html.twig', [
"page" => $this->getPageOrFail($em, 'about'),
]);
}
#[Route('/contact', name: 'contactUs')]
public function contactUs(EntityManagerInterface $em): Response {
return $this->render('frontend/contact.html.twig', [
"page" => $this->getPageOrFail($em, 'contact'),
]);
}
#[Route('/members/register', name: 'registerMember')]
public function registerMember(Request $request, UserPasswordHasherInterface $userPasswordHasher, UserAuthenticatorInterface $userAuthenticator, AppCustomAuthenticator $authenticator, EntityManagerInterface $entityManager): Response {
$newuser = new User();
$form = $this->createForm(RegistrationFormType::class, $newuser);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
// encode the plain password
$newuser->setPassword(
$userPasswordHasher->hashPassword(
$newuser,
$form->get('plainPassword')->getData()
)
);
$entityManager->persist($newuser);
$entityManager->flush();
// do anything else you need here, like send an email
$this->addFlash("success", "Member has been successfully added.");
return $userAuthenticator->authenticateUser(
$newuser,
$authenticator,
$request
);
}
$data = [
'registrationForm' => $form->createView(),
];
return $this->render('members/register.html.twig', $data);
}
#[Route('/members/vote/{slug}', name: 'MemberVote')]
public function getPolls(Request $request, EntityManagerInterface $em, $slug): Response {
$ip = $_SERVER['REMOTE_ADDR'];
$today = new \DateTime();
$thepoll = $em->getRepository(Poll::class)->getPoll($slug);
$data["found"] = 0;
if ($thepoll) {
foreach ($thepoll as $poll) {
foreach ($poll->getPollQuestions() as $questions) {
foreach ($questions->getPollQuestionAnswers() as $answers) {
if ($answers->getIpaddress() == $ip) {
$data["found"] = 1;
}
}
}
}
$data["poll"] = $thepoll;
} else {
$data["poll"] = "";
}
return $this->render('frontend/poll_list.html.twig', $data);
}
#[Route('/members/vote-cast/{id}', name: 'MemberVoteCast')]
public function MemberVoteCast($id, Request $request, EntityManagerInterface $em): Response {
$question = $em->getRepository(PollQuestion::class)->findOneBy(array("id" => $id));
if ($question) {
$ip = $_SERVER['REMOTE_ADDR'];
$poll = $question->getPoll();
// If not anonymous, require student verification
if (!$poll->isAnonymous()) {
$surname = $request->request->get('surname');
$dob = $request->request->get('dob');
if (!$surname || !$dob) {
return new JsonResponse(['status' => 'not_found']);
}
$student = $em->getRepository(Student::class)->findOneBy([
'surname' => $surname,
'dateofbirth' => $dob,
]);
if (!$student) {
return new JsonResponse(['status' => 'not_found']);
}
$answer = new PollQuestionAnswer();
$answer->setQuestion($question);
$answer->setIpaddress($ip);
$answer->setSubmitted(new \DateTime());
$answer->setStudent($student);
$em->persist($answer);
$em->flush();
return new JsonResponse(['status' => 'success']);
}
// Anonymous poll — just record IP
$answer = new PollQuestionAnswer();
$answer->setQuestion($question);
$answer->setIpaddress($ip);
$answer->setSubmitted(new \DateTime());
$em->persist($answer);
$em->flush();
return new JsonResponse(['status' => 'success']);
}
return new JsonResponse(['status' => 'failed']);
}
}