<?php
namespace App\Ox\HoardBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Email;
use App\Ox\HoardBundle\Form\ContactType;
use Symfony\Component\Mailer\Transport;
use Symfony\Component\Mailer\Mailer;
class DefaultController extends AbstractController
{
/**
* @Route("/", name="hoards_home")
* @Template()
*/
public function dashboardAction()
{
$em = $this->getDoctrine()->getManager();
$qb = $em->createQueryBuilder();
$qb->select( 'c' )
->from( 'OxHoardBundle:ContentPage', 'c' )
->where ('c.type = 0')
->orderBy('c.creationDate', 'DESC')
->setMaxResults(2);
$newsItems = $qb->getQuery()->getArrayResult();
$qb = $em->createQueryBuilder();
$qb->select('COUNT(h.id)') //deleted hoards (h.deletedAt != NULL) are afilteredout by doctrine
->from( 'OxHoardBundle:Hoard', 'h' )
->andWhere ('h IS NOT NULL')
->andWhere ('h.hideFrom = 3 OR h.hideWhat < 3 OR h.hideWhat IS NULL');
$all_hoards = $qb->getQuery()->getSingleScalarResult();
$qb->andWhere ('h.validatedByUser = 1');
$validated_hoards = $qb->getQuery()->getSingleScalarResult();
$qb = $em->createQueryBuilder();
$qb->select("sum(c.quantity) as coin_count")
->from('OxHoardBundle:Coin', 'c')
->leftJoin('c.hoard', 'h')
->andWhere ('h IS NOT NULL')
->andWhere ('h.hideFrom = 3 OR h.hideWhat < 3 OR h.hideWhat IS NULL');
$all_coins = $qb->getQuery()->getSingleScalarResult();
$qb->andWhere ('h.coinDataValidatedByUser = 1');
$validated_coins = $qb->getQuery()->getSingleScalarResult();
$isAuthenticated = false;
if($this->getUser()) {
$isAuthenticated = true;
}
return array(
'news_items'=>$newsItems,
'is_authenticated'=>$isAuthenticated,
'validated_hoards' => $validated_hoards,
'validated_coins' => $validated_coins,
'all_hoards' => $all_hoards,
'all_coins' => $all_coins,
);
}
/**
* @Route("/contact", name="contact_form", methods={"GET"})
* @Template("@OxHoardBundle/default/contact.html.twig")
*/
public function contactFormAction()
{
$form = $this->createContactForm();
return array(
'form' => $form->createView(),
);
}
/**
* @Route("/contact", name="contact_send", methods={"POST"})
* @Template()
*/
public function contactAction(Request $request, MailerInterface $mailer)
{
$formData = $request->request->get('ox_hoardbundle_contact');
$submittedForm = $this->createContactForm($formData);
$submittedForm->handleRequest($request);
if ($submittedForm->isValid()) {
$msgBody = "From:\n".$formData['name']."\n\n";
$msgBody .= "Email:\n".$formData['email']."\n\n";
$msgBody .= "Message:\n".$formData['message'];
$transport = Transport::fromDsn('smtp://ashweb9.versantus.co.uk');
$mailer = new Mailer($transport);
$email = (new Email())
->from('chre@ashmus.ox.ac.uk')
->to('chre@ashmus.ox.ac.uk')
->replyTo($formData['email'])
->subject('CHRE message from '.$formData['name'])
->text($msgBody);
try {
$mailer->send($email);
$request->getSession()
->getFlashBag()
->add('success', 'Thank you for contacting us. We will aim to respond to you soon.');
} catch (TransportExceptionInterface $e) {
$request->getSession()
->getFlashBag()
->add('error', 'An error prevented the email sending'.$e);
}
return $this->redirect($this->generateUrl('hoards_home'));
} else {
$request->getSession()
->getFlashBag()
->add('error', 'Invalid ReCaptcha.');
$form = $this->createContactForm();
return array(
'form' => $form->createView(),
);
}
}
private function createContactForm()
{
$form = $this->createForm(ContactType::class, null, array(
'action' => $this->generateUrl('contact_send'),
'method' => 'POST',
));
$form->add('submit', SubmitType::class, array('label' => 'Send'));
return $form;
}
}