From 161c979458c325d3f246542b2d958f1a3bb7579f Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 26 Aug 2010 21:14:13 -0500 Subject: [PATCH] Reformatting --- .../controllers/embedded_videos.php | 334 +++++++++--------- modules/embed_videos/helpers/embed_videos.php | 46 +-- .../helpers/embed_videos_event.php | 49 ++- .../helpers/embed_videos_installer.php | 46 ++- .../embed_videos/models/embedded_video.php | 18 + .../views/embed_videoplayer.html.php | 3 - modules/embed_videos/views/photo.html.php | 25 -- 7 files changed, 273 insertions(+), 248 deletions(-) delete mode 100644 modules/embed_videos/views/embed_videoplayer.html.php delete mode 100644 modules/embed_videos/views/photo.html.php diff --git a/modules/embed_videos/controllers/embedded_videos.php b/modules/embed_videos/controllers/embedded_videos.php index 39a39da1..4c5a4768 100644 --- a/modules/embed_videos/controllers/embedded_videos.php +++ b/modules/embed_videos/controllers/embedded_videos.php @@ -18,179 +18,179 @@ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ class Embedded_videos_Controller extends Controller { - public function show($movie) { - if (!is_object($movie)) { - // show() must be public because we route to it in url::parse_url(), so make - // sure that we're actually receiving an object - throw new Kohana_404_Exception(); + public function show($movie) { + if (!is_object($movie)) { + // show() must be public because we route to it in url::parse_url(), so make + // sure that we're actually receiving an object + throw new Kohana_404_Exception(); + } + access::required("view", $movie); + $where = array(array("type", "!=", "album")); + $position = $movie->parent()->get_position($movie, $where); + if ($position > 1) { + list($previous_item, $ignore, $next_item) = $movie->parent()->children(3, $position - 2, $where); + } else { + $previous_item = null; + list($next_item) = $movie->parent()->viewable()->children(1, $position, $where); + } + $embedded_video = ORM::factory("embedded_video")->where("item_id", "=", $movie->id)->find(); + //$db = Database::instance(); + //$result = $db->from('embedded_videos')->select('embed_code')->where('item_id',$movie->id)->get(); + $template = new Theme_View("page.html", "item", "embedded_video"); + $template->set_global("item", $movie); + $template->set_global("embedded_video", $embedded_video->embed_code); + $template->set_global("children", array()); + $template->set_global("children_count", 0); + $template->set_global("parents", $movie->parents()); + $template->set_global("next_item", $next_item); + $template->set_global("previous_item", $previous_item); + $template->set_global("sibling_count", $movie->parent()->viewable()->children_count($where)); + $template->set_global("position", $position); + $template->content = new View("embedded_video.html"); + db::query("UPDATE {items} SET `view_count` = `view_count` + 1 WHERE `id` = $movie->id")->execute(); + //$movie->view_count++; + //$movie->save(); + print $template; + } + public function update($movie_id) { + access::verify_csrf(); + $movie = ORM::factory("item", $movie_id); + access::required("view", $movie); + access::required("edit", $movie); + $form = embed_videos::get_edit_form($movie); + try { + $valid = $form->validate(); + $movie->title = $form->edit_item->title->value; + $movie->description = $form->edit_item->description->value; + $movie->slug = $form->edit_item->slug->value; + //$movie->name = $form->edit_item->inputs["name"]->value; + $movie->validate(); + } + catch(ORM_Validation_Exception $e) { + // Translate ORM validation errors into form error messages + foreach($e->validation->errors() as $key => $error) { + $form->edit_item->inputs[$key]->add_error($error, 1); + } + $valid = false; + } + if ($valid) { + $movie->save(); + module::event("item_edit_form_completed", $movie, $form); + log::success("content", "Updated embed", "url() }\">view"); + message::success(t("Saved embed %movie_title", array("movie_title" => $movie->title))); + if ($form->from_id->value == $movie->id) { + // Use the new url; it might have changed. + print json_encode(array("result" => "success", "location" => $movie->url())); + } else { + // Stay on the same page + print json_encode(array("result" => "success")); + } + } else { + print json_encode(array("result" => "error", "form" => (string)$form)); + } + } + public function create($id) { + $album = ORM::factory("item", $id); + access::required("view", $album); + access::required("add", $album); + access::verify_csrf(); + $form = embed_videos::get_add_form($album); + $temp_filename = ""; + //$form->add_rules('youtubeid', array('required', 'length[11]')); + //$form->add_callback('youtubeid', 'valid_youtubeid'); + batch::start(); + try { + $valid = $form->validate(); + if ($form->add_embedded_video->inputs['video_url']->value != "") { + $youtubeUrlPattern="youtube"; + $youtubeApiUrl="http://gdata.youtube.com/feeds/api/"; + $youtubeThumbnailUrl="http://img.youtube.com/vi/"; + $valid_url=false; + $embedded_video = ORM::factory("embedded_video"); + $item = ORM::factory("item"); + $item->type = "photo"; + $url = $form->add_embedded_video->inputs['video_url']->value; + if(preg_match("/$youtubeUrlPattern/",$url)) { + if(preg_match("/watch\?v=(.*?)(&\S+=\S+)/",$url,$matches)) { + $video_id = $matches[1]; + $embedded_video->embed_code = ''; + $embedded_video->source = "YouTube"; + $content = file_get_contents("http://img.youtube.com/vi/" . $video_id . "/0.jpg"); + $itemname = "youtube_" . $video_id . ".jpg"; + $temp_filename = VARPATH . "tmp/$itemname"; + if ($content) { + $valid_url = true; + } + } } - access::required("view", $movie); - $where = array(array("type", "!=", "album")); - $position = $movie->parent()->get_position($movie, $where); - if ($position > 1) { - list($previous_item, $ignore, $next_item) = $movie->parent()->children(3, $position - 2, $where); + //$item->validate(); + //$content = file_get_contents("http://img.youtube.com/vi/" . $form->add_embedded_video->inputs['name']->value . "/0.jpg"); + if ($valid_url) { + $file = fopen($temp_filename, "wb"); + fwrite($file, $content); + fclose($file); + gallery_graphics::composite($temp_filename, $temp_filename, array("file" => "modules/embed_videos/images/embed_video_icon.png", "position" => "center", "transparency" => 95)); + $item->set_data_file($temp_filename); + $item->name = basename($itemname); + $item->title = $form->add_embedded_video->title->value; + $item->parent_id = $album->id; + $item->description = $form->add_embedded_video->description->value; + $item->slug = $form->add_embedded_video->slug->value; + $path_info = @pathinfo($temp_filename); + $item->save(); + db::query("UPDATE {items} SET `type` = 'embedded_video' WHERE `id` = $item->id")->execute(); + $embedded_video->item_id = $item->id; + $embedded_video->validate(); + $embedded_video->save(); + log::success("content", t("Added a embedded video"), html::anchor("embeds/$item->id", t("view video"))); + module::event("add_event_form_completed", $item, $form); } else { - $previous_item = null; - list($next_item) = $movie->parent()->viewable()->children(1, $position, $where); - } - $embedded_video = ORM::factory("embedded_video")->where("item_id", "=", $movie->id)->find(); - //$db = Database::instance(); - //$result = $db->from('embedded_videos')->select('embed_code')->where('item_id',$movie->id)->get(); - $template = new Theme_View("page.html", "item", "embedded_video"); - $template->set_global("item", $movie); - $template->set_global("embedded_video", $embedded_video->embed_code); - $template->set_global("children", array()); - $template->set_global("children_count", 0); - $template->set_global("parents", $movie->parents()); - $template->set_global("next_item", $next_item); - $template->set_global("previous_item", $previous_item); - $template->set_global("sibling_count", $movie->parent()->viewable()->children_count($where)); - $template->set_global("position", $position); - $template->content = new View("embedded_video.html"); - db::query("UPDATE {items} SET `view_count` = `view_count` + 1 WHERE `id` = $movie->id")->execute(); - //$movie->view_count++; - //$movie->save(); - print $template; - } - public function update($movie_id) { - access::verify_csrf(); - $movie = ORM::factory("item", $movie_id); - access::required("view", $movie); - access::required("edit", $movie); - $form = embed_videos::get_edit_form($movie); - try { - $valid = $form->validate(); - $movie->title = $form->edit_item->title->value; - $movie->description = $form->edit_item->description->value; - $movie->slug = $form->edit_item->slug->value; - //$movie->name = $form->edit_item->inputs["name"]->value; - $movie->validate(); - } - catch(ORM_Validation_Exception $e) { - // Translate ORM validation errors into form error messages - foreach($e->validation->errors() as $key => $error) { - $form->edit_item->inputs[$key]->add_error($error, 1); - } - $valid = false; - } - if ($valid) { - $movie->save(); - module::event("item_edit_form_completed", $movie, $form); - log::success("content", "Updated embed", "url() }\">view"); - message::success(t("Saved embed %movie_title", array("movie_title" => $movie->title))); - if ($form->from_id->value == $movie->id) { - // Use the new url; it might have changed. - print json_encode(array("result" => "success", "location" => $movie->url())); - } else { - // Stay on the same page - print json_encode(array("result" => "success")); - } - } else { - print json_encode(array("result" => "error", "form" => (string)$form)); + $form->add_embedded_video->inputs['video_url']->add_error('invalid_id', 1); + $valid = false; } + } else { + $form->add_embedded_video->inputs['video_url']->add_error('invalid_id', 1); + $valid = false; + } } - public function create($id) { - $album = ORM::factory("item", $id); - access::required("view", $album); - access::required("add", $album); - access::verify_csrf(); - $form = embed_videos::get_add_form($album); - $temp_filename = ""; - //$form->add_rules('youtubeid', array('required', 'length[11]')); - //$form->add_callback('youtubeid', 'valid_youtubeid'); - batch::start(); - try { - $valid = $form->validate(); - if ($form->add_embedded_video->inputs['video_url']->value != "") { - $youtubeUrlPattern="youtube"; - $youtubeApiUrl="http://gdata.youtube.com/feeds/api/"; - $youtubeThumbnailUrl="http://img.youtube.com/vi/"; - $valid_url=false; - $embedded_video = ORM::factory("embedded_video"); - $item = ORM::factory("item"); - $item->type = "photo"; - $url = $form->add_embedded_video->inputs['video_url']->value; - if(preg_match("/$youtubeUrlPattern/",$url)) { - if(preg_match("/watch\?v=(.*?)(&\S+=\S+)/",$url,$matches)) { - $video_id = $matches[1]; - $embedded_video->embed_code = ''; - $embedded_video->source = "YouTube"; - $content = file_get_contents("http://img.youtube.com/vi/" . $video_id . "/0.jpg"); - $itemname = "youtube_" . $video_id . ".jpg"; - $temp_filename = VARPATH . "tmp/$itemname"; - if ($content) { - $valid_url = true; - } - } - } - //$item->validate(); - //$content = file_get_contents("http://img.youtube.com/vi/" . $form->add_embedded_video->inputs['name']->value . "/0.jpg"); - if ($valid_url) { - $file = fopen($temp_filename, "wb"); - fwrite($file, $content); - fclose($file); - gallery_graphics::composite($temp_filename, $temp_filename, array("file" => "modules/embed_videos/images/embed_video_icon.png", "position" => "center", "transparency" => 95)); - $item->set_data_file($temp_filename); - $item->name = basename($itemname); - $item->title = $form->add_embedded_video->title->value; - $item->parent_id = $album->id; - $item->description = $form->add_embedded_video->description->value; - $item->slug = $form->add_embedded_video->slug->value; - $path_info = @pathinfo($temp_filename); - $item->save(); - db::query("UPDATE {items} SET `type` = 'embedded_video' WHERE `id` = $item->id")->execute(); - $embedded_video->item_id = $item->id; - $embedded_video->validate(); - $embedded_video->save(); - log::success("content", t("Added a embedded video"), html::anchor("embeds/$item->id", t("view video"))); - module::event("add_event_form_completed", $item, $form); - } else { - $form->add_embedded_video->inputs['video_url']->add_error('invalid_id', 1); - $valid = false; - } - } else { - $form->add_embedded_video->inputs['video_url']->add_error('invalid_id', 1); - $valid = false; - } - } - catch(Exception $e) { - // Lame error handling for now. Just record the exception and move on - Kohana_Log::add("error", $e->getMessage() . "\n" . $e->getTraceAsString()); - // Ugh. I hate to use instanceof, But this beats catching the exception separately since - // we mostly want to treat it the same way as all other exceptions - if ($e instanceof ORM_Validation_Exception) { - Kohana_Log::add("error", "Validation errors: " . print_r($e->validation->errors(), 1)); - foreach($e->validation->errors() as $key => $error) { - $form->add_embed->inputs[$key]->add_error($error, 1); - } - $valid = false; - } - if (file_exists($temp_filename)) { - unlink($temp_filename); - } - } - if (file_exists($temp_filename)) { - unlink($temp_filename); - } - batch::stop(); - if ($valid) { - //print json_encode(array("result" => "success")); - json::reply(array("result" => "success", "location" => $item->url())); - } else { - //json::reply(array("result" => "error", "form" => (string)$form)); - print $form; + catch(Exception $e) { + // Lame error handling for now. Just record the exception and move on + Kohana_Log::add("error", $e->getMessage() . "\n" . $e->getTraceAsString()); + // Ugh. I hate to use instanceof, But this beats catching the exception separately since + // we mostly want to treat it the same way as all other exceptions + if ($e instanceof ORM_Validation_Exception) { + Kohana_Log::add("error", "Validation errors: " . print_r($e->validation->errors(), 1)); + foreach($e->validation->errors() as $key => $error) { + $form->add_embed->inputs[$key]->add_error($error, 1); } + $valid = false; + } + if (file_exists($temp_filename)) { + unlink($temp_filename); + } } - public function form_add($album_id) { - $album = ORM::factory("item", $album_id); - access::required("view", $album); - access::required("add", $album); - print embed_videos::get_add_form($album); + if (file_exists($temp_filename)) { + unlink($temp_filename); } - public function form_edit($id) { - $embed = ORM::factory("item", $id); - access::required("view", $embed); - access::required("edit", $embed); - print embed_videos::get_edit_form($embed); + batch::stop(); + if ($valid) { + //print json_encode(array("result" => "success")); + json::reply(array("result" => "success", "location" => $item->url())); + } else { + //json::reply(array("result" => "error", "form" => (string)$form)); + print $form; } + } + public function form_add($album_id) { + $album = ORM::factory("item", $album_id); + access::required("view", $album); + access::required("add", $album); + print embed_videos::get_add_form($album); + } + public function form_edit($id) { + $embed = ORM::factory("item", $id); + access::required("view", $embed); + access::required("edit", $embed); + print embed_videos::get_edit_form($embed); + } } diff --git a/modules/embed_videos/helpers/embed_videos.php b/modules/embed_videos/helpers/embed_videos.php index b46d4d73..edf6b875 100644 --- a/modules/embed_videos/helpers/embed_videos.php +++ b/modules/embed_videos/helpers/embed_videos.php @@ -19,34 +19,34 @@ */ /** - * This is the API for handling photos. + * This is the API for handling embedded videos. * * Note: by design, this class does not do any permission checking. */ class embed_videos_Core { - + static function get_add_form($album) { $form = new Forge("embedded_videos/create/{$album->id}", "", "post", array("id" => "g-add-embed-form")); $group = $form->group("add_embedded_video") - ->label(t("Add embedded video to %album_title", array("album_title" => $album->title))); + ->label(t("Add embedded video to %album_title", array("album_title" => $album->title))); $group->input("title")->label(t("Title")) - ->error_messages("required", t("You must provide a title")) - ->error_messages("length", t("Your title is too long")); + ->error_messages("required", t("You must provide a title")) + ->error_messages("length", t("Your title is too long")); $group->input("video_url")->label(t("Video URL")) - ->error_messages( + ->error_messages( "conflict", t("There is already a movie with this ID")) - ->error_messages("required", t("You must provide a Youtube ID")) - ->error_messages("length", t("Invalid Youtube ID")) - ->error_messages("invalid_id", t("Invalid Youtube ID")); + ->error_messages("required", t("You must provide a Youtube ID")) + ->error_messages("length", t("Invalid Youtube ID")) + ->error_messages("invalid_id", t("Invalid Youtube ID")); $group->textarea("description")->label(t("Description")); $group->input("slug")->label(t("Internet Address")) - ->error_messages( + ->error_messages( "conflict", t("There is already a movie, photo or album with this internet address")) - ->error_messages( + ->error_messages( "not_url_safe", - t("The internet address should contain only letters, numbers, hyphens and underscores")) - ->error_messages("required", t("You must provide an internet address")) - ->error_messages("length", t("Your internet address is too long")); + t("The internet address should contain only letters, numbers, hyphens and underscores")) + ->error_messages("required", t("You must provide an internet address")) + ->error_messages("length", t("Your internet address is too long")); module::event("item_add_form", $album, $form); @@ -55,23 +55,23 @@ class embed_videos_Core { return $form; } - - static function get_edit_form($photo) { + + static function get_edit_form($photo) { $form = new Forge("embedded_videos/update/$photo->id", "", "post", array("id" => "g-edit-embed-form")); $form->hidden("from_id")->value($photo->id); $group = $form->group("edit_item")->label(t("Edit Embedded Video")); $group->input("title")->label(t("Title"))->value($photo->title) - ->error_messages("required", t("You must provide a title")) - ->error_messages("length", t("Your title is too long")); + ->error_messages("required", t("You must provide a title")) + ->error_messages("length", t("Your title is too long")); $group->textarea("description")->label(t("Description"))->value($photo->description); $group->input("slug")->label(t("Internet Address"))->value($photo->slug) - ->error_messages( + ->error_messages( "conflict", t("There is already a movie, photo or album with this internet address")) - ->error_messages( + ->error_messages( "not_url_safe", - t("The internet address should contain only letters, numbers, hyphens and underscores")) - ->error_messages("required", t("You must provide an internet address")) - ->error_messages("length", t("Your internet address is too long")); + t("The internet address should contain only letters, numbers, hyphens and underscores")) + ->error_messages("required", t("You must provide an internet address")) + ->error_messages("length", t("Your internet address is too long")); module::event("item_edit_form", $photo, $form); diff --git a/modules/embed_videos/helpers/embed_videos_event.php b/modules/embed_videos/helpers/embed_videos_event.php index 8cc83827..30a946f5 100644 --- a/modules/embed_videos/helpers/embed_videos_event.php +++ b/modules/embed_videos/helpers/embed_videos_event.php @@ -1,9 +1,26 @@ type == "embedded_video") { // Build our thumbnail/resizes. @@ -11,8 +28,8 @@ class embed_videos_event_Core { graphics::generate($item); } catch (Exception $e) { log::error("graphics", t("Couldn't create a thumbnail or resize for %item_title", - array("item_title" => $item->title)), - html::anchor($item->abs_url(), t("details"))); + array("item_title" => $item->title)), + html::anchor($item->abs_url(), t("details"))); Kohana_Log::add("error", $e->getMessage() . "\n" . $e->getTraceAsString()); } @@ -23,21 +40,21 @@ class embed_videos_event_Core { } } } -static function item_deleted($item) { - ORM::factory("embedded_video") - ->where("item_id", "=", $item->id) - ->find() - ->delete(); -} - static function site_menu($menu, $theme) { + static function item_deleted($item) { + ORM::factory("embedded_video") + ->where("item_id", "=", $item->id) + ->find() + ->delete(); + } + static function site_menu($menu, $theme) { $item = $theme->item(); if ($can_add = $item && access::can("add", $item)) { $menu->get("add_menu") - ->append(Menu::factory("dialog") - ->id("embed_add") - ->label(t("Embed Video")) - ->url(url::site("form/add/embedded_videos/$item->id"))); + ->append(Menu::factory("dialog") + ->id("embed_add") + ->label(t("Embed Video")) + ->url(url::site("form/add/embedded_videos/$item->id"))); } } } diff --git a/modules/embed_videos/helpers/embed_videos_installer.php b/modules/embed_videos/helpers/embed_videos_installer.php index 2c3c847a..dd69489a 100644 --- a/modules/embed_videos/helpers/embed_videos_installer.php +++ b/modules/embed_videos/helpers/embed_videos_installer.php @@ -1,9 +1,27 @@ query("CREATE TABLE {embedded_videos} ( + static function install() { + $db = Database::instance(); + $db->query("CREATE TABLE {embedded_videos} ( `id` int(9) NOT NULL auto_increment, `embed_code` varchar(2048) DEFAULT NULL, `source` varchar(64) DEFAULT NULL, @@ -11,16 +29,16 @@ class embed_videos_installer { PRIMARY KEY (`id`), KEY (`item_id`, `id`)) DEFAULT CHARSET=utf8;"); - module::set_version("embed_videos", 2); - //exec("cd modules/gallery/controllers/; ln -s ../../embed/controllers/embeds.php embeds.php"); - } - - static function deactivate() { + module::set_version("embed_videos", 2); + //exec("cd modules/gallery/controllers/; ln -s ../../embed/controllers/embeds.php embeds.php"); + } - } - static function uninstall() { - $db = Database::instance(); - $db->query("DROP TABLE IF EXISTS {embedded_videos};"); - module::delete("embed_videos"); - } + static function deactivate() { + + } + static function uninstall() { + $db = Database::instance(); + $db->query("DROP TABLE IF EXISTS {embedded_videos};"); + module::delete("embed_videos"); + } } diff --git a/modules/embed_videos/models/embedded_video.php b/modules/embed_videos/models/embedded_video.php index 6c849d72..6ea97e8f 100644 --- a/modules/embed_videos/models/embedded_video.php +++ b/modules/embed_videos/models/embedded_video.php @@ -1,4 +1,22 @@ - diff --git a/modules/embed_videos/views/photo.html.php b/modules/embed_videos/views/photo.html.php deleted file mode 100644 index 8d96ebdc..00000000 --- a/modules/embed_videos/views/photo.html.php +++ /dev/null @@ -1,25 +0,0 @@ - -where('item_id', $item->id)->find(); - if ($embedded_video->loaded()) { -?> -
- photo_top() ?> - - paginator() ?> - -
- resize_top($item) ?> - embed_code ?> - resize_bottom($item) ?> -
- -
-

title) ?>

-
description)) ?>
-
- - photo_bottom() ?> -
-name . 'views/photo.html.php'); - } ?> \ No newline at end of file