/**
  * Render a navigation using the navigation code as the fetch criteria
  *
  * @param Request $request The Request
  * @param string  $code     The navigation code
  * @param int     $maxLevel The level maximum limit, this is the rendering loop level limit, not the section entity level
  * @param bool    $exploded When false only the currently selected tree path is displayed
  * @param string  $template Force the template code to use
  * @param array   $attr     Array of attribure to add to the element (Ex. id="aaa" class="bbb")
  *
  * @return Response
  *
  * @throws \Exception
  */
 public function byCodeAction(Request $request, $code, $maxLevel = 10, $exploded = false, $template = '', $attr = array())
 {
     // Cache
     $response = new Response();
     $response->setPublic();
     $sectionLastUpdate = $this->sectionRepository->findLastUpdate();
     $sectionNavigationLastUpdate = $this->sectionNavigationRepository->findLastUpdate();
     $response->setEtag($sectionLastUpdate . $sectionNavigationLastUpdate);
     if ($response->isNotModified($request)) {
         return $response;
     }
     // Rebuild the cache
     $app_id = $this->getApp()->getId();
     $navigation = $this->navigationRepository->findOneByCodeAndApp($code, $app_id);
     if (false == $navigation) {
         throw new \Exception('Can\'t find a navigation entity using code "' . $code . '"');
     }
     $sections = $this->sectionRepository->findByNavigationAndApp($navigation->getId(), $app_id);
     $template = $template ? '_' . $template : '';
     $navigationBuilder = $this->get('unifik_system.navigation_builder');
     $navigationBuilder->setElements($sections, true, $maxLevel);
     $navigationBuilder->setSelectedElement($this->getCore()->getSection());
     $navigationBuilder->build();
     $elements = $navigationBuilder->getElements();
     return $this->render('UnifikSystemBundle:Frontend/Navigation:by_code' . $template . '.html.twig', array('code' => $code, 'sections' => $elements, 'maxLevel' => $maxLevel, 'currentSection' => $this->getSection(), 'attr' => $attr, 'exploded' => $exploded), $response);
 }
 /**
  * Displays a form to edit an existing Section entity or create a new one.
  *
  * @param integer $id      The id of the Section to edit
  * @param Request $request
  *
  * @return RedirectResponse|Response
  */
 public function editAction($id, Request $request)
 {
     $entity = $this->sectionRepository->find($id);
     if (false == $entity) {
         $entity = $this->initEntity(new Section());
         $entity->setApp($this->getApp());
     }
     $this->pushNavigationElement($entity);
     $form = $this->createForm(new RootSectionType(), $entity, array('current_section' => $entity, 'managed_app' => $this->getApp()));
     if ('POST' == $request->getMethod()) {
         $form->submit($request);
         if ($form->isValid()) {
             $this->getEm()->persist($entity);
             // On insert
             if (false == $id) {
                 $sectionModuleBar = $this->navigationRepository->find(NavigationRepository::SECTION_MODULE_BAR_ID);
                 $backendApp = $this->appRepository->find(1);
                 $mapping = new Mapping();
                 $mapping->setSection($entity);
                 $mapping->setApp($backendApp);
                 $mapping->setType('route');
                 $mapping->setTarget('unifik_system_backend_text');
                 $entity->addMapping($mapping);
                 $mapping = new Mapping();
                 $mapping->setSection($entity);
                 $mapping->setApp($backendApp);
                 $mapping->setNavigation($sectionModuleBar);
                 $mapping->setType('render');
                 $mapping->setTarget('UnifikSystemBundle:Backend/Text/Navigation:SectionModuleBar');
                 $entity->addMapping($mapping);
                 $mapping = new Mapping();
                 $mapping->setSection($entity);
                 $mapping->setApp($backendApp);
                 $mapping->setNavigation($sectionModuleBar);
                 $mapping->setType('render');
                 $mapping->setTarget('UnifikSystemBundle:Backend/Section/Navigation:SectionModuleBar');
                 $entity->addMapping($mapping);
                 // Frontend mapping
                 $mapping = new Mapping();
                 $mapping->setSection($entity);
                 $mapping->setApp($this->getApp());
                 $mapping->setType('route');
                 $mapping->setTarget('unifik_system_frontend_text');
                 $entity->addMapping($mapping);
             }
             $this->getEm()->flush();
             $this->get('unifik_system.router_invalidator')->invalidate();
             $this->addFlashSuccess($this->get('translator')->trans('%entity% has been saved.', array('%entity%' => $entity)));
             if ($request->request->has('save')) {
                 return $this->redirect($this->generateUrl('unifik_system_backend_section_root', array('appSlug' => $this->getApp()->getSlug())));
             }
             return $this->redirect($this->generateUrl('unifik_system_backend_section_root_edit', array('id' => $entity->getId() ?: 0, 'appSlug' => $this->getApp()->getSlug())));
         } else {
             $this->addFlashError('Some fields are invalid.');
         }
     }
     return $this->render('UnifikSystemBundle:Backend/Section/Root:edit.html.twig', array('entity' => $entity, 'form' => $form->createView(), 'managedApp' => $this->getApp()));
 }