<?php
/**
* User: remmel
* Date: 1/3/16
* Time: 5:33 PM
*/
namespace App\Controller\Frontend;
use App\Entity\Advert;
use App\Entity\model\Conveyance;
use App\Form\AdvertType;
use App\Repository\BlockRepository;
use App\Repository\StopRepository;
use App\Service\UrlRewrite;
use DateTime;
use Doctrine\Persistence\ManagerRegistry;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Email;
use Symfony\Component\Routing\Annotation\Route;
class AdvertController extends AbstractController {
/**
* @Route("/revendre-billet-de-bus/{id}", name="page_advert_show", options={"i18n_locales"={"fr","en"}})
*/
public function secondHandticket(Advert $ad, UrlRewrite $urlRewriteService) {
$form = $this->createForm(AdvertType::class, $ad, ['disabled' => true]);
$url = $urlRewriteService
->generateFromAtoBUrl($ad->getDepStop(), $ad->getArrStop(), $ad->getDepDatetime()->format('Y-m-d'), null, true, null, Conveyance::BUS);
$label = 'Bus '.$ad->getDepStop()->getName().' '.$ad->getArrStop()->getName();
$link = new Link($label, $url);
return $this->render("Default/advert/show.html.twig",[
'form' => $form->createView(),
'ad' => $ad,
'noIndex' => true,
'link' => $link
]);
}
/**
* @Route("/revendre-billet-de-bus", name="page_advert_new", options={"i18n_locales"={"fr"}})
* @Template("Default/advert/new.html.twig")
*/
public function sellTicketFormAction(Request $request, MailerInterface $mailer, StopRepository $stopRepo, ManagerRegistry $doctrine, BlockRepository $blockRepo) {
$ad = new Advert();
$ad->setCreatedAt(new DateTime());
$ad->setDepDatetime(new DateTime('tomorrow'));
$ad->setArrDatetime(new DateTime('tomorrow'));
$ad->setDepStop($stopRepo->find(10));
$ad->setArrStop($stopRepo->find(6));
$form = $this->createForm(AdvertType::class, $ad);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$message = (new Email())
->subject('New Advert')
->from('remy.cloutrier@gmail.com')
->to('contact@comparabus.com')
->html(
json_encode($ad),
'text/html'
);
$mailer->send($message);
$em = $doctrine->getManager();
$em->persist($ad);
$em->flush();
$this->addFlash('ad_created', null);
return $this->redirectToRoute('page_advert_show', ['id' => $ad->getId()]);
}
$block = $blockRepo
->findOnebyPathNotNull($request->getPathInfo().'#modification');
return [
'form' => $form->createView(),
'block' => $block
];
}
}