1
0

Implement the update functionality

This commit is contained in:
Tim Almdal 2009-12-21 11:30:38 -08:00
parent f0308bf695
commit 4c0cb783a2
10 changed files with 353 additions and 182 deletions

View File

@ -1,7 +1,25 @@
<?php defined('SYSPATH') OR die('No direct access allowed.'); <?php defined('SYSPATH') OR die('No direct access allowed.');
/** /**
* @package Core * Gallery - a web based photo album viewer and editor
* Copyright (C) 2000-2009 Bharat Mediratta
* *
* Sets the default route to "welcome" * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
*/ */
// Redirect edit requests to edit handler.
$config["^(edit|add|delete)_(\w+)(.*)$"] = "g3_handlers/$1/$2$3";
// Set the default route
$config['_default'] = 'g3_client'; $config['_default'] = 'g3_client';

View File

@ -0,0 +1,48 @@
<?php defined('SYSPATH') OR die('No direct access allowed.');
/**
* @package Session
*
* Session driver name.
*/
$config['driver'] = 'cookie';
/**
* Session storage parameter, used by drivers.
*/
$config['storage'] = '';
/**
* Session name.
* It must contain only alphanumeric characters and underscores. At least one letter must be present.
*/
$config['name'] = 'kohanasession';
/**
* Session parameters to validate: user_agent, ip_address, expiration.
*/
$config['validate'] = array('user_agent');
/**
* Enable or disable session encryption.
* Note: this has no effect on the native session driver.
*/
$config['encryption'] = FALSE;
/**
* Session lifetime. Number of seconds that each session will last.
* A value of 0 will keep the session active until the browser is closed (with a limit of 24h).
*/
$config['expiration'] = 43200; // 24 hours
/**
* Number of page loads before the session id is regenerated.
* A value of 0 will disable automatic session id regeneration.
* NOTE: Enabling automatic session regeneration can cause a race condition see the
* docs for details: http://docs.kohanaphp.com/libraries/session#regenerate
*/
$config['regenerate'] = 0;
/**
* Percentage probability that the gc (garbage collection) routine is started.
*/
$config['gc_probability'] = 2;

View File

@ -25,8 +25,8 @@ class G3_Client_Controller extends Template_Controller {
$this->template->title = 'G3 Web Client'; $this->template->title = 'G3 Web Client';
if (Session::instance()->get("g3_client_access_token")) { if (Session::instance()->get("g3_client_access_token")) {
$resource = G3Remote::instance()->get_resource("gallery"); $response = G3Remote::instance()->get_resource("gallery");
$this->template->content = $this->_get_main_view($resource); $this->template->content = $this->_get_main_view($response->resource);
} else { } else {
$this->template->content = new View('login.html'); $this->template->content = new View('login.html');
$this->template->content->errors = $this->template->content->form = $this->template->content->errors = $this->template->content->form =
@ -64,84 +64,16 @@ class G3_Client_Controller extends Template_Controller {
public function albums() { public function albums() {
$path = $this->input->get("path"); $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; $this->auto_render = false;
print $this->_get_album_tree($resource); print $this->_get_album_tree($response->resource);
} }
public function detail() { public function detail() {
$path = $this->input->get("path"); $path = $this->input->get("path");
$resource = G3Remote::instance()->get_resource("gallery/$path"); $response = G3Remote::instance()->get_resource("gallery/$path");
$this->auto_render = false; $this->auto_render = false;
print $this->_get_detail($resource); print $this->_get_detail($response->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;
} }
private function _get_album_tree($resource) { private function _get_album_tree($resource) {
@ -167,15 +99,10 @@ class G3_Client_Controller extends Template_Controller {
private function _get_detail($resource) { private function _get_detail($resource) {
$v = new View("{$resource->type}_detail.html"); $v = new View("{$resource->type}_detail.html");
$v->resource = $resource; $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) { if (strrpos($v->parent_path, "/") == strlen($v->parent_path) - 1) {
$v->parent_path = substr($v->parent_path, 0, -1); $v->parent_path = substr($v->parent_path, 0, -1);
} }
return $v; return $v;
} }
private function _extract_form_data($resource) {
return $form;
}
} // End G3 Client Controller } // End G3 Client Controller

View File

@ -0,0 +1,86 @@
<?php defined('SYSPATH') OR die('No direct access allowed.');
/**
* Gallery - a web based photo album viewer and editor
* Copyright (C) 2000-2009 Bharat Mediratta
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
*/
class G3_Handlers_Controller extends Controller {
public function edit($type) {
$path = $this->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

View File

@ -0,0 +1,65 @@
<?php defined('SYSPATH') OR die('No direct access allowed.');
/**
* Gallery - a web based photo album viewer and editor
* Copyright (C) 2000-2009 Bharat Mediratta
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
*/
class g3_client_Core {
static function get_form($type, $add_form, $path, $data=null) {
$form = new stdClass();
$form->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);
}
}

View File

@ -225,39 +225,46 @@ class G3Remote {
$param["limit"] = $limit; $param["limit"] = $limit;
} }
$headers = array(); return $this->_do_request("get", $path, $params);
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;
} }
public function delete_resource($path) { public function delete_resource($path) {
$request = "{$this->_config["gallery3_site"]}/$path"; return $this->_do_request("delete", $path);
$headers["X_GALLERY_REQUEST_METHOD"] = "DELETE"; }
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)) { if (!empty($this->_access_token)) {
$headers["X_GALLERY_REQUEST_KEY"] = $this->_access_token; $headers["X_GALLERY_REQUEST_KEY"] = $this->_access_token;
} }
list ($response_status, $response_headers, $response_body) = 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)) { if (url_connection::success($response_status)) {
$response = json_decode($response_body); $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}"); throw new Exception("Remote host failure: {$response->message}");
} }
} else { } else {
throw new Exception("Remote host failure: $response_status"); throw new Exception("Remote host failure: $response_status");
} }
return "success";
} }
} }

View File

@ -1,35 +1,50 @@
<?php defined("SYSPATH") or die("No direct script access.") ?> <?php defined("SYSPATH") or die("No direct script access.") ?>
<div id="wc-edit"> <div id="wc-edit">
<?= form::open($path) ?> <?= form::open("edit_{$type}?path=$path") ?>
<fieldset> <fieldset>
<legend style="display: none">Update Resource</legend> <legend style="display: none"><?= $title ?></legend>
<ul> <ul>
<li> <li>
<?= form::label("title", "Title:") ?><br/> <?= form::label("title", "{$form->title->label}:") ?><br/>
<?= form::input("title", $form["title"]["value"], "readonly={$form["title"]["readonly"]}") ?> <?= form::input("title", $form->title->value,
<?= empty($errors["title"]) ? "" : "<span class=\"error\">{$errors["title"]}</span>" ?> empty($form->title->readonly) ? "" : "readonly={$form->title->readonly}") ?>
</li> <?= empty($errors->title) ? "" : "<span class=\"error\">{$errors->title}</span>" ?>
<li> </li>
<?= form::label("description", "Description:") ?><br/> <li>
<?= form::textarea("description", $form["description"]["value"]) ?> <?= form::label("name", "{$form->name->label}:") ?><br/>
<?= empty($errors["description"]) ? "" : "<span class=\"error\">{$errors["description"]}</span>" ?> <?= form::input("name", $form->name->value,
</li> empty($form->name->readonly) ? "" : "readonly={$form->name->readonly}") ?>
<li> <?= empty($errors->name) ? "" : "<span class=\"error\">{$errors->name}</span>" ?>
<?= form::label("name", "Name:") ?><br/> </li>
<?= form::input("name", $form["name"]["value"], "readonly={$form["name"]["readonly"]}") ?> <li>
<?= empty($errors["name"]) ? "" : "<span class=\"error\">{$errors["name"]}</span>" ?> <?= form::label("description", "{$form->description->label}:") ?><br/>
</li> <?= form::textarea("description", $form->description->value) ?>
<li> <?= empty($errors->description) ? "" : "<span class=\"error\">{$errors->description}</span>" ?>
<?= form::label("slug", "Internet Address:") ?><br/> </li>
<?= form::input("slug", $form["slug"]["value"], "readonly={$form["slug"]["readonly"]}") ?> <li>
<?= empty($errors["slug"]) ? "" : "<span class=\"error\">{$errors["slug"]}</span>" ?> <?= form::label("slug", "{$form->slug->label}:") ?><br/>
</li> <?= form::input("slug", $form->slug->value,
<li style="text-align: center"> empty($form->slug->readonly) ? "" : "readonly={$form->slug->readonly}") ?>
<?= form::submit("submit", "Update") ?> <?= empty($errors->slug) ? "" : "<span class=\"error\">{$errors->slug}</span>" ?>
<?= form::input(array('type'=>'reset','name'=>'reset'), "Reset") ?> </li>
</li> <? if (!empty($form->image_file)): ?>
</ul> <li>
</fieldset> <?= form::label("image_file", "{$form->image_file->label}:") ?><br/>
</form> <?= form::upload("image_file") ?>
<?= empty($errors->image_file) ? "" : "<span class=\"error\">{$errors->image_file}</span>" ?>
</li>
<? endif ?>
<? if (!empty($errors->form_error)): ?>
<li>
<span class="error"><?= $errors->form_error ?></span>
</li>
<? endif ?>
<li style="text-align: center">
<?= form::submit("submit", "Update") ?>
<?= form::input(array('type'=>'reset','name'=>'reset'), "Reset") ?>
</li>
</ul>
</fieldset>
</form>
</div> </div>

View File

@ -29,6 +29,7 @@
<ul> <ul>
<li ref="add_album">Add Album</li> <li ref="add_album">Add Album</li>
<li ref="add_photo">Add Photo</li> <li ref="add_photo">Add Photo</li>
<li ref="add_movie">Add Movie</li>
</ul> </ul>
</div> </div>
</div> </div>

View File

@ -59,7 +59,8 @@ form legend {
} }
span.error { span.error {
color: #FF0000; color: #FF9933;
background-color: #880000;
} }
#wc-footer { #wc-footer {
@ -181,7 +182,7 @@ span.error {
} }
.ui-selected { .ui-selected {
background: #222222 url(images/ui-bg_glass_100_f6f6f6_1x400png) repeat-x scroll 50% 50%; background: #222222;
border: 1px solid #CCCCCC !important; border: 1px solid #CCCCCC !important;
color: #FF9933; color: #FF9933;
padding: 0 5px; padding: 0 5px;

View File

@ -139,15 +139,16 @@
break; break;
case "edit": case "edit":
case "delete": case "delete":
case "add_album": _open_dialog(action + "_" + resource_type, $("span", this).attr("ref"));
case "add_photo":
var url = /^add_.*/.test(action) ? action : action + "_" + resource_type;
_open_dialog(url, $("span", this).attr("ref"));
break; break;
default: default:
console.group("process toolbar button click: " + $(this).attr("ref")); if (/^add_.*/.test(action)) {
console.log(($("span", this).attr("ref"))); _open_dialog(action, $("span", this).attr("ref"));
console.groupEnd(); } else {
console.group("process toolbar button click: " + $(this).attr("ref"));
console.log(($("span", this).attr("ref")));
console.groupEnd();
}
} }
return false; return false;
}); });
@ -188,55 +189,57 @@
$("#g-dialog").dialog("destroy").remove(); $("#g-dialog").dialog("destroy").remove();
} }
}); });
$.get("/g3_client/index.php/g3_client/" + dialog, {path: resource_path}, function(data) { $.getJSON("/g3_client/index.php/" + dialog, {path: resource_path}, function(data) {
$("#g-dialog").html(data); $("#g-dialog").html(data.form);
$("#g-dialog").dialog("open"); $("#g-dialog").dialog("open");
if ($("#g-dialog fieldset legend").length) { if ($("#g-dialog fieldset legend").length) {
$("#g-dialog").dialog('option', 'title', $("#g-dialog fieldset legend:eq(0)").html()); $("#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) { function get_detail(path, callback) {
$.get("/g3_client/index.php/g3_client/detail", {path: path}, function(data, textStatus) { $.get("/g3_client/index.php/g3_client/detail", {path: path}, function(data, textStatus) {
$("#wc-detail").html(data); $("#wc-detail").html(data);