diff options
author | Bharat Mediratta <bharat@menalto.com> | 2008-12-23 07:30:14 +0000 |
---|---|---|
committer | Bharat Mediratta <bharat@menalto.com> | 2008-12-23 07:30:14 +0000 |
commit | 76b1500209e5c5becef180eae2abed78d18d09db (patch) | |
tree | 19c929f1a8183bb48db5e09dbbd8081c22644430 | |
parent | 69daaffb054f20574cb59078ce205920f4678da6 (diff) |
Clean up the way that we upload via dialogs:
1) If the create request is Ajax, then return a 201 Created response code
2) If we receive back a 201 response, then switch to the new page.
3) Display a status message when we do an upload.
-rw-r--r-- | core/controllers/items.php | 33 | ||||
-rw-r--r-- | core/helpers/photo.php | 3 | ||||
-rw-r--r-- | themes/default/js/ui.init.js | 62 |
3 files changed, 61 insertions, 37 deletions
diff --git a/core/controllers/items.php b/core/controllers/items.php index 57be0046..b79d28d2 100644 --- a/core/controllers/items.php +++ b/core/controllers/items.php @@ -63,7 +63,13 @@ class Items_Controller extends REST_Controller { $owner_id); log::add("content", "Created an album", log::INFO, html::anchor("albums/$album->id", "view album")); - url::redirect("albums/$album->id"); + message::add(_("Successfully created album")); + if (request::is_ajax()) { + rest::http_status(rest::CREATED); + rest::http_location(url::site("albums/$album->id")); + } else { + url::redirect("albums/$album->id"); + } break; case "photo": @@ -72,21 +78,28 @@ class Items_Controller extends REST_Controller { for ($i = 0; $i < $count - 1; $i++) { if ($_FILES["file"]["error"][$i] == 0) { $photo = photo::create( - $item->id, + $item, $_FILES["file"]["tmp_name"][$i], $_FILES["file"]["name"][$i], $_FILES["file"]["name"][$i], "", $owner_id); } else { - throw new Exception("@todo ERROR_IN_UPLOAD_FILE"); + log::add("content", "Error uploading photo", log::WARNING); + message::add(sprintf(_("Error uploading photo %s"), + html::specialchars($_FILES["file"]["name"][$i]))); } } log::add("content", "Added $count photos", log::INFO, html::anchor("albums/$item->id", "view album")); - url::redirect("albums/$item->id"); + if (request::is_ajax()) { + rest::http_status(rest::CREATED); + rest::http_location(url::site("albums/$item->id")); + } else { + url::redirect("albums/$item->id"); + } } else { $photo = photo::create( - $item->id, + $item, $_FILES["file"]["tmp_name"], $_FILES["file"]["name"], $this->input->post("title", $this->input->post("name")), @@ -94,14 +107,20 @@ class Items_Controller extends REST_Controller { $owner_id); log::add("content", "Added a photo", log::INFO, html::anchor("photos/$photo->id", "view photo")); - url::redirect("photos/$photo->id"); + message::add(_("Successfully added photo")); + if (request::is_ajax()) { + rest::http_status(rest::CREATED); + rest::http_location(url::site("photos/$photo->id")); + } else { + url::redirect("photos/$photo->id"); + } } break; } } public function _delete($item) { - // @todo Production this code + // @todo Productionize this code // 1) Add security checks $parent = $item->parent(); if ($parent->id) { diff --git a/core/helpers/photo.php b/core/helpers/photo.php index d8fc2595..e5ed2b22 100644 --- a/core/helpers/photo.php +++ b/core/helpers/photo.php @@ -95,8 +95,7 @@ class photo_Core { } static function get_add_form($parent) { - $form = new Forge("albums/{$parent->id}", "", "post", - array("id" => "gAddPhotoForm", "enctype" => "multipart/form-data")); + $form = new Forge("albums/{$parent->id}", "", "post", array("id" => "gAddPhotoForm")); $group = $form->group("add_photo")->label(sprintf(_("Add Photo to %s"), $parent->title)); $group->input("name")->label(_("Name")); $group->input("title")->label(_("Title")); diff --git a/themes/default/js/ui.init.js b/themes/default/js/ui.init.js index 394c18b0..f4a7c49c 100644 --- a/themes/default/js/ui.init.js +++ b/themes/default/js/ui.init.js @@ -10,7 +10,7 @@ $("document").ready(function() { // Add Superfish menu class $("ul.gMenu").addClass("sf-menu"); $("ul#gViewMenu").addClass("sf-menu"); - + // Superfish menu options $('ul.sf-menu').superfish({ delay: 500, @@ -41,24 +41,24 @@ $("document").ready(function() { for (var i=0; i < dialogLinks.length; i++) { $(dialogLinks[i]).bind("click", {element: dialogLinks[i]}, handleDialogEvent); }; - + // Declare which forms are short forms $("#gHeader #gSearchForm").addClass("gShortForm"); $("#gSidebar #gAddTagForm").addClass("gShortForm"); - + // Get the short forms var shortForms = $(".gShortForm"); - + // Set up short form behavior for (var i=0; i < shortForms.length; i++) { // Set variables var shortFormID = "#" + $(shortForms[i]).attr("id"); var shortInputID = "#" + $(shortFormID + " input:first").attr("id"); var shortLabelValue = $(shortFormID + " label:first").html(); - + // Set default input value equal to label text $(shortInputID).val(shortLabelValue); - + // Attach event listeners to inputs $(shortInputID).bind("focus blur", function(e){ var eLabelVal = $(this).siblings("label").html(); @@ -72,12 +72,12 @@ $("document").ready(function() { } }); }; - + }); /** * Fire openDialog() and prevent links from opening - * + * * @see openDialog() */ function handleDialogEvent(event) { @@ -86,14 +86,14 @@ function handleDialogEvent(event) { } /** - * Display modal dialogs, populate dialog with trigger link's title and href - * + * Display modal dialogs, populate dialog with trigger link's title and href + * * @requires ui.core * @requires ui.draggable * @requires ui.resizable * @requires ui.dialog * @see handleDialogEvent() - * + * * @todo Set dialog attributes dynamically (width, height, drag, resize) * @todo Set ui-dialog-buttonpane button values equal to the original form button value * @todo Display loading animation on form submit @@ -105,19 +105,25 @@ function openDialog(element) { $("body").append(eDialog); var buttons = {}; - buttons["Submit"] = function() { - var form = $("#gDialog").find("form"); - var options = - $(form).ajaxSubmit({ - success: function(data, textStatus) { - if (data == "") { - window.location.reload(); - $("#gDialog").dialog("close"); - } - $("#gDialog").html(data); + var form = $("#gDialog").find("form"); + var ajaxify_dialog = function() { + $(form).ajaxForm({ + complete: function(xhr, statusText) { + if (xhr.status == 201) { + $("#gDialog").dialog("close"); + window.location = xhr.getResponseHeader("Location"); + } else { + $("#gDialog").replaceWith(data.responseText); + ajaxify_dialog(); + } } }); }; + ajaxify_dialog(); + buttons["Submit"] = function() { + var form = $("#gDialog").find("form"); + form[0].submit(); + }; buttons["Reset"] = function() { var form = $("#gDialog").find("form"); form[0].reset(); @@ -140,18 +146,18 @@ function openDialog(element) { $("#gDialog").dialog('destroy').remove(); } }); - loading("#gDialog") + loading("#gDialog"); $(".ui-dialog-content").height(400); $("#gDialog").html(sHref); $.get(sHref, function(data) { - loading("#gDialog"); + loading("#gDialog"); $("#gDialog").html(data).hide().fadeIn(); // Get dialog and it's contents' height - var contentHt = $(".ui-dialog-titlebar").height() - + $(".ui-dialog-content form").height() - + $(".ui-dialog-buttonpane").height() + var contentHt = $(".ui-dialog-titlebar").height() + + $(".ui-dialog-content form").height() + + $(".ui-dialog-buttonpane").height() + 60; - // Resize height if content's shorter than dialog + // Resize height if content's shorter than dialog if (contentHt < $("#gDialog").data("height.dialog")) { $(".ui-dialog").animate({height: contentHt}); } @@ -161,7 +167,7 @@ function openDialog(element) { /** * Toggle the processing indicator, both large and small - * + * * @param element ID to which to apply the loading class, including # * @param size Either Large or Small */ |