src/Controller/IndexController.php line 18

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Component\Debug\Exception\FlattenException;
  4. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\Asset\PathPackage;
  8. use Symfony\Component\Asset\VersionStrategy\StaticVersionStrategy;
  9. use Uicms\App\Service\Model;
  10. use App\Service\MenuHelper;
  11. class IndexController extends AbstractController
  12. {
  13.     public function index(Model $modelRequest $requestMenuHelper $menu_helper$slug=''$action=''$locale='')
  14.     {
  15.         if($locale && !preg_match("'^[a-z]{2}$'"$locale)) {
  16.             throw $this->createNotFoundException('This locale does not exist!');
  17.         }
  18.         
  19.         $version 'v1.2';
  20.         $this->get('session')->set('theme_path', new PathPackage('themes/app', new StaticVersionStrategy($version)));
  21.         $this->get('session')->set('js_path', new PathPackage('js', new StaticVersionStrategy($version)));
  22.         
  23.         # UI Config
  24.         $ui_config $this->getParameter('ui_config');
  25.         $this->get('session')->set('ui_config'$ui_config);
  26.         
  27.         # Locale
  28.         $session $this->get('session');
  29.         $session->set('locale'$locale);
  30.         $request->setLocale($locale);
  31.         
  32.         # Current page
  33.         if(!$slug) {
  34.             $page $model->get('Page')->getRow(['dir'=>0]);
  35.         } else {
  36.             $page $model->get('Page')->getRow(['findby'=>['slug' => $slug]]);
  37.         }
  38.         while($page->getIsDir()) {
  39.             if(!$page $model->get('Page')->getRow(['dir'=>$page->getId()])) {
  40.                 break;
  41.             }
  42.         }
  43.         if(!$page) {
  44.             throw $this->createNotFoundException('No data found for page '.$slug);
  45.         }
  46.         
  47.         # Current action
  48.         if(!$action) {
  49.             $action $page->getAction() ? $page->getAction() : 'index';
  50.         }
  51.         $session->set('current_action'$action);
  52.         
  53.         # Menu
  54.         $menu1 $model->get('Page')->getAll(['dir'=>0'findby'=>['menu'=>'menu1']]);
  55.         foreach($menu1 as $i=>$menu_page) {
  56.             $menu_page->helper_html $menu_helper->get($menu_page);
  57.             $menu_page->children $model->get('Page')->getAll(['dir'=>$menu_page->getId()]);
  58.         }
  59.         $session->set('menu1'$menu1);
  60.         $menu2 $model->get('Page')->getAll(['dir'=>0'findby'=>['menu'=>'menu2']]);
  61.         foreach($menu2 as $i=>$menu_page) {
  62.             $menu_page->helper_html $menu_helper->get($menu_page);
  63.             $menu_page->children $model->get('Page')->getAll(['dir'=>$menu_page->getId()]);
  64.         }
  65.         $session->set('menu2'$menu2);
  66.         
  67.         # Pages slugs
  68.         if($authentication_page $model->get('Page')->getRow(['findby'=>['controller'=>'authentication']])) {
  69.             $this->get('session')->set('authentication_page_slug'$authentication_page->getSlug());
  70.         }
  71.         if($resources_page $model->get('Page')->getRow(['findby'=>['controller'=>'resources']])) {
  72.             $this->get('session')->set('resources_page_slug'$resources_page->getSlug());
  73.         }
  74.         if($events_page $model->get('Page')->getRow(['findby'=>['controller'=>'events']])) {
  75.             $this->get('session')->set('events_page_slug'$events_page->getSlug());
  76.         }
  77.         if($contributions_page $model->get('Page')->getRow(['findby'=>['controller'=>'contributions']])) {
  78.             $this->get('session')->set('contributions_page_slug'$contributions_page->getSlug());
  79.         }
  80.         if($directory_page $model->get('Page')->getRow(['findby'=>['controller'=>'directory']])) {
  81.             $this->get('session')->set('directory_page_slug'$directory_page->getSlug());
  82.         }
  83.         if($home_page $model->get('Page')->getRow(['findby'=>['class'=>'home']])) {
  84.             $this->get('session')->set('home_page_slug'$home_page->getSlug());
  85.         }
  86.         if($projets_page $model->get('Page')->getRow(['findby'=>['controller'=>'projects']])) {
  87.             $this->get('session')->set('projects_page_slug'$projets_page->getSlug());
  88.         }
  89.         # Filters
  90.         if(!$this->get('session')->get('filter')) {
  91.             $this->get('session')->set('filter'null);
  92.         }        
  93.         $this->get('session')->set('keywords'$model->get('Keyword')->getAll());
  94.         # Attributes
  95.         $attributes array_merge($request->query->all(), $request->request->all(), $request->attributes->all(), ['page' => $page]);
  96.         
  97.         # Params
  98.         $req $model->get('Param')->getAll();
  99.         $params = [];
  100.         foreach($req as $i=>$param) {
  101.             $params[$param->getName()] = $param->getValue();
  102.         }
  103.         $session->set('params'$params);
  104.         # Authentication
  105.         $authenticated_controllers = [
  106.             'contributions',
  107.             'resources',
  108.         ];
  109.         
  110.         if($authentication_page && in_array($page->getController(), $authenticated_controllers) && !$this->get('session')->get('contributor')) {
  111.                 return $this->redirectToRoute('app_page_action', array('slug'=>$this->get('session')->get('authentication_page_slug'), 'action'=>'index''locale'=>$this->get('session')->get('locale')));
  112.         } else if($this->get('session')->get('contributor')) {
  113.             $this->get('session')->set('contributor'$model->get('contributor')->getRowById($this->get('session')->get('contributor')->getId()));
  114.         }
  115.         
  116.         return $this->forward("App\\Controller\\" ucfirst($page->getController()) . "Controller::" $action$attributes);
  117.     }
  118.     
  119.     public function error(FlattenException $exceptionModel $modelRequest $request$slug=''$action=''$locale='')
  120.     {
  121.         return $this->render(
  122.             'app/tpl/error/error.html.twig',
  123.             ['exception'=>$exception]
  124.         );
  125.     }
  126. }