src/Controller/View/Magazine/MagazineController.php line 254

Open in your IDE?
  1. <?php
  2. namespace App\Controller\View\Magazine;
  3. use App\Application\Sonata\MediaBundle\Entity\GalleryHasMedia;
  4. use App\Application\Sonata\MediaBundle\Entity\Media;
  5. use App\Entity\Account\Subscription;
  6. use App\Entity\Magazine\Magazine;
  7. use App\Entity\Magazine\MagazinePage;
  8. use App\Entity\Project\Publisher;
  9. use App\Service\SerializePageService;
  10. use Symfony\Component\HttpFoundation\File\File;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  15. use Symfony\Component\Serializer\Encoder\JsonEncoder;
  16. use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
  17. use Symfony\Component\Serializer\Serializer;
  18. /**
  19.  * @Route("/view/magazine")
  20.  */
  21. class MagazineController extends AbstractController
  22. {
  23.     /**
  24.      * @Route("/", methods={"GET"})
  25.      */
  26.     public function all(Request $request)
  27.     {
  28.         $user $this->getUser();
  29. //        if($request->query->has('exclude')) {
  30. //            $magazines = $this->getDoctrine()->getRepository(Magazine::class)->findAllExcludeMagazines($request->query->get('exclude'));
  31. //        } else {
  32. //            $magazines = $this->getDoctrine()->getRepository(Magazine::class)->findBy(['enabled' => true], ['sort' => 'ASC']);
  33. //        }
  34.         $magazines $this->getDoctrine()->getRepository(Subscription::class)->findBy(['user' => $user->getId()]);
  35.         $encoder = new JsonEncoder();
  36.         $normalizer = new ObjectNormalizer();
  37.         $normalizer->setIgnoredAttributes([
  38.             'enabled''pages''toc''file''subscriptions''__initializer__''__cloner__''__isInitialized__''updatedAt''user''createdAt',
  39.             'type''period''publisher''bookmarks''collection''products'
  40.         ]);
  41.         $normalizer->setCallbacks([
  42.             'cover' => $this->callbackImage(),
  43.             'thumbnail' => $this->callbackImage(),
  44.             'publisher' => $this->callbackPublisher(),
  45.             'startDate' => $this->callbackDatetime(),
  46.             'download' => $this->callbackDownload(),
  47.             'startedAt' => $this->callbackDatetime(),
  48.             'endDate' => $this->callbackDatetime(),
  49.         ]);
  50.         $normalizer->setCircularReferenceHandler(function ($object) {
  51.             return $object->getId();
  52.         });
  53.         $serializer = new Serializer(array($normalizer), array($encoder));
  54. //        if($request->query->has('page')) {
  55. //            $page = $this->getDoctrine()->getRepository(Page::class)->findOneBySlug($request->query->get('page'));
  56. //            if($page && $page->isEnabled()) {
  57. //                $page = $serializePageService->normalize($page);
  58. //                return $this->json(['page' => $page, 'magazines' => $serializer->normalize($magazines, 'json')]);
  59. //            }
  60. //        }
  61.         return $this->json($serializer->normalize($magazines'json'));
  62.     }
  63.     /**
  64.      * @Route("/{slug}", methods={"GET"})
  65.      */
  66.     public function show(Request $request$slug)
  67.     {
  68.         $magazine $this->getDoctrine()->getRepository(Magazine::class)->findOneBy(['slug'=>$slug'paid'=>false]);
  69.         if(!$magazine) {
  70.             return $this->json(['message' => 'Magazine not found'], Response::HTTP_NOT_FOUND);
  71.         }
  72.         if($request->get('provider') != 'preprod' && $magazine->getPreprod() == true) {
  73.             return $this->json(['message' => 'Magazine not found'], Response::HTTP_NOT_FOUND);
  74.         }
  75.         if($request->get('detail') == 'true') {
  76.             $count $this->getDoctrine()->getRepository(MagazinePage::class)->countPagesBy($slug);
  77.             $magazine->setTotalPages((int) $count);
  78.         }
  79.         if ($request->get('pagination') == 1)
  80.             $pages $this->getDoctrine()->getRepository(MagazinePage::class)->findfirstPagedPagesBy($slug$magazine->getPagination());
  81. //        elseif ($request->get('pagination') == 2)
  82. //            $pages = $this->getDoctrine()->getRepository(MagazinePage::class)->findsecondPagedPagesBy($id, $request->get('pagination'));
  83.         else
  84.             $pages $this->getDoctrine()->getRepository(MagazinePage::class)->findPagedPagesBy($slug$request->get('pagination'), $magazine->getPagination());
  85.         if(!$pages) {
  86.             return $this->json(['message' => 'pages not found'], Response::HTTP_NOT_FOUND);
  87.         }
  88.         //$magazineEditedCss = $magazine->getCss();
  89.         foreach ($pages as $page) {
  90.             $gallery $page->getGallery();
  91. //            if($page->getStylesheet() !== null && $page->getStylesheet() !== "") {
  92. //                $magazineEditedCss .= "/***** page-". $page->getPosition() ."*****/";
  93. //                $magazineEditedCss = $magazineEditedCss . preg_replace('~[\r\n]+~', "", $page->getStylesheet());
  94. //            }
  95.             if ($gallery != null) {
  96.                 $newGallery = ['image'=>[], 'file'=>[], 'audio'=>[], 'video'=>[], 'link'=>[]];
  97.                 $hasMedia $this->getDoctrine()->getRepository(GalleryHasMedia::class)->findby(['gallery' => $gallery->getId()]);
  98.                 foreach ($hasMedia as $allmedia) {
  99.                     if ($allmedia->getMedia() != null) {
  100.                         $media $this->getDoctrine()->getRepository(Media::class)->find($allmedia->getMedia()->getId());
  101.                         // dump($media->getUrlMethod());
  102.                         switch ($media->getProviderName()) {
  103.                             case "sonata.media.provider.youtube":
  104.                                 $myVideo = new Video();
  105.                                 $myVideo->url $media->getProviderReference();
  106.                                 $myVideo->title $media->getName();
  107.                                 $myVideo->thumb = ($media->getThumbnail() !== null) ? $media->getThumbnail()->getUrlMethod() : $media->getProviderMetadata()['thumbnail_url'];
  108.                                 $myVideo->type "youtube";
  109.                                 array_push($newGallery['video'], $myVideo);
  110.                                 break;
  111.                             case "sonata.media.provider.vimeo":
  112.                                 $myVideo = new Video();
  113.                                 $myVideo->url $media->getProviderReference();
  114.                                 $myVideo->title $media->getName();
  115.                                 $myVideo->thumb = ($media->getThumbnail() !== null) ? $media->getThumbnail()->getUrlMethod() : $media->getProviderMetadata()['thumbnail_url'];
  116.                                 $myVideo->type "vimeo";
  117.                                 array_push($newGallery['video'], $myVideo);
  118.                                 break;
  119.                             case "sonata.media.provider.video":
  120.                                 $myVideo = new Video();
  121.                                 $myVideo->url $media->getUrlMethod();
  122.                                 $myVideo->title $media->getName();
  123.                                 $myVideo->thumb = ($media->getThumbnail() !== null) ? $media->getThumbnail()->getUrlMethod() : null;
  124.                                 $myVideo->type "video";
  125.                                 /* if need file mimetype */
  126.                                 // $myVideo->type = $media->getContentType();
  127.                                 array_push($newGallery['video'], $myVideo);
  128.                                 break;
  129.                             case "sonata.media.provider.audio":
  130.                                 $myAudio = new Audio();
  131.                                 $myAudio->url $media->getUrlMethod();
  132.                                 $myAudio->title $media->getName();
  133.                                 $myAudio->thumb = ($media->getThumbnail() !== null) ? $media->getThumbnail()->getUrlMethod() : null;
  134.                                 array_push($newGallery['audio'], $myAudio);
  135.                                 break;
  136.                             case "sonata.media.provider.file":
  137.                                 $myDoc = new Document();
  138.                                 $myDoc->url $media->getUrlMethod();
  139.                                 $myDoc->title $media->getName();
  140.                                 $myDoc->thumb = ($media->getThumbnail() !== null) ? $media->getThumbnail()->getUrlMethod() : null;
  141.                                 $myDoc->type $media->getContentType();
  142.                                 array_push($newGallery['file'], $myDoc);
  143.                                 break;
  144.                             case "sonata.media.provider.image":
  145.                                 $myImage = new Image();
  146.                                 $myImage->url $media->getUrlMethod();
  147.                                 $myImage->title $media->getName();
  148.                                 array_push($newGallery['image'], $myImage);
  149.                                 break;
  150.                             case "sonata.media.provider.link":
  151.                                 $myLink = new Link();
  152.                                 $myLink->url $media->getProviderReference();
  153.                                 $myLink->title $media->getName();
  154.                                 array_push($newGallery['link'], $myLink);
  155.                                 break;
  156.                         }
  157.                         // array_push($newGallery, $myImage);
  158.                     }
  159.                 }
  160.                 //dump($newGallery);
  161.                 $page->setGallery($newGallery);
  162.                 // die('end');
  163.             }
  164.         }
  165.         //$magazine->setCss($magazineEditedCss);
  166.         $encoder = new JsonEncoder();
  167.         $normalizer = new ObjectNormalizer();
  168.         $normalizer2 = new ObjectNormalizer();
  169.         $normalizer->setIgnoredAttributes([
  170.             'enabled''file''subscriptions''users''root''parent''lft''lvl''rgt''toc''createdAt''updatedAt''startedAt''user''__initializer__''__cloner__''__isInitialized__''pages',
  171.             'content''collection''products'
  172.         ]);
  173.         $normalizer2->setIgnoredAttributes([
  174.             'enabled''file''subscriptions''users''root''parent''lft''lvl''rgt''toc''createdAt''updatedAt''startedAt''user''__initializer__''__cloner__''__isInitialized__''pages',
  175.             'magazine''collection''products''imageList'
  176.         ]);
  177.         $normalizer->setCallbacks([
  178.             'cover' => $this->callbackImage(),
  179.             'thumbnail' => $this->callbackImage(),
  180.             'download' => $this->callbackDownload(),
  181.             'publisher' => $this->callbackPublisher(),
  182.             'startDate' => $this->callbackDatetime(),
  183.             'endDate' => $this->callbackDatetime(),
  184.             'imageJson' => $this->callbackJson(),
  185.             'bgJson' => $this->callbackJson(),
  186.             'productJson' => $this->callbackJson(),
  187.             'themeBgColor' => $this->callbackColor(),
  188.             'themeTextColor' => $this->callbackColor(),
  189.             'contrastBgColor' => $this->callbackColor(),
  190.             'contrastTextColor' => $this->callbackColor(),
  191.             'opacityColor' => $this->callbackColor(),
  192.             'translations' => $this->callbackTranslation(),
  193.         ]);
  194.         $normalizer2->setCallbacks([
  195.             'cover' => $this->callbackImage(),
  196.             'thumbnail' => $this->callbackImage(),
  197.             'download' => $this->callbackDownload(),
  198.             'publisher' => $this->callbackPublisher(),
  199.             'startDate' => $this->callbackDatetime(),
  200.             'endDate' => $this->callbackDatetime(),
  201.         ]);
  202.         $normalizer->setCircularReferenceHandler(function ($object) {
  203.             return $object->getId();
  204.         });
  205.         $normalizer2->setCircularReferenceHandler(function ($object) {
  206.             return $object->getId();
  207.         });
  208.         $serializer2 = new Serializer(array($normalizer2), array($encoder));
  209.         $pagination $serializer2->normalize($pages'json');
  210.         if($request->get('detail') == 'true') {
  211.             $serializer = new Serializer(array($normalizer), array($encoder));
  212.             $detail $serializer->normalize($magazine'json');
  213.             return $this->json(['detail' => $detail'pages' => $pagination]);
  214.         }
  215.         return $this->json(['pages' => $pagination]);
  216.     }
  217.     private function callbackImage()
  218.     {
  219.         $callback = function ($image) {
  220.             return $image instanceof Media
  221.                 $image->getUrlMethod()
  222.                 : null;
  223.         };
  224.         return $callback;
  225.     }
  226.     private function callbackDownload()
  227.     {
  228.         $callback = function ($pdf) {
  229.             return $pdf instanceof Media
  230.                 $pdf->getUrlMethod()
  231.                 : null;
  232.         };
  233.         return $callback;
  234.     }
  235.     private function callbackTranslation()
  236.     {
  237.         $callback = function ($translations) {
  238.             if (!$translations->isEmpty()) {
  239.                 foreach ($translations as $translation)
  240.                     $arrtranslations = [
  241.                         $translation->getLocale() => $translation->getSlug(),
  242.                     ];
  243.                 return $arrtranslations;
  244.             }else
  245.                 return null;
  246.         };
  247.         return $callback;
  248.     }
  249.     private function callbackJson()
  250.     {
  251.         $callback = function ($imageJson) {
  252.             if ($imageJson != null)
  253.                 return json_decode($imageJsontrue);
  254.             else
  255.                 return null;
  256.         };
  257.         return $callback;
  258.     }
  259.     private function callbackDatetime()
  260.     {
  261.         $callback = function ($dateTime) {
  262.             return $dateTime instanceof \DateTime
  263.                 $dateTime->format(\DateTime::ISO8601)
  264.                 : $dateTime;
  265.         };
  266.         return $callback;
  267.     }
  268.     private function callbackColor()
  269.     {
  270.         $callback = function ($color) {
  271.             if ($color != null) {
  272.                 (strlen($color) === 4) ? list($r$g$b) = sscanf($color"#%1x%1x%1x") : list($r$g$b) = sscanf($color"#%2x%2x%2x");
  273.                 $color $r "," $g "," $b;
  274.             } else {
  275.                 $color null;
  276.             }
  277.             return $color;
  278.         };
  279.         return $callback;
  280.     }
  281.     private function callbackPublisher() {
  282.         $callback = function ($publisher) {
  283.             if ($publisher instanceof Publisher) {
  284.                 $publisher = [
  285.                     'name' => $publisher->getName(),
  286.                     'logo' => ($publisher->getLogo()) ? $publisher->getLogo()->getUrlMethod() : null,
  287.                     'website' => $publisher->getWebsite(),
  288.                     'email' => $publisher->getEmail(),
  289.                     'address' => $publisher->getAddress(),
  290.                     'phone' => $publisher->getPhone(),
  291.                 ];
  292.             } else {
  293.                 $publisher null;
  294.             }
  295.             return $publisher;
  296.         };
  297.         return $callback;
  298.     }
  299. }
  300. class Image
  301. {
  302.     public $url;
  303.     public $title;
  304. }
  305. class Audio
  306. {
  307.     public $url;
  308.     public $title;
  309.     public $thumb;
  310.     public $type;
  311. }
  312. class Document
  313. {
  314.     public $url;
  315.     public $title;
  316.     public $thumb;
  317.     public $type;
  318. }
  319. class Video
  320. {
  321.     public $url;
  322.     public $title;
  323.     public $thumb;
  324.     public $type;
  325. }
  326. class Link
  327. {
  328.     public $url;
  329.     public $title;
  330. }