SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Widgets/VideoPlaylistWidget.php

  1. <?php
  2. /*
  3.  * Copyright (C) 2017 Karmabunny Pty Ltd.
  4.  *
  5.  * This file is a part of SproutCMS.
  6.  *
  7.  * SproutCMS is free software: you can redistribute it and/or modify it under the terms
  8.  * of the GNU General Public License as published by the Free Software Foundation, either
  9.  * version 2 of the License, or (at your option) any later version.
  10.  *
  11.  * For more information, visit <http://getsproutcms.com>.
  12.  */
  13.  
  14. namespace Sprout\Widgets;
  15.  
  16. use Kohana;
  17.  
  18. use Sprout\Exceptions\ValidationException;
  19. use Sprout\Helpers\Enc;
  20. use Sprout\Helpers\Form;
  21. use Sprout\Helpers\HttpReq;
  22. use Sprout\Helpers\Notification;
  23. use Sprout\Helpers\View;
  24.  
  25.  
  26. /**
  27.  * Renders a gallery of videos
  28.  */
  29. class VideoPlaylistWidget extends Widget
  30. {
  31. protected $friendly_name = "Video gallery";
  32. protected $friendly_desc = 'YouTube play-list gallery';
  33. public $classname = "VideoPlaylist";
  34.  
  35.  
  36. /**
  37.   * Renders the front-end view of this widget
  38.   *
  39.   * @param int $orientation The orientation of the widget.
  40.   * @return string HTML
  41.   */
  42. public function render($orientation)
  43. {
  44. if (empty($this->settings['playlist_id'])) return;
  45. $this->settings['captions'] = (int) @$this->settings['captions'];
  46. $this->settings['thumb_rows'] = (int) @$this->settings['thumb_rows'];
  47. $videos = $this->requestYoutubePlaylist($this->settings['playlist_id']);
  48.  
  49. if ($this->settings['thumb_rows'] <= 0) {
  50. $this->settings['thumb_rows'] = 4;
  51. }
  52.  
  53. $view = new View('sprout/video_playlist');
  54. $view->videos = $videos;
  55. $view->captions = $this->settings['captions'];
  56. $view->thumb_rows = $this->settings['thumb_rows'];
  57.  
  58. return $view->render();
  59. }
  60.  
  61.  
  62. /**
  63.   * Renders the admin settings form for this widget
  64.   *
  65.   * @return string HTML
  66.   */
  67. public function getSettingsForm()
  68. {
  69. $this->settings['captions'] = (int) @$this->settings['captions'];
  70. $this->settings['thumb_rows'] = (int) @$this->settings['thumb_rows'];
  71.  
  72. if ($this->settings['thumb_rows'] <= 0) {
  73. $this->settings['thumb_rows'] = 4;
  74. }
  75.  
  76. $view = new View('sprout/video_playlist_settings');
  77. $view->settings = $this->settings;
  78. $view->thumbs = [1=>1,2=>2,3=>3,4=>4];
  79.  
  80. return $view->render();
  81. }
  82.  
  83.  
  84. /**
  85.   * Request YouTube playlist data for given URL
  86.   *
  87.   * @param string $video_url Playlist URL
  88.   * @return array List of key-value pairs of [id, thumb_url, description, title]
  89.   * @return bool False on error
  90.   */
  91. private static function requestYoutubePlaylist($video_url)
  92. {
  93. $url_query = parse_url($video_url, PHP_URL_QUERY);
  94. $url_params = [];
  95. parse_str($url_query, $url_params);
  96.  
  97. // Validate given URL
  98. if (empty($url_params['list'])) {
  99. Notification::error('Unable to determine YouTube playlist from given URL');
  100. return false;
  101. }
  102.  
  103. // Validate API key
  104. if (empty(Kohana::config('sprout.google_youtube_api')) or Kohana::config('sprout.google_youtube_api') == 'please_generate_me') {
  105. Notification::error('Please configure your Google API key for YouTube videos');
  106. return false;
  107. }
  108.  
  109. // Build the request URL
  110. $params = [];
  111. $params['part'] = 'snippet';
  112. $params['maxResults'] = 50;
  113. $params['playlistId'] = $url_params['list'];
  114. $params['key'] = Kohana::config('sprout.google_youtube_api');
  115.  
  116. $url = 'https://www.googleapis.com/youtube/v3/playlistItems?' . http_build_query($params);
  117.  
  118. // Request options
  119. $opts = [];
  120. $opts['method'] = 'GET';
  121. $opts['headers'] = ['Referer' => $_SERVER['SERVER_NAME']];
  122.  
  123. // Do the request
  124. $playlist = HttpReq::req($url, $opts);
  125. if (!$playlist) {
  126. Notification::error('Unable to connect to YouTube API');
  127. return false;
  128. }
  129.  
  130. // Decode results
  131. $playlist = @json_decode($playlist);
  132. if (!$playlist) {
  133. Notification::error('Unable to decode data from YouTube API');
  134. return false;
  135. }
  136.  
  137. // Validate API request
  138. if (isset($playlist->error)) {
  139. Notification::error('YouTube API error: ' . $playlist->error->code . ' ' . $playlist->error->message);
  140. return false;
  141. }
  142.  
  143. // Build results as array
  144. $videos = [];
  145. foreach ($playlist->items as $video) {
  146. $snippet = $video->snippet;
  147.  
  148. if (isset($snippet->thumbnails->standard)) {
  149. $thumb = $snippet->thumbnails->standard;
  150. } else if (isset($snippet->thumbnails->high)) {
  151. $thumb = $snippet->thumbnails->high;
  152. } else if (isset($snippet->thumbnails->medium)) {
  153. $thumb = $snippet->thumbnails->medium;
  154. } else {
  155. continue;
  156. }
  157.  
  158. $video = [];
  159. $video['id'] = $snippet->resourceId->videoId;
  160. $video['thumb_url'] = $thumb->url;
  161. $video['description'] = $snippet->description;
  162. $video['title'] = $snippet->title;
  163.  
  164. $videos[] = $video;
  165. }
  166.  
  167. return $videos;
  168. }
  169. }
  170.