From 73c7ec53102c24de248d1424fdf8d5ba347c2200 Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Tue, 18 May 2010 06:16:47 -0700 Subject: Save the item before updating the order of the children. Also always increment the weight count (even if it is equal to the weight of the current child) --- modules/gallery/helpers/item_rest.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'modules/gallery/helpers/item_rest.php') diff --git a/modules/gallery/helpers/item_rest.php b/modules/gallery/helpers/item_rest.php index 36d2ca62..298c2f4a 100644 --- a/modules/gallery/helpers/item_rest.php +++ b/modules/gallery/helpers/item_rest.php @@ -126,18 +126,19 @@ class item_rest_Core { } } } + $item->save(); - $weight = 0; if (isset($request->params->members)) { + $weight = 0; foreach ($request->params->members as $url) { $child = rest::resolve($url); if ($child->parent_id == $item->id && $child->weight != $weight) { - $child->weight = $weight++; + $child->weight = $weight; $child->save(); } + $weight++; } } - $item->save(); } static function post($request) { -- cgit v1.2.3 From ae595795f09edbe0883d233a7a8483f6445b9ed7 Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Fri, 28 May 2010 09:41:42 -0700 Subject: If the file is empty (i.e. the upload failed, then throw a 'bad request' exception before trying to create the item. --- modules/gallery/helpers/item_rest.php | 3 +++ 1 file changed, 3 insertions(+) (limited to 'modules/gallery/helpers/item_rest.php') diff --git a/modules/gallery/helpers/item_rest.php b/modules/gallery/helpers/item_rest.php index 298c2f4a..ec86ce93 100644 --- a/modules/gallery/helpers/item_rest.php +++ b/modules/gallery/helpers/item_rest.php @@ -160,6 +160,9 @@ class item_rest_Core { case "photo": case "movie": + if (empty($request->file)) { + throw new Rest_Exception("Bad Request: Upload failed", 400); + } $item->type = $entity->type; $item->parent_id = $parent->id; $item->set_data_file($request->file); -- cgit v1.2.3 From aeee88031fed7029c3320800d237b69993e8b6d4 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Sun, 6 Jun 2010 13:06:08 -0700 Subject: Fix an unused variable caused by converting straight query params to $entity based params. --- modules/gallery/helpers/item_rest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'modules/gallery/helpers/item_rest.php') diff --git a/modules/gallery/helpers/item_rest.php b/modules/gallery/helpers/item_rest.php index 298c2f4a..c88f92d9 100644 --- a/modules/gallery/helpers/item_rest.php +++ b/modules/gallery/helpers/item_rest.php @@ -152,7 +152,7 @@ class item_rest_Core { $item->type = "album"; $item->parent_id = $parent->id; $item->name = $entity->name; - $item->title = isset($entity->title) ? $entity->title : $name; + $item->title = isset($entity->title) ? $entity->title : $entity->name; $item->description = isset($entity->description) ? $entity->description : null; $item->slug = isset($entity->slug) ? $entity->slug : null; $item->save(); -- cgit v1.2.3 From d5b80f29444e03aadc1130ab1624a09c0689fb93 Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Tue, 8 Jun 2010 14:35:35 -0700 Subject: Don't use the standard error formatting for exceptions that have occurred as part of a REST request. Format the exception as a json encoded text string so the client can extract the fault information if they so choose. --- modules/gallery/helpers/item_rest.php | 2 +- modules/rest/controllers/rest.php | 102 ++++++++++++++++++++++------------ 2 files changed, 66 insertions(+), 38 deletions(-) (limited to 'modules/gallery/helpers/item_rest.php') diff --git a/modules/gallery/helpers/item_rest.php b/modules/gallery/helpers/item_rest.php index f99afbc2..763e586f 100644 --- a/modules/gallery/helpers/item_rest.php +++ b/modules/gallery/helpers/item_rest.php @@ -161,7 +161,7 @@ class item_rest_Core { case "photo": case "movie": if (empty($request->file)) { - throw new Rest_Exception("Bad Request: Upload failed", 400); + throw new Rest_Exception("file: Upload failed", 400); } $item->type = $entity->type; $item->parent_id = $parent->id; diff --git a/modules/rest/controllers/rest.php b/modules/rest/controllers/rest.php index 38f28171..6392838f 100644 --- a/modules/rest/controllers/rest.php +++ b/modules/rest/controllers/rest.php @@ -39,54 +39,82 @@ class Rest_Controller extends Controller { } public function __call($function, $args) { - $input = Input::instance(); - $request = new stdClass(); - - switch ($method = strtolower($input->server("REQUEST_METHOD"))) { - case "get": - $request->params = (object) $input->get(); - break; - - default: - $request->params = (object) $input->post(); - if (isset($_FILES["file"])) { - $request->file = upload::save("file"); + try { + $input = Input::instance(); + $request = new stdClass(); + + switch ($method = strtolower($input->server("REQUEST_METHOD"))) { + case "get": + $request->params = (object) $input->get(); + break; + + default: + $request->params = (object) $input->post(); + if (isset($_FILES["file"])) { + $request->file = upload::save("file"); + } + break; } - break; - } - if (isset($request->params->entity)) { - $request->params->entity = json_decode($request->params->entity); - } - if (isset($request->params->members)) { - $request->params->members = json_decode($request->params->members); - } + if (isset($request->params->entity)) { + $request->params->entity = json_decode($request->params->entity); + } + if (isset($request->params->members)) { + $request->params->members = json_decode($request->params->members); + } - $request->method = strtolower($input->server("HTTP_X_GALLERY_REQUEST_METHOD", $method)); - $request->access_key = $input->server("HTTP_X_GALLERY_REQUEST_KEY"); + $request->method = strtolower($input->server("HTTP_X_GALLERY_REQUEST_METHOD", $method)); + $request->access_key = $input->server("HTTP_X_GALLERY_REQUEST_KEY"); - if (empty($request->access_key) && !empty($request->params->access_key)) { - $request->access_key = $request->params->access_key; - } + if (empty($request->access_key) && !empty($request->params->access_key)) { + $request->access_key = $request->params->access_key; + } + + $request->url = url::abs_current(true); - $request->url = url::abs_current(true); + rest::set_active_user($request->access_key); - rest::set_active_user($request->access_key); + $handler_class = "{$function}_rest"; + $handler_method = $request->method; - $handler_class = "{$function}_rest"; - $handler_method = $request->method; + if (!method_exists($handler_class, $handler_method)) { + throw new Rest_Exception("Bad Request", 400); + } - if (!method_exists($handler_class, $handler_method)) { - throw new Rest_Exception("Bad Request", 400); + $response = call_user_func(array($handler_class, $handler_method), $request); + } catch (Exception $e) { + $response = $this->_format_exception_response($e); } - try { - rest::reply(call_user_func(array($handler_class, $handler_method), $request)); - } catch (ORM_Validation_Exception $e) { - foreach ($e->validation->errors() as $key => $value) { - $msgs[] = "$key: $value"; + rest::reply($response); + } + + private function _format_exception_response($e) { + // Add this exception to the log + Kohana_Log::add('error', Kohana_Exception::text($e)); + + $e->sendHeaders(); + + $rest_exception = array(); + if ($e instanceof ORM_Validation_Exception) { + $detail_response = true; + $rest_exception["code"] = 400; + $rest_exception["message"] = t("Validation errors"); + $rest_exception["fields"] = $e->validation->errors; + } else if ($e instanceof Rest_Exception) { + $rest_exception["code"] = $e->getCode(); + if ($e->getMessage() != "Bad Request") { + $rest_exception["message"] = "Bad Request"; + $rest_exception["fields"] = array("type", $e->getMessage()); + } else { + $rest_exception["message"] = $e->getMessage(); } - throw new Rest_Exception("Bad Request: " . join(", ", $msgs), 400); + header("HTTP/1.1 400 Bad Request"); + } else { + $rest_exception["code"] = 500; + $rest_exception["message"] = t("Remote server call failed. Please contact the Adminstrator."); } + + return $rest_exception; } } \ No newline at end of file -- cgit v1.2.3 From 603d4640141a43350f50da747d747456b28fdd93 Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Tue, 15 Jun 2010 11:20:04 -0700 Subject: Change the item rest update processing to call the itemm::move(source, target) helper when the parent member has changed. Using the move method insures that names and slugs that could conflict in the target album are resolved properly. Also, only change the weights of the album children if the item sort_column is set to weight. --- modules/gallery/helpers/item_rest.php | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) (limited to 'modules/gallery/helpers/item_rest.php') diff --git a/modules/gallery/helpers/item_rest.php b/modules/gallery/helpers/item_rest.php index 763e586f..27542dea 100644 --- a/modules/gallery/helpers/item_rest.php +++ b/modules/gallery/helpers/item_rest.php @@ -99,7 +99,7 @@ class item_rest_Core { if ($entity = $request->params->entity) { // Only change fields from a whitelist. foreach (array("album_cover", "captured", "description", - "height", "mime_type", "name", "parent", "rand_key", "resize_dirty", + "height", "mime_type", "name", "rand_key", "resize_dirty", "resize_height", "resize_width", "slug", "sort_column", "sort_order", "thumb_dirty", "thumb_height", "thumb_width", "title", "view_count", "width") as $key) { @@ -112,23 +112,21 @@ class item_rest_Core { } break; - case "parent": - if (property_exists($entity, "parent")) { - $parent = rest::resolve($entity->parent); - access::required("edit", $parent); - $item->parent_id = $parent->id; - } - break; default: if (property_exists($entity, $key)) { $item->$key = $entity->$key; } } } + + $item->save(); + if (property_exists($entity, "parent")) { + $parent = rest::resolve($entity->parent); + item::move($item, $parent); + } } - $item->save(); - if (isset($request->params->members)) { + if (isset($request->params->members) && $item->sort_column == "weight") { $weight = 0; foreach ($request->params->members as $url) { $child = rest::resolve($url); -- cgit v1.2.3 From 00c4cb3f6399319326cd3393ee2f15fc8b111088 Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Tue, 15 Jun 2010 11:38:46 -0700 Subject: Revert "Change the item rest update processing to call the itemm::move(source, target) helper when the parent member has changed. Using the move method insures that names and slugs that could conflict in the target album are resolved properly. Also, only change the weights of the album children if the item sort_column is set to weight." This reverts commit 603d4640141a43350f50da747d747456b28fdd93. --- modules/gallery/helpers/item_rest.php | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) (limited to 'modules/gallery/helpers/item_rest.php') diff --git a/modules/gallery/helpers/item_rest.php b/modules/gallery/helpers/item_rest.php index 27542dea..763e586f 100644 --- a/modules/gallery/helpers/item_rest.php +++ b/modules/gallery/helpers/item_rest.php @@ -99,7 +99,7 @@ class item_rest_Core { if ($entity = $request->params->entity) { // Only change fields from a whitelist. foreach (array("album_cover", "captured", "description", - "height", "mime_type", "name", "rand_key", "resize_dirty", + "height", "mime_type", "name", "parent", "rand_key", "resize_dirty", "resize_height", "resize_width", "slug", "sort_column", "sort_order", "thumb_dirty", "thumb_height", "thumb_width", "title", "view_count", "width") as $key) { @@ -112,21 +112,23 @@ class item_rest_Core { } break; + case "parent": + if (property_exists($entity, "parent")) { + $parent = rest::resolve($entity->parent); + access::required("edit", $parent); + $item->parent_id = $parent->id; + } + break; default: if (property_exists($entity, $key)) { $item->$key = $entity->$key; } } } - - $item->save(); - if (property_exists($entity, "parent")) { - $parent = rest::resolve($entity->parent); - item::move($item, $parent); - } } + $item->save(); - if (isset($request->params->members) && $item->sort_column == "weight") { + if (isset($request->params->members)) { $weight = 0; foreach ($request->params->members as $url) { $child = rest::resolve($url); -- cgit v1.2.3 From 207f6beb61cf2969d07bbc6f959bba967f54b271 Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Tue, 15 Jun 2010 11:40:01 -0700 Subject: Only change the weights of the album children if the item sort_column is set to weight. --- modules/gallery/helpers/item_rest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'modules/gallery/helpers/item_rest.php') diff --git a/modules/gallery/helpers/item_rest.php b/modules/gallery/helpers/item_rest.php index 763e586f..0839b144 100644 --- a/modules/gallery/helpers/item_rest.php +++ b/modules/gallery/helpers/item_rest.php @@ -128,7 +128,7 @@ class item_rest_Core { } $item->save(); - if (isset($request->params->members)) { + if (isset($request->params->members) && $item->sort_column == "weight") { $weight = 0; foreach ($request->params->members as $url) { $child = rest::resolve($url); -- cgit v1.2.3 From 2492280cc0ec9eb64a8daeccc7b5698ece7fea66 Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Tue, 15 Jun 2010 12:52:28 -0700 Subject: Change the item rest update processing to call the item::move(source, target) helper when the parent member has changed. Using the move method insures that names and slugs that could conflict in the target album are resolved properly. Changed the item::move method so it returns a message to be displayed if the caller chooses. And changed the move controller to display the message returned by the move if the item name was renamed as part of the move. --- modules/gallery/controllers/move.php | 10 ++++------ modules/gallery/helpers/item.php | 15 +++++++++------ modules/gallery/helpers/item_rest.php | 16 +++++++++------- 3 files changed, 22 insertions(+), 19 deletions(-) (limited to 'modules/gallery/helpers/item_rest.php') diff --git a/modules/gallery/controllers/move.php b/modules/gallery/controllers/move.php index f8b85b6f..3ce44546 100644 --- a/modules/gallery/controllers/move.php +++ b/modules/gallery/controllers/move.php @@ -34,12 +34,10 @@ class Move_Controller extends Controller { $source = ORM::factory("item", $source_id); $target = ORM::factory("item", Input::instance()->post("target_id")); - access::required("view", $source); - access::required("edit", $source); - access::required("view", $target); - access::required("edit", $target); - - item::move($source, $target); + $message = item::move($source, $target); + if (!empty($message)) { + message.info($message); + } print json_encode( array("result" => "success", diff --git a/modules/gallery/helpers/item.php b/modules/gallery/helpers/item.php index 15bbe977..6a740de4 100644 --- a/modules/gallery/helpers/item.php +++ b/modules/gallery/helpers/item.php @@ -47,27 +47,28 @@ class item_Core { $orig_name_filename = pathinfo($source->name, PATHINFO_FILENAME); $orig_name_extension = pathinfo($source->name, PATHINFO_EXTENSION); $orig_slug = $source->slug; + $message = ""; for ($i = 0; $i < 5; $i++) { try { $source->save(); if ($orig_name != $source->name) { switch ($source->type) { case "album": - message::info( + $message = t("Album %old_name renamed to %new_name to avoid a conflict", - array("old_name" => $orig_name, "new_name" => $source->name))); + array("old_name" => $orig_name, "new_name" => $source->name)); break; case "photo": - message::info( + $message = t("Photo %old_name renamed to %new_name to avoid a conflict", - array("old_name" => $orig_name, "new_name" => $source->name))); + array("old_name" => $orig_name, "new_name" => $source->name)); break; case "movie": - message::info( + $message = t("Movie %old_name renamed to %new_name to avoid a conflict", - array("old_name" => $orig_name, "new_name" => $source->name))); + array("old_name" => $orig_name, "new_name" => $source->name)); break; } } @@ -95,6 +96,8 @@ class item_Core { if ($target->album_cover_item_id == null) { item::make_album_cover($source); } + + return $message; } static function make_album_cover($item) { diff --git a/modules/gallery/helpers/item_rest.php b/modules/gallery/helpers/item_rest.php index 0839b144..692d0895 100644 --- a/modules/gallery/helpers/item_rest.php +++ b/modules/gallery/helpers/item_rest.php @@ -99,7 +99,7 @@ class item_rest_Core { if ($entity = $request->params->entity) { // Only change fields from a whitelist. foreach (array("album_cover", "captured", "description", - "height", "mime_type", "name", "parent", "rand_key", "resize_dirty", + "height", "mime_type", "name", "rand_key", "resize_dirty", "resize_height", "resize_width", "slug", "sort_column", "sort_order", "thumb_dirty", "thumb_height", "thumb_width", "title", "view_count", "width") as $key) { @@ -113,11 +113,6 @@ class item_rest_Core { break; case "parent": - if (property_exists($entity, "parent")) { - $parent = rest::resolve($entity->parent); - access::required("edit", $parent); - $item->parent_id = $parent->id; - } break; default: if (property_exists($entity, $key)) { @@ -125,8 +120,15 @@ class item_rest_Core { } } } + + // There is an explicit save in item::move + if (property_exists($entity, "parent")) { + $parent = rest::resolve($entity->parent); + item::move($item, $parent); + } else { + $item->save(); + } } - $item->save(); if (isset($request->params->members) && $item->sort_column == "weight") { $weight = 0; -- cgit v1.2.3 From 9504f71efcadc7ed27f6f09e5d663e8025bf3b86 Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Tue, 15 Jun 2010 14:18:23 -0700 Subject: Fix for ticket #1118. Create a item::save_with_retries helper method, which encapsulates saving an item and handling name and slug conflicts. Call this instead of doing a save directly. --- modules/gallery/controllers/simple_uploader.php | 10 +++++++--- modules/gallery/helpers/item.php | 17 +++++++++++------ modules/gallery/helpers/item_rest.php | 8 +++----- 3 files changed, 21 insertions(+), 14 deletions(-) (limited to 'modules/gallery/helpers/item_rest.php') diff --git a/modules/gallery/controllers/simple_uploader.php b/modules/gallery/controllers/simple_uploader.php index c7e5031b..8ac1fc8b 100644 --- a/modules/gallery/controllers/simple_uploader.php +++ b/modules/gallery/controllers/simple_uploader.php @@ -65,12 +65,16 @@ class Simple_Uploader_Controller extends Controller { if (array_key_exists("extension", $path_info) && in_array(strtolower($path_info["extension"]), array("flv", "mp4"))) { $item->type = "movie"; - $item->save(); + } else { + $item->type = "photo"; + } + + item::save_with_retries($item); + + if ($item->type == "movie") { log::success("content", t("Added a movie"), html::anchor("movies/$item->id", t("view movie"))); } else { - $item->type = "photo"; - $item->save(); log::success("content", t("Added a photo"), html::anchor("photos/$item->id", t("view photo"))); } diff --git a/modules/gallery/helpers/item.php b/modules/gallery/helpers/item.php index 6a740de4..0710d8b2 100644 --- a/modules/gallery/helpers/item.php +++ b/modules/gallery/helpers/item.php @@ -43,6 +43,17 @@ class item_Core { // Moving may result in name or slug conflicts. If that happens, try up to 5 times to pick a // random name (or slug) to avoid the conflict. + $message = item::save_with_retries($source); + + // If the target has no cover item, make this it. + if ($target->album_cover_item_id == null) { + item::make_album_cover($source); + } + + return $message; + } + + static function save_with_retries($source, $retries=5) { $orig_name = $source->name; $orig_name_filename = pathinfo($source->name, PATHINFO_FILENAME); $orig_name_extension = pathinfo($source->name, PATHINFO_EXTENSION); @@ -91,12 +102,6 @@ class item_Core { } } } - - // If the target has no cover item, make this it. - if ($target->album_cover_item_id == null) { - item::make_album_cover($source); - } - return $message; } diff --git a/modules/gallery/helpers/item_rest.php b/modules/gallery/helpers/item_rest.php index 692d0895..74fab2e7 100644 --- a/modules/gallery/helpers/item_rest.php +++ b/modules/gallery/helpers/item_rest.php @@ -112,8 +112,6 @@ class item_rest_Core { } break; - case "parent": - break; default: if (property_exists($entity, $key)) { $item->$key = $entity->$key; @@ -126,7 +124,7 @@ class item_rest_Core { $parent = rest::resolve($entity->parent); item::move($item, $parent); } else { - $item->save(); + $item::save_with_retries($item); } } @@ -157,7 +155,7 @@ class item_rest_Core { $item->title = isset($entity->title) ? $entity->title : $entity->name; $item->description = isset($entity->description) ? $entity->description : null; $item->slug = isset($entity->slug) ? $entity->slug : null; - $item->save(); + $item::save_with_retries($item); break; case "photo": @@ -172,7 +170,7 @@ class item_rest_Core { $item->title = isset($entity->title) ? $entity->title : $entity->name; $item->description = isset($entity->description) ? $entity->description : null; $item->slug = isset($entity->slug) ? $entity->slug : null; - $item->save(); + $item::save_with_retries($item); break; default: -- cgit v1.2.3 From 48dc07dbc8189eb16f97b7013b0481982286ab2c Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Tue, 15 Jun 2010 17:17:25 -0700 Subject: Revert "Fix for ticket #1118. Create a item::save_with_retries helper method, which encapsulates saving an item and handling name and slug conflicts. Call this instead of doing a save directly." Rolled this back because it fails KISS. We already have an API for saving models with Item_Model::save() that's consistent with all of our other model code. Adding a new way to save items is confusing and inconsistent. This reverts commit 9504f71efcadc7ed27f6f09e5d663e8025bf3b86. --- modules/gallery/controllers/simple_uploader.php | 10 +++------- modules/gallery/helpers/item.php | 17 ++++++----------- modules/gallery/helpers/item_rest.php | 8 +++++--- 3 files changed, 14 insertions(+), 21 deletions(-) (limited to 'modules/gallery/helpers/item_rest.php') diff --git a/modules/gallery/controllers/simple_uploader.php b/modules/gallery/controllers/simple_uploader.php index 8ac1fc8b..c7e5031b 100644 --- a/modules/gallery/controllers/simple_uploader.php +++ b/modules/gallery/controllers/simple_uploader.php @@ -65,16 +65,12 @@ class Simple_Uploader_Controller extends Controller { if (array_key_exists("extension", $path_info) && in_array(strtolower($path_info["extension"]), array("flv", "mp4"))) { $item->type = "movie"; - } else { - $item->type = "photo"; - } - - item::save_with_retries($item); - - if ($item->type == "movie") { + $item->save(); log::success("content", t("Added a movie"), html::anchor("movies/$item->id", t("view movie"))); } else { + $item->type = "photo"; + $item->save(); log::success("content", t("Added a photo"), html::anchor("photos/$item->id", t("view photo"))); } diff --git a/modules/gallery/helpers/item.php b/modules/gallery/helpers/item.php index 0710d8b2..6a740de4 100644 --- a/modules/gallery/helpers/item.php +++ b/modules/gallery/helpers/item.php @@ -43,17 +43,6 @@ class item_Core { // Moving may result in name or slug conflicts. If that happens, try up to 5 times to pick a // random name (or slug) to avoid the conflict. - $message = item::save_with_retries($source); - - // If the target has no cover item, make this it. - if ($target->album_cover_item_id == null) { - item::make_album_cover($source); - } - - return $message; - } - - static function save_with_retries($source, $retries=5) { $orig_name = $source->name; $orig_name_filename = pathinfo($source->name, PATHINFO_FILENAME); $orig_name_extension = pathinfo($source->name, PATHINFO_EXTENSION); @@ -102,6 +91,12 @@ class item_Core { } } } + + // If the target has no cover item, make this it. + if ($target->album_cover_item_id == null) { + item::make_album_cover($source); + } + return $message; } diff --git a/modules/gallery/helpers/item_rest.php b/modules/gallery/helpers/item_rest.php index 74fab2e7..692d0895 100644 --- a/modules/gallery/helpers/item_rest.php +++ b/modules/gallery/helpers/item_rest.php @@ -112,6 +112,8 @@ class item_rest_Core { } break; + case "parent": + break; default: if (property_exists($entity, $key)) { $item->$key = $entity->$key; @@ -124,7 +126,7 @@ class item_rest_Core { $parent = rest::resolve($entity->parent); item::move($item, $parent); } else { - $item::save_with_retries($item); + $item->save(); } } @@ -155,7 +157,7 @@ class item_rest_Core { $item->title = isset($entity->title) ? $entity->title : $entity->name; $item->description = isset($entity->description) ? $entity->description : null; $item->slug = isset($entity->slug) ? $entity->slug : null; - $item::save_with_retries($item); + $item->save(); break; case "photo": @@ -170,7 +172,7 @@ class item_rest_Core { $item->title = isset($entity->title) ? $entity->title : $entity->name; $item->description = isset($entity->description) ? $entity->description : null; $item->slug = isset($entity->slug) ? $entity->slug : null; - $item::save_with_retries($item); + $item->save(); break; default: -- cgit v1.2.3 From a432a43b3b39fbec70d4cece1eb0ba5625b2679c Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Tue, 15 Jun 2010 17:18:22 -0700 Subject: Revert "Change the item rest update processing to call the item::move(source, target) helper when the parent member has changed. Using the move method insures that names and slugs that could conflict in the target album are resolved properly. Changed the item::move method so it returns a message to be displayed if the caller chooses. And changed the move controller to display the message returned by the move if the item name was renamed as part of the move." Rolling this back for a couple of reasons: 1) Bug in move.php ("message.info" is not a function name) 2) Having the message come back from the API call as a side-effect is sloppy. We should find a cleaner way to do this checking. 3) having item::move() call save() on any changed values in the ORM is counter-intuitive. Move should move, save should save. I think the right approach here is to roll the move() code properly into save(). This reverts commit 2492280cc0ec9eb64a8daeccc7b5698ece7fea66. --- modules/gallery/controllers/move.php | 10 ++++++---- modules/gallery/helpers/item.php | 15 ++++++--------- modules/gallery/helpers/item_rest.php | 16 +++++++--------- 3 files changed, 19 insertions(+), 22 deletions(-) (limited to 'modules/gallery/helpers/item_rest.php') diff --git a/modules/gallery/controllers/move.php b/modules/gallery/controllers/move.php index 3ce44546..f8b85b6f 100644 --- a/modules/gallery/controllers/move.php +++ b/modules/gallery/controllers/move.php @@ -34,10 +34,12 @@ class Move_Controller extends Controller { $source = ORM::factory("item", $source_id); $target = ORM::factory("item", Input::instance()->post("target_id")); - $message = item::move($source, $target); - if (!empty($message)) { - message.info($message); - } + access::required("view", $source); + access::required("edit", $source); + access::required("view", $target); + access::required("edit", $target); + + item::move($source, $target); print json_encode( array("result" => "success", diff --git a/modules/gallery/helpers/item.php b/modules/gallery/helpers/item.php index 6a740de4..15bbe977 100644 --- a/modules/gallery/helpers/item.php +++ b/modules/gallery/helpers/item.php @@ -47,28 +47,27 @@ class item_Core { $orig_name_filename = pathinfo($source->name, PATHINFO_FILENAME); $orig_name_extension = pathinfo($source->name, PATHINFO_EXTENSION); $orig_slug = $source->slug; - $message = ""; for ($i = 0; $i < 5; $i++) { try { $source->save(); if ($orig_name != $source->name) { switch ($source->type) { case "album": - $message = + message::info( t("Album %old_name renamed to %new_name to avoid a conflict", - array("old_name" => $orig_name, "new_name" => $source->name)); + array("old_name" => $orig_name, "new_name" => $source->name))); break; case "photo": - $message = + message::info( t("Photo %old_name renamed to %new_name to avoid a conflict", - array("old_name" => $orig_name, "new_name" => $source->name)); + array("old_name" => $orig_name, "new_name" => $source->name))); break; case "movie": - $message = + message::info( t("Movie %old_name renamed to %new_name to avoid a conflict", - array("old_name" => $orig_name, "new_name" => $source->name)); + array("old_name" => $orig_name, "new_name" => $source->name))); break; } } @@ -96,8 +95,6 @@ class item_Core { if ($target->album_cover_item_id == null) { item::make_album_cover($source); } - - return $message; } static function make_album_cover($item) { diff --git a/modules/gallery/helpers/item_rest.php b/modules/gallery/helpers/item_rest.php index 692d0895..0839b144 100644 --- a/modules/gallery/helpers/item_rest.php +++ b/modules/gallery/helpers/item_rest.php @@ -99,7 +99,7 @@ class item_rest_Core { if ($entity = $request->params->entity) { // Only change fields from a whitelist. foreach (array("album_cover", "captured", "description", - "height", "mime_type", "name", "rand_key", "resize_dirty", + "height", "mime_type", "name", "parent", "rand_key", "resize_dirty", "resize_height", "resize_width", "slug", "sort_column", "sort_order", "thumb_dirty", "thumb_height", "thumb_width", "title", "view_count", "width") as $key) { @@ -113,6 +113,11 @@ class item_rest_Core { break; case "parent": + if (property_exists($entity, "parent")) { + $parent = rest::resolve($entity->parent); + access::required("edit", $parent); + $item->parent_id = $parent->id; + } break; default: if (property_exists($entity, $key)) { @@ -120,15 +125,8 @@ class item_rest_Core { } } } - - // There is an explicit save in item::move - if (property_exists($entity, "parent")) { - $parent = rest::resolve($entity->parent); - item::move($item, $parent); - } else { - $item->save(); - } } + $item->save(); if (isset($request->params->members) && $item->sort_column == "weight") { $weight = 0; -- cgit v1.2.3 From 41ca2b0195bf6a29429dfc5405f3c2073b1c3aba Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Sat, 19 Jun 2010 13:52:48 -0700 Subject: Rework our exception framework to fit into Kohana's model better. Instead of overwriting Kohana_Exception::handle() (which we were doing in MY_Kohana_Exception) we instead use their existing template system. gallery/views/kohana/error.php overrides system/views/kohana/error.php and is the standard error template for all exceptions. Our version of error.php figures out the appropriate view based on context (cli, authenticated admin, guest viewing a 404, guest viewing a system error) and delegates appropriately. Each delegated view has a narrow responsibility. This paves the way for us to add new error views per module. For example, the rest module will define its own template in Rest_Exception and then its exceptions can be rendered the way that it wants (json encoded, in that case). --- modules/gallery/helpers/item_rest.php | 24 +- modules/gallery/libraries/MY_Kohana_Exception.php | 62 ----- modules/gallery/views/error_admin.html.php | 272 ++++++++++++++++++ modules/gallery/views/error_cli.txt.php | 3 + modules/gallery/views/error_user.html.php | 42 +++ modules/gallery/views/kohana/error.php | 321 +++------------------- 6 files changed, 372 insertions(+), 352 deletions(-) create mode 100644 modules/gallery/views/error_admin.html.php create mode 100644 modules/gallery/views/error_cli.txt.php create mode 100644 modules/gallery/views/error_user.html.php (limited to 'modules/gallery/helpers/item_rest.php') diff --git a/modules/gallery/helpers/item_rest.php b/modules/gallery/helpers/item_rest.php index 0839b144..6869181d 100644 --- a/modules/gallery/helpers/item_rest.php +++ b/modules/gallery/helpers/item_rest.php @@ -161,20 +161,22 @@ class item_rest_Core { case "photo": case "movie": if (empty($request->file)) { - throw new Rest_Exception("file: Upload failed", 400); + throw new Rest_Exception( + "Bad Request", 400, array("errors" => array("file" => t("Upload failed")))); } - $item->type = $entity->type; - $item->parent_id = $parent->id; - $item->set_data_file($request->file); - $item->name = $entity->name; - $item->title = isset($entity->title) ? $entity->title : $entity->name; - $item->description = isset($entity->description) ? $entity->description : null; - $item->slug = isset($entity->slug) ? $entity->slug : null; - $item->save(); - break; + $item->type = $entity->type; + $item->parent_id = $parent->id; + $item->set_data_file($request->file); + $item->name = $entity->name; + $item->title = isset($entity->title) ? $entity->title : $entity->name; + $item->description = isset($entity->description) ? $entity->description : null; + $item->slug = isset($entity->slug) ? $entity->slug : null; + $item->save(); + break; default: - throw new Rest_Exception("Invalid type: $entity->type", 400); + throw new Rest_Exception( + "Bad Request", 400, array("errors" => array("type" => "invalid"))); } return array("url" => rest::url("item", $item)); diff --git a/modules/gallery/libraries/MY_Kohana_Exception.php b/modules/gallery/libraries/MY_Kohana_Exception.php index 11556f7a..72cb2ac0 100644 --- a/modules/gallery/libraries/MY_Kohana_Exception.php +++ b/modules/gallery/libraries/MY_Kohana_Exception.php @@ -29,68 +29,6 @@ class Kohana_Exception extends Kohana_Exception_Core { $e->getTraceAsString()); } - public static function handle(Exception $e) { - if ($e instanceof ORM_Validation_Exception) { - Kohana_Log::add("error", "Validation errors: " . print_r($e->validation->errors(), 1)); - } - try { - $user = identity::active_user(); - $try_themed_view = $user && !$user->admin; - } catch (Exception $e2) { - $try_themed_view = false; - } - - if ($try_themed_view) { - try { - return self::_show_themed_error_page($e); - } catch (Exception $e3) { - Kohana_Log::add("error", "Exception in exception handling code: " . self::text($e3)); - return parent::handle($e); - } - } else { - return parent::handle($e); - } - } - - /** - * Shows a themed error page. - * @see Kohana_Exception::handle - */ - private static function _show_themed_error_page(Exception $e) { - // Create a text version of the exception - $error = Kohana_Exception::text($e); - - // Add this exception to the log - Kohana_Log::add("error", $error); - - // Manually save logs after exceptions - Kohana_Log::save(); - - if (!headers_sent()) { - if ($e instanceof Kohana_Exception) { - $e->sendHeaders(); - } else { - header("HTTP/1.1 500 Internal Server Error"); - } - } - - $view = new Theme_View("page.html", "other", "error"); - if ($e instanceof Kohana_404_Exception) { - $view->page_title = t("Dang... Page not found!"); - $view->content = new View("error_404.html"); - $user = identity::active_user(); - $view->content->is_guest = $user && $user->guest; - if ($view->content->is_guest) { - $view->content->login_form = new View("login_ajax.html"); - $view->content->login_form->form = auth::get_login_form("login/auth_html"); - } - } else { - $view->page_title = t("Dang... Something went wrong!"); - $view->content = new View("error.html"); - } - print $view; - } - /** * @see Kohana_Exception::dump() */ diff --git a/modules/gallery/views/error_admin.html.php b/modules/gallery/views/error_admin.html.php new file mode 100644 index 00000000..40eb7374 --- /dev/null +++ b/modules/gallery/views/error_admin.html.php @@ -0,0 +1,272 @@ + + + + + + + + <?= t("Something went wrong!") ?> + + + + + +
+

+ +

+

+ +

+
+
+

+ +

+
+

+ + [ ]: + + + + +

+
+
    +
  1. +

    + + [ ] + +

    + +
    + $row): ?>"> + +
    +
  2. + + + $step): ?> +
  3. +

    + + + + [ ] + + [ ] + + + {} + + + » + ( + + ) +

    + + + + + + +
  4. + + +
+ + +
+

+ " onclick="return koggle('')"> +

+ +
+
+ + diff --git a/modules/gallery/views/error_cli.txt.php b/modules/gallery/views/error_cli.txt.php new file mode 100644 index 00000000..b4f87fa6 --- /dev/null +++ b/modules/gallery/views/error_cli.txt.php @@ -0,0 +1,3 @@ + + + diff --git a/modules/gallery/views/error_user.html.php b/modules/gallery/views/error_user.html.php new file mode 100644 index 00000000..74c6a8fb --- /dev/null +++ b/modules/gallery/views/error_user.html.php @@ -0,0 +1,42 @@ + + + + + + + <?= t("Something went wrong!") ?> + + +
+

+ +

+

+ +

+

+ +

+
+ + diff --git a/modules/gallery/views/kohana/error.php b/modules/gallery/views/kohana/error.php index d55105a0..b0f0e907 100644 --- a/modules/gallery/views/kohana/error.php +++ b/modules/gallery/views/kohana/error.php @@ -1,280 +1,43 @@ - - - - - - - <?= t("Something went wrong!") ?> - - - - - - admin) ?> -
-

- -

-

- -

- -

- -

- -
- -
-

- -

-
-

- - [ ]: - - - - -

-
-
    -
  1. -

    - - [ ] - -

    - -
    - $row): ?>"> - -
    -
  2. - - - $step): ?> -
  3. -

    - - - - [ ] - - [ ] - - - {} - - - » - ( - - ) -

    - - - - - - -
  4. - - -
- - -
-

- " onclick="return koggle('')"> -

- -
-
- - - +validation->errors(), 1)); +} + +if (php_sapi_name() == "cli") { + include Kohana::find_file("views", "error_cli.txt"); + return; +} + +try { + // Admins get a special error page + $user = identity::active_user(); + if ($user && $user->admin) { + include Kohana::find_file("views", "error_admin.html"); + return; + } +} catch (Exception $ignored) { +} + +// Try to show a themed error page for 404 errors +if ($e instanceof Kohana_404_Exception) { + $view = new Theme_View("page.html", "other", "error"); + $view->page_title = t("Dang... Page not found!"); + $view->content = new View("error_404.html"); + $user = identity::active_user(); + $view->content->is_guest = $user && $user->guest; + if ($view->content->is_guest) { + $view->content->login_form = new View("login_ajax.html"); + $view->content->login_form->form = auth::get_login_form("login/auth_html"); + } + print $view; + return; +} + +header("HTTP/1.1 500 Internal Server Error"); +include Kohana::find_file("views", "error_user.html"); +?> -- cgit v1.2.3