PageRenderTime 51ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/common/libraries/plugin/google/demos/Zend/Gdata/YouTubeVideoBrowser/index.php

https://bitbucket.org/chamilo/chamilo-dev/
PHP | 294 lines | 178 code | 16 blank | 100 comment | 9 complexity | 8dc28f2002274aa0e06defd00699a209 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.1, LGPL-3.0, GPL-3.0, MIT
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Gdata
  17. * @subpackage Demos
  18. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * PHP sample code for the YouTube data API. Utilizes the Zend Framework
  23. * Zend_Gdata component to communicate with the YouTube data API.
  24. *
  25. * Requires the Zend Framework Zend_Gdata component and PHP >= 5.1.4
  26. *
  27. * This sample is run from within a web browser. These files are required:
  28. * index.php - the main logic, which interfaces with the YouTube API
  29. * interface.html - the HTML to represent the web UI
  30. * web_browser.css - the CSS to define the interface style
  31. * web_browser.js - the JavaScript used to provide the video list AJAX interface
  32. *
  33. * NOTE: If using in production, some additional precautions with regards
  34. * to filtering the input data should be used. This code is designed only
  35. * for demonstration purposes.
  36. */
  37. /**
  38. * @see Zend_Loader
  39. */
  40. require_once 'Zend/Loader.php';
  41. /**
  42. * @see Zend_Gdata_YouTube
  43. */
  44. Zend_Loader :: loadClass('Zend_Gdata_YouTube');
  45. /**
  46. * Finds the URL for the flash representation of the specified video
  47. *
  48. * @param Zend_Gdata_YouTube_VideoEntry $entry The video entry
  49. * @return string|null The URL or null, if the URL is not found
  50. */
  51. function findFlashUrl($entry)
  52. {
  53. foreach ($entry->mediaGroup->content as $content)
  54. {
  55. if ($content->type === 'application/x-shockwave-flash')
  56. {
  57. return $content->url;
  58. }
  59. }
  60. return null;
  61. }
  62. /**
  63. * Returns a feed of top rated videos for the specified user
  64. *
  65. * @param string $user The username
  66. * @return Zend_Gdata_YouTube_VideoFeed The feed of top rated videos
  67. */
  68. function getTopRatedVideosByUser($user)
  69. {
  70. $userVideosUrl = 'http://gdata.youtube.com/feeds/users/' . $user . '/uploads';
  71. $yt = new Zend_Gdata_YouTube();
  72. $ytQuery = $yt->newVideoQuery($userVideosUrl);
  73. // order by the rating of the videos
  74. $ytQuery->setOrderBy('rating');
  75. // retrieve a maximum of 5 videos
  76. $ytQuery->setMaxResults(5);
  77. // retrieve only embeddable videos
  78. $ytQuery->setFormat(5);
  79. return $yt->getVideoFeed($ytQuery);
  80. }
  81. /**
  82. * Returns a feed of videos related to the specified video
  83. *
  84. * @param string $videoId The video
  85. * @return Zend_Gdata_YouTube_VideoFeed The feed of related videos
  86. */
  87. function getRelatedVideos($videoId)
  88. {
  89. $yt = new Zend_Gdata_YouTube();
  90. $ytQuery = $yt->newVideoQuery();
  91. // show videos related to the specified video
  92. $ytQuery->setFeedType('related', $videoId);
  93. // order videos by rating
  94. $ytQuery->setOrderBy('rating');
  95. // retrieve a maximum of 5 videos
  96. $ytQuery->setMaxResults(5);
  97. // retrieve only embeddable videos
  98. $ytQuery->setFormat(5);
  99. return $yt->getVideoFeed($ytQuery);
  100. }
  101. /**
  102. * Echo img tags for the first thumbnail representing each video in the
  103. * specified video feed. Upon clicking the thumbnails, the video should
  104. * be presented.
  105. *
  106. * @param Zend_Gdata_YouTube_VideoFeed $feed The video feed
  107. * @return void
  108. */
  109. function echoThumbnails($feed)
  110. {
  111. foreach ($feed as $entry)
  112. {
  113. $videoId = $entry->getVideoId();
  114. echo '<img src="' . $entry->mediaGroup->thumbnail[0]->url . '" ';
  115. echo 'width="80" height="72" onclick="ytvbp.presentVideo(\'' . $videoId . '\')">';
  116. }
  117. }
  118. /**
  119. * Echo the video embed code, related videos and videos owned by the same user
  120. * as the specified videoId.
  121. *
  122. * @param string $videoId The video
  123. * @return void
  124. */
  125. function echoVideoPlayer($videoId)
  126. {
  127. $yt = new Zend_Gdata_YouTube();
  128. $entry = $yt->getVideoEntry($videoId);
  129. $videoTitle = $entry->mediaGroup->title;
  130. $videoUrl = findFlashUrl($entry);
  131. $relatedVideoFeed = getRelatedVideos($entry->getVideoId());
  132. $topRatedFeed = getTopRatedVideosByUser($entry->author[0]->name);
  133. print <<<END
  134. <b>$videoTitle</b><br />
  135. <object width="425" height="350">
  136. <param name="movie" value="${videoUrl}&autoplay=1"></param>
  137. <param name="wmode" value="transparent"></param>
  138. <embed src="${videoUrl}&autoplay=1" type="application/x-shockwave-flash" wmode="transparent"
  139. width=425" height="350"></embed>
  140. </object>
  141. END;
  142. echo '<br />';
  143. echoVideoMetadata($entry);
  144. echo '<br /><b>Related:</b><br />';
  145. echoThumbnails($relatedVideoFeed);
  146. echo '<br /><b>Top rated videos by user:</b><br />';
  147. echoThumbnails($topRatedFeed);
  148. }
  149. /**
  150. * Echo video metadata
  151. *
  152. * @param Zend_Gdata_YouTube_VideoEntry $entry The video entry
  153. * @return void
  154. */
  155. function echoVideoMetadata($entry)
  156. {
  157. $title = $entry->mediaGroup->title;
  158. $description = $entry->mediaGroup->description;
  159. $authorUsername = $entry->author[0]->name;
  160. $authorUrl = 'http://www.youtube.com/profile?user=' . $authorUsername;
  161. $tags = $entry->mediaGroup->keywords;
  162. $duration = $entry->mediaGroup->duration->seconds;
  163. $watchPage = $entry->mediaGroup->player[0]->url;
  164. $viewCount = $entry->statistics->viewCount;
  165. $rating = $entry->rating->average;
  166. $numRaters = $entry->rating->numRaters;
  167. $flashUrl = findFlashUrl($entry);
  168. print <<<END
  169. <b>Title:</b> ${title}<br />
  170. <b>Description:</b> ${description}<br />
  171. <b>Author:</b> <a href="${authorUrl}">${authorUsername}</a><br />
  172. <b>Tags:</b> ${tags}<br />
  173. <b>Duration:</b> ${duration} seconds<br />
  174. <b>View count:</b> ${viewCount}<br />
  175. <b>Rating:</b> ${rating} (${numRaters} ratings)<br />
  176. <b>Flash:</b> <a href="${flashUrl}">${flashUrl}</a><br />
  177. <b>Watch page:</b> <a href="${watchPage}">${watchPage}</a> <br />
  178. END;
  179. }
  180. /**
  181. * Echo the list of videos in the specified feed.
  182. *
  183. * @param Zend_Gdata_YouTube_VideoFeed $feed The video feed
  184. * @return void
  185. */
  186. function echoVideoList($feed)
  187. {
  188. echo '<table class="videoList">';
  189. echo '<tbody width="100%">';
  190. foreach ($feed as $entry)
  191. {
  192. $videoId = $entry->getVideoId();
  193. $thumbnailUrl = $entry->mediaGroup->thumbnail[0]->url;
  194. $videoTitle = $entry->mediaGroup->title;
  195. $videoDescription = $entry->mediaGroup->description;
  196. print <<<END
  197. <tr onclick="ytvbp.presentVideo('${videoId}')">
  198. <td width="130"><img src="${thumbnailUrl}" /></td>
  199. <td width="100%">
  200. <a href="#">${videoTitle}</a>
  201. <p class="videoDescription">${videoDescription}</p>
  202. </td>
  203. </tr>
  204. END;
  205. }
  206. echo '</table>';
  207. }
  208. /*
  209. * The main controller logic of the YouTube video browser demonstration app.
  210. */
  211. $queryType = isset($_POST['queryType']) ? $_POST['queryType'] : null;
  212. if ($queryType === null)
  213. {
  214. /* display the entire interface */
  215. include 'interface.html';
  216. }
  217. else
  218. if ($queryType == 'show_video')
  219. {
  220. /* display an individual video */
  221. if (array_key_exists('videoId', $_POST))
  222. {
  223. $videoId = $_POST['videoId'];
  224. echoVideoPlayer($videoId);
  225. }
  226. else
  227. if (array_key_exists('videoId', $_GET))
  228. {
  229. $videoId = $_GET['videoId'];
  230. echoVideoPlayer($videoId);
  231. }
  232. else
  233. {
  234. echo 'No videoId found.';
  235. exit();
  236. }
  237. }
  238. else
  239. {
  240. /* display a list of videos */
  241. $searchTerm = $_POST['searchTerm'];
  242. $startIndex = $_POST['startIndex'];
  243. $maxResults = $_POST['maxResults'];
  244. $yt = new Zend_Gdata_YouTube();
  245. $query = $yt->newVideoQuery();
  246. $query->setQuery($searchTerm);
  247. $query->setStartIndex($startIndex);
  248. $query->setMaxResults($maxResults);
  249. /* check for one of the standard feeds, or list from 'all' videos */
  250. switch ($queryType)
  251. {
  252. case 'most_viewed' :
  253. $query->setFeedType('most viewed');
  254. $query->setTime('this_week');
  255. $feed = $yt->getVideoFeed($query);
  256. break;
  257. case 'most_recent' :
  258. $query->setFeedType('most recent');
  259. $feed = $yt->getVideoFeed($query);
  260. break;
  261. case 'recently_featured' :
  262. $query->setFeedType('recently featured');
  263. $feed = $yt->getVideoFeed($query);
  264. break;
  265. case 'top_rated' :
  266. $query->setFeedType('top rated');
  267. $query->setTime('this_week');
  268. $feed = $yt->getVideoFeed($query);
  269. break;
  270. case 'all' :
  271. $feed = $yt->getVideoFeed($query);
  272. break;
  273. default :
  274. echo 'ERROR - unknown queryType - "' . $queryType . '"';
  275. break;
  276. }
  277. echoVideoList($feed);
  278. }