diff --git a/web_client/application/config/routes.php b/web_client/application/config/routes.php index b5e9399a..32e5cccc 100644 --- a/web_client/application/config/routes.php +++ b/web_client/application/config/routes.php @@ -1,7 +1,25 @@ template->title = 'G3 Web Client'; if (Session::instance()->get("g3_client_access_token")) { - $resource = G3Remote::instance()->get_resource("gallery"); - $this->template->content = $this->_get_main_view($resource); + $response = G3Remote::instance()->get_resource("gallery"); + $this->template->content = $this->_get_main_view($response->resource); } else { $this->template->content = new View('login.html'); $this->template->content->errors = $this->template->content->form = @@ -64,84 +64,16 @@ class G3_Client_Controller extends Template_Controller { public function albums() { $path = $this->input->get("path"); - $resource = G3Remote::instance()->get_resource("gallery/$path", "album"); + $response = G3Remote::instance()->get_resource("gallery/$path", "album"); $this->auto_render = false; - print $this->_get_album_tree($resource); + print $this->_get_album_tree($response->resource); } public function detail() { $path = $this->input->get("path"); - $resource = G3Remote::instance()->get_resource("gallery/$path"); + $response = G3Remote::instance()->get_resource("gallery/$path"); $this->auto_render = false; - print $this->_get_detail($resource); - } - - public function __call($function, $args) { - $path = $this->input->get("path"); - $resource = G3Remote::instance()->get_resource("gallery/$path"); - - $this->auto_render = false; - switch ($function) { - case "edit_album": - case "edit_photo": - $readonly = empty($resource->path) ? "readonly" : ""; - $form = array("name" => array("value" => $resource->name, "readonly" => $readonly), - "description" => array("value" => $resource->description, - "readonly" => $readonly), - "slug" => array("value" => $resource->internet_address, - "readonly" => $readonly), - "title" => array("value" => $resource->title, "readonly" => $readonly)); - $errors = array_fill_keys(array_keys($form), ""); - - if ($_POST) { - } else { - $v = new View("edit.html"); - $v->form = $form; - $v->errors = $errors; - $v->path = "g3_client/$function/?path=$path"; - $v->type = $resource->type; - } - break; - case "add_album": - case "add_photo": - $errors = $form = array( - "name" => "", - "description" => "", - "slug" => "", - "image_file" => "", - "title" => ""); - if ($_POST) { - } else { - $v = new View("add.html"); - $v->form = $form; - $v->errors = $errors; - $v->path = "g3_client/$function/?path=$path"; - $v->function = $function; - $function_parts = explode("_", $function); - $v->type = $function_parts[1]; - } - break; - case "delete_album": - case "delete_photo": - if ($_POST) { - try { - $result = G3Remote::instance()->delete_resource("gallery/$path"); - print json_encode(array("result" => $result, "path" => $resource->parent_path)); - } catch (Exception $e) { - print json_encode(array("result" => "fail", "message" => $e->getMessage())); - } - return; - } else { - $v = new View("delete.html"); - $v->title = $resource->title; - $v->path = "g3_client/$function/?path=$path"; - } - break; - default: - throw new Kohana_404_Exception(); - } - - print $v; + print $this->_get_detail($response->resource); } private function _get_album_tree($resource) { @@ -167,15 +99,10 @@ class G3_Client_Controller extends Template_Controller { private function _get_detail($resource) { $v = new View("{$resource->type}_detail.html"); $v->resource = $resource; - $v->parent_path = substr($resource->path, 0, -strlen($resource->internet_address)); + $v->parent_path = substr($resource->path, 0, -strlen($resource->slug)); if (strrpos($v->parent_path, "/") == strlen($v->parent_path) - 1) { $v->parent_path = substr($v->parent_path, 0, -1); } return $v; } - - private function _extract_form_data($resource) { - return $form; - } - } // End G3 Client Controller diff --git a/web_client/application/controllers/g3_handlers.php b/web_client/application/controllers/g3_handlers.php new file mode 100644 index 00000000..09d1a447 --- /dev/null +++ b/web_client/application/controllers/g3_handlers.php @@ -0,0 +1,86 @@ +input->get("path"); + if ($_POST) { + try { + unset($_POST["submit"]); + $result = G3Remote::instance()->update_resource("gallery/$path", $_POST); + if ($result->status == "OK") { + $form = null; + $result = "success"; + } else { + $form = g3_client::get_form($type, false, $path, (object)$_POST); + foreach (array_keys($_POST) as $field) { + if (isset($result->fields->$field)) { + $form->errors[$field] = $result->fields->$field; + } + } + $result = "display"; + } + } catch (Exception $e) { + $form = g3_client::get_form($type, false, $path, (object)$_POST); + $form->errors["form_error"] = $e->getMessage(); + $result = "error"; + } + } else { + $response = G3Remote::instance()->get_resource("gallery/$path"); + $form = g3_client::get_form($type, false, $path, $response->resource); + $result = "display"; + } + + print g3_client::format_response($type, $path, $form, $result); + } + + public function add($type) { + $path = $this->input->get("path"); + $form = g3_client::get_form($type, true); + $result = array(); + if ($_POST) { + $form->errors["form_error"] = "Add $type not implemented."; + $result = "error"; + } else { + $result = "display"; + } + + print g3_client::format_response($type, $path, $form, $result); + } + + public function delete($type) { + $path = $this->input->get("path"); + if ($_POST) { + try { + $response = G3Remote::instance()->delete_resource("gallery/$path"); + print json_encode(array("result" => "success", "path" => $response->resource->parent_path, + "type" => $type)); + } catch (Exception $e) { + print json_encode(array("result" => "fail", "message" => $e->getMessage())); + } + } else { + $response = G3Remote::instance()->get_resource("gallery/$path"); + $v = new View("delete.html"); + $v->title = $response->resource->title; + $v->path = "g3_client/delete_album/?path=$path"; + } + + print json_encode(array("form" => (string)$v)); + } +} // End G3 Album Controller diff --git a/web_client/application/helpers/g3_client.php b/web_client/application/helpers/g3_client.php new file mode 100644 index 00000000..608c289f --- /dev/null +++ b/web_client/application/helpers/g3_client.php @@ -0,0 +1,65 @@ +form = array("title" => (object)array("value" => "", "label" => "Title"), + "name" => (object)array("value" => "", "label" => "Name"), + "description" => (object)array("value" => "", "label" => "Description"), + "slug" => (object)array("value" => "", "label" => "Internet Address")); + // @todo add sort column sort order fields + $form->errors = array("title" => "", "name" => "", "description" => "", "slug" => ""); + if ($type != "album" && $add_form) { + $form->form["image_file"] = (object)array("value" => "", "label" => "Image File"); + $form->errors["image_file"] = ""; + } + + if (empty($path) && !$add_form) { + $form->form["name"]->readonly = $form->form["slug"]->readonly = "readonly"; + } + + if ($data) { + foreach (array_keys($form->form) as $field) { + if (isset($data->$field)) { + $form->form[$field]->value = $data->$field; + } + } + } + return $form; + } + + static function format_response($type, $path, $form, $result) { + $json = (object)array("result" => $result); + if ($result != "success") { + $json->form = new View("edit.html"); + $json->form->title = "Update " . ucwords($type); + $json->form->path = $path; + $json->form->type = $type; + $json->form->form = (object)$form->form; + $json->form->errors = (object)$form->errors; + $json->form = (string)$json->form; + } else { + $json->path = $path; + $json->type = $type; + } + + return json_encode($json); + } +} diff --git a/web_client/application/libraries/G3Remote.php b/web_client/application/libraries/G3Remote.php index 32042566..98b964ea 100644 --- a/web_client/application/libraries/G3Remote.php +++ b/web_client/application/libraries/G3Remote.php @@ -225,39 +225,46 @@ class G3Remote { $param["limit"] = $limit; } - $headers = array(); - if (!empty($this->_access_token)) { - $headers["X_GALLERY_REQUEST_KEY"] = $this->_access_token; - } - list ($response_status, $response_headers, $response_body) = - url_connection::get($request, $params, $headers); - if (url_connection::success($response_status)) { - $response = json_decode($response_body); - if ($response->status != "OK") { - throw new Exception("Remote host failure: {$response->message}"); - } - } else { - throw new Exception("Remote host failure: $response_status"); - } - return $response->resource; + return $this->_do_request("get", $path, $params); } public function delete_resource($path) { - $request = "{$this->_config["gallery3_site"]}/$path"; - $headers["X_GALLERY_REQUEST_METHOD"] = "DELETE"; + return $this->_do_request("delete", $path); + } + + public function update_resource($path, $params) { + return $this->_do_request("put", $path, $params); + } + + public function add_resource($path, $params) { + return $this->_do_request("post", $path, $params); + } + + private function _do_request($method, $path, $params=array()) { + $request_path = "{$this->_config["gallery3_site"]}/$path"; + $headers = array(); + if ($method == "put" || $method == "delete") { + $headers["X_GALLERY_REQUEST_METHOD"] = $method; + $method = "post"; + } if (!empty($this->_access_token)) { $headers["X_GALLERY_REQUEST_KEY"] = $this->_access_token; } + list ($response_status, $response_headers, $response_body) = - url_connection::post($request, array(), $headers); + call_user_func("url_connection::$method", $request_path, $params, $headers); + if (url_connection::success($response_status)) { $response = json_decode($response_body); - if ($response->status != "OK") { + switch ($response->status) { + case "OK": + case "VALIDATE_ERROR": + return $response; + default: throw new Exception("Remote host failure: {$response->message}"); } } else { throw new Exception("Remote host failure: $response_status"); } - return "success"; } } diff --git a/web_client/application/views/edit.html.php b/web_client/application/views/edit.html.php index f90907a3..39fb1576 100644 --- a/web_client/application/views/edit.html.php +++ b/web_client/application/views/edit.html.php @@ -1,35 +1,50 @@
- -
- Update Resource - -
- + +
+ + +
+
diff --git a/web_client/application/views/main.html.php b/web_client/application/views/main.html.php index 52e911e1..5ddf0b9d 100644 --- a/web_client/application/views/main.html.php +++ b/web_client/application/views/main.html.php @@ -29,6 +29,7 @@ diff --git a/web_client/css/g3_client.css b/web_client/css/g3_client.css index 2f0e91b6..02e1b4ed 100644 --- a/web_client/css/g3_client.css +++ b/web_client/css/g3_client.css @@ -59,7 +59,8 @@ form legend { } span.error { - color: #FF0000; + color: #FF9933; + background-color: #880000; } #wc-footer { @@ -181,7 +182,7 @@ span.error { } .ui-selected { - background: #222222 url(images/ui-bg_glass_100_f6f6f6_1x400png) repeat-x scroll 50% 50%; + background: #222222; border: 1px solid #CCCCCC !important; color: #FF9933; padding: 0 5px; diff --git a/web_client/js/g3_client.js b/web_client/js/g3_client.js index 61b177dd..af1c3531 100644 --- a/web_client/js/g3_client.js +++ b/web_client/js/g3_client.js @@ -139,15 +139,16 @@ break; case "edit": case "delete": - case "add_album": - case "add_photo": - var url = /^add_.*/.test(action) ? action : action + "_" + resource_type; - _open_dialog(url, $("span", this).attr("ref")); + _open_dialog(action + "_" + resource_type, $("span", this).attr("ref")); break; default: - console.group("process toolbar button click: " + $(this).attr("ref")); - console.log(($("span", this).attr("ref"))); - console.groupEnd(); + if (/^add_.*/.test(action)) { + _open_dialog(action, $("span", this).attr("ref")); + } else { + console.group("process toolbar button click: " + $(this).attr("ref")); + console.log(($("span", this).attr("ref"))); + console.groupEnd(); + } } return false; }); @@ -188,55 +189,57 @@ $("#g-dialog").dialog("destroy").remove(); } }); - $.get("/g3_client/index.php/g3_client/" + dialog, {path: resource_path}, function(data) { - $("#g-dialog").html(data); + $.getJSON("/g3_client/index.php/" + dialog, {path: resource_path}, function(data) { + $("#g-dialog").html(data.form); $("#g-dialog").dialog("open"); if ($("#g-dialog fieldset legend").length) { $("#g-dialog").dialog('option', 'title', $("#g-dialog fieldset legend:eq(0)").html()); } + _ajaxifyDialog(); - if ($("#g-dialog form").length) { - $("#g-dialog form").ajaxForm({ - dataType: "json", - beforeSubmit: function(formData, form, options) { - form.find(":submit") - .addClass("ui-state-disabled") - .attr("disabled", "disabled"); - return true; - }, - success: function(data) { - if (data.form) { - $("#g-dialog form").replaceWith(data.form); - $("#g-dialog form :submit").removeClass("ui-state-disabled") - .attr("disabled", null); - self._ajaxify_dialog(); - self.form_loaded(null, $("#g-dialog form")); - if (typeof data.reset == 'function') { - eval(data.reset + '()'); - } - } - if (data.result == "success") { - $("#g-dialog").dialog('close'); - get_detail(data.path, _set_active_album); - if (dialog == "delete_album") { - var parent = $("#album_tree li[ref=" + resource_path + "]").parents("li:first"); - $.get("/g3_client/index.php/g3_client/albums", - {path: $(parent).attr("ref")}, - function(data, textStatus) { - $(parent).replaceWith(data); - }); - } - } else if (data.result == "fail") { - $("#g-dialog").dialog('close'); - alert(data.message); - } - } - }); - } }); } + function _ajaxifyDialog() { + if ($("#g-dialog form").length) { + $("#g-dialog form").ajaxForm({ + dataType: "json", + beforeSubmit: function(formData, form, options) { + form.find(":submit") + .addClass("ui-state-disabled") + .attr("disabled", "disabled"); + return true; + }, + success: function(data) { + if (data.form) { + $("#g-dialog form").replaceWith(data.form); + $("#g-dialog form :submit").removeClass("ui-state-disabled") + .attr("disabled", null); + _ajaxifyDialog(); + } + if (data.result == "success") { + $("#g-dialog").dialog('close'); + get_detail(data.path, _set_active_album); + if (data.type == "album") { + var path = data.path; + $.get("/g3_client/index.php/g3_client/albums", + {path: path}, + function(data, textStatus) { + var selector = "#album_tree li[ref=" + path + "]"; + $(selector).replaceWith(data); + $(selector + " .tree-title:first").addClass("ui-selected"); + }); + } + } else if (data.result == "fail") { + $("#g-dialog").dialog('close'); + alert(data.message); + } + } + }); + } + } + function get_detail(path, callback) { $.get("/g3_client/index.php/g3_client/detail", {path: path}, function(data, textStatus) { $("#wc-detail").html(data);