[44] | 1 | <?php
|
---|
| 2 | /**
|
---|
| 3 | * Copyright 2006, 2007, 2008, 2009 Eric D. Hough (http://ehough.com)
|
---|
| 4 | *
|
---|
| 5 | * This file is part of TubePress (http://tubepress.org)
|
---|
| 6 | *
|
---|
| 7 | * TubePress is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * TubePress is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with TubePress. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | *
|
---|
| 20 | */
|
---|
| 21 |
|
---|
| 22 | function_exists('tubepress_load_classes')
|
---|
| 23 | || require(dirname(__FILE__) . '/../../../tubepress_classloader.php');
|
---|
| 24 | tubepress_load_classes(array('org_tubepress_ioc_ContainerAware',
|
---|
| 25 | 'net_php_pear_HTML_Template_IT',
|
---|
| 26 | 'org_tubepress_options_category_Feed',
|
---|
| 27 | 'org_tubepress_options_category_Display',
|
---|
| 28 | 'org_tubepress_player_Player',
|
---|
| 29 | 'org_tubepress_util_StringUtils',
|
---|
| 30 | 'org_tubepress_gdata_inspection_FeedInspectionService',
|
---|
| 31 | 'org_tubepress_gdata_retrieval_FeedRetrievalService',
|
---|
| 32 | 'org_tubepress_message_MessageService',
|
---|
| 33 | 'org_tubepress_options_manager_OptionsManager',
|
---|
| 34 | 'org_tubepress_pagination_PaginationService',
|
---|
| 35 | 'org_tubepress_querystring_QueryStringService',
|
---|
| 36 | 'org_tubepress_thumbnail_ThumbnailService',
|
---|
| 37 | 'org_tubepress_url_UrlBuilder',
|
---|
| 38 | 'org_tubepress_video_factory_VideoFactory'));
|
---|
| 39 |
|
---|
| 40 | /**
|
---|
| 41 | * Parent class of all TubePress galleries
|
---|
| 42 | */
|
---|
| 43 | abstract class org_tubepress_gallery_AbstractGallery implements org_tubepress_ioc_ContainerAware
|
---|
| 44 | {
|
---|
| 45 | private $_feedInspectionService;
|
---|
| 46 | private $_feedRetrievalService;
|
---|
| 47 | private $_iocContainer;
|
---|
| 48 | private $_templateDirectory;
|
---|
| 49 | private $_messageService;
|
---|
| 50 | private $_optionsManager;
|
---|
| 51 | private $_paginationService;
|
---|
| 52 | private $_queryStringService;
|
---|
| 53 | private $_thumbnailService;
|
---|
| 54 | private $_thumbnailTemplate;
|
---|
| 55 | private $_urlBuilder;
|
---|
| 56 | private $_videoFactory;
|
---|
| 57 |
|
---|
| 58 | /**
|
---|
| 59 | * Generates the content of this gallery
|
---|
| 60 | *
|
---|
| 61 | * @return The HTML content for this gallery
|
---|
| 62 | */
|
---|
| 63 | public final function generateThumbs($galleryId = 1)
|
---|
| 64 | {
|
---|
| 65 | /* load up the gallery template */
|
---|
| 66 | $tpl = new net_php_pear_HTML_Template_IT($this->_templateDirectory);
|
---|
| 67 | if (!$tpl->loadTemplatefile("gallery.tpl.html", true, true)) {
|
---|
| 68 | throw new Exception("Couldn't load gallery template");
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | $currentPage = $this->_queryStringService->getPageNum($_GET);
|
---|
| 72 | $url = $this->_urlBuilder->buildGalleryUrl($currentPage);
|
---|
| 73 | $useCache = $this->_optionsManager->get(
|
---|
| 74 | org_tubepress_options_category_Feed::CACHE_ENABLED);
|
---|
| 75 | $xml = $this->_feedRetrievalService->fetch($url, $useCache);
|
---|
| 76 | $totalResults = $this->_feedInspectionService->getTotalResultCount($xml);
|
---|
| 77 | $queryResult = $this->_feedInspectionService->getQueryResultCount($xml);
|
---|
| 78 |
|
---|
| 79 | /* see if we got any */
|
---|
| 80 | if ($totalResults == 0) {
|
---|
| 81 | throw new Exception("YouTube returned no videos for your query!");
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | /* Figure out how many videos we're going to show */
|
---|
| 85 | $vidLimit =
|
---|
| 86 | $this->_optionsManager->get(org_tubepress_options_category_Display::RESULTS_PER_PAGE);
|
---|
| 87 | if ($queryResult < $vidLimit) {
|
---|
| 88 | $vidLimit = $queryResult;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | $videos = $this->_videoFactory->dom2TubePressVideoArray($xml, $vidLimit);
|
---|
| 92 |
|
---|
| 93 | if ($this->_optionsManager->get(org_tubepress_options_category_Display::ORDER_BY) == "random") {
|
---|
| 94 | shuffle($videos);
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | $thumbsHtml = "";
|
---|
| 98 | $playerName =
|
---|
| 99 | $this->_optionsManager->
|
---|
| 100 | get(org_tubepress_options_category_Display::CURRENT_PLAYER_NAME);
|
---|
| 101 | $player = $this->_iocContainer->safeGet($playerName . "-player", org_tubepress_player_Player::NORMAL . "-player");
|
---|
| 102 |
|
---|
| 103 | $tpl->setVariable('GALLERYID', $galleryId);
|
---|
| 104 |
|
---|
| 105 | for ($x = 0; $x < sizeof($videos); $x++) {
|
---|
| 106 |
|
---|
| 107 | /* ignore videos we can't display */
|
---|
| 108 | if (!$videos[$x]->isDisplayable()) {
|
---|
| 109 | continue;
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | /* Top of the gallery is special */
|
---|
| 113 | if ($x == 0) {
|
---|
| 114 | $tpl->setVariable("PRE_GALLERY_PLAYER_HTML",
|
---|
| 115 | $player->getPreGalleryHtml($this->_getPreGalleryVideo($videos), $galleryId));
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | /* Here's where each thumbnail gets printed */
|
---|
| 119 | $thumbsHtml .= $this->_thumbnailService->getHtml(
|
---|
| 120 | $this->_templateDirectory, $videos[$x], $galleryId);
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | $tpl->setVariable("THUMBS", $thumbsHtml);
|
---|
| 124 |
|
---|
| 125 | /* Spit out the top/bottom pagination if we have any videos */
|
---|
| 126 | if ($vidLimit > 0) {
|
---|
| 127 | $this->_parsePaginationHTML($totalResults, $tpl);
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | return org_tubepress_util_StringUtils::removeEmptyLines($tpl->get());
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | private function _countUnavailableVideos($videos)
|
---|
| 134 | {
|
---|
| 135 | $result = 0;
|
---|
| 136 | foreach ($videos as $video) {
|
---|
| 137 | if (!$video->isDisplayable()) {
|
---|
| 138 | $result++;
|
---|
| 139 | }
|
---|
| 140 | }
|
---|
| 141 | return $result;
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | private function _getPreGalleryVideo($videos)
|
---|
| 145 | {
|
---|
| 146 | $customVideoId = $this->_queryStringService->getCustomVideo($_GET);
|
---|
| 147 | if ($customVideoId != "") {
|
---|
| 148 | $videoUrl = $this->_urlBuilder->buildSingleVideoUrl($customVideoId);
|
---|
| 149 | $results = $this->_feedRetrievalService->fetch($videoUrl,
|
---|
| 150 | $this->_optionsManager->get(org_tubepress_options_category_Feed::CACHE_ENABLED));
|
---|
| 151 | $videoArray = $this->_videoFactory->dom2TubePressVideoArray($results, 1);
|
---|
| 152 | return $videoArray[0];
|
---|
| 153 | }
|
---|
| 154 | return $videos[0];
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | /**
|
---|
| 158 | * Handles the parsing of pagination links ("next" and "prev")
|
---|
| 159 | *
|
---|
| 160 | * @param int $vidCount The grand total video count
|
---|
| 161 | * @param net_php_pear_HTML_Template_IT $tpl The HTML template to write to
|
---|
| 162 | *
|
---|
| 163 | * @return void
|
---|
| 164 | */
|
---|
| 165 | private function _parsePaginationHTML($vidCount,
|
---|
| 166 | net_php_pear_HTML_Template_IT $tpl)
|
---|
| 167 | {
|
---|
| 168 | $pagination = $this->_paginationService->getHtml($vidCount);
|
---|
| 169 |
|
---|
| 170 | if ($this->_optionsManager->get(org_tubepress_options_category_Display::PAGINATE_ABOVE)) {
|
---|
| 171 | $tpl->setVariable('TOPPAGINATION', $pagination);
|
---|
| 172 | }
|
---|
| 173 | if ($this->_optionsManager->get(org_tubepress_options_category_Display::PAGINATE_BELOW)) {
|
---|
| 174 | $tpl->setVariable('BOTPAGINATION', $pagination);
|
---|
| 175 | }
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | public function setContainer(org_tubepress_ioc_IocService $container)
|
---|
| 179 | {
|
---|
| 180 | $this->_iocContainer = $container;
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | public function setTemplateDirectory($directory)
|
---|
| 184 | {
|
---|
| 185 | $this->_templateDirectory = $directory;
|
---|
| 186 | }
|
---|
| 187 |
|
---|
| 188 | public function setFeedInspectionService(org_tubepress_gdata_inspection_FeedInspectionService $feedInspector)
|
---|
| 189 | {
|
---|
| 190 | $this->_feedInspectionService = $feedInspector;
|
---|
| 191 | }
|
---|
| 192 |
|
---|
| 193 | public function setFeedRetrievalService(org_tubepress_gdata_retrieval_FeedRetrievalService $feedRetriever)
|
---|
| 194 | {
|
---|
| 195 | $this->_feedRetrievalService = $feedRetriever;
|
---|
| 196 | }
|
---|
| 197 |
|
---|
| 198 | public function setMessageService(org_tubepress_message_MessageService $messageService)
|
---|
| 199 | {
|
---|
| 200 | $this->_messageService = $messageService;
|
---|
| 201 | }
|
---|
| 202 |
|
---|
| 203 | public function setOptionsManager(org_tubepress_options_manager_OptionsManager $tpom)
|
---|
| 204 | {
|
---|
| 205 | $this->_optionsManager = $tpom;
|
---|
| 206 | }
|
---|
| 207 |
|
---|
| 208 | public function setPaginationService(org_tubepress_pagination_PaginationService $paginator)
|
---|
| 209 | {
|
---|
| 210 | $this->_paginationService = $paginator;
|
---|
| 211 | }
|
---|
| 212 |
|
---|
| 213 | public function setQueryStringService(org_tubepress_querystring_QueryStringService $qss)
|
---|
| 214 | {
|
---|
| 215 | $this->_queryStringService = $qss;
|
---|
| 216 | }
|
---|
| 217 |
|
---|
| 218 | public function setThumbnailService(org_tubepress_thumbnail_ThumbnailService $thumbService)
|
---|
| 219 | {
|
---|
| 220 | $this->_thumbnailService = $thumbService;
|
---|
| 221 | }
|
---|
| 222 |
|
---|
| 223 | public function setUrlBuilderService(org_tubepress_url_UrlBuilder $urlBuilder)
|
---|
| 224 | {
|
---|
| 225 | $this->_urlBuilder = $urlBuilder;
|
---|
| 226 | }
|
---|
| 227 |
|
---|
| 228 | public function setVideoFactory(org_tubepress_video_factory_VideoFactory $factory)
|
---|
| 229 | {
|
---|
| 230 | $this->_videoFactory = $factory;
|
---|
| 231 | }
|
---|
| 232 |
|
---|
| 233 | }
|
---|