3 namespace Rapsys\AirBundle\Controller
; 
   5 use Symfony\Component\HttpFoundation\Request
; 
   6 use Symfony\Component\Routing\RequestContext
; 
   7 use Symfony\Component\Routing\Exception\MethodNotAllowedException
; 
   8 use Symfony\Component\Routing\Exception\ResourceNotFoundException
; 
   9 use Rapsys\AirBundle\Entity\Location
; 
  10 use Rapsys\AirBundle\Entity\Snippet
; 
  11 use Rapsys\AirBundle\Entity\User
; 
  13 class SnippetController 
extends DefaultController 
{ 
  17          * @desc Persist snippet in database 
  19          * @param Request $request The request instance 
  21          * @return Response The rendered view or redirection 
  23          * @throws \RuntimeException When user has not at least guest role 
  25         public function add(Request 
$request) { 
  26                 //Prevent non-guest to access here 
  27                 $this->denyAccessUnlessGranted('ROLE_GUEST', null, $this->translator
->trans('Unable to access this page without role %role%!', ['%role%' => $this->translator
->trans('Guest')])); 
  29                 //Create ApplicationType form 
  30                 $form = $this->createForm('Rapsys\AirBundle\Form\SnippetType', null, [ 
  32                         'action' => $this->generateUrl('rapsys_air_snippet_add'), 
  33                         //Set the form attribute 
  37                 //Refill the fields in case of invalid form 
  38                 $form->handleRequest($request); 
  40                 //Prevent creating snippet for other user unless admin 
  41                 if ($form->get('user')->getData() !== $this->getUser()) { 
  42                         //Prevent non-admin to access here 
  43                         $this->denyAccessUnlessGranted('ROLE_ADMIN', null, $this->translator
->trans('Unable to access this page without role %role%!', ['%role%' => $this->translator
->trans('Admin')])); 
  47                 if (!$form->isSubmitted() || !$form->isValid()) { 
  49                         $section = $this->translator
->trans('Snippet add'); 
  52                         $title = $this->translator
->trans($this->config
['site']['title']).' - '.$section; 
  55                         return $this->render('@RapsysAir/snippet/add.html.twig', ['title' => $title, 'section' => $section, 'form' => $form->createView()]+
$this->context
); 
  59                 $doctrine = $this->getDoctrine(); 
  62                 $manager = $doctrine->getManager(); 
  65                 $snippet = $form->getData(); 
  68                 $snippet->setCreated(new \
DateTime('now')); 
  71                 $snippet->setUpdated(new \
DateTime('now')); 
  74                 $manager->persist($snippet); 
  76                 //Flush to get the ids 
  80                 $this->addFlash('notice', $this->translator
->trans('Snippet in %locale% %location% for %user% created', ['%locale%' => $snippet->getLocale(), '%location%' => $this->translator
->trans('at '.$snippet->getLocation()), '%user%' => $snippet->getUser()])); 
  82                 //Extract and process referer 
  83                 if ($referer = $request->headers
->get('referer')) { 
  84                         //Create referer request instance 
  85                         $req = Request
::create($referer); 
  88                         $path = $req->getPathInfo(); 
  90                         //Get referer query string 
  91                         $query = $req->getQueryString(); 
  94                         $path = str_replace($request->getScriptName(), '', $path); 
  96                         //Try with referer path 
  99                                 $oldContext = $this->router
->getContext(); 
 101                                 //Force clean context 
 102                                 //XXX: prevent MethodNotAllowedException because current context method is POST in onevendor/symfony/routing/Matcher/Dumper/CompiledUrlMatcherTrait.php+42 
 103                                 $this->router
->setContext(new RequestContext()); 
 105                                 //Retrieve route matching path 
 106                                 $route = $this->router
->match($path); 
 109                                 $this->router
->setContext($oldContext); 
 115                                 $name = $route['_route']; 
 117                                 //Remove route and controller from route defaults 
 118                                 unset($route['_route'], $route['_controller']); 
 120                                 //Check if snippet view route 
 121                                 if ($name == 'rapsys_air_organizer_view' && !empty($route['id'])) { 
 123                                         $route['id'] = $snippet->getUser()->getId(); 
 127                                         $route['snippet'] = $snippet->getId(); 
 131                                 return $this->redirectToRoute($name, $route); 
 133                         } catch(MethodNotAllowedException
|ResourceNotFoundException 
$e) { 
 134                                 //Unset referer to fallback to default route 
 139                 //Redirect to cleanup the form 
 140                 return $this->redirectToRoute('rapsys_air', ['snippet' => $snippet->getId()]); 
 146          * @desc Persist snippet in database 
 148          * @param Request $request The request instance 
 150          * @return Response The rendered view or redirection 
 152          * @throws \RuntimeException When user has not at least guest role 
 154         public function edit(Request 
$request, $id) { 
 155                 //Prevent non-guest to access here 
 156                 $this->denyAccessUnlessGranted('ROLE_GUEST', null, $this->translator
->trans('Unable to access this page without role %role%!', ['%role%' => $this->translator
->trans('Guest')])); 
 159                 $doctrine = $this->getDoctrine(); 
 162                 if (empty($snippet = $doctrine->getRepository(Snippet
::class)->findOneById($id))) { 
 163                         throw $this->createNotFoundException($this->translator
->trans('Unable to find snippet: %id%', ['%id%' => $id])); 
 166                 //Create ApplicationType form 
 167                 $form = $this->createForm('Rapsys\AirBundle\Form\SnippetType', $snippet, [ 
 169                         'action' => $this->generateUrl('rapsys_air_snippet_edit', ['id' => $id]), 
 170                         //Set the form attribute 
 174                 //Refill the fields in case of invalid form 
 175                 $form->handleRequest($request); 
 177                 //Prevent creating snippet for other user unless admin 
 178                 if ($form->get('user')->getData() !== $this->getUser()) { 
 179                         //Prevent non-admin to access here 
 180                         $this->denyAccessUnlessGranted('ROLE_ADMIN', null, $this->translator
->trans('Unable to access this page without role %role%!', ['%role%' => $this->translator
->trans('Admin')])); 
 183                 //Handle invalid form 
 184                 if (!$form->isSubmitted() || !$form->isValid()) { 
 186                         $section = $this->translator
->trans('Snippet %id%', ['%id%' => $id]); 
 189                         $title = $this->translator
->trans($this->config
['site']['title']).' - '.$section; 
 192                         return $this->render('@RapsysAir/snippet/edit.html.twig', ['id' => $id, 'title' => $title, 'section' => $section, 'form' => $form->createView()]+
$this->context
); 
 196                 $manager = $doctrine->getManager(); 
 199                 $snippet->setUpdated(new \
DateTime('now')); 
 202                 $manager->persist($snippet); 
 204                 //Flush to get the ids 
 208                 $this->addFlash('notice', $this->translator
->trans('Snippet %id% in %locale% %location% for %user% updated', ['%id%' => $id, '%locale%' => $snippet->getLocale(), '%location%' => $this->translator
->trans('at '.$snippet->getLocation()), '%user%' => $snippet->getUser()])); 
 210                 //Extract and process referer 
 211                 if ($referer = $request->headers
->get('referer')) { 
 212                         //Create referer request instance 
 213                         $req = Request
::create($referer); 
 216                         $path = $req->getPathInfo(); 
 218                         //Get referer query string 
 219                         $query = $req->getQueryString(); 
 222                         $path = str_replace($request->getScriptName(), '', $path); 
 224                         //Try with referer path 
 227                                 $oldContext = $this->router
->getContext(); 
 229                                 //Force clean context 
 230                                 //XXX: prevent MethodNotAllowedException because current context method is POST in onevendor/symfony/routing/Matcher/Dumper/CompiledUrlMatcherTrait.php+42 
 231                                 $this->router
->setContext(new RequestContext()); 
 233                                 //Retrieve route matching path 
 234                                 $route = $this->router
->match($path); 
 237                                 $this->router
->setContext($oldContext); 
 243                                 $name = $route['_route']; 
 245                                 //Remove route and controller from route defaults 
 246                                 unset($route['_route'], $route['_controller']); 
 248                                 //Check if snippet view route 
 249                                 if ($name == 'rapsys_air_organizer_view' && !empty($route['id'])) { 
 251                                         $route['id'] = $snippet->getUser()->getId(); 
 255                                         $route['snippet'] = $snippet->getId(); 
 259                                 return $this->redirectToRoute($name, $route); 
 261                         } catch(MethodNotAllowedException
|ResourceNotFoundException 
$e) { 
 262                                 //Unset referer to fallback to default route 
 267                 //Redirect to cleanup the form 
 268                 return $this->redirectToRoute('rapsys_air', ['snippet' => $snippet->getId()]);