3 namespace Rapsys\AirBundle\Controller
; 
   5 use Symfony\Component\HttpFoundation\Request
; 
   6 use Symfony\Component\HttpFoundation\Response
; 
   7 use Symfony\Component\Routing\RequestContext
; 
   8 use Symfony\Component\Routing\Exception\MethodNotAllowedException
; 
   9 use Symfony\Component\Routing\Exception\ResourceNotFoundException
; 
  10 use Rapsys\AirBundle\Entity\Location
; 
  11 use Rapsys\AirBundle\Entity\Snippet
; 
  12 use Rapsys\AirBundle\Entity\User
; 
  14 class SnippetController 
extends DefaultController 
{ 
  18          * @desc Persist snippet in database 
  20          * @param Request $request The request instance 
  22          * @return Response The rendered view or redirection 
  24          * @throws \RuntimeException When user has not at least guest role 
  26         public function add(Request 
$request) { 
  27                 //Prevent non-guest to access here 
  28                 $this->denyAccessUnlessGranted('ROLE_GUEST', null, $this->translator
->trans('Unable to access this page without role %role%!', ['%role%' => $this->translator
->trans('Guest')])); 
  30                 //Create SnippetType form 
  31                 $form = $this->container
->get('form.factory')->createNamed( 
  33                         'snipped_'.$request->getLocale().'_'.$request->get('location'), 
  35                         'Rapsys\AirBundle\Form\SnippetType', 
  41                                 'action' => $this->generateUrl('rapsys_air_snippet_add', ['location' => $request->get('location')]), 
  42                                 //Set the form attribute 
  47                 //Refill the fields in case of invalid form 
  48                 $form->handleRequest($request); 
  50                 //Prevent creating snippet for other user unless admin 
  51                 if ($form->get('user')->getData() !== $this->getUser()) { 
  52                         //Prevent non-admin to access here 
  53                         $this->denyAccessUnlessGranted('ROLE_ADMIN', null, $this->translator
->trans('Unable to access this page without role %role%!', ['%role%' => $this->translator
->trans('Admin')])); 
  57                 if (!$form->isSubmitted() || !$form->isValid()) { 
  59                         $section = $this->translator
->trans('Snippet add'); 
  62                         $title = $this->translator
->trans($this->config
['site']['title']).' - '.$section; 
  65                         return $this->render('@RapsysAir/snippet/add.html.twig', ['title' => $title, 'section' => $section, 'form' => $form->createView()]+
$this->context
); 
  69                 $doctrine = $this->getDoctrine(); 
  72                 $manager = $doctrine->getManager(); 
  75                 $snippet = $form->getData(); 
  78                 $snippet->setCreated(new \
DateTime('now')); 
  81                 $snippet->setUpdated(new \
DateTime('now')); 
  84                 $manager->persist($snippet); 
  86                 //Flush to get the ids 
  90                 $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()])); 
  92                 //Extract and process referer 
  93                 if ($referer = $request->headers
->get('referer')) { 
  94                         //Create referer request instance 
  95                         $req = Request
::create($referer); 
  98                         $path = $req->getPathInfo(); 
 100                         //Get referer query string 
 101                         $query = $req->getQueryString(); 
 104                         $path = str_replace($request->getScriptName(), '', $path); 
 106                         //Try with referer path 
 109                                 $oldContext = $this->router
->getContext(); 
 111                                 //Force clean context 
 112                                 //XXX: prevent MethodNotAllowedException because current context method is POST in onevendor/symfony/routing/Matcher/Dumper/CompiledUrlMatcherTrait.php+42 
 113                                 $this->router
->setContext(new RequestContext()); 
 115                                 //Retrieve route matching path 
 116                                 $route = $this->router
->match($path); 
 119                                 $this->router
->setContext($oldContext); 
 125                                 $name = $route['_route']; 
 127                                 //Remove route and controller from route defaults 
 128                                 unset($route['_route'], $route['_controller']); 
 130                                 //Check if snippet view route 
 131                                 if ($name == 'rapsys_air_user_view' && !empty($route['id'])) { 
 133                                         $route['id'] = $snippet->getUser()->getId(); 
 137                                         $route['snippet'] = $snippet->getId(); 
 141                                 return $this->redirectToRoute($name, $route); 
 143                         } catch(MethodNotAllowedException
|ResourceNotFoundException 
$e) { 
 144                                 //Unset referer to fallback to default route 
 149                 //Redirect to cleanup the form 
 150                 return $this->redirectToRoute('rapsys_air', ['snippet' => $snippet->getId()]); 
 156          * @desc Persist snippet in database 
 158          * @param Request $request The request instance 
 160          * @return Response The rendered view or redirection 
 162          * @throws \RuntimeException When user has not at least guest role 
 164         public function edit(Request 
$request, $id) { 
 165                 //Prevent non-guest to access here 
 166                 $this->denyAccessUnlessGranted('ROLE_GUEST', null, $this->translator
->trans('Unable to access this page without role %role%!', ['%role%' => $this->translator
->trans('Guest')])); 
 169                 $doctrine = $this->getDoctrine(); 
 172                 if (empty($snippet = $doctrine->getRepository(Snippet
::class)->findOneById($id))) { 
 173                         throw $this->createNotFoundException($this->translator
->trans('Unable to find snippet: %id%', ['%id%' => $id])); 
 176                 //Create SnippetType form 
 177                 $form = $this->container
->get('form.factory')->createNamed( 
 179                         'snipped_'.$request->getLocale().'_'.$snippet->getLocation()->getId(), 
 181                         'Rapsys\AirBundle\Form\SnippetType', 
 187                                 'action' => $this->generateUrl('rapsys_air_snippet_edit', ['id' => $id]), 
 188                                 //Set the form attribute 
 193                 //Refill the fields in case of invalid form 
 194                 $form->handleRequest($request); 
 196                 //Prevent creating snippet for other user unless admin 
 197                 if ($form->get('user')->getData() !== $this->getUser()) { 
 198                         //Prevent non-admin to access here 
 199                         $this->denyAccessUnlessGranted('ROLE_ADMIN', null, $this->translator
->trans('Unable to access this page without role %role%!', ['%role%' => $this->translator
->trans('Admin')])); 
 202                 //Handle invalid form 
 203                 if (!$form->isSubmitted() || !$form->isValid()) { 
 205                         $section = $this->translator
->trans('Snippet %id%', ['%id%' => $id]); 
 208                         $title = $this->translator
->trans($this->config
['site']['title']).' - '.$section; 
 211                         return $this->render('@RapsysAir/snippet/edit.html.twig', ['id' => $id, 'title' => $title, 'section' => $section, 'form' => $form->createView()]+
$this->context
); 
 215                 $manager = $doctrine->getManager(); 
 218                 $snippet->setUpdated(new \
DateTime('now')); 
 221                 $manager->persist($snippet); 
 223                 //Flush to get the ids 
 227                 $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()])); 
 229                 //Extract and process referer 
 230                 if ($referer = $request->headers
->get('referer')) { 
 231                         //Create referer request instance 
 232                         $req = Request
::create($referer); 
 235                         $path = $req->getPathInfo(); 
 237                         //Get referer query string 
 238                         $query = $req->getQueryString(); 
 241                         $path = str_replace($request->getScriptName(), '', $path); 
 243                         //Try with referer path 
 246                                 $oldContext = $this->router
->getContext(); 
 248                                 //Force clean context 
 249                                 //XXX: prevent MethodNotAllowedException because current context method is POST in onevendor/symfony/routing/Matcher/Dumper/CompiledUrlMatcherTrait.php+42 
 250                                 $this->router
->setContext(new RequestContext()); 
 252                                 //Retrieve route matching path 
 253                                 $route = $this->router
->match($path); 
 256                                 $this->router
->setContext($oldContext); 
 262                                 $name = $route['_route']; 
 264                                 //Remove route and controller from route defaults 
 265                                 unset($route['_route'], $route['_controller']); 
 267                                 //Check if snippet view route 
 268                                 if ($name == 'rapsys_air_user_view' && !empty($route['id'])) { 
 270                                         $route['id'] = $snippet->getUser()->getId(); 
 274                                         $route['snippet'] = $snippet->getId(); 
 278                                 return $this->redirectToRoute($name, $route); 
 280                         } catch(MethodNotAllowedException
|ResourceNotFoundException 
$e) { 
 281                                 //Unset referer to fallback to default route 
 286                 //Redirect to cleanup the form 
 287                 return $this->redirectToRoute('rapsys_air', ['snippet' => $snippet->getId()]);