src/Controller/MaterialController.php line 85

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Service\ApiRequest;
  4. use App\Service\ContentAds;
  5. use App\Service\UserService;
  6. use App\Service\ContentService;
  7. use Mobile_Detect;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. class MaterialController extends AbstractController
  12. {
  13.     /**
  14.      * @Route("/materials/", name="materials")
  15.      */
  16.     public function index(ApiRequest $apiRequestContentAds $contentAdsUserService $userService)
  17.     {
  18.         // Featured materials for header gallery
  19.         $featured $apiRequest->getFeatured('material');
  20.         // Starting content for search section
  21.         $search $apiRequest->getContentPaginated('material',1,8);
  22.         // Schedule
  23.         $calendar $apiRequest->getArticlesBySubtypeCalendar(date('Y/m').'/01');
  24.         // Añadir anuncios
  25.         $search['data'] = $contentAds->insertAdsGoogle($search['data']);
  26.         // Sidebar categories
  27.         $sidebar $apiRequest->getCategories();
  28.         // Logos media partners
  29.         $mediaPartners $apiRequest->getMediaPartners();
  30.         // Determine if mobile
  31.         $detect = new Mobile_Detect;
  32.         $mobile false;
  33.         if ($detect->isMobile()) {
  34.             $mobile true;
  35.         }
  36.         return $this->render('materials/index.html.twig', [
  37.             'controller_name' => 'MaterialController',
  38.             'featured' => $featured,
  39.             'search' => $search,
  40.             'sidebar' => $sidebar,
  41.             'mediaPartners' => $mediaPartners,
  42.             'user' => $userService->checkUser(),
  43.             'calendar' => $calendar,
  44.             'mobile' => $mobile
  45.         ]);
  46.     }
  47.     /**
  48.      * @Route("/materials/search/", name="materials_search")
  49.      */
  50.     public function search(Request $requestApiRequest $apiRequestContentAds $contentAds)
  51.     {
  52.         if($request->isXmlHttpRequest()){
  53.             $result $apiRequest->getContentPaginated(
  54.                 'material',
  55.                 $request->get('page'1),
  56.                 $request->get('limit'8),
  57.                 $request->get('search'''),
  58.                 null,
  59.                 $request->get('category''')
  60.             );
  61.             // Añadir anuncios
  62.             $result['data'] = $contentAds->insertAdsGoogle($result['data']);
  63.             return $this->render('materials/search_result.v2.html.twig', [
  64.                 'search' => $result
  65.             ]);
  66.         }else{
  67.             return $this->redirect('/page-not-found');
  68.         }
  69.     }
  70.     /**
  71.      * @Route("/materials/{slug}/", name="material")
  72.      */
  73.     public function show($slugApiRequest $apiRequestUserService $userServiceContentService $contentService)
  74.     {
  75.         $user $userService->checkUser();
  76.         $token null;
  77.         if($user$token $user['token'];
  78.         $csrfToken $this->get('security.csrf.token_manager')->getToken('token_id')->getValue();
  79.         $material $apiRequest->getContent($slug$token'material');
  80.         if(!$material){
  81.             return $this->redirect('/page-not-found');
  82.         }
  83.         $material['breadcrumb'] = [];
  84.         if(@$material['category'][0]){
  85.             $hierachy $apiRequest->getHierarchy($material['category'][0]['id']);
  86.             if(!array_key_exists('error',$hierachy)){
  87.                 $material['breadcrumb'] = $hierachy;
  88.             }
  89.         }
  90.         $material['related_content'] = $apiRequest->getRelated($material['id']);
  91.         /*
  92.          * VIDEOS
  93.          *    Platforms
  94.          *      1: YouTube
  95.          *      2: Vimeo
  96.          */
  97.         $videos = [];
  98.         foreach($material['video'] as $video){
  99.             if($video['platform'] == 1){
  100.                 /*
  101.                  * Supports:
  102.                  * youtube.com/v/vidid
  103.                  * youtube.com/vi/vidid
  104.                  * youtube.com/?v=vidid
  105.                  * youtube.com/?vi=vidid
  106.                  * youtube.com/watch?v=vidid
  107.                  * youtube.com/watch?vi=vidid
  108.                  * youtu.be/vidid
  109.                  * youtube.com/embed/vidid
  110.                  * http://youtube.com/v/vidid
  111.                  * http://www.youtube.com/v/vidid
  112.                  * https://www.youtube.com/v/vidid
  113.                  * youtube.com/watch?v=vidid&wtv=wtv
  114.                  * http://www.youtube.com/watch?dev=inprogress&v=vidid&feature=related
  115.                  * https://m.youtube.com/watch?v=vidid
  116.                  */
  117.                 preg_match("/^(?:http(?:s)?:\/\/)?(?:www\.)?(?:m\.)?(?:youtu\.be\/|youtube\.com\/(?:(?:watch)?\?(?:.*&)?v(?:i)?=|(?:embed|v|vi|user)\/))([^\?&\"'>]+)/"$video['url'], $matches);
  118.                 if(@$matches[1]){
  119.                     $videos[] = '<iframe src="https://www.youtube.com/embed/'.$matches[1].'" width="100%" frameBorder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen style="height:400px;"></iframe>';
  120.                 }
  121.             }elseif(@$material['video']['url']){
  122.                 /*
  123.                  * Supports:
  124.                  * All vimeo urls which end with the video id
  125.                  */
  126.                 preg_match("/\/(\d+)$/"$video['url'], $matches);
  127.                 $videos[] = '<iframe src="https://player.vimeo.com/video/'.$matches[1].'" width="100%" frameBorder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen style="height:400px;"></iframe>';
  128.             }
  129.         }
  130.         $material['videos'] = $videos;
  131.         // Logos media partners
  132.         $mediaPartners $apiRequest->getMediaPartners();
  133.         // Determine if mobile
  134.         $detect = new Mobile_Detect;
  135.         $mobile false;
  136.         if ($detect->isMobile()) {
  137.             $mobile true;
  138.         }
  139.         // Si no hay usuario, se sustituyen los enlaces a PDF por el modal de registro
  140.         if(!$user){
  141.             $material['description']['es'] = $contentService->modifyPdfLinks($material['description']['es']);
  142.         }
  143.         return $this->render('materials/material.html.twig', [
  144.             'controller_name' => 'MaterialController',
  145.             'material' => $material,
  146.             'mediaPartners' => $mediaPartners,
  147.             'user' => $user,
  148.             'mobile' => $mobile,
  149.             'csrfToken' => $csrfToken
  150.         ]);
  151.     }
  152. }