src/Ox/HoardBundle/Controller/DefaultController.php line 27

Open in your IDE?
  1. <?php
  2. namespace App\Ox\HoardBundle\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  6. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  7. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
  8. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  9. use Symfony\Component\Mailer\MailerInterface;
  10. use Symfony\Component\Mime\Email;
  11. use App\Ox\HoardBundle\Form\ContactType;
  12. use Symfony\Component\Mailer\Transport;
  13. use Symfony\Component\Mailer\Mailer;
  14. class DefaultController extends AbstractController
  15. {
  16.     /**
  17.      * @Route("/", name="hoards_home")
  18.      * @Template()
  19.      */
  20.     public function dashboardAction()
  21.     {
  22.         $em $this->getDoctrine()->getManager();
  23.         $qb $em->createQueryBuilder();
  24.         $qb->select'c' )
  25.                 ->from'OxHoardBundle:ContentPage',  'c' )
  26.                 ->where ('c.type = 0')
  27.                 ->orderBy('c.creationDate''DESC')
  28.                 ->setMaxResults(2);
  29.         $newsItems $qb->getQuery()->getArrayResult();
  30.         $qb $em->createQueryBuilder();
  31.         $qb->select('COUNT(h.id)'//deleted hoards (h.deletedAt != NULL) are afilteredout by doctrine
  32.                 ->from'OxHoardBundle:Hoard',  'h' )
  33.                 ->andWhere ('h IS NOT NULL')
  34.                 ->andWhere ('h.hideFrom = 3 OR h.hideWhat < 3 OR h.hideWhat IS NULL');
  35.         $all_hoards $qb->getQuery()->getSingleScalarResult();
  36.         $qb->andWhere ('h.validatedByUser = 1');
  37.         $validated_hoards $qb->getQuery()->getSingleScalarResult();
  38.         $qb $em->createQueryBuilder();
  39.         $qb->select("sum(c.quantity) as coin_count")
  40.             ->from('OxHoardBundle:Coin''c')
  41.             ->leftJoin('c.hoard''h')
  42.             ->andWhere ('h IS NOT NULL')
  43.             ->andWhere ('h.hideFrom = 3 OR h.hideWhat < 3 OR h.hideWhat IS NULL');
  44.         $all_coins $qb->getQuery()->getSingleScalarResult();
  45.         
  46.         $qb->andWhere ('h.coinDataValidatedByUser = 1');
  47.         $validated_coins $qb->getQuery()->getSingleScalarResult();
  48.         $isAuthenticated false;
  49.         if($this->getUser()) {
  50.             $isAuthenticated true;
  51.         }
  52.         return array(
  53.             'news_items'=>$newsItems,
  54.             'is_authenticated'=>$isAuthenticated,
  55.             'validated_hoards' => $validated_hoards,
  56.             'validated_coins' => $validated_coins,
  57.             'all_hoards' => $all_hoards,
  58.             'all_coins' => $all_coins,
  59.         );
  60.     }
  61.     /**
  62.      * @Route("/contact", name="contact_form", methods={"GET"})
  63.      * @Template("@OxHoardBundle/default/contact.html.twig")
  64.      */
  65.     public function contactFormAction()
  66.     {
  67.         $form $this->createContactForm();
  68.         return array(
  69.             'form' => $form->createView(),
  70.         );
  71.     }
  72.     /**
  73.      * @Route("/contact", name="contact_send", methods={"POST"})
  74.      * @Template()
  75.      */
  76.     public function contactAction(Request $requestMailerInterface $mailer)
  77.     {
  78.         $formData $request->request->get('ox_hoardbundle_contact');
  79.         $submittedForm $this->createContactForm($formData);
  80.         $submittedForm->handleRequest($request);
  81.         if ($submittedForm->isValid()) {
  82.             $msgBody "From:\n".$formData['name']."\n\n";
  83.             $msgBody .= "Email:\n".$formData['email']."\n\n";
  84.             $msgBody .= "Message:\n".$formData['message'];
  85.             $transport Transport::fromDsn('smtp://ashweb9.versantus.co.uk');
  86.             $mailer = new Mailer($transport);
  87.             $email = (new Email())
  88.             ->from('chre@ashmus.ox.ac.uk')
  89.             ->to('chre@ashmus.ox.ac.uk')
  90.             ->replyTo($formData['email'])
  91.             ->subject('CHRE message from '.$formData['name'])
  92.             ->text($msgBody);
  93.             
  94.             try {
  95.                 $mailer->send($email);
  96.                 $request->getSession()
  97.                     ->getFlashBag()
  98.                     ->add('success''Thank you for contacting us. We will aim to respond to you soon.');
  99.             } catch (TransportExceptionInterface $e) {
  100.                 $request->getSession()
  101.                     ->getFlashBag()
  102.                     ->add('error''An error prevented the email sending'.$e);
  103.             }
  104.             return $this->redirect($this->generateUrl('hoards_home'));
  105.         } else {
  106.             $request->getSession()
  107.                 ->getFlashBag()
  108.                 ->add('error''Invalid ReCaptcha.');
  109.             $form $this->createContactForm();
  110.             return array(
  111.                 'form' => $form->createView(),
  112.             );
  113.         }
  114.     }
  115.     private function createContactForm()
  116.     {
  117.         $form $this->createForm(ContactType::class, null, array(
  118.             'action' => $this->generateUrl('contact_send'),
  119.             'method' => 'POST',
  120.         ));
  121.         $form->add('submit'SubmitType::class, array('label' => 'Send'));
  122.         return $form;
  123.     }
  124. }