<?php
namespace App\Controller;
use App\Service\ApiRequest;
use App\Service\ContentAds;
use App\Service\UserService;
use App\Service\ContentService;
use Mobile_Detect;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
class MaterialController extends AbstractController
{
/**
* @Route("/materials/", name="materials")
*/
public function index(ApiRequest $apiRequest, ContentAds $contentAds, UserService $userService)
{
// Featured materials for header gallery
$featured = $apiRequest->getFeatured('material');
// Starting content for search section
$search = $apiRequest->getContentPaginated('material',1,8);
// Schedule
$calendar = $apiRequest->getArticlesBySubtypeCalendar(date('Y/m').'/01');
// Añadir anuncios
$search['data'] = $contentAds->insertAdsGoogle($search['data']);
// Sidebar categories
$sidebar = $apiRequest->getCategories();
// Logos media partners
$mediaPartners = $apiRequest->getMediaPartners();
// Determine if mobile
$detect = new Mobile_Detect;
$mobile = false;
if ($detect->isMobile()) {
$mobile = true;
}
return $this->render('materials/index.html.twig', [
'controller_name' => 'MaterialController',
'featured' => $featured,
'search' => $search,
'sidebar' => $sidebar,
'mediaPartners' => $mediaPartners,
'user' => $userService->checkUser(),
'calendar' => $calendar,
'mobile' => $mobile
]);
}
/**
* @Route("/materials/search/", name="materials_search")
*/
public function search(Request $request, ApiRequest $apiRequest, ContentAds $contentAds)
{
if($request->isXmlHttpRequest()){
$result = $apiRequest->getContentPaginated(
'material',
$request->get('page', 1),
$request->get('limit', 8),
$request->get('search', ''),
null,
$request->get('category', '')
);
// Añadir anuncios
$result['data'] = $contentAds->insertAdsGoogle($result['data']);
return $this->render('materials/search_result.v2.html.twig', [
'search' => $result
]);
}else{
return $this->redirect('/page-not-found');
}
}
/**
* @Route("/materials/{slug}/", name="material")
*/
public function show($slug, ApiRequest $apiRequest, UserService $userService, ContentService $contentService)
{
$user = $userService->checkUser();
$token = null;
if($user) $token = $user['token'];
$csrfToken = $this->get('security.csrf.token_manager')->getToken('token_id')->getValue();
$material = $apiRequest->getContent($slug, $token, 'material');
if(!$material){
return $this->redirect('/page-not-found');
}
$material['breadcrumb'] = [];
if(@$material['category'][0]){
$hierachy = $apiRequest->getHierarchy($material['category'][0]['id']);
if(!array_key_exists('error',$hierachy)){
$material['breadcrumb'] = $hierachy;
}
}
$material['related_content'] = $apiRequest->getRelated($material['id']);
/*
* VIDEOS
* Platforms
* 1: YouTube
* 2: Vimeo
*/
$videos = [];
foreach($material['video'] as $video){
if($video['platform'] == 1){
/*
* Supports:
* youtube.com/v/vidid
* youtube.com/vi/vidid
* youtube.com/?v=vidid
* youtube.com/?vi=vidid
* youtube.com/watch?v=vidid
* youtube.com/watch?vi=vidid
* youtu.be/vidid
* youtube.com/embed/vidid
* http://youtube.com/v/vidid
* http://www.youtube.com/v/vidid
* https://www.youtube.com/v/vidid
* youtube.com/watch?v=vidid&wtv=wtv
* http://www.youtube.com/watch?dev=inprogress&v=vidid&feature=related
* https://m.youtube.com/watch?v=vidid
*/
preg_match("/^(?:http(?:s)?:\/\/)?(?:www\.)?(?:m\.)?(?:youtu\.be\/|youtube\.com\/(?:(?:watch)?\?(?:.*&)?v(?:i)?=|(?:embed|v|vi|user)\/))([^\?&\"'>]+)/", $video['url'], $matches);
if(@$matches[1]){
$videos[] = '<iframe src="https://www.youtube.com/embed/'.$matches[1].'" width="100%" frameBorder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen style="height:400px;"></iframe>';
}
}elseif(@$material['video']['url']){
/*
* Supports:
* All vimeo urls which end with the video id
*/
preg_match("/\/(\d+)$/", $video['url'], $matches);
$videos[] = '<iframe src="https://player.vimeo.com/video/'.$matches[1].'" width="100%" frameBorder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen style="height:400px;"></iframe>';
}
}
$material['videos'] = $videos;
// Logos media partners
$mediaPartners = $apiRequest->getMediaPartners();
// Determine if mobile
$detect = new Mobile_Detect;
$mobile = false;
if ($detect->isMobile()) {
$mobile = true;
}
// Si no hay usuario, se sustituyen los enlaces a PDF por el modal de registro
if(!$user){
$material['description']['es'] = $contentService->modifyPdfLinks($material['description']['es']);
}
return $this->render('materials/material.html.twig', [
'controller_name' => 'MaterialController',
'material' => $material,
'mediaPartners' => $mediaPartners,
'user' => $user,
'mobile' => $mobile,
'csrfToken' => $csrfToken
]);
}
}