source: trunk/www.guidonia.net/wp/wp-content/plugins/tubepress/classes/org/tubepress/thumbnail/SimpleThumbnailService.class.php@ 44

Last change on this file since 44 was 44, checked in by luciano, 14 years ago
File size: 9.6 KB
Line 
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
22function_exists('tubepress_load_classes')
23 || require(dirname(__FILE__) . '/../../../tubepress_classloader.php');
24tubepress_load_classes(array('org_tubepress_thumbnail_ThumbnailService',
25 'org_tubepress_video_Video',
26 'net_php_pear_HTML_Template_IT',
27 'org_tubepress_options_manager_OptionsManager',
28 'org_tubepress_options_category_Advanced',
29 'org_tubepress_options_category_Display',
30 'org_tubepress_options_category_Embedded',
31 'org_tubepress_options_category_Meta',
32 'org_tubepress_message_MessageService',
33 'org_tubepress_util_StringUtils'));
34
35/**
36 * Handles the parsing of the meta info below each video thumbnail
37 *
38 */
39class org_tubepress_thumbnail_SimpleThumbnailService implements org_tubepress_thumbnail_ThumbnailService
40{
41 private $_tpl;
42 private $_tpom;
43 private $_msg;
44 private $_tppf;
45 private $_tpeps;
46
47 public function getHtml($template, org_tubepress_video_Video $vid, $galleryId)
48 {
49 $this->_tpl = new net_php_pear_HTML_Template_IT($template);
50 if (!$this->_tpl->loadTemplatefile("thumbnail.tpl.html", true, true)) {
51 throw new Exception("Couldn't load thumbnail template");
52 }
53 $this->_getCommonStuff($vid, $galleryId);
54 $this->_getMetaStuff($vid);
55 return org_tubepress_util_StringUtils::removeEmptyLines($this->_tpl->get());
56 }
57
58 public function setOptionsManager(org_tubepress_options_manager_OptionsManager $tpom)
59 {
60 $this->_tpom = $tpom;
61 }
62
63 private function _getCommonStuff(org_tubepress_video_Video $vid, $galleryId)
64 {
65 $randomizeOpt = $this->_tpom->
66 get(org_tubepress_options_category_Advanced::RANDOM_THUMBS);
67 $thumbWidth = $this->_tpom->
68 get(org_tubepress_options_category_Display::THUMB_WIDTH);
69 $thumbHeight = $this->_tpom->
70 get(org_tubepress_options_category_Display::THUMB_HEIGHT);
71 $height = $this->_tpom->
72 get(org_tubepress_options_category_Embedded::EMBEDDED_HEIGHT);
73 $width = $this->_tpom->
74 get(org_tubepress_options_category_Embedded::EMBEDDED_WIDTH);
75
76 $encodedTitle = rawurlencode($vid->getTitle());
77 $playerName = $this->_tpom->get(org_tubepress_options_category_Display::CURRENT_PLAYER_NAME);
78 $esName = $this->_tpom->get(org_tubepress_options_category_Embedded::PLAYER_IMPL);
79 $this->_tpl->setVariable('VIDEO_IMG_ANCHOR_ID', 'tubepress_image_' . $vid->getId() . '_' . $galleryId);
80 $this->_tpl->setVariable('VIDEO_IMG_ANCHOR_REL', 'tubepress_' . $esName . '_' . $playerName . '_' . $galleryId);
81 $this->_tpl->setVariable('VIDEO_IMG_ALT', $vid->getTitle());
82 $this->_tpl->setVariable('VIDEO_TITLE_ANCHOR_ID', 'tubepress_title_' . $vid->getId() . '_' . $galleryId);
83 $this->_tpl->setVariable('VIDEO_TITLE_ANCHOR_REL', 'tubepress_' . $esName . '_' . $playerName . '_' . $galleryId);
84
85 if ($randomizeOpt) {
86 $this->_tpl->setVariable('THUMBURL', $vid->getRandomThumbURL());
87 } else {
88 $this->_tpl->setVariable('THUMBURL', $vid->getDefaultThumbURL());
89 }
90
91 $this->_tpl->setVariable('THUMBWIDTH', $thumbWidth);
92 $this->_tpl->setVariable('THUMBHEIGHT', $thumbHeight);
93 }
94
95 private function _getMetaStuff(org_tubepress_video_Video $vid)
96 {
97 $class = new ReflectionClass("org_tubepress_options_category_Meta");
98
99 /* go through each option in the category */
100 foreach ($class->getConstants() as $constant) {
101
102 if ($this->_tpom->get($constant) != 1
103 && $this->_tpom->get($constant) != $constant) {
104 continue;
105 }
106
107 $nofollow = $this->_tpom->get(org_tubepress_options_category_Advanced::NOFOLLOW_LINKS);
108
109 switch ($constant) {
110
111 case org_tubepress_options_category_Meta::TITLE:
112 $this->_tpl->setVariable('TITLE', $vid->getTitle());
113 $this->_tpl->parse('title');
114 break;
115
116 case org_tubepress_options_category_Meta::LENGTH:
117 $this->_tpl->setVariable('RUNTIME', $vid->getLength());
118 $this->_tpl->parse('runtime');
119 break;
120
121 case org_tubepress_options_category_Meta::DESCRIPTION:
122 $limit = $this->_tpom->get(org_tubepress_options_category_Display::DESC_LIMIT);
123 $desc = $vid->getDescription();
124 if ($limit > 0 && strlen($desc) > $limit) {
125 $desc = substr($desc, 0, $limit) . "...";
126 }
127 $this->_tpl->setVariable('DESCRIPTION', $desc);
128 $this->_tpl->parse('description');
129 break;
130
131 case org_tubepress_options_category_Meta::AUTHOR:
132 $this->_tpl->setVariable('METANAME', $this->_msg->_("video-" . $constant));
133 $this->_tpl->setVariable('AUTHOR', $vid->getAuthor());
134 if ($nofollow) { $this->_tpl->setVariable("NOFOLLOW", "rel=\"external nofollow\""); }
135 $this->_tpl->parse('author');
136 break;
137
138 case org_tubepress_options_category_Meta::TAGS:
139 $tags = implode(" ", $vid->getTags());
140 $this->_tpl->setVariable('METANAME', $this->_msg->_("video-" . $constant));
141 $this->_tpl->setVariable('SEARCHSTRING', rawurlencode($tags));
142 $this->_tpl->setVariable('TAGS', $tags);
143 if ($nofollow) { $this->_tpl->setVariable("NOFOLLOW", "rel=\"external nofollow\""); }
144 $this->_tpl->parse('tags');
145 break;
146
147 case org_tubepress_options_category_Meta::URL:
148 $this->_tpl->setVariable('LINKVALUE', $vid->getYouTubeUrl());
149 $this->_tpl->setVariable('LINKTEXT', $this->_msg->_("video-" . $constant));
150 $this->_tpl->parse('url');
151 break;
152
153 default:
154 $this->_tpl->setVariable('METANAME', $this->_msg->_("video-" . $constant));
155
156 switch ($constant) {
157
158 case org_tubepress_options_category_Meta::VIEWS:
159 $this->_tpl->setVariable('METAVALUE', $vid->getViews());
160 break;
161
162 case org_tubepress_options_category_Meta::ID:
163 $this->_tpl->setVariable('METAVALUE', $vid->getId());
164 break;
165
166 case org_tubepress_options_category_Meta::RATING:
167 $this->_tpl->setVariable('METAVALUE', $vid->getRating());
168 break;
169
170 case org_tubepress_options_category_Meta::RATINGS:
171 $this->_tpl->setVariable('METAVALUE', $vid->getRatings());
172 break;
173
174 case org_tubepress_options_category_Meta::UPLOADED:
175 $niceDate = $vid->getUploadTime();
176 if ($niceDate != "N/A") {
177 if ($this->_tpom->get(org_tubepress_options_category_Display::RELATIVE_DATES)) {
178 $niceDate =
179 $this->_relativeTime($vid->getUploadTime());
180 } else {
181 $niceDate = date($this->_tpom->
182 get(org_tubepress_options_category_Advanced::DATEFORMAT),
183 $vid->getUploadTime());
184 }
185 }
186 $this->_tpl->setVariable('METAVALUE', $niceDate);
187 break;
188
189 case org_tubepress_options_category_Meta::CATEGORY:
190 $this->_tpl->setVariable('METAVALUE', $vid->getCategory());
191 }
192 $this->_tpl->parse('meta');
193 }
194 }
195 }
196
197 public function setMessageService(org_tubepress_message_MessageService $messageService)
198 {
199 $this->_msg = $messageService;
200 }
201
202 //Grabbed from http://www.weberdev.com/get_example-4769.html
203
204 private function _relativeTime($timestamp){
205 $difference = time() - $timestamp;
206 $periods = array("sec", "min", "hour", "day", "week", "month", "year", "decade");
207 $lengths = array("60","60","24","7","4.35","12","10");
208
209 if ($difference > 0) { // this was in the past
210 $ending = "ago";
211 } else { // this was in the future
212 $difference = -$difference;
213 $ending = "to go";
214 }
215 for($j = 0; $difference >= $lengths[$j]; $j++) $difference /= $lengths[$j];
216 $difference = round($difference);
217 if($difference != 1) $periods[$j].= "s";
218 $text = "$difference $periods[$j] $ending";
219 return $text;
220 }
221}
Note: See TracBrowser for help on using the repository browser.