From bd59fd55caede2a13ec00662152e044d2746522c Mon Sep 17 00:00:00 2001 From: Sem Date: Sun, 6 Nov 2011 20:06:26 +0100 Subject: Add/edit accion. --- actions/videolist/edit.php | 106 +++++++++++++++++++++++------------ lib/videolist.php | 135 +++++++++++++++++++++++++++++---------------- start.php | 9 +-- 3 files changed, 162 insertions(+), 88 deletions(-) diff --git a/actions/videolist/edit.php b/actions/videolist/edit.php index 3aa536e4c..2ca99c1c4 100644 --- a/actions/videolist/edit.php +++ b/actions/videolist/edit.php @@ -1,40 +1,76 @@ username); - exit; + +$variables = elgg_get_config('videolist'); +$input = array(); +foreach ($variables as $name => $type) { + $input[$name] = get_input($name); + if ($name == 'title') { + $input[$name] = strip_tags($input[$name]); + } + if ($type == 'tags') { + $input[$name] = string_to_tag_array($input[$name]); + } +} + +// Get guids +$videolist_guid = (int)get_input('videolist_guid'); +$container_guid = (int)get_input('container_guid'); + +elgg_make_sticky_form('videolist'); + +elgg_load_library('elgg:videolist'); + +if (!$input['video_url']) { + register_error(elgg_echo('videolist:error:no_url')); + forward(REFERER); +} + +$parsed_url = videolist_parseurl($input['video_url']); + +if(!$parsed) { + register_error(elgg_echo('videolist:error:invalid_url')); +} + +if ($video_guid) { + $video = get_entity($video_guid); + if (!$video || !$video->canEdit()) { + register_error(elgg_echo('videolist:error:no_save')); + forward(REFERER); + } + $new_video = false; +} else { + $video = new ElggObject(); + $video->subtype = 'videolist_item'; + $new_video = true; } - -$result = false; - -$container_guid = $video->container_guid; -$container = get_entity($container_guid); - -if ($video->canEdit()) { - - $video->access_id = $access_id; - $video->title = $title; - - // Save tags - $tags = explode(",", $tags); - $video->tags = $tags; - $result = $video->save(); + +$input = array_merge($input, videolist_get_data($parsed_url)); + +if (sizeof($input) > 0) { + foreach ($input as $name => $value) { + $video->$name = $value; + } +} + +$video->container_guid = $container_guid; + +if ($video->save()) { + + elgg_clear_sticky_form('videolist'); + + system_message(elgg_echo('videolist:saved')); + + if ($new_video) { + add_to_river('river/object/videolist_item/create', 'create', elgg_get_logged_in_user_guid(), $video->guid); + } + + forward($video->getURL()); +} else { + register_error(elgg_echo('videolist:error:no_save')); + forward(REFERER); } - -if ($result) - system_message(elgg_echo("videolist:editsaved")); -else - register_error(elgg_echo("videolist:editfailed")); - -forward($_SERVER['HTTP_REFERER']); diff --git a/lib/videolist.php b/lib/videolist.php index 50f83c923..a71315027 100644 --- a/lib/videolist.php +++ b/lib/videolist.php @@ -1,6 +1,10 @@ $domain, + 'videoid' => $hash, + ); } -function video_vimeo_parse_url($url) { - if (!preg_match('/(http:\/\/)([a-zA-Z]{2,3}\.)(vimeo\.com\/)(.*)/', $url, $matches)) { +function videolist_parseurl_vimeo($url) { + if (!preg_match('/(http:\/\/)([a-zA-Z]{2,3}\.)*(vimeo\.com\/)(.*)/', $url, $matches)) { return false; } $domain = $matches[2] . $matches[3]; - $path = $matches[4]; - - $hash = $matches[2]; + $hash = $matches[4]; - return $domain . '/' . $hash; + return array( + 'domain' => $domain, + 'videoid' => $hash, + ); } -function video_metacafe_parse_url($url) { +function videolist_parseurl_metacafe($url) { if (!preg_match('/(http:\/\/)([a-zA-Z]{2,3}\.)(metacafe\.com\/)(.*)/', $url, $matches)) { return false; } @@ -39,48 +48,76 @@ function video_metacafe_parse_url($url) { $hash = $matches[2]; - return $domain . '/' . $hash; + return array( + 'domain' => $domain, + 'videoid' => $hash, + ); } -if(isset($confirm_action) && ($confirm_action == 'add_video')) { - if(isset($title_videourl) && ($title_videourl != '')) { - if($Pagecontainer != "youtube" || $Pagecontainer != "vimeo" || $Pagecontainer != "metacafe"){ - if(preg_match("/youtube/i", $title_videourl)) { - $Pagecontainer = "youtube"; - } - - if(preg_match("/vimeo/i", $title_videourl)) { - $Pagecontainer = "vimeo"; - } - - if(preg_match("/metacafe/i", $title_videourl)) { - $Pagecontainer = "metacafe"; - } - } - if($Pagecontainer == "youtube") { - $is_valid_video = video_youtube_parse_url($title_videourl); - } else if($Pagecontainer == "vimeo") { - $is_valid_video = video_vimeo_parse_url($title_videourl); - $is_valid_video = $get_addvideourl; - } else if($Pagecontainer == "metacafe"){ - $is_valid_video = video_metacafe_parse_url($title_videourl); - $is_valid_video = $get_addvideourl; - } - - if($is_valid_video) { - $error['no-video'] = 1; - $_SESSION['candidate_profile_video'] = $is_valid_video; - $_SESSION['candidate_profile_video_access_id'] = $access_id; - $_SESSION['videolisttags'] = $tags; - $_SESSION['Pagecontainer'] = $Pagecontainer; - $_SESSION['container_guid'] = $container_guid; - $url = "action/videolist/add?__elgg_ts={$timestamp}&__elgg_token={$token}"; - forward($url); - } - else - $error['no-video'] = 0; +function videolist_parseurl($url){ + if ($parsed = videolist_parseurl_youtube($url)){ + $parsed['site'] = YOUTUBE; + return $parsed; + } elseif ($parsed = videolist_parseurl_vimeo($url)) { + $parsed['site'] = VIMEO; + return $parsed; + } elseif ($parsed = videolist_parseurl_metacafe($url)) { + $parsed['site'] = METACAFE; + return $parsed; + } else { + return array(); } - else { - $error['no-video'] = 0; +} + +function videolist_get_data($video_parsed_url) { + $site = $video_parsed_url['site']; + $videoid = $video_parsed_url['videoid']; + switch($site){ + case YOUTUBE: return videolist_get_data_youtube($videoid); + case VIMEO: return videolist_get_data_vimeo($videoid); + case METACAFE: return videolist_get_data_metacafe($videoid); + default: return array(); } } + + +function videolist_get_data_youtube($videoid){ + $buffer = file_get_contents('http://gdata.youtube.com/feeds/api/videos/'.$videoid); + $xml = new SimpleXMLElement($buffer); + + return array( + 'title' => sanitize_string($xml->title), + 'description' => sanitize_string($xml->content), + 'icon' => "http://img.youtube.com/vi/$videoid/default.jpg", + ); +} + +function videolist_get_data_vimeo($videoid){ + $buffer = file_get_contents("http://vimeo.com/api/v2/video/$videoid.xml"); + $xml = new SimpleXMLElement($buffer); + + $videos = $xml->children(); + $video = $videos[0]; + + return array( + 'title' => sanitize_string($video->title), + 'description' => sanitize_string($video->description), + 'icon' => sanitize_string($video->thumbnail_medium), + ); +} + +function videolist_get_data_metacafe($videoid){ //FIXME + $buffer = file_get_contents("http://www.metacafe.com/api/item/$videoid"); + $xml = new SimpleXMLElement($buffer); + + $children = $xml->children(); + $channel = $children[1]; + + preg_match('/]+src[\\s=\'"]+([^"\'>\\s]+)/is', $channel->description, $matches); + + return array( + 'title' => $channel->title, + 'description' => $channel->description, + 'icon' => $matches[1], + ); +} diff --git a/start.php b/start.php index 94a07a35c..c0ed1a0bf 100644 --- a/start.php +++ b/start.php @@ -75,10 +75,11 @@ function videolist_init() { elgg_register_plugin_hook_handler('entity:icon:url', 'user', 'profile_usericon_hook'); // Register actions - elgg_register_action("videolist/add", elgg_get_plugins_path() . "videolist/actions/add.php"); - elgg_register_action("videolist/edit", elgg_get_plugins_path() . "videolist/actions/edit.php"); - elgg_register_action("videolist/tubesearch", elgg_get_plugins_path() . "videolist/actions/tubesearch.php"); - elgg_register_action("videolist/delete", elgg_get_plugins_path() . "videolist/actions/delete.php"); + $actions_path = elgg_get_plugins_path() . "videolist/actions/videolist"; + elgg_register_action("videolist/add", "$actions_path/add.php"); + elgg_register_action("videolist/edit", "$actions_path/edit.php"); + elgg_register_action("videolist/tubesearch", "$actions_path/tubesearch.php"); + elgg_register_action("videolist/delete", "$actions_path/delete.php"); } /** -- cgit v1.2.3