Here we are. The basics are still the same of the first part – making you own file explorer popup and using CKEditor.insertHtml to inject the desired generated code into ckeditor.
Of course, the same may be applied to webservices; including data form external sources is not harder than inserting your uploaded files.
At the times, I found this class – it was the best usable php youtube api wrapper, if not the only. Probably there is something new on github – but php5Tube does the job, and does it well. You may use your own choice, of course.
Here is my fork – a sligthly modified version with some features I needed
- <?php
-
- /**
- * Project: Php5tube: A PHP class for using the Youtube API<br />
- * File: Php5tube.php<br />
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or any later version.<br /><br />
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.<br /><br />
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA<br /><br />
- *
- * Any modifications to the library should be indicated clearly in the source code
- * to inform users that the changes are not a part of the original software.<br /><br />
- *
- * @link http://www.debuggeddesigns.com/open-source-projects/php5tube Php5tube Youtube API Wrapper
- * @link http://sourceforge.net/projects/php5tube/ Download Latest Version
- * @link http://www.debuggeddesigns.com/open-source-projects/php5tube/docs Online Documentation
- * @copyright 2008 Debugged Interactive Designs
- * @author debuggeddesigns <info@debuggeddesigns.com>
- * @version 0.0.3 (October 07, 2008)
- *
- */
-
- /**
- *
- * Fork by Stefano Manfredini
- *
- * added getVideosByKeyword and getVideosByCategory methods, and tweaked some other code
- *
- * this is not intended as a full and all-purpose version of the class;
- * it's mainly a fast add-on to use it for my needs at the moment
- * ..but you can use it and build on it
- *
- ChangeLog
- 0.4
- - Suppressed warning messages from bad feed returns and checks for empty results. If there was a problem
- the user will recieve empty arrays out of the methods
- -Added private method parse_multi_video() to reduced the size of php5tube by reducing reused code
- -Author array entry in multi-video search is now a string and no longer a simple_xml_object
-
- 0.3
- - Added ability to get a list of the most viewed videos on youtube [getMostViewed()]
-
- 0.2
- - Added ability to get a video's information, given the video's youtube id [getVideoInfo()]
- - Added ability to get a video's comments, given the video's youtube id [getVideoComments()]
-
- 0.1
- - Added ability to get a user's information, given the user's account name [getUserInfo()]
- - Added ability to get a user's videos, give the user's account name [getUserVideos()]
-
- */
-
- /**
- * Php5tube Class.
- *
- * Php5tube is a class written by Debugged Interactive Designs in PHP5
- * to act as a wrapper for Youtube's API.
- *
- * Methods process the response XML and return a friendly array of data
- * to make development simple and intuitive.
- *
- */
-
- class Php5tube {
-
- /*
- * Current Working functions
- *
- * getVideoInfo()
- * getUserVideos()
- * getUserInfo()
- * getMostViewed()
- * getVideoComments()
- *
- * Functions Under Production
- *
- * getVideosByKeywords($start,$max,$order)
- * getVideosByCategory($start,$max,$order)
- *
- *
- */
-
- //CLASS VARIABLES
-
- /**
- * The video object name in the returned array
- *
- * @var string
- */
- var $video_object = null;
-
- /**
- * The user object name in the returned array
- *
- * @var string
- */
- var $user_object = null;
-
- /**
- * The comment object name in the returned array
- *
- * @var string
- */
- var $comment_object = null;
-
- /**
- * Set to the next index in the query
- * or -1, if there are no more videos left
- *
- * @var int
- */
- var $next_index = -1;
-
- var $previous_index = -1;
-
- //CLASS CONSTRUCTOR
-
- /**
- * Class constructor.
- *
- * <code>
- * <?php
- * include 'Php5tube.php';
- * $php5tube = new Php5tube('Video','User','Comment');
- * ?>
- * </code>
- *
- * @param string $video The video object name in the returned array.
- * @param string $user The user object name in the returned array
- * @param string $comment The comment object name in the returned array
- */
- function Php5tube($video, $user, $comment) {
- $this->video_object = $video;
- $this->user_object = $user;
- $this->comment_object = $comment;
- }
-
- //CLASS FUNCTIONS
-
- /**
- * Returns all youtube videos that belong to the corresponding user.
- *
- * <code>
- * <?php
- * include 'Php5tube.php';
- * $php5tube = new Php5tube('Video','User','Comment');
- * $user_vids = $php5tube->getUserVideos('rickrolled','',1,10);
- * ?>
- * </code>
- *
- * @param string $user The account name of the youtube user.
- * @param string $category The category to search.
- * @param integer $start_index The first video id you want to grab. The default is 1.
- * @param integer $max The maximum number of videos returned.
- *
- * @return array array[<numbered_index>][$this->video_object][<field_name>] where <field_name> =<br />
- * {'youtube_id', 'author', 'title', 'description', 'keywords', 'url', 'thumbnail1', 'thumbnail2', 'thumbnail3', 'thumbnail4', 'length', 'view_count', 'favorite_count', 'comments_count'}
- */
-
- function getUserVideos($user, $category = null, $start_index = 1, $max = 25) {
- //set up the url to the feed
- $feedURL = 'http://gdata.youtube.com/feeds/api/videos?author=' . $user . '&start-index=' . $start_index . '&max-results=' . $max;
-
- //add category variable to feed url, if it exists
- if ($category != null) {
- $feedURL = $feedURL . '&category=' . $category;
- }
-
- //read the feed and place it in an simple_xml object
-
- //get the next start index.
- $this->next_index = (int) preg_replace('/&max-results=' . $max . '/', '', preg_replace('/http:\/\/gdata.youtube.com\/feeds\/api\/videos\?author=' . $user . '&start-index=/', '', (string) $xml->link[4]->attributes()->href));
- } //if empty, then set $this->nex_index to -1 to let the user know they have reached the end of the list
- else {
- $this->next_index = -1;
- }
-
- $videos = $this->parse_multi_video($xml);
- }
-
- return $videos;
- }
-
- /**
- * Returns the account information of a youtube user.
- *
- * <code>
- * <?php
- * include 'Php5tube.php';
- * $php5tube = new Php5tube('Video','User','Comment');
- * $user_info = $php5tube->getUserInfo('rickrolled');
- * ?>
- * </code>
- *
- * @param string $user - The account name of the youtube user.
- *
- * @return array array[$this->user_object][<field_name>] where <field_name> =<br />
- * {'username', 'first_name', 'last_name', 'age', 'hobbies', 'relationship', 'occupation', 'music','movies', 'location', 'hometown', 'gender', 'description'}
- *
- */
- function getUserInfo($user) {
-
- $authorURL = 'http://gdata.youtube.com/feeds/api/users/' . $user;
-
- $author = $xml->children('http://gdata.youtube.com/schemas/2007');
- //user's user name'
- $author_array[$this->user_object]['username'] = (string) $author->username;
- //users first name (if supplied)
- $author_array[$this->user_object]['first_name'] = (string) $author->firstName;
- //users last name (if supplied)
- $author_array[$this->user_object]['last_name'] = (string) $author->lastName;
- //users age (if supplied)
- $author_array[$this->user_object]['age'] = (string) $author->age;
- //users hobbies (if supplied)
- $author_array[$this->user_object]['hobbies'] = (string) $author->hobbies;
- //users relationship (if supplied) ie single, married, etc...
- $author_array[$this->user_object]['relationship'] = (string) $author->relationship;
- //users occupation
- $author_array[$this->user_object]['occupation'] = (string) $author->occupation;
- //favorite music
- $author_array[$this->user_object]['music'] = (string) $author->music;
- //favorite movies (not youtube videos) ... the rest are pretty obvious
- $author_array[$this->user_object]['movies'] = (string) $author->movies;
- $author_array[$this->user_object]['location'] = (string) $author->location;
- $author_array[$this->user_object]['hometown'] = (string) $author->hometown;
- $author_array[$this->user_object]['gender'] = (string) $author->gender;
- $author_array[$this->user_object]['description'] = (string) $author->description;
- }
- return $author_array;
- }
-
- /**
- * Returns a list of the most frequently viewed videos on youtube.
- *
- * <code>
- * <?php
- * include 'Php5tube.php';
- * $php5tube = new Php5tube('Video','User','Comment');
- * $most_viewed = $php5tube->getMostViewed(1,10);
- * ?>
- * </code>
- *
- * @param int $start_index Start at the nth most viewed video. The default is to start the number 1 most viewed video.
- * @param int $max The maximum number of videos returned in list. The default is 25 videos.
- *
- * @return array array[<numbered_index>][$this->video_object][<field_name>] where <field_name> =<br />
- * {'youtube_id', 'author', 'title', 'description', 'keywords', 'url', 'thumbnail1', 'thumbnail2', 'thumbnail3', 'thumbnail4', 'length', 'view_count', 'favorite_count', 'comments_count'}
- *
- */
- function getMostViewed($start_index = 1, $max = 25) {
- $feedURL = 'http://gdata.youtube.com/feeds/api/standardfeeds/most_viewed?start-index=' . $start_index . '&max-results=' . $max;
-
-
- {
- $videos = $this->parse_multi_video($xml);
- }
-
- return $videos;
- }
-
- /**
- * Returns the video comments of a youtube video.<br />
- * Note: this function does not set the next starting index instance variable in the class.
- *
- * <code>
- * <?php
- * include 'Php5tube.php';
- * $php5tube = new Php5tube('Video','User','Comment');
- * $vid_comments = $php5tube->getVideoComments('oHg5SJYRHA0',1,10);
- * ?>
- * </code>
- *
- * @param string $id The video's youtube id.
- * @param int start_index The first entry returned.
- * @param int max The number of results returned.
- *
- * @return array array[<numbered_index>][$this->video_object][<field_name>] where <field_name> =<br />
- * {'youtube_id', 'author','title','content','updated','published'}
- *
- */
-
- function getVideoComments($id, $start_index = 1, $max = 25) {
- $commentURL = 'http://gdata.youtube.com/feeds/api/videos/' . $id . '/comments?start-index=' . $start_index . '&max-results=' . $max;
-
- $i = 0;
- //check to see if we've reached the end of the feed pages
- //if there is no link to the next set of comments we are done.
- //so either set this->next_index to the next start_index or set it to -1 to notify the user we are done.
- /*
- if(!empty($xml->link[6]))
- {
- $this->next_index = $start_index + $max;
- }
- else
- {
- $this->next_index = -1;
- }
- */
- foreach ($xml->entry as $entry) {
- $comment_array[$i][$this->comment_object]['youtube_id'] = $id;
- //get the author's name
- $comment_array[$i][$this->comment_object]['author'] = (string) $entry->author->name;
- //get title of the comment
- $comment_array[$i][$this->comment_object]['title'] = (string) $entry->title;
- //get comment content
- $comment_array[$i][$this->comment_object]['content'] = (string) $entry->content;
- //get the last time the comment was updated
- $comment_array[$i][$this->comment_object]['updated'] = $entry->updated;
- //get the time the comment was published
- $comment_array[$i][$this->comment_object]['published'] = $entry->published;
- $i++;
- }
-
- return $comment_array;
- }
-
- /**
- * Returns the information of a youtube video.
- *
- * <code>
- * <?php
- * include 'Php5tube.php';
- * $php5tube = new Php5tube('Video','User','Comment');
- * $vid_info = $php5tube->getVideoInfo('oHg5SJYRHA0');
- * ?>
- * </code>
- *
- * @param string $id - The video's youtube id.
- *
- * @return array array[$this->video_object][<field_name>] where <field_name> =<br />
- * {'youtube_id', 'author', 'title', 'description', 'keywords', 'url', 'thumbnail1', 'thumbnail2', 'thumbnail3', 'thumbnail4', 'length', 'view_count', 'favorite_count', 'comments_count'}
- *
- */
-
- function getVideoInfo($id) {
- //set up the feed url
- $feedURL = 'http://gdata.youtube.com/feeds/api/videos/' . $id;
- //read the feed and place it in an simple_xml object
- //create an empty array to deal with foreach loops incase nothing is returned from the feed query
-
- //if the query returned a value and the xml object is not empty
-
- //store the youtube_id in the object.
- $video_array[$this->video_object]['youtube_id'] = $id;
- //get the authors name
- $video_array[$this->video_object]['author'] = $xml->author->name;
- $video = $xml->children('http://search.yahoo.com/mrss/');
-
- $player = $video->group->player->attributes();
- $thumbnail1 = $video->group->thumbnail[0]->attributes(); //get value for first thumbnail variable
- $thumbnail2 = $video->group->thumbnail[1]->attributes(); //get value for first thumbnail variable
- $thumbnail3 = $video->group->thumbnail[2]->attributes(); //get value for first thumbnail variable
- $thumbnail4 = $video->group->thumbnail[3]->attributes(); //get value for first thumbnail variable
- //get the url to the thumbnails for the video
- $video_array[$this->video_object]['thumbnail_url1'] = (string) $thumbnail1['url'];
- $video_array[$this->video_object]['thumbnail_url2'] = (string) $thumbnail2['url'];
- $video_array[$this->video_object]['thumbnail_url3'] = (string) $thumbnail3['url'];
- $video_array[$this->video_object]['thumbnail_url4'] = (string) $thumbnail4['url'];
-
- //get the video's title
- $video_array[$this->video_object]['title'] = (string) $video->group->title;
- //get the video's description
- $video_array[$this->video_object]['description'] = (string) $video->group->description;
- //get the video's category
- $video_array[$this->video_object]['category'] = (string) $video->group->category;
- //get the video's tags/keywords
- $video_array[$this->video_object]['keywords'] = (string) $video->group->keywords;
- //get the url to the video
- $video_array[$this->video_object]['url'] = (string) $player['url'];
-
- $yt = $video->children('http://gdata.youtube.com/schemas/2007');
- $length = $yt->duration->attributes();
- //get the video length in seconds
- $video_array[$this->video_object]['length'] = (string) $length['seconds'];
- $video = $xml->children('http://gdata.youtube.com/schemas/2007');
- //get the number of times the video has been viewed and marked as favorite
- $views = $video->statistics->attributes();
-
- //if results were returned - removes php warning
- //number of times the video has been viewed
- if ($views['viewCount'] == '') {
- $video_array[$this->video_object]['view_count'] = '0';
- } else {
- $video_array[$this->video_object]['view_count'] = (string) $views['viewCount'];
- }
- //number of times the video has been set as favorite
- if ($views['favoriteCount'] == '') {
- $video_array[$this->video_object]['favorite_count'] = '0';
- } else {
- $video_array[$this->video_object]['favorite_count'] = (string) $views['favoriteCount'];
- }
- } else { //results were not returned
- $video_array[$this->video_object]['view_count'] = '0';
- $video_array[$this->video_object]['favorite_count'] = '0';
- }
-
- $video = $xml->children('http://schemas.google.com/g/2005');
-
- //check to see if there are any comments/any information about the comments
- //if there is get the feedlink and number of comments
- if ($video->comments->feedLink) {
- $comments = $video->comments->feedLink->attributes();
- //get the number of comments for the video
- $video_array[$this->video_object]['comments_count'] = (string) $comments['countHint'];
- }
- }
-
- //return the current video array
- return $video_array;
-
- }
-
- ///*
- function getVideosByKeywords($keywords, $start_index = 1 ,$max_results = 25, $order = 'ViewCount', $category = null)
- {
-
- $feedURL = 'http://gdata.youtube.com/feeds/api/videos?q=' . $keywords . '&start-index=' . $start_index . '&max-results=' . $max_results; //.'&v=2';
-
- //add category variable to feed url, if it exists
- if ($category != null) {
- $feedURL = $feedURL . '&category=' . $category;
- }
-
- //read the feed and place it in an simple_xml object
- //debug($xml);
-
- //get the next start index.
- if($xml->link[5]->attributes()->rel == 'next') $this->next_index = $start_index + $max_results;
- }
- if($xml->link[4]->attributes()->rel == 'next') $this->next_index = $start_index + $max_results;
- if($xml->link[4]->attributes()->rel == 'previous') $this->previous_index = $start_index - $max_results;
- //$this->next_index = (int) preg_replace('/&max-results=' . $max_results . '/', '', preg_replace('/http:\/\/gdata.youtube.com\/feeds\/api\/videos\?q=' . $keywords . '&start-index=/', '', (string) $xml->link[4]->attributes()->href));
- //http://gdata.youtube.com/feeds/api/videos?q=volontariato+ferrara&start-index=11&max-results=10
- } //if empty, then set $this->nex_index to -1 to let the user know they have reached the end of the list
- else {
- $this->next_index = -1;
- }
- //debug($this->next_index);debug($this->previous_index);
- $videos = $this->parse_multi_video($xml);
- }
-
- return $videos;
- }
-
- /**
- * function getVideosByCategory
- * returns a list of videos which fall under a certain category
- *
- *$category - category of videos you want to see (can be the url or the category)
- *
- * @return an array structured like array[<numbered_index>][$this->video_object][<field_name>]
- *
- * <field_name> = {'id','title','description','keywords','url','thumbnail','length','views','comments_url','comments_count'}
- */
- function getVideosByCategory($category,$max = 25, $order) {
- //pull out the category incase the user gave us the whole url..
- //this is to make it easier for the user to use data from one query to launch this one
- $feedURL = 'http://gdata.youtube.com/feeds/api/videos/-/' . $category . '?max-results=' . $max;
- $i = 0;
- foreach ($xml->entry as $entry) {
- //id is the full url to get the video feed. the characters after the last slash is the actual video id
- //so id here could be 'http://gdata.youtube.com/feeds/api/videos/2jkExrrm_sQ' with the actual video id being 2jkExrrm_sQ
- $video_array[$i][$this->video_object]['id'] = (string) $entry->id;
- $video = $entry->children('http://search.yahoo.com/mrss/');
- $player = $video->group->player->attributes();
- $thumbnail = $video->group->thumbnail[0]->attributes();
-
- $video_array[$i][$this->video_object]['title'] = (string) $video->group->title;
- $video_array[$i][$this->video_object]['description'] = (string) $video->group->description;
- //keywords are a number of keywords in one string separated by commas
- $video_array[$i][$this->video_object]['keywords'] = (string) $video->group->keywords;
- //url is the actual url to the videos page
- $video_array[$i][$this->video_object]['url'] = (string) $player['url'];
- //url to the first thumbnail image
- $video_array[$i][$this->video_object]['thumbnail'] = (string) $thumbnail['url'];
-
- $yt = $video->children('http://gdata.youtube.com/schemas/2007');
- $length = $yt->duration->attributes();
- //videos length in seconds
- $video_array[$i][$this->video_object]['length'] = (string) $length['seconds'];
-
- $video = $entry->children('http://gdata.youtube.com/schemas/2007');
- $views = $video->statistics->attributes();
-
- //number of times the video has been watched
- $video_array[$i][$this->video_object]['views'] = (string) $views['viewCount'];
-
- $video = $entry->children('http://schemas.google.com/g/2005');
-
- //check to see if there are any comments/any information about the comments
- //if there is get the feedlink and number of comments
- if ($video->comments->feedLink) {
- $comments = $video->comments->feedLink->attributes();
- //number of comments the video has
- $video_array[$i][$this->video_object]['comments_count'] = (string) $comments['countHint'];
- }
-
- $i++;
- }
- return $video_array;
- }
-
- //*/
-
- /*
- * Start Helper code reduction methods.
- */
-
- private function parse_multi_video($xml) {
- $i = 0;
- foreach ($xml->entry as $entry) {
- //extract youtube id from url
- $temp_video_array[$this->video_object]['youtube_id'] = preg_replace('/http:\/\/gdata.youtube.com\/feeds\/api\/videos\//', '', (string) $entry->id);
- $temp_video_array[$this->video_object]['author'] = (string) $entry->author->name;
-
- //magically gets new variables for each video
- $video = $entry->children('http://search.yahoo.com/mrss/');
- $player = $video->group->player->attributes(); //get value for player variable
- $thumbnail1 = $video->group->thumbnail[0]->attributes(); //get value for first thumbnail variable
- $thumbnail2 = $video->group->thumbnail[1]->attributes(); //get value for first thumbnail variable
- $thumbnail3 = $video->group->thumbnail[2]->attributes(); //get value for first thumbnail variable
- $thumbnail4 = $video->group->thumbnail[3]->attributes(); //get value for first thumbnail variable
-
- $temp_video_array[$this->video_object]['title'] = (string) $video->group->title;
- $temp_video_array[$this->video_object]['description'] = (string) $video->group->description;
- //keywords are a number of keywords in one string separated by commas
- $temp_video_array[$this->video_object]['keywords'] = (string) $video->group->keywords;
- //category is a single keyword
- $temp_video_array[$this->video_object]['category'] = (string) $video->group->category;
- //url is the actual url to the videos page
- $temp_video_array[$this->video_object]['url'] = (string) $player['url'];
- //urls to thethumbnail images
- $temp_video_array[$this->video_object]['thumbnail_url1'] = (string) $thumbnail1['url'];
- $temp_video_array[$this->video_object]['thumbnail_url2'] = (string) $thumbnail2['url'];
- $temp_video_array[$this->video_object]['thumbnail_url3'] = (string) $thumbnail3['url'];
- $temp_video_array[$this->video_object]['thumbnail_url4'] = (string) $thumbnail4['url'];
-
- //magically get the duration for video in seconds
- $yt = $video->children('http://gdata.youtube.com/schemas/2007');
- $duration = $yt->duration->attributes(); //extract from xml
- $temp_video_array[$this->video_object]['duration'] = (string) $duration['seconds'];
-
- //magically get the duration for video in seconds
- $video = $entry->children('http://gdata.youtube.com/schemas/2007');
- $views = $video->statistics->attributes();
-
- //if results were returned - removes php warning
- //number of times the video has been viewed
- if ($views['viewCount'] == '') {
- $temp_video_array[$this->video_object]['view_count'] = '0';
- } else {
- $temp_video_array[$this->video_object]['view_count'] = (string) $views['viewCount'];
- }
- //number of times the video has been set as favorite
- if ($views['favoriteCount'] == '') {
- $temp_video_array[$this->video_object]['favorite_count'] = '0';
- } else {
- $temp_video_array[$this->video_object]['favorite_count'] = (string) $views['favoriteCount'];
- }
- } else { //results were not returned
- $temp_video_array[$this->video_object]['view_count'] = '0';
- $temp_video_array[$this->video_object]['favorite_count'] = '0';
- }
- /*
- $video = array();
- $video = $entry->children('http://schemas.google.com/g/2005');
- $ratings = array();
- //$ratings = $video->rating->attributes();
- $rating = $video->rating;
-
- print_r($i);
- print_r($rating);
-
- if(!empty($video)){
- if(isset($rating)){
- if(!empty($rating)){
- if($ratings['min']==''){ $c[$this->video_object]['min_rating'] = '0'; }
- else{ $c[$this->video_object]['min_rating'] = (string) $ratings['min']; }
- if($ratings['max']==''){ $c[$this->video_object]['max_rating'] = '0'; }
- else{ $c[$this->video_object]['max_rating'] = (string) $ratings['max']; }
- if($ratings['average']==''){ $c[$this->video_object]['avg_rating'] = '0.00'; }
- else{ $c[$this->video_object]['avg_rating'] = (string) $ratings['average']; }
- if($ratings['numRaters']){ $c[$this->video_object]['num_raters'] = '0'; }
- else{ $c[$this->video_object]['num_raters'] = (string) $ratings['numRaters']; }
- } else {
- $c[$this->video_object]['min_rating'] = '0';
- $c[$this->video_object]['max_rating'] = '0';
- $c[$this->video_object]['avg_rating'] = '0.00';
- $c[$this->video_object]['num_raters'] = '0';
- }
- } else {
- $c[$this->video_object]['min_rating'] = '0';
- $c[$this->video_object]['max_rating'] = '0';
- $c[$this->video_object]['avg_rating'] = '0.00';
- $c[$this->video_object]['num_raters'] = '0';
- }
- }
-
- //check to see if there are any comments/any information about the comments
- //if there is get the feedlink and number of comments
- if ($video->comments->feedLink) {
- $comments = $video->comments->feedLink->attributes();
- //number of comments the video has
- $c[$this->video_object]['comments_count'] = (string) $comments['countHint'];
- }
- */
- $video_array[$i] = $temp_video_array;
- $i++;
- }
-
- //set the array to instance variable $user_videos
- return $video_array;
- }
- }
- ?>
Basically, I added the search videos by keywords and category functions. (not only by user, as it was originally)
And added some tweaks to adapt it to my needs.
- ///*
- function getVideosByKeywords($keywords, $start_index = 1 ,$max_results = 25, $order = 'ViewCount', $category = null)
- {
-
- $feedURL = 'http://gdata.youtube.com/feeds/api/videos?q=' . $keywords . '&start-index=' . $start_index . '&max-results=' . $max_results; //.'&v=2';
-
- //add category variable to feed url, if it exists
- if ($category != null) {
- $feedURL = $feedURL . '&category=' . $category;
- }
-
- //read the feed and place it in an simple_xml object
- //debug($xml);
-
- //get the next start index.
- if($xml->link[5]->attributes()->rel == 'next') $this->next_index = $start_index + $max_results;
- }
- if($xml->link[4]->attributes()->rel == 'next') $this->next_index = $start_index + $max_results;
- if($xml->link[4]->attributes()->rel == 'previous') $this->previous_index = $start_index - $max_results;
- //$this->next_index = (int) preg_replace('/&max-results=' . $max_results . '/', '', preg_replace('/http:\/\/gdata.youtube.com\/feeds\/api\/videos\?q=' . $keywords . '&start-index=/', '', (string) $xml->link[4]->attributes()->href));
- //http://gdata.youtube.com/feeds/api/videos?q=volontariato+ferrara&start-index=11&max-results=10
- } //if empty, then set $this->nex_index to -1 to let the user know they have reached the end of the list
- else {
- $this->next_index = -1;
- }
- //debug($this->next_index);debug($this->previous_index);
- $videos = $this->parse_multi_video($xml);
- }
-
- return $videos;
- }
Having this class in the vendors folder is half the work.
Let’s see the action and the popup view.
in our asset_controller.php
- function admin_youtubebrowser($opener_instance, $start_index = 1,$max_results = 10, $order = 'Relevance', $category = null) {
-
- $category = null;
-
- //gets named parameters like posted variables - useful for filtered pagination
- $this->_clean_params();
-
- if (isset($this->data['Asset']['keyword']) && $this->data['Asset']['keyword'] != '') $keyword = $this->data['Asset']['keyword']; else $keyword = 'volontariato';
- if (isset($this->data['Asset']['category']) && $this->data['Asset']['category'] != '') $category = $this->data['Asset']['category'];
- if (isset($this->data['Asset']['author']) && $this->data['Asset']['author'] != '') $youtube_user = $this->data['Asset']['author'];
- if (isset($this->data['Asset']['page']) && $this->data['Asset']['page'] != '') $page = $this->data['Asset']['page'];
- } else {
- $keyword = 'volontariato ferrara';
- }
-
- //set the ckeditor instance id
- $this->set('opener_instance', $opener_instance);
-
- //create php5tube object and get videos
- if(App::import("Vendor", "Php5tube", array('file' => '../vendors/Php5tube.php'))) { //, array('file' => '../vendors/Php5tube.php')
- $php5tube = new Php5tube('Video','User','Comment');
-
- //here we are
- if($keyword)
- $videos = $php5tube->getVideosByKeywords($keyword, $start_index, $max_results, $order, $category);
- else if ($user)
- $videos = $php5tube->getUserVideos($youtube_user, $category, $start_index, $max_results);
-
- $this->set('videos', $videos);
-
- //paginate returned videos- create urls for next and previous N videos request to youtube
- if($php5tube->next_index != -1) {
- $next_index = $php5tube->next_index;
- $this->set('next_index', $next_index);
- $opener_instance,
- $next_index,
- $max_results
- );
-
- $this->set('next_url', $url);
- }
-
- if($php5tube->previous_index != -1) {
- $previous_index = $php5tube->previous_index;
- $this->set('previous_index', $previous_index);
- $opener_instance,
- $previous_index,
- $max_results
- );
-
- $this->set('previous_url', $url);
- }
- }
- else {
- $this->Session->setFlash(__('Problem: cannot import Php5tube Vendor class',true));
- }
- $this->render('admin_youtubebrowser', 'basic');
-
- }
The actions gets the filters (keywords, category or author) from the form or url parameters (preserving filters in pagination); calls the appropriate method from the php5Tube class -retieving videos data from youtube-, and then sets the variables for the view (including urls for paginated requests to youtube)
Here is the _clean_params method:
- function _clean_params() {
-
- foreach ($this->params['named'] as $key => $value) {
-
- $this->params['named']['Asset'][$short_key] = $value;
- //unset($this->params['named'][$key] );
- } else {
- $this->params['named']['Asset'][$key] = $value;
- }
- //debug($this->params['named']);debug($this->params['named']);
- };
-
- else
- $this->data['Asset'] = $this->params['named']['Asset'];
- }
-
- }
-
- }
Then the view: the popup. like the image one in the previous article, prepares the code to be injected back to ckeditor.
The view, admin_youtubebrowser.ctp
- <script type="text/javascript">
- <!--
- function InsertHTML(passed)
- {
- var oEditor = opener.CKEDITOR.instances.<?php echo $opener_instance ?>;
- // Check the active editing mode.
- if ( oEditor.mode == 'wysiwyg' )
- {
- // Insert the desired HTML.
- oEditor.insertHtml( passed ) ;
- }
- else
- alert('<?php echo __('You must be on WYSIWYG mode!', true); ?>') ;
-
- window.close();
- }
- -->
- </script>
- <div class="pannello">
- <h2><?php __('Video');?></h2>
- <div class="art-Block">
- <div class="art-Block-body">
- <div class="art-BlockHeader">
- <div class="l"></div>
- <div class="r"></div>
- <div class="art-header-tag-icon">
- <div class="t"><?php
- echo __('Search youtube videos');
- ?></div>
- </div>
- </div><div class="art-BlockContent">
- <div class="art-BlockContent-body">
- <?php echo $form->create('Asset',
- , 'type' => 'post'));?>
-
- <table border="0" cellpadding="10">
- <tbody>
- <tr>
- <td>
- <?php echo $html->image('social_me/big/youtube.png', array('align' => 'absmiddle', 'hspace' => '2'));; ?>
- </td>
- <td valign="middle">
- <?php
- ?></td>
- <td valign="middle"><?php
- ?></td>
- <td valign="middle"><?php
- ?></td>
- </tr>
- </tbody>
- </table>
- <?php echo $form->end(); ?>
- </div>
- </div>
- </div>
- </div>
- <div style="clear:both"></div>
- <h2><?php
- </h2>
- <table cellpadding="0" cellspacing="0">
- foreach($videos as $video) { ?>
- <tr> <!--
- <td>
- <div style="padding:2px;"><img src="<?php //echo $video['Video']['thumbnail_url2']; ?>"></div>
-
- </td> -->
- <td>
- <div style="padding:10px;">
- <object width="280" height="170"><param name="movie" value="http://www.youtube.com/v/<?php echo $video['Video']['youtube_id']; ?>&hl=it_IT&fs=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/<?php echo $video['Video']['youtube_id']; ?>&hl=it_IT&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="280" height="170"></embed></object>
-
- </div>
- </td>
- <td>
- <div class="box_right">
- <?php
-
- // generated HTML to be embedded in CKeditor
- $insert_big = '<object width="640" height="505"><param name="movie" value="http://www.youtube.com/v/'.$video['Video']['youtube_id'].'&hl=it_IT&fs=1&color1=0x3a3a3a&color2=0x999999"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/'.$video['Video']['youtube_id'].'&hl=it_IT&fs=1&color1=0x3a3a3a&color2=0x999999" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="505"></embed></object>';
- $insert_small = '<object width="320" height="265"><param name="movie" value="http://www.youtube.com/v/'.$video['Video']['youtube_id'].'&hl=it_IT&fs=1&color1=0x3a3a3a&color2=0x999999"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/'.$video['Video']['youtube_id'].'&hl=it_IT&fs=1&color1=0x3a3a3a&color2=0x999999" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="320" height="265"></embed></object>';
-
- $insert_left =
- $html->div('imageleft',
- "<p>".
- $insert_small ."</p><p><em>".htmlentities($video['Video']['title'], ENT_QUOTES)."</em></p>",null,false). ' '
- ;
-
- $insert_right =
- $html->div('imageright',
- "<p>".
- $insert_small ."</p><p><em>".htmlentities($video['Video']['title'], ENT_QUOTES)."</em></p>",null,false). ' '
- ;
-
- $insert_center =
- $html->div('imagecenter',
- "<p>".$insert_big
- ."</p><p><em>".htmlentities($video['Video']['title'], ENT_QUOTES)."</em></p>",null,false). ' '
- ;
-
- echo $html->link($html->image('icons_big/Youtube_Embed_left.png',
- array('alt' => __('embed video',true), 'title' => __('embed video from youtube (small, left)', true), 'border' => 0, 'align' => 'absmiddle')),
- //'javascript:;',
- 'javascript:InsertHTML(\''. $insert_left .'\');',
- //'onclick' => "javascript:InsertHTML('". $insert_left ."'); return false;"
- ),
- null, false
- );
-
- echo $html->link($html->image('icons_big/Youtube_Embed_right.png',
- array('alt' => __('embed video',true), 'title' => __('embed video from youtube (small, right)', true), 'border' => 0, 'align' => 'absmiddle')),
- //'javascript:;',
- 'javascript:InsertHTML(\''. $insert_right .'\');',
- //'onclick' => "javascript:InsertHTML('". $insert_right ."'); return false;"
- ),
- null, false
- );
-
- echo '<div>';
- echo $html->link($html->image('icons_big/Youtube_Embed.png',
- array('alt' => __('embed video',true), 'title' => __('embed video from youtube (big)', true), 'border' => 0, 'align' => 'absmiddle')),
- //'javascript:;',
- 'javascript:InsertHTML(\''. $insert_center .'\');',
- //'onclick' => "javascript:InsertHTML('". $insert_center ."'); return false;"
- ),
- null, false
- );
- echo '</div>';
-
- ?>
- </div>
- <div style="font-size:16px;"><?php echo $video['Video']['title']; ?></div>
- <div><b><?php __('Category'); ?></b> = <?php echo $video['Video']['category']; ?> </div>
- <!-- <div><b>Tags</b> = <?php //echo $video['Video']['keywords']; ?> </div> -->
- <div><b><?php __('Description'); ?></b> = <?php echo $video['Video']['description']; ?></div>
- <div><b><?php __('Views'); ?></b> = <?php echo $video['Video']['view_count']; ?> times</div>
- <div><b><?php __('Marked as favorite'); ?></b> = <?php echo $video['Video']['favorite_count']; ?> times</div>
-
- </td>
-
- </tr>
- <?php }
- } ?>
- </table>
- <h2><?php
- </h2>
- </div>
Like we did for images, there is a choiche for different sizes and align for the video to be inserted.
With a few lines of code we have a functional browser (and search by keyword, category or author) of yuotube videos, with thumbnails and the chance to preview the actual video, integrated in our application.
It’s easy with cakephp.
–
This – extended form the first article- is the relevant excerpt of the view calling the popups:
- <?php
- $javascript->link('ckeditor/ckeditor', false);
- ?>
- [..]
- <?php
- ?>
-
- <!-- Show the image / links foropening the "browser" popups -->
- <h5>
- <?php
- . __(' Files ', true),
- 'javascript:;',
- 'controller' => 'assets',
- 'action'=>'filebrowser',
- $Type.'Summary' // or ContentWholeContent
- )
- )."','_blank', 'toolbar=0,scrollbars=1,location=0,status=1,menubar=0,resizable=1,width=800,height=680'); return false;"
- ),
- null, false
- );
-
- echo " | ";
- __('Images', true),
- 'javascript:;',
- 'controller' => 'assets',
- 'action'=>'imagebrowser',
- $Type.'Summary' // or ContentWholeContent
- )
- )."','_blank', 'toolbar=0,scrollbars=1,location=0,status=1,menubar=0,resizable=1,width=800,height=680'); return false;"
- ),
- null, false
- );
- echo " | ";
- __(' YT Video', true),
- 'javascript:;',
- 'controller' => 'assets',
- 'action'=>'youtubebrowser',
- $Type.'Summary' // or ContentWholeContent
- )
- )."','_blank', 'toolbar=0,scrollbars=1,location=0,status=1,menubar=0,resizable=1,width=800,height=680'); return false;"
- ),
- null, false
- );
-
- echo " | ";
- __(' Flickr (slides)', true),
- 'javascript:;',
- 'controller' => 'assets',
- 'action'=>'flickrbrowser',
- $Type.'Summary' // or ContentWholeContent
- )
- )."','_blank', 'toolbar=0,scrollbars=1,location=0,status=1,menubar=0,resizable=1,width=800,height=680'); return false;"
- ),
- null, false
- );
-
- ?>
- </h5>
- [..]
- <script type="text/javascript">
- var editor = CKEDITOR.replace( "<?php echo $Type ?>Summary" , {customConfig : "/js/ckeditor/alternate_config.js", height : "150px"});
- CKEDITOR.add
- CKEDITOR.config.contentsCss = '<?php echo $html->webroot('/js/ckeditor/mycontents.css') ?>' ;
- </script>
That’s all – please feel free to comment for info, doubts or anything.
Next (last) time, a flickr browser: search, browse and embed images or photoset slideshows form flickr.