Ejemplo n.º 1
0
 public function getIndex()
 {
     if (Config::get("degradedService.enabled")) {
         $view = View::make("home.degradedIndex");
         $view->contactEmail = Config::get("contactEmails.development");
         $this->setContent($view, "home", "home-degraded", array(), null, 200, array());
         return;
     }
     $promoMediaItem = MediaItem::with("liveStreamItem", "liveStreamItem.liveStream", "videoItem")->accessible()->whereNotNull("time_promoted")->orderBy("time_promoted", "desc")->first();
     if (!is_null($promoMediaItem)) {
         $liveStreamItem = $promoMediaItem->liveStreamItem;
         if (!is_null($liveStreamItem) && !$liveStreamItem->getIsAccessible()) {
             $liveStreamItem = null;
         }
         $videoItem = $promoMediaItem->videoItem;
         if (!is_null($videoItem) && !$videoItem->getIsAccessible()) {
             $videoItem = null;
         }
         $shouldShowItem = false;
         // if there is a live stream which is in the "not live" state the player won't display the vod
         // even if there is one. It will show the countdown to the start of the live stream.
         if (is_null($liveStreamItem) || !$liveStreamItem->isNotLive()) {
             if (!is_null($videoItem) && $videoItem->getIsLive()) {
                 $shouldShowItem = true;
             } else {
                 if (!is_null($liveStreamItem) && $liveStreamItem->hasWatchableContent()) {
                     $shouldShowItem = true;
                 }
             }
         }
         if (!$shouldShowItem) {
             $promoMediaItem = null;
         }
     }
     $promoPlaylist = null;
     if (!is_null($promoMediaItem)) {
         $promoPlaylist = $promoMediaItem->getDefaultPlaylist();
     }
     $promotedItems = MediaItem::getCachedPromotedItems();
     $promotedItemsData = array();
     // if there is an item to promote insert it at the start of the carousel
     if (!is_null($promoMediaItem)) {
         $coverArtResolutions = Config::get("imageResolutions.coverArt");
         $isLiveShow = !is_null($promoMediaItem->liveStreamItem) && !$promoMediaItem->liveStreamItem->isOver();
         $liveNow = $isLiveShow && $promoMediaItem->liveStreamItem->isLive();
         $promotedItemsData[] = array("coverArtUri" => $promoPlaylist->getMediaItemCoverArtUri($promoMediaItem, $coverArtResolutions['full']['w'], $coverArtResolutions['full']['h']), "name" => $promoMediaItem->name, "seriesName" => !is_null($promoPlaylist->show) ? $promoPlaylist->generateName() : null, "availableMsg" => $liveNow ? "Live Now!" : $this->buildTimeStr($isLiveShow, $promoMediaItem->scheduled_publish_time), "uri" => $promoPlaylist->getMediaItemUri($promoMediaItem));
     }
     foreach ($promotedItems as $a) {
         $mediaItem = $a['mediaItem'];
         if (!is_null($promoMediaItem) && intval($mediaItem->id) === intval($promoMediaItem->id)) {
             // prevent duplicate
             continue;
         }
         $isLiveShow = !is_null($mediaItem->liveStreamItem) && !$mediaItem->liveStreamItem->isOver();
         $liveNow = $isLiveShow && $mediaItem->liveStreamItem->isLive();
         $promotedItemsData[] = array("coverArtUri" => $a['coverArtUri'], "name" => $mediaItem->name, "seriesName" => $a['seriesName'], "availableMsg" => $liveNow ? "Live Now!" : $this->buildTimeStr($isLiveShow, $mediaItem->scheduled_publish_time), "uri" => $a['uri']);
     }
     $coverArtResolutions = Config::get("imageResolutions.coverArt");
     $recentlyAddedItems = MediaItem::getCachedRecentItems();
     $recentlyAddedTableData = array();
     foreach ($recentlyAddedItems as $i => $a) {
         $mediaItem = $a['mediaItem'];
         $recentlyAddedTableData[] = array("uri" => $a['uri'], "active" => false, "title" => $mediaItem->name, "escapedDescription" => null, "playlistName" => $a['playlistName'], "episodeNo" => $i + 1, "thumbnailUri" => $a['coverArtUri'], "thumbnailFooter" => null, "duration" => $a['duration']);
     }
     $mostPopularItems = MediaItem::getCachedMostPopularItems();
     $mostPopularTableData = array();
     foreach ($mostPopularItems as $i => $a) {
         $mediaItem = $a['mediaItem'];
         $mostPopularTableData[] = array("uri" => $a['uri'], "active" => false, "title" => $mediaItem->name, "escapedDescription" => null, "playlistName" => $a['playlistName'], "episodeNo" => $i + 1, "thumbnailUri" => $a['coverArtUri'], "thumbnailFooter" => null, "duration" => $a['duration']);
     }
     $view = View::make("home.index");
     $view->promotedItemsData = $promotedItemsData;
     $view->recentlyAddedPlaylistFragment = count($recentlyAddedTableData) > 0 ? View::make("fragments.home.playlist", array("stripedTable" => true, "headerRowData" => null, "tableData" => $recentlyAddedTableData)) : null;
     $view->mostPopularPlaylistFragment = count($mostPopularTableData) > 0 ? View::make("fragments.home.playlist", array("stripedTable" => true, "headerRowData" => null, "tableData" => $mostPopularTableData)) : null;
     $view->twitterWidgetId = Config::get("twitter.timeline_widget_id");
     $hasPromoItem = !is_null($promoMediaItem);
     $showPromoItem = $hasPromoItem;
     if ($hasPromoItem) {
         // determine if the user has already seen the promo
         $cookieVal = Cookie::get('seenPromo-' . $promoMediaItem->id);
         if (!is_null($cookieVal) && $cookieVal === $promoMediaItem->time_promoted->timestamp) {
             // user already seen promo
             $showPromoItem = false;
         }
         // put a cookie in the users browser to inform us in the future that the user has seen this promo video
         // store the time so that if the item is repromoted in the future, it will be shown again.
         Cookie::queue('seenPromo-' . $promoMediaItem->id, $promoMediaItem->time_promoted->timestamp, 40320);
         // store for 4 weeks
     }
     $view->showPromoItem = $showPromoItem;
     if ($showPromoItem) {
         $userHasMediaItemsPermission = false;
         if (Auth::isLoggedIn()) {
             $userHasMediaItemsPermission = Auth::getUser()->hasPermission(Config::get("permissions.mediaItems"), 0);
         }
         $view->promoPlayerInfoUri = PlayerHelpers::getInfoUri($promoPlaylist->id, $promoMediaItem->id);
         $view->promoRegisterWatchingUri = PlayerHelpers::getRegisterWatchingUri($promoPlaylist->id, $promoMediaItem->id);
         $view->promoRegisterLikeUri = PlayerHelpers::getRegisterLikeUri($promoPlaylist->id, $promoMediaItem->id);
         $view->promoAdminOverrideEnabled = $userHasMediaItemsPermission;
         $view->promoLoginRequiredMsg = "Please log in to use this feature.";
     }
     $this->setContent($view, "home", "home", array(), null, 200, array());
 }
 public function generateMediaItemsResponseData($limit, $sortMode, $sortDirection, $vodIncludeSetting, $streamIncludeSetting, $showStreamUris, $showVodUris)
 {
     $maxLimit = Config::get("api.mediaItemsMaxRetrieveLimit");
     if ($limit > $maxLimit) {
         $limit = $maxLimit;
     }
     $mediaItems = null;
     if ($sortMode === "POPULARITY") {
         if ($sortDirection === "ASC") {
             throw new Exception("ASC sort direction not supported for POPULARITY sort mode.");
         } else {
             if ($sortDirection === "DESC") {
                 // intentional
             } else {
                 throw new Exception("Invalid sort direction.");
             }
         }
         $items = MediaItem::getCachedMostPopularItems();
         $allMediaItems = array_column($items, 'mediaItem');
         $mediaItems = [];
         foreach ($allMediaItems as $a) {
             $includeVod = null;
             if ($vodIncludeSetting === "VOD_OPTIONAL") {
                 // intentional
             } else {
                 if ($vodIncludeSetting === "HAS_VOD") {
                     $includeVod = !is_null($a->videoItem) && $a->videoItem->getIsAccessible();
                 } else {
                     if ($vodIncludeSetting === "HAS_AVAILABLE_VOD") {
                         $includeVod = !is_null($a->videoItem) && $a->getIsAccessible() && $a->videoItem->getIsLive();
                     } else {
                         throw new Exception("Invalid vod include setting.");
                     }
                 }
             }
             $includeStream = null;
             if ($streamIncludeSetting === "STREAM_OPTIONAL") {
                 // intentional
             } else {
                 if ($streamIncludeSetting === "HAS_STREAM") {
                     $includeStream = !is_null($a->liveStreamItem) && $a->liveStreamItem->getIsAccessible();
                 } else {
                     if ($streamIncludeSetting === "HAS_LIVE_STREAM") {
                         $includeStream = !is_null($a->liveStreamItem) && $a->liveStreamItem->getIsAccessible() && $a->liveStreamItem->isLive();
                     } else {
                         throw new Exception("Invalid stream include setting.");
                     }
                 }
             }
             if (is_null($includeStream) && is_null($includeVod) || $includeStream || $includeVod) {
                 $mediaItems[] = $a;
             }
         }
     } else {
         if ($sortMode === "SCHEDULED_PUBLISH_TIME") {
             $mediaItems = MediaItem::with("liveStreamItem", "liveStreamItem.stateDefinition", "liveStreamItem.liveStream", "videoItem", "videoItem.sourceFile.vodData")->accessible();
             $mediaItems = $mediaItems->where(function ($q) use(&$vodIncludeSetting, &$streamIncludeSetting) {
                 if ($vodIncludeSetting === "VOD_OPTIONAL") {
                     // intentional
                 } else {
                     if ($vodIncludeSetting === "HAS_VOD") {
                         $q->whereHas("videoItem", function ($q2) {
                             $q2->accessible();
                         });
                     } else {
                         if ($vodIncludeSetting === "HAS_AVAILABLE_VOD") {
                             $q->whereHas("videoItem", function ($q2) {
                                 $q2->live();
                             });
                         } else {
                             throw new Exception("Invalid vod include setting.");
                         }
                     }
                 }
                 if ($streamIncludeSetting === "STREAM_OPTIONAL") {
                     // intentional
                 } else {
                     if ($streamIncludeSetting === "HAS_STREAM") {
                         $q->orWhereHas("liveStreamItem", function ($q2) {
                             $q2->accessible();
                         });
                     } else {
                         if ($streamIncludeSetting === "HAS_LIVE_STREAM") {
                             $q->orWhereHas("liveStreamItem", function ($q2) {
                                 $q2->live();
                             });
                         } else {
                             throw new Exception("Invalid stream include setting.");
                         }
                     }
                 }
             });
             $sortAsc = null;
             if ($sortDirection === "ASC") {
                 $sortAsc = true;
             } else {
                 if ($sortDirection === "DESC") {
                     $sortAsc = false;
                 } else {
                     throw new Exception("Invalid sort direction.");
                 }
             }
             $mediaItems = $mediaItems->orderBy("media_items.scheduled_publish_time", $sortAsc ? "asc" : "desc")->orderBy("id", "asc")->take($limit)->get()->all();
         } else {
             throw new Exception("Invalid sort mode.");
         }
     }
     $mediaItemsAndPlaylists = [];
     foreach ($mediaItems as $a) {
         $mediaItemsAndPlaylists[] = [null, $a];
     }
     $data = ["mediaItems" => $this->mediaItemTransformer->transformCollection($mediaItemsAndPlaylists, $this->getMediaItemTransformerOptions($showStreamUris, $showVodUris))];
     return new ApiResponseData($data);
 }