<?php
namespace App\Controller;
use App\Entity\Playlist;
use App\Repository\ConceptRepository;
use App\Repository\GroupeRepository;
use App\Repository\GroupItemRepository;
use App\Repository\PlaylistRepository;
use App\Repository\VideoRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Yectep\PhpSpreadsheetBundle\Factory;
class PlaylistController extends AbstractController
{
/**
* @Route("/api/get-last-video-time", name="playlist_get_last_video_time")
*/
public function playlist_get_last_video_time(PlaylistRepository $playlistRepository): Response
{
$video = $playlistRepository->getLastPlay();
if($video) {
if(time() < (int)($video->getStartPlay() + $video->getDuration()))
return new Response((int)($video->getStartPlay() + $video->getDuration()));
}
return new Response(time() + 60*5);
}
/**
* @Route("/api/test-video-time", name="playlist_test_video_time")
*/
public function playlist_test_video_time(PlaylistRepository $playlistRepository, Request $request): Response
{
$time = $request->get('time', null);
$duration = $request->get('duration', null);
$videosOverlay = [];
if($time && $duration) {
$videos = $playlistRepository->getVideoOverlay($time, $time + $duration);
if ($videos) {
foreach ($videos as $video) {
$videosOverlay[] = [
'id' => $video->getId(),
'name' => $video->getName(),
'startPlay' => $video->getStartPlay(),
'duration' => $video->getDuration(),
'start' => $video->getStart(),
'end' => $video->getEnd(),
];
}
}
}
return new JsonResponse($videosOverlay);
}
/**
* @Route("/api/get-current-playlist", name="playlist_get_current")
*/
public function playlist_get_current(PlaylistRepository $playlistRepository, Request $request, ConceptRepository $conceptRepository): Response
{
$day = $request->get('day', date('d.m.Y'));
$videos = $playlistRepository->getVideoByDay($day);
$response = [];
$concepts_ = $conceptRepository->findAll();
$reportConcept = [];
foreach ($concepts_ as $concept) {
$reportConcept[$concept->getId()] = [
'name' => $concept->getName(),
'coor' => 0,
'need' => $concept->getNeead(),
];
}
$reportLang = [];
foreach ($videos as $video) {
$response[] = [
'id' => $video->getId(),
'file' => $video->getFile(),
'concept' => $video->getConcept(),
'startPlay' => $video->getStartPlay(),
'duration' => $video->getDuration(),
'name' => $video->getName(),
'theme' => $video->getTheme(),
'autor' => $video->getAutor(),
'lang' => $video->getLang(),
'production' => $video->getProduction(),
'licension' => $video->getLicension(),
'prev' => $video->getPrev(),
];
if(isset($reportConcept[$video->getConcept()])) {
$reportConcept[$video->getConcept()]['coor'] += $video->getDuration();
}
if(isset($reportLang[$video->getLang()])) {
$reportLang[$video->getLang()]['coor'] += $video->getDuration();
} else {
$reportLang[$video->getLang()] = [
'name' => $video->getLang(),
'coor' => $video->getDuration(),
];
}
}
$clearReportConcept = [];
foreach ($reportConcept as $item) {
$clearReportConcept[] = $item;
}
return new JsonResponse([
'videos' => $response,
'reportConcept' => $clearReportConcept,
'reportLang' => $reportLang,
]);
}
/**
* @Route("/api/get-current-playlist-exel", name="playlist_get_current_exel")
*/
public function playlist_get_current_exel(PlaylistRepository $playlistRepository, Request $request, ConceptRepository $conceptRepository): Response
{
$day = $request->get('day', date('d.m.Y'));
$videos = $playlistRepository->getVideoByDay($day);
$response = [];
$concepts_ = $conceptRepository->findAll();
$reportConcept = [];
foreach ($concepts_ as $concept) {
$reportConcept[$concept->getId()] = [
'name' => $concept->getName(),
'coor' => 0,
'need' => $concept->getNeead(),
];
}
$reportLang = [];
foreach ($videos as $video) {
$response[] = [
'id' => $video->getId(),
'file' => $video->getFile(),
'concept' => $video->getConcept(),
'startPlay' => $video->getStartPlay(),
'duration' => $video->getDuration(),
'name' => $video->getName(),
'theme' => $video->getTheme(),
'autor' => $video->getAutor(),
'lang' => $video->getLang(),
'production' => $video->getProduction(),
'licension' => $video->getLicension(),
];
if(isset($reportConcept[$video->getConcept()])) {
$reportConcept[$video->getConcept()]['coor'] += $video->getDuration();
}
if(isset($reportLang[$video->getLang()])) {
$reportLang[$video->getLang()]['coor'] += $video->getDuration();
} else {
$reportLang[$video->getLang()] = [
'name' => $video->getLang(),
'coor' => $video->getDuration(),
];
}
}
$clearReportConcept = [];
foreach ($reportConcept as $item) {
$clearReportConcept[] = $item;
}
$factory = new Factory();
$spreadsheet = $factory->createSpreadsheet();
$spreadsheet->getProperties()->setCreator('Playout')
->setLastModifiedBy('Playout')
->setTitle('Video: '.$day)
->setSubject('Video: '.$day)
->setDescription('Export of scan results with orders.');
$sheet = $spreadsheet->getActiveSheet();
$sheet->setTitle('Export');
$sheet->setCellValue('A1', 'Початок');
$sheet->setCellValue('B1', 'Завершення');
$sheet->setCellValue('C1', 'Назва');
$sheet->setCellValue('D1', 'Тема');
$sheet->setCellValue('E1', 'Автор/ведучий');
$sheet->setCellValue('F1', 'Мова');
$sheet->setCellValue('G1', 'Хронометраж');
$sheet->setCellValue('H1', 'Виробництво');
$sheet->setCellValue('I1', 'Лізензіар');
$sheet->setCellValue('J1', 'Жанр');
$row = 2;
foreach ($response as $respons) {
$sheet->setCellValue('A' . $row, date('H:i:s', $respons['startPlay']));
$sheet->setCellValue('B' . $row, date('H:i:s', $respons['startPlay'] + $respons['duration']));
$sheet->setCellValue('C' . $row, $respons['name']);
$sheet->setCellValue('D' . $row, $respons['theme']);
$sheet->setCellValue('E' . $row, $respons['autor']);
$sheet->setCellValue('F' . $row, $respons['lang']);
$sheet->setCellValue('G' . $row, gmdate("H:i:s", $respons['duration']));
$sheet->setCellValue('H' . $row, $respons['production']);
$sheet->setCellValue('I' . $row, $respons['licension']);
$concept = $conceptRepository->find($respons['concept']);
$sheet->setCellValue('J' . $row, $concept->getName());
$row++;
}
$row++;
foreach ($reportConcept as $item) {
if($item['coor']) {
$sheet->setCellValue('A' . $row, $item['name']);
$sheet->setCellValue('B' . $row, gmdate("H:i:s", $item['coor']));
$row++;
}
}
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, "Xlsx");
$writer->save("/home/admin/web/apipl.mhu.news/public_html/public/report_".time().".xlsx");
return $this->redirect('https://apipl.mhu.news/report_'.time().'.xlsx');
}
/**
* @Route("/api/set-current-playlist-up", name="playlist_set_current_up")
*/
public function playlist_set_current_up(PlaylistRepository $playlistRepository, Request $request, ConceptRepository $conceptRepository): Response
{
$em = $this->getDoctrine()->getManager();
$day = $request->get('day', date('d.m.Y'));
$videoId = $request->get('id');
$videos = $playlistRepository->getVideoByDay($day);
$startPlay = 0;
foreach ($videos as $key => $video) {
if($video->getId() == $videoId && isset($videos[$key - 1])) {
$video->setStartPlay($videos[$key - 1]->getStartPlay());
$startPlay = $videos[$key - 1]->getStartPlay() + $video->getDuration();
$em->persist($video);
$em->flush();
$videos[$key - 1]->setStartPlay($startPlay);
$startPlay = $startPlay + $videos[$key - 1]->getDuration();
$em->persist($videos[$key - 1]);
$em->flush();
} elseif($startPlay) {
$video->setStartPlay($startPlay);
$startPlay = $startPlay + $videos[$key]->getDuration();
$em->persist($videos[$key]);
$em->flush();
}
}
return new JsonResponse([]);
}
/**
* @Route("/api/set-current-playlist-down", name="playlist_set_current_down")
*/
public function playlist_set_current_down(PlaylistRepository $playlistRepository, Request $request, ConceptRepository $conceptRepository): Response
{
$em = $this->getDoctrine()->getManager();
$day = $request->get('day', date('d.m.Y'));
$videoId = $request->get('id');
$videos = $playlistRepository->getVideoByDay($day);
$startPlay = 0;
$nex = null;
foreach ($videos as $key => $video) {
if($video->getId() == $videoId && isset($videos[$key + 1])) {
$videos[$key + 1]->setStartPlay($video->getStartPlay());
$startPlay = $video->getStartPlay() + $videos[$key + 1]->getDuration();
$em->persist($videos[$key + 1]);
$em->flush();
$video->setStartPlay($startPlay);
$startPlay = $startPlay + $video->getDuration();
$em->persist($video);
$em->flush();
$nex = $videos[$key + 1]->getId();
} elseif($startPlay && $nex != $video->getId()) {
$video->setStartPlay($startPlay);
$startPlay = $startPlay + $videos[$key]->getDuration();
$em->persist($videos[$key]);
$em->flush();
}
}
return new JsonResponse([]);
}
/**
* @Route("/api/set-current-temp", name="playlist_set_current_temp")
*/
public function playlist_set_current_temp(PlaylistRepository $playlistRepository, Request $request, ConceptRepository $conceptRepository): Response
{
$em = $this->getDoctrine()->getManager();
$lastVideo = $playlistRepository->find(1661);
$startPlay = $lastVideo->getStartPlay() + $lastVideo->getDuration();
$timestart = strtotime("2024-07-29 00:00:00");
$timeend = strtotime("2024-07-30 00:00:01");
$videos = $playlistRepository->getVideoDayPlay($timestart, $timeend);
/*if(!$videos) {
$videos = $this->checkVideoDay(1);
}*/
$i = 1;
//for($i = 1; $i <=23; $i++) {
// if($i < 10) $i = '0'.$i;
$startPlay = strtotime("2024-09-24 15:45:03");
foreach ($videos as $video) {
echo 'add new: '.$video->getName()." Time: ".date("H:i:s", $startPlay)."\n";
$newVideo = new Playlist();
$newVideo->setName($video->getName());
$newVideo->setFile($video->getFile());
$newVideo->setDuration($video->getDuration());
$newVideo->setAutor($video->getAutor());
$newVideo->setConcept($video->getConcept());
$newVideo->setEnd($video->getEnd());
$newVideo->setLang($video->getLang());
$newVideo->setLicension($video->getLicension());
$newVideo->setProduction($video->getProduction());
$newVideo->setTheme($video->getTheme());
$newVideo->setStart($video->getStart());
$newVideo->setPrev($video->getPrev());
$newVideo->setStartPlay($startPlay);
$em->persist($newVideo);
$em->flush();
$startPlay += $newVideo->getDuration();
}
//}
/*$videos = $playlistRepository->getVideoDayPlayTemp();
foreach ($videos as $video) {
$newVideo = new Playlist();
$newVideo->setName($video->getName());
$newVideo->setFile($video->getFile());
$newVideo->setDuration($video->getDuration());
$newVideo->setAutor($video->getAutor());
$newVideo->setConcept($video->getConcept());
$newVideo->setEnd($video->getEnd());
$newVideo->setLang($video->getLang());
$newVideo->setLicension($video->getLicension());
$newVideo->setProduction($video->getProduction());
$newVideo->setTheme($video->getTheme());
$newVideo->setStart($video->getStart());
$newVideo->setStartPlay($startPlay);
echo $newVideo->getName().' : '.date('d..m.Y H:i:s', $newVideo->getStartPlay()).' : '.date('d..m.Y H:i:s', $newVideo->getStartPlay()+ $newVideo->getDuration())."\n";
$startPlay += $newVideo->getDuration();
$em->persist($newVideo);
$em->flush();
}*/
return new JsonResponse([]);
}
/**
* @Route("/api/set-current-playlist-remove", name="playlist_set_current_remove")
*/
public function playlist_set_current_remove(PlaylistRepository $playlistRepository, Request $request, ConceptRepository $conceptRepository): Response
{
$em = $this->getDoctrine()->getManager();
$day = $request->get('day', date('d.m.Y'));
$videoId = $request->get('id');
$videos = $playlistRepository->getVideoByDay($day);
$startPlay = 0;
$nex = null;
foreach ($videos as $key => $video) {
if($video->getId() == $videoId) {
if(isset($videos[$key + 1])) {
$videos[$key + 1]->setStartPlay($video->getStartPlay());
$startPlay = $video->getStartPlay() + $videos[$key + 1]->getDuration();
$em->persist($videos[$key + 1]);
$em->flush();
$nex = $videos[$key + 1]->getId();
}
$em->remove($video);
$em->flush();
} elseif($startPlay && $nex != $video->getId()) {
$video->setStartPlay($startPlay);
$startPlay = $startPlay + $videos[$key]->getDuration();
$em->persist($videos[$key]);
$em->flush();
}
}
return new JsonResponse([]);
}
/**
* @Route("/api/replace-video", name="replace_video")
*/
public function replace_video(Request $request, PlaylistRepository $playlistRepository, VideoRepository $videoRepository): Response
{
$data = json_decode($request->getContent());
$base = $playlistRepository->find($data->base);
$replace = $videoRepository->find($data->replace);
$duration = $data->duration;
$video = $data->video;
if($data->replace == '1-1') {
$base->setName($video->name);
$base->setFile('_efir:1');
$base->setDuration($duration);
$base->setAutor($video->autor);
$base->setConcept($video->concept);
$base->setEnd($duration);
$base->setLang($video->lang);
$base->setLicension($video->licension);
$base->setProduction($video->production);
$base->setTheme($video->theme);
$base->setStart(0);
$base->setStartPlay($base->getStartPlay());
$base->setPrev('');
} else {
$base->setName($replace->getName());
$base->setFile($replace->getFile());
$base->setDuration($replace->getDuration());
$base->setAutor($replace->getAutor());
$base->setConcept($replace->getConcept());
$base->setEnd($base->getEnd());
$base->setLang($replace->getLang());
$base->setLicension($replace->getLicension());
$base->setProduction($replace->getProduction());
$base->setTheme($replace->getTheme());
$base->setStart($base->getStart());
$base->setStartPlay($base->getStartPlay());
$base->setPrev($base->getPrev());
}
$em = $this->getDoctrine()->getManager();
$em->persist($base);
$em->flush();
return new JsonResponse([]);
}
/**
* @Route("/api/replace-video-group", name="replace_video_group")
*/
public function replace_video_group(Request $request, GroupItemRepository $groupItemRepository, VideoRepository $videoRepository): Response
{
$data = json_decode($request->getContent());
$base = $groupItemRepository->find($data->base);
$replace = $videoRepository->find($data->replace);
$base->setName($replace->getName());
$base->setFile($replace->getFile());
$base->setDuration($replace->getDuration());
$base->setAutor($replace->getAutor());
$base->setConcept($replace->getConcept());
$base->setLang($replace->getLang());
$base->setLicension($replace->getLicension());
$base->setProduction($replace->getProduction());
$base->setTheme($replace->getTheme());
$em = $this->getDoctrine()->getManager();
$em->persist($base);
$em->flush();
return new JsonResponse([]);
}
/**
* @Route("/api/recalc-group", name="recalc_video_group")
*/
public function recalc_video_group(Request $request, GroupItemRepository $groupItemRepository, GroupeRepository $groupeRepository): Response
{
$data = json_decode($request->getContent());
$groupe = $groupeRepository->find($data->group);
$duration = 0;
$items = $groupItemRepository->findBy(['groupe' => $data->group]);
foreach ($items as $item) {
$duration += $item->getDuration();
}
$groupe->setDuration($duration);
$em = $this->getDoctrine()->getManager();
$em->persist($groupe);
$em->flush();
return new JsonResponse([]);
}
}