예제 #1
0
 /**
  * Generates the proper html depending on the content_type_id
  * @param object $subject the an instance of the Subject class
  * @param string $mode in wich to return information(html or array of data)
  * @return mixed string the html content OR array with content values
  */
 public function subject_content($subject, $mode = 'html')
 {
     switch ($subject->content_type_id) {
         case 1:
             if ($subject->content_image->url) {
                 $img_url = $subject->content_image->url;
             } else {
                 $img_url = Yii::app()->getRequest()->getBaseUrl(true) . '/' . $subject->content_image->path . '/';
                 $filename = $subject->content_image->id . '.' . $subject->content_image->extension;
                 $img_url .= Yii::app()->getTheme()->name == 'mobile' ? "small_" . $filename : $filename;
             }
             $html = '<img src="' . $img_url . '" class="content_image">';
             //Yii::app()->getRequest()->getHostInfo().
             if ($subject->content_image->url) {
                 $parsed_url = parse_url($subject->content_image->url);
                 $html .= "<br><span>Image source: " . $parsed_url['scheme'] . '://' . $parsed_url['host'];
             }
             $arr_content['image'] = $img_url;
             break;
         case 2:
             $html = SiteHelper::formatted($subject->content_text->text);
             break;
         case 3:
             preg_match_all('#\\bhttps?://[^\\s()<>]+(?:\\([\\w\\d]+\\)|([^[:punct:]\\s]|/))#', $subject->content_video->embed_code, $match);
             //would be nice to use yii validator to see if its an url but: http://code.google.com/p/yii/issues/detail?id=1324
             if (SiteLibrary::valid_url($match[0][0])) {
                 //if its an url(if we could extract an url from the content)
                 $parsed_url = parse_url($match[0][0]);
                 $query_arr = SiteLibrary::parse_url_query($parsed_url['query']);
                 if (stristr($parsed_url['host'], 'youtube.com')) {
                     //and its from youtube
                     //get the V value $parsed_url['query'];
                     if (array_key_exists('v', $query_arr)) {
                         //set ?wmode=opaque because so that the the #header_top div does not get bellow the movie
                         //http://stackoverflow.com/questions/3820325/overlay-opaque-div-over-youtube-iframe
                         $time = $query_arr['t'] ? '#at=' . $query_arr['t'] : '';
                         //#at=** is the correct syntax for embed url, ie clic on the youtube logo while wathching a video on X seconds: it will open a new window with that param
                         $html = '<iframe width="640" height="390" src="http://www.youtube.com/embed/' . $query_arr['v'] . '?wmode=opaque' . $time . '" frameborder="0" allowfullscreen></iframe>';
                     } else {
                         $time = $query_arr['t'] ? '#at=' . $query_arr['t'] : '';
                         //#at=** is the correct syntax for embed url, ie clic on the youtube logo while wathching a video on X seconds: it will open a new window with that param
                         $vid_url = str_replace("/v/", "", $parsed_url['path']);
                         //remove the old embed code variable just in case it is present
                         if (strpos($vid_url, "?")) {
                             $vid_url = substr($vid_url, 0, strpos($vid_url, "?"));
                         } elseif (strpos($vid_url, "&")) {
                             $vid_url = substr($vid_url, 0, strpos($vid_url, "&"));
                         }
                         //remove the old params
                         $vid_url = str_replace("/embed/", "", $vid_url);
                         //remove the old embed code variable just in case it is present
                         $html = '<iframe width="640" height="390" src="http://www.youtube.com/embed/' . $vid_url . '?wmode=opaque' . $time . '" frameborder="0" allowfullscreen></iframe>';
                     }
                 } elseif (stristr($parsed_url['host'], 'youtu.be')) {
                     //and its from youtube shortly
                     $time = $query_arr['t'] ? '#at=' . $query_arr['t'] : '';
                     //#at=** is the correct syntax for embed url, ie clic on the youtube logo while wathching a video on X seconds: it will open a new window with that param
                     $html = '<iframe width="640" height="390" src="http://www.youtube.com/embed/' . $parsed_url['path'] . '?wmode=opaque' . $time . '" frameborder="0" allowfullscreen></iframe>';
                 } elseif (stristr($parsed_url['host'], 'dailymotion.com')) {
                     //the code is before the first undersore for the video source(pending verify if thats the syntax for all cases)
                     if ($last_code_pos = stripos($parsed_url['path'], '_')) {
                         $video_code = substr($parsed_url['path'], 1, $last_code_pos - 1);
                         if ($video_code) {
                             $html = '<iframe frameborder="0" width="640" height="390" src="http://www.dailymotion.com/embed/' . $video_code . '"></iframe>';
                         }
                         //$video_code already contains: /videdo/
                     }
                 } elseif (stristr($parsed_url['host'], 'vimeo.com')) {
                     if ($parsed_url['path']) {
                         //nice, we can play with params here, title, portrait, etc
                         $html = '<iframe src="http://player.vimeo.com/video' . $parsed_url['path'] . '?title=0&byline=0&portrait=0" width="640" height="390" frameborder="0"></iframe>';
                     }
                 } else {
                     $html = SiteHelper::formatted($subject->content_video->embed_code);
                 }
             } else {
                 $html = SiteHelper::formatted($subject->content_video->embed_code);
             }
             //Get the iframe source url and set the image url for ogtags
             //Also set opaque property for youtube videos
             //src="...."    second match (((?!").)*) searches for any string NOT containing " as that is our delimitter used in third match
             $pattern = '/(src=")(((?!").)*)(")/i';
             preg_match($pattern, $html, $matches);
             //position 0 is the full match
             $arr_content['url'] = $matches[2];
             if (SiteLibrary::valid_url($arr_content['url'])) {
                 //if its an url
                 $parsed_url = parse_url($arr_content['url']);
                 $query_arr = SiteLibrary::parse_url_query($parsed_url['query']);
                 if (stripos($parsed_url['host'], 'youtube.com')) {
                     $pattern2 = strpos($arr_content['url'], '?') ? '/(embed\\/)(.+)(\\?)/i' : '/(embed\\/)(.+)$/i';
                     //the url can have extra params (a ? mark to pass extra params to the video)
                     preg_match($pattern2, $arr_content['url'], $matches);
                     //position 0 is the full match
                     $arr_content['code'] = $matches[2];
                     $arr_content['image'] = 'http://img.youtube.com/vi/' . $arr_content['code'] . '/default.jpg';
                     //Intercept the content html iframe url of youtube videos and set opaque property if not set
                     if (!strpos($arr_content['url'], 'wmode=opaque')) {
                         $opaque_mode = strpos($arr_content['url'], '?') ? '&wmode=opaque' : '?wmode=opaque';
                         $html = preg_replace('/(.*)(src=")(((?!").)*)(")(.*)/i', '${1}$2' . $arr_content['url'] . $opaque_mode . '$5$6', $html);
                     }
                 }
                 //TODO: get schema for images in other providers
             }
             //intercept the content html and resize any width or height depending on current theme
             if (Yii::app()->getTheme()->name == 'mobile') {
                 $max_width = 250;
                 $max_height = 190;
             } else {
                 $max_width = 640;
                 $max_height = 440;
             }
             $pattern = '/(width=")(\\d+)(")/i';
             //Replace: width="***"  ---> width="MOBILE_WIDTH"
             preg_match($pattern, $html, $matches);
             //width will fall in thrid position as position 0 is the full match(see func definition)
             if ((int) $matches[2] > $max_width) {
                 $html = preg_replace($pattern, '${1}' . $max_width . '$3', $html);
             }
             $pattern = '/(height=")(\\d+)(")/i';
             preg_match($pattern, $html, $matches);
             //width will fall in thrid position as position 0 is the full match(see func definition)
             if ((int) $matches[2] > $max_height) {
                 $html = preg_replace($pattern, '${1}' . $max_height . '$3', $html);
             }
             $replacement = '${1}190$3';
             //TODO: http://css-tricks.com/7066-fluid-width-youtube-videos/
             break;
     }
     return $mode == 'array' ? $arr_content : $html;
 }