<?php
namespace App\Controller;
use App\ClassTheatre\asides;
use App\ClassTheatre\TheatreController;
use App\Service\Abonnements\Abonnements;
use App\Service\Functions;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use TheatreCore\Entity\BookmarkExtranetuser;
use TheatreCore\Entity\Commandes;
use TheatreCore\Entity\Contacts;
use TheatreCore\Entity\Contents;
use TheatreCore\Entity\Dvds;
use TheatreCore\Entity\Extranetusers;
use TheatreCore\Entity\MultimediaDownload;
use TheatreCore\Entity\MultimediaExtranetuser;
use TheatreCore\Entity\Multimedias;
use TheatreCore\Entity\UserAction;
class VideoController extends TheatreController
{
protected $serviceAbonnements;
protected $request;
// {{{ _construct()
/** constructeur hérité
*
*/
public function __construct(EntityManagerInterface $em, TranslatorInterface $translator, TokenStorageInterface $tokenStorage, Functions $func, Abonnements $abonnements)
{
// load new aside
$this->asides = new asides();
$GLOBALS['rub_courante'] = 'video';
$this->em = $em;
$this->func = $func;
$this->serviceAbonnements = $abonnements;
// hérite du constructeur parent
parent::__construct($translator, $tokenStorage);
}
public function InitController($slug = null, $action = null)
{
$this->context['action'] = $action;
$this->RouterOptions($slug, $action, $GLOBALS['rub_courante']);
$this->view->controller = $GLOBALS['rub_courante'];
}
/**
* @Route("/video/{slug}", name="video_slug")
*/
public function action_view(Request $request, $slug = null, SpectacleController $spectacleController)
{
$this->request = $request;
$this->InitController($slug, 'view');
$idmultimedia = $this->id;
if (! $idmultimedia) {
return $this->redirectToRoute('erreur_type', ['type' => 'introuvable']);
}
$multimedias = $this->em->getRepository(Multimedias::class);
// pas de cache
$multimedias->setParameters(array('nocache' => date('YmdHis')));
// video
$video = $multimedias->getInfosMultimedia($idmultimedia, false, false, array('tableau' => true));
// video non publiée ??
if (! $video['publish']) {
// si pas authentifie, on redirige sur la page de connexion
if (!$this->context['session'] instanceof Extranetusers) {
return $this->redirectToRoute('app_login', ['urifrom' => '/video/' . $video['url_clean']]);
}
$idextranetuser = $this->getUser()->getIdextranetuser();
if ($this->isGranted('ROLE_ISROOT') === false) {
if ($video['idextranetuser'] != $idextranetuser && !$this->em->getRepository(MultimediaExtranetuser::class)->isAuthorized($idmultimedia, $idextranetuser)) {
return $this->redirectToRoute('erreur_type', ['type' => 'non-accessible']);
}
}
$this->view->unpublished_video = true;
}
// voir si l'utilisateur est dans les utilisateurs autorisés pour un des contacts/partenaires
if ($this->isGranted('ROLE_ISROOT')) {
$this->context['view']->is_contact_user = true;
} elseif (!empty($video['partenaires'])) {
foreach ($video['partenaires'] as $k => $v) {
// controlleur pour l'edition
// $edit_utils = new Utils_edit($this->context,array(
// 'object' => 'contacts',
// 'idobject' => $v['idcontact'],
// ));
//
// // les droits
// $object_rights = $edit_utils->checkObjectRights();
// if(!empty($object_rights['select'])) {
// $this->context['view']->is_contact_user = true;
// break;
// }
}
}
// si c'est une vidéo en accès restreint
// si pas authentifie, on redirige sur la page de connexion
if (!empty($video['status']) && $video['status'] == 4) {
if (!$this->isGranted('ROLE_ISROOT')) {
return $this->redirectToRoute('app_login', ['urifrom' => '/video/' . $video['url_clean'], 'entete_connexion' => 'video']);
} else {
// marquer la vue dans user_action
$this->em->getRepository(UserAction::class)->setAction('multimedias', $idmultimedia, 'view', $this->getUser()->getIdextranetuser());
}
// si VOD, on vérifie que la personne est encore le droit de visionner
} elseif (!empty($video['status']) && $video['status'] == 5) {
$commandes = $this->em->getRepository(Commandes::class);
$this->view->vod = true;
// si pas logué => message classique avec prix ect...
if (!$this->isGranted('ROLE_ISROOT')) {
// au cas ou le prix est vide
if (empty($video['price'])) {
$this->view->vod = false;
}
} elseif ($commandes->isExists('multimedias', $idmultimedia, $this->getUser()->getIdextranetUser())) {
// récupèrer les infos sur la commande
$this->view->vod_status = $commandes->getInfosCommande(null, array(
'object' => 'multimedias',
'idobject' => $idmultimedia,
'idextranetuser' => $this->getUser()->getIdextranetuser()
));
}
}
// calcul de la hauteur de la vidéo pour une argeur de 560
$width_video = 560;
$height_video = round(($width_video * $video['flv_height']) / $video['flv_width']);
$multimedias->setDimensions(array('width' => $width_video, 'height' => $height_video));
// paramètre pour les stats
$multimedias->setStatsParameters();
// récupération du player
$video['player'] = $multimedias->getPlayer($video);
// trouver si il faut afficher en 16/9 ou en 4/3 (560x440)
// si policy_access == 1
if ($video['status'] == 1) {
// calculer la hauteur à partir de la largeur
$width_video_embed = 480;
$height_video_embed = round(($width_video_embed * $video['flv_height'] / $video['flv_width']));
if (!empty($video['embed'])) {
$video['embed'] = str_replace(480, $width_video_embed, $video['embed']);
$video['embed'] = str_replace(405, $height_video_embed, $video['embed']);
// suppression paramètres stats et autres dans la version embed
$video['embed'] = preg_replace('#(/video/swf/[a-z0-9]+)%3F[^"]*"#i', '\1"', $video['embed']);
}
} else {
unset($video['embed']);
}
//$this->context['view']->new_player = true;
// chercher s'il y a des Dvds
$dvds = $this->em->getRepository(Dvds::class);
$this->view->list_dvds = $dvds->getListDvds(
array(
'special' => array(
'idmultimedias' => array($idmultimedia)
)
)
);
// créer la vignette static sans fleche de 140x120 si elle n'existe pas
if (!empty($video['image_media']) &&
(!empty($video['audio_video']) && $video['audio_video'] == 2)) {
$static_thumbnail = $multimedias->getStaticThumbnail($idmultimedia, $video['code']);
$video['static_thumbnail'] = $static_thumbnail;
}
// partage
$video['share'] = true;
//nombre de multimedia à 0 par defaut
$this->context['view']->nb_multimedias = 0;
// partenaires de la vidéo
if (!empty($video['partenaires'])) {
foreach ($video['partenaires'] as $k => $v) {
// tester si il y a une image de profile
if (count($video['partenaires']) == 1) {
$profilepicture = $this->em->getRepository(Contacts::class)->getVignetteContact($v['idcontact'], array('width' => 40, 'height' => 40), 'class_pp');
if (strpos($profilepicture, 'normal-def') === false) {
$this->view->profilepicture = $profilepicture;
}
}
//on récupère le nombre de vidéo par contact
$this->view->contact = $v;
// trouver les videos de ce contact
$sql_all_videos = 'SELECT COUNT(DISTINCT om.idmultimedia)
FROM object_multimedia om, multimedias m
WHERE om.idmultimedia=m.idmultimedia
AND m.audio_video=2
AND m.publish=1
AND om.idobject=' . $this->view->contact['idcontact'] . '
AND om.object=\'contacts\' ';
$nb_multimedias = $multimedias->queryOne($sql_all_videos);
if ($nb_multimedias > 1) {
$this->context['view']->nb_multimedias += $nb_multimedias;
$video['partenaires'][$k]['nb_multimedias'] = $nb_multimedias;
}
//unset($video['partenaires']);
}
}
if (empty($this->context['view']->no_partenaires_top)) {
$this->context['view']->partenaires_top = true;
}
// meta keywords et description
if (empty($meta_keywords)) $meta_keywords = '';
// ajout des noms des auteurs et metteurs en scène aux keywords
foreach (array('authors', 'directors') as $field) {
if (!empty($video[$field])) {
// ajout des auteurs aux keywords
foreach ($video[$field] as $k => $v) {
$meta_keywords .= ' ' . $v['lastname'];
}
}
}
// ajout du lieu aux keywords
if (!empty($video['lieu'])) $meta_keywords .= ' ' . $video['lieu'];
// ajout des titres de spectacles aux keywords
if (!empty($video['spectacles'])) {
foreach ($video['spectacles'] as $k => $v) {
$meta_keywords .= ' ' . $v['title'];
}
}
//description
$meta_description = '';
$video['description'] = $this->em->getRepository(Contents::class)->wikiTransform($video['description']);
$this->func->makeMetaFromHtml($video['description'], $meta_description, $meta_keywords);
function isSecure()
{
return
(!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off')
|| $_SERVER['SERVER_PORT'] == 443;
}
if (isSecure()) {
$video['player'] = str_replace('http://', 'https://', $video['player']);
}
// couper la description
$taille_description = 200;
if (mb_strlen($video['description'], 'UTF-8') > $taille_description) {
$video['description_courte'] = $multimedias->cutSentence(html_entity_decode($video['description'], ENT_QUOTES, 'UTF-8'), $taille_description, true);
$video['cut_description'] = true;
}
if (!empty($video['mediatype'])) {
$video['typemultimedia'] = $multimedias->getTypeMultimediaLabel($video['mediatype']);
}
$video['langue'] = $multimedias->getNameLanguage($video['language']);
if (!empty($video['audioVideo']) && $video['audioVideo'] == 1) {
$video['only_sound'] = true;
}
// l'url encodé pour le qr code
$video['url_clean_encode'] = urlencode($_ENV['URL_THTV'] . '/video/' . $video['url_clean']);
// code qr démandé ?
if (isset($_GET['qrcode'])) {
$this->view->qrcode = true;
}
// com démandé ?
if (isset($_GET['comment'])) {
$this->view->comment = true;
}
// format json pour iphoque
if (isset($params) && array_key_exists('json', $params)) {
// vignette
$json_thumbnail = $multimedias->getStaticThumbnail($idmultimedia, $video['code'], 110);
$video['vignette'] = $json_thumbnail;
// string spéaré par des virgules pour authors, directors, actors, spectacles
foreach (array('authors', 'directors', 'actors', 'spectacles', 'texts') as $f) {
$string_format = null;
$fullname = array();
if (!empty($video[$f])) {
if ($f != 'spectacles') {
foreach ($video[$f] as $k => $v) {
$fullname[] = $video[$f][$k]['firstname'] . ' ' . $video[$f][$k]['lastname'];
}
} else {
foreach ($video[$f] as $k => $v) {
$fullname[] = $video[$f][$k]['title'];
}
}
$string_format = join(', ', $fullname);
$video[$f . '_string'] = $string_format;
}
}
$this->context['action'] = 'json';
//$this->view->video = json_encode($video);
$ajaxreponse = true;
//header('Content-Type: application/xml');
$json = true;
} else {
$this->view->video = $video;
}
// ajout meta oembed
$this->context['view']->meta_fb[] = array(
'rel' => 'alternate',
'type' => 'application/json+oembed',
// 'href' => URL_THTV.'/oembed?url='.urlencode($video['permalink']),
'title' => $video['sujet_titre'] . ' oEmbed Profile',
);
$this->context['view']->meta_fb[] = array(
'rel' => 'alternate',
'type' => 'text/xml+oembed',
// 'href' => URL_THTV.'/oembed?url='.urlencode($video['permalink']).'&format=xml',
'title' => $video['sujet_titre'] . ' oEmbed Profile',
);
// version mobile
if (isset($_GET['mobile'])) {
$this->context['action'] = 'mobile_' . $this->context['action'];
}
/*print_r($this->view->video);
exit;*/
// paramètres pour les coms
$params_comments = array(
'url_clean' => '/video/' . $video['url_clean'],
'url_clean_rss' => '/video/' . $video['url_clean'] . '?rss-commentaires',
'object' => 'multimedias',
'idobject' => $idmultimedia,
//'nb_comments' => $nb_comments,
);
// réponse rest
if (isset($params) && array_key_exists('xml', $params)) {
// authentifier le web service
$extranetusers = $this->em->getRepository(Extranetusers::class);
// skin avignon
if (!empty($_GET['skin']) && $_GET['skin'] == 'avignon' && !empty($video['embed'])) {
$this->view->video['embed'] = preg_replace('#(/video/swf/[a-z0-9]+)]*"#i', '\1' . urlencode('?skin=avignon') . '"', $video['embed']);
$this->view->skin_avignon = true;
}
// cle et nom de domaine source (client IP ?)
if (!empty($params['k'])) {
if (!$extranetusers->checkAPIKey($params['k'])) {
$this->view->api_key_error = true;
}
} else {
$this->view->api_key_error = true;
}
$this->context['action'] = 'xml';
$ajaxreponse = true;
header('Content-Type: application/xml');
}
if (empty($title_page)) {
$title_page = $video['sujet_titre'];
}
// vidéos en relation
if (empty($no_related_video)) {
$liste_related_videos = $multimedias->getAssociateMultimedias($idmultimedia, 6, 'AND m.mp3=0');
if (!empty($liste_related_videos)) {
// trouver les videos en fonction du type
$sql = 'SELECT * FROM ' . $multimedias->__table . '
WHERE idmultimedia IN (' . implode(',', $liste_related_videos) . ')';
//echo $sql;
$multimedias->query($sql);
$set_format = (array('width' => 130, 'height' => 110, 'direction' => 'crop', 'fleche' => 'moyen'));
while ($multimedias->fetch()) {
$idm = $multimedias->idmultimedia;
$videos[$idm] = $multimedias->getInfosMultimedia($idm, false, false, array('vignette' => $set_format, 'tableau' => true));
$videos[$idm]['description_courte'] = $multimedias->cutSentence($videos[$idm]['description'], 100);
}
$this->view->videos = $videos;
$this->view->first_spectacles = true;
if (!empty($json)) {
$video['related'] = $videos;
}
}
}
// demande le squelette ajax
if (isset($_GET['fjs'])) {
// template
$this->context['action'] = 'simple';
// pas de com et de related
$this->view->no_apropos = true;
$this->view->no_com = true;
$this->view->videos = null;
// pas de tidy
$ajaxreponse = true;
}
if (!empty($json)) {
$this->view->video = json_encode($video);
}
// autres infos
$mtinfos = array('object' => 'multimedias', 'idobject' => $idmultimedia, 'urifrom' => '/video/' . $video['url_clean'], 'info_rub' => 'cette vidéo');
$this->view->mtinfos = $mtinfos;
// voir si l'objet est bookmarke
if ($this->isGranted('ROLE_ISROOT')) {
$bookmark = $this->em->getRepository(BookmarkExtranetuser::class);
if ($bookmark->isBookmark($idmultimedia, 'multimedias', $this->getUser()->getIdextranetuser())) {
$this->view->is_bookmark = true;
}
}
// meta
$this->view->meta_video = true;
// action pour add/delete bookmark
if (isset($_GET['bookmark'])) {
$this->view->bookmark = true;
$do_action = $_GET['do_action'];
$this->view->$do_action = true;
$this->view->fem = 'e';
}
//setDebug($this->view->video);
if (empty($this->view->urifrom_connect)) {
$this->view->urifrom_connect = '/video/' . $video['url_clean'];
}
// bouton de partage
$this->view->use_addthis = true;
// infos barre d'admin
$mtinfos = array(
'object' => 'multimedias',
'idobject' => $idmultimedia,
'special_pattern' => 'admin_barre'
);
$this->video = $video;
if (!empty($video['spectacles']) && count($video['spectacles']) == 1) {
$spectacleController->id = key($video['spectacles']);
$spectacleController->InitController();
$spectacleController->getBuildMenu(['remove' => ['videos', 'evenements']]);
// $spectacleController->action_videos();
$this->view->menu_spectacle = $spectacleController->view->menu_spectacle;
$x = $this->view->data_spectacles = $spectacleController->getDataSpectacles();
}
return $this->view('video/view.html.twig');
}
/**
* @Route("/video/{slug}/download/format/{p_format}", name="video_slug_download_format")
*/
public function action_download(Request $request, $slug = null, $p_format = null)
{
$this->request = $request;
$this->InitController($slug, 'view');
$params['format'] = $this->context['params']['format'] = $p_format;
$idmultimedia = $this->id;
$multimedias = $this->em->getRepository(Multimedias::class);
$video = $multimedias->getInfosMultimedia($idmultimedia, false, false, array('tableau' => true));
if (!$video) {
return $this->redirectToRoute('index');
}
$formats_disponibles = array('2160HD' => 'hd2160', '1080HD' => 'hd1080', '720HD' => 'hd720', 'Mobile' => 'h264m', '240p' => 'h264');
if (!empty($params['format']) && array_key_exists($params['format'], $formats_disponibles)) {
$params_format = $formats_disponibles[$params['format']];
} else {
return $this->redirectToRoute('video_slug', ['slug' => $video['url_clean'], 'erreur' => 'format']);
}
// pas de caches
$multimedias->setParameters(array('nocache' => date('YmdHis')));
// retrouver l'abonnement de l'utilisateur
$this->view->abonnement = $this->abonnementIsActive();
//Si l'abonneent de l'utilisateur est actif
if ($this->view->abonnement) {
$this->view->abonnement = true;
$values_download = array(
'idmultimedia' => $video['idmultimedia'],
'idextranetuser' => $this->getUser()->getIdextranetuser(), // date d'insertion,
'format' => $params_format
);
//On mets à jours les stats
$multimedia_download = $this->em->getRepository(MultimediaDownload::class);
// preparer les champs
$multimedia_download->setUpdateFields($values_download);
$res = $multimedia_download->setData(null, true);
//Si la vidéo est sous licence etalab
if (!empty($video['etalab']) && $video['etalab']) {
$path_to_somefile = $video['formats'][$params_format]['medialocalpath'];
$somefile = "video-" . uniqid() . "-" . $params_format . ".mp4";
header("X-Sendfile: $path_to_somefile");
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"$somefile\"");
} // Erreur
else {
return $this->redirectToRoute('video_slug', ['slug' => $video['url_clean'], 'erreur' => 'etalab']);
}
} else {
return $this->redirectToRoute('video_slug', ['slug' => $video['url_clean'], 'erreur' => 'abonnement']);
}
}
public function finalizeAction()
{
if (!empty($_REQUEST['erreur'])) {
// $this->view->erreur_etalab = true;
if ($_REQUEST['erreur'] == 'etalab') {
$this->view->erreur['etalab'] = true;
} else if ($_REQUEST['erreur'] == 'format') {
$this->view->erreur['format'] = true;
} else if ($_REQUEST['erreur'] == 'abonnement') {
$this->view->erreur['abonnement'] = true;
}
}
if (!empty($_REQUEST['d'])) {
$this->view->download_ok = true;
}
// retrouver l'abonnement de l'utilisateur
$this->view->abonnement = $this->abonnementIsActive();
// $('url_clean'($video);
if (isset($this->video)) {
$this->request->getSession()->set('currentVideo', md5($this->video['idmultimedia']));
}
$locate = array(0 => 'Video');
$title_page = $this->video['sujet_titre'];
$rub_courante = 'videos';
$menu = array(
array(
'name' => 'Mes videos favorites',
'key_sub' => 'videos',
),
array(
'name' => 'Mes videos modifiables',
'key_sub' => 'mesvideos',
),
);
$asides = new asides();
$asides->load('ajoutez_MT.inc');
$asides->load(['url_lists_alone' => array(
'exec' => ['color' => 'index'],
'php' => [
'method' => 'asidesUrlLists',
'urls' => $menu,
'url' => '/montheatre/key_sub'
],
'title' => 'Mon théâtre'
)]);
$asides->load('videos_suggestions');
$this->context['view']->aside = $asides->view();
asides::setEntityManager($this->em);
//On affiche la publicité google
$asides_publicite = new asides();
asides::loadaside('Publicite')->addGoogle($asides_publicite, $this->serviceAbonnements);
$this->context['view']->asides_publicite = $asides_publicite->view($this->context['view']);
$asides_publicite = new asides();
asides::loadaside('Publicite')->addGoogleCorrespondantImageVideo($asides_publicite, $this->serviceAbonnements);
$this->context['view']->asides_publicite_footer = $asides_publicite->view($this->context['view']);
}
}