<?php
namespace App\Controller\View\Magazine;
use App\Application\Sonata\MediaBundle\Entity\GalleryHasMedia;
use App\Application\Sonata\MediaBundle\Entity\Media;
use App\Entity\Account\Subscription;
use App\Entity\Magazine\Magazine;
use App\Entity\Magazine\MagazinePage;
use App\Entity\Project\Publisher;
use App\Service\SerializePageService;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;
/**
* @Route("/view/magazine")
*/
class MagazineController extends AbstractController
{
/**
* @Route("/", methods={"GET"})
*/
public function all(Request $request)
{
$user = $this->getUser();
// if($request->query->has('exclude')) {
// $magazines = $this->getDoctrine()->getRepository(Magazine::class)->findAllExcludeMagazines($request->query->get('exclude'));
// } else {
// $magazines = $this->getDoctrine()->getRepository(Magazine::class)->findBy(['enabled' => true], ['sort' => 'ASC']);
// }
$magazines = $this->getDoctrine()->getRepository(Subscription::class)->findBy(['user' => $user->getId()]);
$encoder = new JsonEncoder();
$normalizer = new ObjectNormalizer();
$normalizer->setIgnoredAttributes([
'enabled', 'pages', 'toc', 'file', 'subscriptions', '__initializer__', '__cloner__', '__isInitialized__', 'updatedAt', 'user', 'createdAt',
'type', 'period', 'publisher', 'bookmarks', 'collection', 'products'
]);
$normalizer->setCallbacks([
'cover' => $this->callbackImage(),
'thumbnail' => $this->callbackImage(),
'publisher' => $this->callbackPublisher(),
'startDate' => $this->callbackDatetime(),
'download' => $this->callbackDownload(),
'startedAt' => $this->callbackDatetime(),
'endDate' => $this->callbackDatetime(),
]);
$normalizer->setCircularReferenceHandler(function ($object) {
return $object->getId();
});
$serializer = new Serializer(array($normalizer), array($encoder));
// if($request->query->has('page')) {
// $page = $this->getDoctrine()->getRepository(Page::class)->findOneBySlug($request->query->get('page'));
// if($page && $page->isEnabled()) {
// $page = $serializePageService->normalize($page);
// return $this->json(['page' => $page, 'magazines' => $serializer->normalize($magazines, 'json')]);
// }
// }
return $this->json($serializer->normalize($magazines, 'json'));
}
/**
* @Route("/{slug}", methods={"GET"})
*/
public function show(Request $request, $slug)
{
$magazine = $this->getDoctrine()->getRepository(Magazine::class)->findOneBy(['slug'=>$slug, 'paid'=>false]);
if(!$magazine) {
return $this->json(['message' => 'Magazine not found'], Response::HTTP_NOT_FOUND);
}
if($request->get('provider') != 'preprod' && $magazine->getPreprod() == true) {
return $this->json(['message' => 'Magazine not found'], Response::HTTP_NOT_FOUND);
}
if($request->get('detail') == 'true') {
$count = $this->getDoctrine()->getRepository(MagazinePage::class)->countPagesBy($slug);
$magazine->setTotalPages((int) $count);
}
if ($request->get('pagination') == 1)
$pages = $this->getDoctrine()->getRepository(MagazinePage::class)->findfirstPagedPagesBy($slug, $magazine->getPagination());
// elseif ($request->get('pagination') == 2)
// $pages = $this->getDoctrine()->getRepository(MagazinePage::class)->findsecondPagedPagesBy($id, $request->get('pagination'));
else
$pages = $this->getDoctrine()->getRepository(MagazinePage::class)->findPagedPagesBy($slug, $request->get('pagination'), $magazine->getPagination());
if(!$pages) {
return $this->json(['message' => 'pages not found'], Response::HTTP_NOT_FOUND);
}
//$magazineEditedCss = $magazine->getCss();
foreach ($pages as $page) {
$gallery = $page->getGallery();
// if($page->getStylesheet() !== null && $page->getStylesheet() !== "") {
// $magazineEditedCss .= "/***** page-". $page->getPosition() ."*****/";
// $magazineEditedCss = $magazineEditedCss . preg_replace('~[\r\n]+~', "", $page->getStylesheet());
// }
if ($gallery != null) {
$newGallery = ['image'=>[], 'file'=>[], 'audio'=>[], 'video'=>[], 'link'=>[]];
$hasMedia = $this->getDoctrine()->getRepository(GalleryHasMedia::class)->findby(['gallery' => $gallery->getId()]);
foreach ($hasMedia as $allmedia) {
if ($allmedia->getMedia() != null) {
$media = $this->getDoctrine()->getRepository(Media::class)->find($allmedia->getMedia()->getId());
// dump($media->getUrlMethod());
switch ($media->getProviderName()) {
case "sonata.media.provider.youtube":
$myVideo = new Video();
$myVideo->url = $media->getProviderReference();
$myVideo->title = $media->getName();
$myVideo->thumb = ($media->getThumbnail() !== null) ? $media->getThumbnail()->getUrlMethod() : $media->getProviderMetadata()['thumbnail_url'];
$myVideo->type = "youtube";
array_push($newGallery['video'], $myVideo);
break;
case "sonata.media.provider.vimeo":
$myVideo = new Video();
$myVideo->url = $media->getProviderReference();
$myVideo->title = $media->getName();
$myVideo->thumb = ($media->getThumbnail() !== null) ? $media->getThumbnail()->getUrlMethod() : $media->getProviderMetadata()['thumbnail_url'];
$myVideo->type = "vimeo";
array_push($newGallery['video'], $myVideo);
break;
case "sonata.media.provider.video":
$myVideo = new Video();
$myVideo->url = $media->getUrlMethod();
$myVideo->title = $media->getName();
$myVideo->thumb = ($media->getThumbnail() !== null) ? $media->getThumbnail()->getUrlMethod() : null;
$myVideo->type = "video";
/* if need file mimetype */
// $myVideo->type = $media->getContentType();
array_push($newGallery['video'], $myVideo);
break;
case "sonata.media.provider.audio":
$myAudio = new Audio();
$myAudio->url = $media->getUrlMethod();
$myAudio->title = $media->getName();
$myAudio->thumb = ($media->getThumbnail() !== null) ? $media->getThumbnail()->getUrlMethod() : null;
array_push($newGallery['audio'], $myAudio);
break;
case "sonata.media.provider.file":
$myDoc = new Document();
$myDoc->url = $media->getUrlMethod();
$myDoc->title = $media->getName();
$myDoc->thumb = ($media->getThumbnail() !== null) ? $media->getThumbnail()->getUrlMethod() : null;
$myDoc->type = $media->getContentType();
array_push($newGallery['file'], $myDoc);
break;
case "sonata.media.provider.image":
$myImage = new Image();
$myImage->url = $media->getUrlMethod();
$myImage->title = $media->getName();
array_push($newGallery['image'], $myImage);
break;
case "sonata.media.provider.link":
$myLink = new Link();
$myLink->url = $media->getProviderReference();
$myLink->title = $media->getName();
array_push($newGallery['link'], $myLink);
break;
}
// array_push($newGallery, $myImage);
}
}
//dump($newGallery);
$page->setGallery($newGallery);
// die('end');
}
}
//$magazine->setCss($magazineEditedCss);
$encoder = new JsonEncoder();
$normalizer = new ObjectNormalizer();
$normalizer2 = new ObjectNormalizer();
$normalizer->setIgnoredAttributes([
'enabled', 'file', 'subscriptions', 'users', 'root', 'parent', 'lft', 'lvl', 'rgt', 'toc', 'createdAt', 'updatedAt', 'startedAt', 'user', '__initializer__', '__cloner__', '__isInitialized__', 'pages',
'content', 'collection', 'products'
]);
$normalizer2->setIgnoredAttributes([
'enabled', 'file', 'subscriptions', 'users', 'root', 'parent', 'lft', 'lvl', 'rgt', 'toc', 'createdAt', 'updatedAt', 'startedAt', 'user', '__initializer__', '__cloner__', '__isInitialized__', 'pages',
'magazine', 'collection', 'products', 'imageList'
]);
$normalizer->setCallbacks([
'cover' => $this->callbackImage(),
'thumbnail' => $this->callbackImage(),
'download' => $this->callbackDownload(),
'publisher' => $this->callbackPublisher(),
'startDate' => $this->callbackDatetime(),
'endDate' => $this->callbackDatetime(),
'imageJson' => $this->callbackJson(),
'bgJson' => $this->callbackJson(),
'productJson' => $this->callbackJson(),
'themeBgColor' => $this->callbackColor(),
'themeTextColor' => $this->callbackColor(),
'contrastBgColor' => $this->callbackColor(),
'contrastTextColor' => $this->callbackColor(),
'opacityColor' => $this->callbackColor(),
'translations' => $this->callbackTranslation(),
]);
$normalizer2->setCallbacks([
'cover' => $this->callbackImage(),
'thumbnail' => $this->callbackImage(),
'download' => $this->callbackDownload(),
'publisher' => $this->callbackPublisher(),
'startDate' => $this->callbackDatetime(),
'endDate' => $this->callbackDatetime(),
]);
$normalizer->setCircularReferenceHandler(function ($object) {
return $object->getId();
});
$normalizer2->setCircularReferenceHandler(function ($object) {
return $object->getId();
});
$serializer2 = new Serializer(array($normalizer2), array($encoder));
$pagination = $serializer2->normalize($pages, 'json');
if($request->get('detail') == 'true') {
$serializer = new Serializer(array($normalizer), array($encoder));
$detail = $serializer->normalize($magazine, 'json');
return $this->json(['detail' => $detail, 'pages' => $pagination]);
}
return $this->json(['pages' => $pagination]);
}
private function callbackImage()
{
$callback = function ($image) {
return $image instanceof Media
? $image->getUrlMethod()
: null;
};
return $callback;
}
private function callbackDownload()
{
$callback = function ($pdf) {
return $pdf instanceof Media
? $pdf->getUrlMethod()
: null;
};
return $callback;
}
private function callbackTranslation()
{
$callback = function ($translations) {
if (!$translations->isEmpty()) {
foreach ($translations as $translation)
$arrtranslations = [
$translation->getLocale() => $translation->getSlug(),
];
return $arrtranslations;
}else
return null;
};
return $callback;
}
private function callbackJson()
{
$callback = function ($imageJson) {
if ($imageJson != null)
return json_decode($imageJson, true);
else
return null;
};
return $callback;
}
private function callbackDatetime()
{
$callback = function ($dateTime) {
return $dateTime instanceof \DateTime
? $dateTime->format(\DateTime::ISO8601)
: $dateTime;
};
return $callback;
}
private function callbackColor()
{
$callback = function ($color) {
if ($color != null) {
(strlen($color) === 4) ? list($r, $g, $b) = sscanf($color, "#%1x%1x%1x") : list($r, $g, $b) = sscanf($color, "#%2x%2x%2x");
$color = $r . "," . $g . "," . $b;
} else {
$color = null;
}
return $color;
};
return $callback;
}
private function callbackPublisher() {
$callback = function ($publisher) {
if ($publisher instanceof Publisher) {
$publisher = [
'name' => $publisher->getName(),
'logo' => ($publisher->getLogo()) ? $publisher->getLogo()->getUrlMethod() : null,
'website' => $publisher->getWebsite(),
'email' => $publisher->getEmail(),
'address' => $publisher->getAddress(),
'phone' => $publisher->getPhone(),
];
} else {
$publisher = null;
}
return $publisher;
};
return $callback;
}
}
class Image
{
public $url;
public $title;
}
class Audio
{
public $url;
public $title;
public $thumb;
public $type;
}
class Document
{
public $url;
public $title;
public $thumb;
public $type;
}
class Video
{
public $url;
public $title;
public $thumb;
public $type;
}
class Link
{
public $url;
public $title;
}