diff --git a/README.md b/README.md index d2f9f9d..57a2173 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,9 @@ -yiitube -======= +# yiitube -yii framework 1.x widget to render videos from videos hosting sites (youtube, vimeo ...) \ No newline at end of file +yii framework 1.x widget to render videos from videos hosting sites (youtube, vimeo ...) + +Original file extension comes from [here](http://www.yiiframework.com/extension/yiitube). + +Initial commit : extension as downloaded, from yii extension repo. + +Doc : [http://www.yiiframework.com/extension/yiitube](http://www.yiiframework.com/extension/yiitube) diff --git a/Yiitube.php b/Yiitube.php new file mode 100644 index 0000000..6ecc36e --- /dev/null +++ b/Yiitube.php @@ -0,0 +1,275 @@ +array( + 'small' => 349, + 'normal' => 390, + 'big' => 510, + 'huge' => 750, + ), + 'megavideo'=>array( + 'small' => 330, + 'normal' => 344, + 'big' => 430, + 'huge' => 551, + ), + 'vimeo'=>array( + 'small' => 225, + 'normal' => 360, + 'big' => 450, + 'huge' => 576 + ), + 'veoh'=>array( + 'small' => 340, + 'normal' => 510, + 'big' => 680, + 'huge' => 850, + ), + ); + /** + * @var array possible standard widths of a youtube video + */ + private $_width = array( + 'youtube'=>array( + 'small' => 560, + 'normal' => 640, + 'big' => 853, + 'huge' => 1280, + ), + 'megavideo'=>array( + 'small' => 450, + 'normal' => 640, + 'big' => 800, + 'huge' => 1024, + ), + 'vimeo'=>array( + 'small' => 400, + 'normal' => 640, + 'big' => 800, + 'huge' => 1024, + ), + 'veoh'=>array( + 'small' => 410, + 'normal' => 615, + 'big' => 820, + 'huge' => 1025, + ), + ); + /** + * @var int the current status. possible values are those of the constants + */ + private $_status; + + /** + * @var string the player to use to display the video + */ + public $player = 'youtube'; + /** + * when setting this parameter you can either use the video code or the whole url. + * @var string the youtube video you want to display. + */ + public $v; + /** + * @var string size options for the video + * @example small, normal, big, huge + */ + public $size = 'normal'; + /** + * @var bool if you want to display the video in HD or not. Defaults to false. + */ + public $hd = false; + + /** + * check the integrity of the video code when initialized + * @see CWidget::init() + */ + public function init() + { + parent::init(); + // set default status + $this->_status = self::OK; + // check if the selected player is supported + $this->playerSupport(); + // check the video code + if ($this->v != NULL) { + if (filter_var($this->v, FILTER_VALIDATE_URL)) + $this->v = $this->getVideoCode(); + } else + $this->_status = self::NO_VIDEO_CODE; + } + + /** + * display the video + * @see CWidget::run() + */ + public function run() + { + if ($this->_status === self::OK) { + $method = strtolower($this->player).'Code'; + echo $this->$method(); + } else + echo ''.$this->renderError().''; + } + + /** + * checks if yiitube is supporting the desired player + * @return bool + */ + protected function playerSupport() + { + switch (strtolower($this->player)) { + case self::YOUTUBE: + return true; + break; + case self::MEGAVIDEO: + return true; + break; + case self::VIMEO: + return true; + break; + case self::VEOH: + return true; + break; + default: + $this->_status = self::NO_SUPPORTED_PLAYER; + return false; + break; + } + } + + /** + * @return string the youtube iframe according to the settings + */ + protected function youtubeCode() + { + return << +YOUTUBE; + } + + /** + * @return string the megavideo object code according to the settings + */ + protected function megavideoCode() + { + return << + + + + +MEGAVIDEO; + } + + /** + * @return string the vimeo iframe code according to the settings + */ + protected function vimeoCode() + { + return << +VIMEO; + } + + /** + * @return string the veoh code according to the settings + */ + protected function veohCode() + { + return << + + + + + +VEOH; + } + + /** + * @return string the video url that has to be used inside the iframe + */ + protected function youtubeVideoURL() + { + $url = "http://www.youtube.com/embed/{$this->v}?rel=0"; + if ($this->hd) + $url .= '&hd=1'; + return $url; + } + + /** + * search into the provided url for the player video code. + * @return string the player video code + */ + protected function getVideoCode() + { + switch (strtolower($this->player)) { + case self::VIMEO: + $video_code = substr(parse_url($this->v, PHP_URL_PATH), 1); + if ($video_code) + return $video_code; + break; + case self::VEOH: + $video_code = substr(parse_url($this->v, PHP_URL_PATH), 7); + if ($video_code) + return $video_code; + break; + default: + $get_vars = explode('&', html_entity_decode(parse_url($this->v, PHP_URL_QUERY))); + foreach($get_vars as $var) { + $exploded_var = explode('=', $var); + if ($exploded_var[0] === 'v') + return $exploded_var[1]; + } + break; + } + $this->_status = self::NO_VIDEO_CODE_IN_URL; + return false; + } + + /** + * @return string the error message to be displayed + */ + protected function renderError() + { + switch ($this->_status) { + case self::NO_VIDEO_CODE: + return Yii::t('yiitube', 'you need to set a video code to use this widget.
Check the online documentation.'); + break; + case self::NO_VIDEO_CODE_IN_URL: + return Yii::t('yiitube', 'the URL you provided does not contain a youtube video code.'); + break; + case self::NO_SUPPORTED_PLAYER: + return Yii::t('yiitube', 'Yiitube doesn\'t support {player}', array('{player}' => $this->player)); + break; + } + } + +} \ No newline at end of file