From 5af564e137a55b71b8a1171cbed259ecf4ce8582 Mon Sep 17 00:00:00 2001 From: Rob Powell Date: Thu, 27 Dec 2018 14:22:34 -0500 Subject: [PATCH 1/3] Add videos to smart playlist --- lib/Brightcove/API/CMS.php | 45 +++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/lib/Brightcove/API/CMS.php b/lib/Brightcove/API/CMS.php index 068a6a9..edc7bdb 100644 --- a/lib/Brightcove/API/CMS.php +++ b/lib/Brightcove/API/CMS.php @@ -144,7 +144,50 @@ public function listPlaylists($sort = NULL, $limit = NULL, $offset = NULL) { if (strlen($query) > 0) { $query = '?' . substr($query, 1); } - return $this->cmsRequest('GET', "/playlists{$query}", Playlist::class, TRUE); + + $playlists = $this->cmsRequest('GET', "/playlists{$query}", Playlist::class, TRUE); + + // Check if there are smart playlists as we need to build their videos + // separately. + $smart_playlists = []; + foreach ($playlists as $key => $playlist) { + /* @var \Drupal\brightcove\Playlist $playlist */ + if ($playlist->getType() !== 'EXPLICIT') { + $smart_playlists[$key] = $playlist->getId(); + } + } + + if (!empty($smart_playlists)) { + foreach ($smart_playlists as $playlist_key => $smart_playlist_id) { + $video_ids = getSmartPlaylistVideos("/playlists/{$smart_playlist_id}/videos"); + + if (!empty($video_ids)) { + /* @var \Drupal\brightcove\Playlist $playlist */ + $playlist = $playlists[$playlist_key]; + $playlist->setVideoIds($video_ids); + } + + } + } + return $playlists; + } + + /** + * Get smart playlist videos to add to the Playlist objects. + * + * @param string $endpoint + * Endpoint to get smart playlist videos. + * + * @return array + * Video ids. + */ + protected function getSmartPlaylistVideos($endpoint) { + $video_results = $this->cmsRequest('GET', $endpoint, NULL); + $video_ids = []; + foreach ($video_results as $video) { + $video_ids[] = $video['id']; + } + return $video_ids; } /** From f71d6d7c205c8a4085e5d5204fbe514fc59e23e4 Mon Sep 17 00:00:00 2001 From: Rob Powell Date: Thu, 27 Dec 2018 14:27:00 -0500 Subject: [PATCH 2/3] Fix namespace for '@var' --- lib/Brightcove/API/CMS.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/Brightcove/API/CMS.php b/lib/Brightcove/API/CMS.php index edc7bdb..14b259a 100644 --- a/lib/Brightcove/API/CMS.php +++ b/lib/Brightcove/API/CMS.php @@ -151,7 +151,7 @@ public function listPlaylists($sort = NULL, $limit = NULL, $offset = NULL) { // separately. $smart_playlists = []; foreach ($playlists as $key => $playlist) { - /* @var \Drupal\brightcove\Playlist $playlist */ + /* @var \Brightcove\Object\Playlist $playlist */ if ($playlist->getType() !== 'EXPLICIT') { $smart_playlists[$key] = $playlist->getId(); } @@ -162,11 +162,10 @@ public function listPlaylists($sort = NULL, $limit = NULL, $offset = NULL) { $video_ids = getSmartPlaylistVideos("/playlists/{$smart_playlist_id}/videos"); if (!empty($video_ids)) { - /* @var \Drupal\brightcove\Playlist $playlist */ + /* @var \Brightcove\Object\Playlist $playlist */ $playlist = $playlists[$playlist_key]; $playlist->setVideoIds($video_ids); } - } } return $playlists; From fa1646b608af54853b0a9276cb2ddd46d1cb0870 Mon Sep 17 00:00:00 2001 From: Rob Powell Date: Fri, 28 Dec 2018 08:52:27 -0500 Subject: [PATCH 3/3] Update to call method getSmartPlaylistVideos --- lib/Brightcove/API/CMS.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Brightcove/API/CMS.php b/lib/Brightcove/API/CMS.php index 14b259a..6d8fb24 100644 --- a/lib/Brightcove/API/CMS.php +++ b/lib/Brightcove/API/CMS.php @@ -159,7 +159,7 @@ public function listPlaylists($sort = NULL, $limit = NULL, $offset = NULL) { if (!empty($smart_playlists)) { foreach ($smart_playlists as $playlist_key => $smart_playlist_id) { - $video_ids = getSmartPlaylistVideos("/playlists/{$smart_playlist_id}/videos"); + $video_ids = $this->getSmartPlaylistVideos("/playlists/{$smart_playlist_id}/videos"); if (!empty($video_ids)) { /* @var \Brightcove\Object\Playlist $playlist */