From 5b84919d6e55a8c43f60cf8b695c3696340d001b Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Sun, 24 Jan 2010 14:21:57 -0800 Subject: First pass at converting server add to model based validation. --- modules/server_add/controllers/server_add.php | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) (limited to 'modules/server_add/controllers') diff --git a/modules/server_add/controllers/server_add.php b/modules/server_add/controllers/server_add.php index d7572b52..d93b63a3 100644 --- a/modules/server_add/controllers/server_add.php +++ b/modules/server_add/controllers/server_add.php @@ -226,16 +226,33 @@ class Server_Add_Controller extends Admin_Controller { $name = basename($entry->file); $title = item::convert_filename_to_title($name); if (is_dir($entry->file)) { - $album = album::create($parent, $name, $title, null, $owner_id); + $album = ORM::factory("item"); + $album->type = "album"; + $album->name = $name; + $album->title = $title; + $album->owner_id = $owner_id; + $album->save(); $entry->item_id = $album->id; } else { try { $extension = strtolower(pathinfo($name, PATHINFO_EXTENSION)); if (in_array($extension, array("gif", "png", "jpg", "jpeg"))) { - $photo = photo::create($parent, $entry->file, $name, $title, null, $owner_id); + $photo = ORM::factory("item"); + $photo->type = "photo"; + $photo->set_data_file($entry->file); + $photo->name = $name; + $photo->title = $title; + $photo->owner_id = $owner_id; + $photo->save(); $entry->item_id = $photo->id; } else if (in_array($extension, array("flv", "mp4"))) { - $movie = movie::create($parent, $entry->file, $name, $title, null, $owner_id); + $movie = ORM::factory("item"); + $movie->type = "movie"; + $movie->set_data_file($entry->file); + $movie->name = $name; + $movie->title = $title; + $movie->owner_id = $owner_id; + $movie->save(); $entry->item_id = $movie->id; } else { // This should never happen, because we don't add stuff to the list that we can't -- cgit v1.2.3 From 7096c2912460d2a92e71f2df0effe29ac37faf12 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Sat, 23 Jan 2010 17:07:09 -0800 Subject: Switch to model based validation. --- modules/server_add/controllers/server_add.php | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) (limited to 'modules/server_add/controllers') diff --git a/modules/server_add/controllers/server_add.php b/modules/server_add/controllers/server_add.php index d93b63a3..4d6d5dfe 100644 --- a/modules/server_add/controllers/server_add.php +++ b/modules/server_add/controllers/server_add.php @@ -24,7 +24,7 @@ class Server_Add_Controller extends Admin_Controller { $files[] = $path; } - $item = ORM::factory("item", $id); + $item = ORM::factory("item")->where("id", "=", $id)->find(); $view = new View("server_add_tree_dialog.html"); $view->item = $item; $view->tree = new View("server_add_tree.html"); @@ -78,7 +78,7 @@ class Server_Add_Controller extends Admin_Controller { */ public function start() { access::verify_csrf(); - $item = ORM::factory("item", Input::instance()->get("item_id")); + $item = ORM::factory("item")->where("id", "=", Input::instance()->get("item_id"))->find(); foreach (Input::instance()->post("paths") as $path) { if (server_add::is_valid_path($path)) { @@ -104,7 +104,7 @@ class Server_Add_Controller extends Admin_Controller { function run($task_id) { access::verify_csrf(); - $task = ORM::factory("task", $task_id); + $task = ORM::factory("task")->where("id", "=", $task_id)->find(); if (!$task->loaded() || $task->owner_id != identity::active_user()->id) { access::forbidden(); } @@ -216,11 +216,12 @@ class Server_Add_Controller extends Admin_Controller { // Look up the parent item for this entry. By now it should exist, but if none was // specified, then this belongs as a child of the current item. - $parent_entry = ORM::factory("server_add_file", $entry->parent_id); + $parent_entry = + ORM::factory("server_add_file")->where("id", "=", $entry->parent_id)->find(); if (!$parent_entry->loaded()) { - $parent = ORM::factory("item", $task->get("item_id")); + $parent = ORM::factory("item")->where("id", "=", $task->get("item_id"))->find(); } else { - $parent = ORM::factory("item", $parent_entry->item_id); + $parent = ORM::factory("item")->where("id", "=", $parent_entry->item_id)->find(); } $name = basename($entry->file); @@ -228,6 +229,7 @@ class Server_Add_Controller extends Admin_Controller { if (is_dir($entry->file)) { $album = ORM::factory("item"); $album->type = "album"; + $album->parent_id = $parent->id; $album->name = $name; $album->title = $title; $album->owner_id = $owner_id; @@ -239,6 +241,7 @@ class Server_Add_Controller extends Admin_Controller { if (in_array($extension, array("gif", "png", "jpg", "jpeg"))) { $photo = ORM::factory("item"); $photo->type = "photo"; + $photo->parent_id = $parent->id; $photo->set_data_file($entry->file); $photo->name = $name; $photo->title = $title; @@ -248,6 +251,7 @@ class Server_Add_Controller extends Admin_Controller { } else if (in_array($extension, array("flv", "mp4"))) { $movie = ORM::factory("item"); $movie->type = "movie"; + $movie->parent_id = $parent->id; $movie->set_data_file($entry->file); $movie->name = $name; $movie->title = $title; -- cgit v1.2.3 From 4b32a71afc7650fe7bdd02ba384c8914f60538f3 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Wed, 27 Jan 2010 22:34:11 -0800 Subject: Convert back to using ORM::factory(..., $id) instead of calling where(). --- modules/comment/models/comment.php | 2 +- modules/comment/tests/Comment_Event_Test.php | 2 +- modules/g2_import/controllers/g2.php | 2 +- modules/g2_import/helpers/g2_import.php | 11 +++++------ modules/gallery/helpers/gallery_installer.php | 2 +- modules/gallery/helpers/item_rest.php | 2 +- modules/gallery/libraries/ORM_MPTT.php | 2 +- modules/gallery/models/item.php | 4 ++-- modules/server_add/controllers/server_add.php | 13 ++++++------- modules/tag/helpers/item_tags_rest.php | 2 +- modules/tag/helpers/tag_item_rest.php | 4 ++-- modules/tag/helpers/tag_items_rest.php | 2 +- modules/tag/helpers/tag_rest.php | 2 +- modules/user/models/group.php | 2 +- modules/user/models/user.php | 2 +- 15 files changed, 26 insertions(+), 28 deletions(-) (limited to 'modules/server_add/controllers') diff --git a/modules/comment/models/comment.php b/modules/comment/models/comment.php index 43c4148f..8be022b5 100644 --- a/modules/comment/models/comment.php +++ b/modules/comment/models/comment.php @@ -108,7 +108,7 @@ class Comment_Model extends ORM { module::event("comment_created", $this); } else { // Updated comment - $original = ORM::factory("comment")->where("id", "=", $this->id)->find(); + $original = ORM::factory("comment", $this->id); $visible_change = $original->state == "published" || $this->state == "published"; parent::save(); module::event("comment_updated", $original, $this); diff --git a/modules/comment/tests/Comment_Event_Test.php b/modules/comment/tests/Comment_Event_Test.php index 27272055..08f55b3f 100644 --- a/modules/comment/tests/Comment_Event_Test.php +++ b/modules/comment/tests/Comment_Event_Test.php @@ -30,6 +30,6 @@ class Comment_Event_Test extends Gallery_Unit_Test_Case { $album->delete(); - $this->assert_false(ORM::factory("comment")->where("id", "=", $comment->id)->find()->loaded()); + $this->assert_false(ORM::factory("comment", $comment->id)->loaded()); } } diff --git a/modules/g2_import/controllers/g2.php b/modules/g2_import/controllers/g2.php index 3e002758..5fd4400c 100644 --- a/modules/g2_import/controllers/g2.php +++ b/modules/g2_import/controllers/g2.php @@ -50,7 +50,7 @@ class G2_Controller extends Admin_Controller { throw new Kohana_404_Exception(); } - $item = ORM::factory("item")->where("id", "=", $g2_map->g3_id)->find(); + $item = ORM::factory("item", $g2_map->g3_id); if (!$item->loaded() || !access::can("view", $item)) { throw new Kohana_404_Exception(); } diff --git a/modules/g2_import/helpers/g2_import.php b/modules/g2_import/helpers/g2_import.php index 74164305..fa95e547 100644 --- a/modules/g2_import/helpers/g2_import.php +++ b/modules/g2_import/helpers/g2_import.php @@ -358,8 +358,7 @@ class g2_import_Core { if ($g2_album->getParentId() == null) { return t("Skipping Gallery 2 root album"); } - $parent_album = - ORM::factory("item")->where("id", "=", self::map($g2_album->getParentId()))->find(); + $parent_album = ORM::factory("item", self::map($g2_album->getParentId())); $album = ORM::factory("item"); $album->type = "album"; @@ -423,8 +422,8 @@ class g2_import_Core { } $item_id = self::map($g2_source->getId()); if ($item_id) { - $item = ORM::factory("item")->where("id", "=", $item_id)->find(); - $g2_album = ORM::factory("item")->where("id", "=", $g3_album_id)->find(); + $item = ORM::factory("item", $item_id); + $g2_album = ORM::factory("item", $g3_album_id); $g2_album->album_cover_item_id = $item->id; $g2_album->thumb_dirty = 1; $g2_album->view_count = g2(GalleryCoreApi::fetchItemViewCount($g2_album_id)); @@ -452,7 +451,7 @@ class g2_import_Core { array("id" => $g2_item_id, "exception" => (string)$e)); } - $parent = ORM::factory("item")->where("id", "=", self::map($g2_item->getParentId()))->find(); + $parent = ORM::factory("item", self::map($g2_item->getParentId())); $g2_type = $g2_item->getEntityType(); $corrupt = 0; @@ -633,7 +632,7 @@ class g2_import_Core { GalleryCoreApi::requireOnce("modules/tags/classes/TagsHelper.class"); $g2_item_id = array_shift($queue); - $g3_item = ORM::factory("item")->where("id", "=", self::map($g2_item_id))->find(); + $g3_item = ORM::factory("item", self::map($g2_item_id)); if (!$g3_item->loaded()) { return; } diff --git a/modules/gallery/helpers/gallery_installer.php b/modules/gallery/helpers/gallery_installer.php index aa297236..bfab4645 100644 --- a/modules/gallery/helpers/gallery_installer.php +++ b/modules/gallery/helpers/gallery_installer.php @@ -228,7 +228,7 @@ class gallery_installer { "updated" => $now, "weight" => 1)) ->execute(); - $root = ORM::factory("item")->where("id", "=", 1)->find(); + $root = ORM::factory("item", 1); access::add_item($root); module::set_var("gallery", "active_site_theme", "wind"); diff --git a/modules/gallery/helpers/item_rest.php b/modules/gallery/helpers/item_rest.php index 2236fbbb..d5ca1456 100644 --- a/modules/gallery/helpers/item_rest.php +++ b/modules/gallery/helpers/item_rest.php @@ -145,7 +145,7 @@ class item_rest_Core { } static function resolve($id) { - $item = ORM::factory("item")->where("id", "=", $id)->find(); + $item = ORM::factory("item", $id); if (!access::can("view", $item)) { throw new Kohana_404_Exception(); } diff --git a/modules/gallery/libraries/ORM_MPTT.php b/modules/gallery/libraries/ORM_MPTT.php index a7bb24ea..83f9b51e 100644 --- a/modules/gallery/libraries/ORM_MPTT.php +++ b/modules/gallery/libraries/ORM_MPTT.php @@ -48,7 +48,7 @@ class ORM_MPTT_Core extends ORM { function save() { if (!$this->loaded()) { $this->lock(); - $parent = ORM::factory("item")->where("id", "=", $this->parent_id)->find(); + $parent = ORM::factory("item", $this->parent_id); try { // Make a hole in the parent for this new item diff --git a/modules/gallery/models/item.php b/modules/gallery/models/item.php index 9706d61f..ae1b6608 100644 --- a/modules/gallery/models/item.php +++ b/modules/gallery/models/item.php @@ -423,7 +423,7 @@ class Item_Model extends ORM_MPTT { // If any significant fields have changed, load up a copy of the original item and // keep it around. - $original = ORM::factory("item")->where("id", "=", $this->id)->find(); + $original = ORM::factory("item", $this->id); if (array_intersect($this->changed, array("parent_id", "name", "slug"))) { $original->_build_relative_caches(); $this->relative_path_cache = null; @@ -787,7 +787,7 @@ class Item_Model extends ORM_MPTT { if ($this->is_movie() || $this->is_photo()) { if ($this->loaded()) { // Existing items can't change their extension - $original = ORM::factory("item")->where("id", "=", $this->id)->find(); + $original = ORM::factory("item", $this->id); $new_ext = pathinfo($this->name, PATHINFO_EXTENSION); $old_ext = pathinfo($original->name, PATHINFO_EXTENSION); if (strcasecmp($new_ext, $old_ext)) { diff --git a/modules/server_add/controllers/server_add.php b/modules/server_add/controllers/server_add.php index 4d6d5dfe..287855b6 100644 --- a/modules/server_add/controllers/server_add.php +++ b/modules/server_add/controllers/server_add.php @@ -24,7 +24,7 @@ class Server_Add_Controller extends Admin_Controller { $files[] = $path; } - $item = ORM::factory("item")->where("id", "=", $id)->find(); + $item = ORM::factory("item", $id); $view = new View("server_add_tree_dialog.html"); $view->item = $item; $view->tree = new View("server_add_tree.html"); @@ -78,7 +78,7 @@ class Server_Add_Controller extends Admin_Controller { */ public function start() { access::verify_csrf(); - $item = ORM::factory("item")->where("id", "=", Input::instance()->get("item_id"))->find(); + $item = ORM::factory("item", Input::instance()->get("item_id")); foreach (Input::instance()->post("paths") as $path) { if (server_add::is_valid_path($path)) { @@ -104,7 +104,7 @@ class Server_Add_Controller extends Admin_Controller { function run($task_id) { access::verify_csrf(); - $task = ORM::factory("task")->where("id", "=", $task_id)->find(); + $task = ORM::factory("task", $task_id); if (!$task->loaded() || $task->owner_id != identity::active_user()->id) { access::forbidden(); } @@ -216,12 +216,11 @@ class Server_Add_Controller extends Admin_Controller { // Look up the parent item for this entry. By now it should exist, but if none was // specified, then this belongs as a child of the current item. - $parent_entry = - ORM::factory("server_add_file")->where("id", "=", $entry->parent_id)->find(); + $parent_entry = ORM::factory("server_add_file", $entry->parent_id); if (!$parent_entry->loaded()) { - $parent = ORM::factory("item")->where("id", "=", $task->get("item_id"))->find(); + $parent = ORM::factory("item", $task->get("item_id")); } else { - $parent = ORM::factory("item")->where("id", "=", $parent_entry->item_id)->find(); + $parent = ORM::factory("item", $parent_entry->item_id); } $name = basename($entry->file); diff --git a/modules/tag/helpers/item_tags_rest.php b/modules/tag/helpers/item_tags_rest.php index ce814f77..43e2cef0 100644 --- a/modules/tag/helpers/item_tags_rest.php +++ b/modules/tag/helpers/item_tags_rest.php @@ -50,7 +50,7 @@ class item_tags_rest_Core { } static function resolve($id) { - $item = ORM::factory("item")->where("id", "=", $id)->find(); + $item = ORM::factory("item", $id); if (!access::can("view", $item)) { throw new Kohana_404_Exception(); } diff --git a/modules/tag/helpers/tag_item_rest.php b/modules/tag/helpers/tag_item_rest.php index cd9bb6fe..60d37437 100644 --- a/modules/tag/helpers/tag_item_rest.php +++ b/modules/tag/helpers/tag_item_rest.php @@ -35,8 +35,8 @@ class tag_item_rest_Core { static function resolve($tuple) { list ($tag_id, $item_id) = split(",", $tuple); - $tag = ORM::factory("tag")->where("id", "=", $tag_id)->find(); - $item = ORM::factory("item")->where("id", "=", $item_id)->find(); + $tag = ORM::factory("tag", $tag_id); + $item = ORM::factory("item", $item_id); if (!$tag->loaded() || !$item->loaded() || !$tag->has($item)) { throw new Kohana_404_Exception(); } diff --git a/modules/tag/helpers/tag_items_rest.php b/modules/tag/helpers/tag_items_rest.php index 369a8d83..ef563ac6 100644 --- a/modules/tag/helpers/tag_items_rest.php +++ b/modules/tag/helpers/tag_items_rest.php @@ -52,7 +52,7 @@ class tag_items_rest_Core { } static function resolve($id) { - return ORM::factory("tag")->where("id", "=", $id)->find(); + return ORM::factory("tag", $id); } static function url($tag) { diff --git a/modules/tag/helpers/tag_rest.php b/modules/tag/helpers/tag_rest.php index 7143daa9..4879cf63 100644 --- a/modules/tag/helpers/tag_rest.php +++ b/modules/tag/helpers/tag_rest.php @@ -77,7 +77,7 @@ class tag_rest_Core { } static function resolve($id) { - $tag = ORM::factory("tag")->where("id", "=", $id)->find(); + $tag = ORM::factory("tag", $id); if (!$tag->loaded()) { throw new Kohana_404_Exception(); } diff --git a/modules/user/models/group.php b/modules/user/models/group.php index 85114ede..851e72e6 100644 --- a/modules/user/models/group.php +++ b/modules/user/models/group.php @@ -55,7 +55,7 @@ class Group_Model extends ORM implements Group_Definition { module::event("group_created", $this); } else { // Updated group - $original = ORM::factory("group")->where("id", "=", $this->id)->find(); + $original = ORM::factory("group", $this->id); parent::save(); module::event("group_updated", $original, $this); } diff --git a/modules/user/models/user.php b/modules/user/models/user.php index 7c97bae7..78c31047 100644 --- a/modules/user/models/user.php +++ b/modules/user/models/user.php @@ -99,7 +99,7 @@ class User_Model extends ORM implements User_Definition { module::event("user_created", $this); } else { // Updated user - $original = ORM::factory("user")->where("id", "=", $this->id)->find(); + $original = ORM::factory("user", $this->id); parent::save(); module::event("user_updated", $original, $this); } -- cgit v1.2.3 From 56d04b1ed83dc098eacd3d05496fef98cfaf8b8a Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Thu, 28 Jan 2010 20:38:48 -0800 Subject: cast $task->done to bool so that it doesn't show up as "0" to the JS, which will interpret that as a true value, when it's not. --- modules/server_add/controllers/server_add.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'modules/server_add/controllers') diff --git a/modules/server_add/controllers/server_add.php b/modules/server_add/controllers/server_add.php index 287855b6..2a948653 100644 --- a/modules/server_add/controllers/server_add.php +++ b/modules/server_add/controllers/server_add.php @@ -110,7 +110,7 @@ class Server_Add_Controller extends Admin_Controller { } $task = task::run($task_id); - print json_encode(array("done" => $task->done, + print json_encode(array("done" => (bool)$task->done, "status" => $task->status, "percent_complete" => $task->percent_complete)); } -- cgit v1.2.3 From 225359637917aa8d3d416da78e38c45d48b6a5f0 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Sun, 31 Jan 2010 15:03:53 -0800 Subject: Remove debug code. --- modules/server_add/controllers/server_add.php | 1 - 1 file changed, 1 deletion(-) (limited to 'modules/server_add/controllers') diff --git a/modules/server_add/controllers/server_add.php b/modules/server_add/controllers/server_add.php index 2a948653..f68335df 100644 --- a/modules/server_add/controllers/server_add.php +++ b/modules/server_add/controllers/server_add.php @@ -42,7 +42,6 @@ class Server_Add_Controller extends Admin_Controller { // Make a tree with the parents back up to the authorized path, and all the children under the // current path. - Kohana_Log::add("error", $path); if (server_add::is_valid_path($path)) { $tree->parents[] = $path; while (server_add::is_valid_path(dirname($tree->parents[0]))) { -- cgit v1.2.3