Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge branch 'master' of git@github.com:gallery/gallery3 into bharat_dev
Conflicts:
	modules/gallery/controllers/rest.php
  • Loading branch information
bharat committed Nov 25, 2009
2 parents 2e42052 + dc67cf6 commit f50dbd9
Show file tree
Hide file tree
Showing 27 changed files with 69 additions and 894 deletions.
129 changes: 7 additions & 122 deletions modules/comment/controllers/comments.php
Expand Up @@ -17,49 +17,12 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
*/
class Comments_Controller extends REST_Controller {
protected $resource_type = "comment";

/**
* Display comments based on criteria.
* @see REST_Controller::_index()
*/
public function _index() {
$item = ORM::factory("item", $this->input->get('item_id'));
access::required("view", $item);

$comments = ORM::factory("comment")
->where("item_id", $item->id)
->where("state", "published")
->orderby("created", "DESC")
->find_all();

switch (rest::output_format()) {
case "json":
foreach ($comments as $comment) {
$data[] = array(
"id" => $comment->id,
"author_name" => html::clean($comment->author_name()),
"created" => $comment->created,
"text" => nl2br(html::purify($comment->text)));
}
print json_encode($data);
break;

case "html":
$view = new Theme_View("comments.html", "other", "comment");
$view->comments = $comments;
print $view;
break;
}
}

class Comments_Controller extends Controller {
/**
* Add a new comment to the collection.
* @see REST_Controller::_create($resource)
*/
public function _create($comment) {
$item = ORM::factory("item", $this->input->post("item_id"));
public function create($id) {
$item = ORM::factory("item", $id);
access::required("view", $item);

$form = comment::get_add_form($item);
Expand Down Expand Up @@ -96,105 +59,27 @@ public function _create($comment) {
}

$form->add_comment->text->value("");
print json_encode(
array("result" => "success",
"resource" => ($comment->state == "published"
? url::site("comments/{$comment->id}")
: null),
"form" => $form->__toString()));
} else {
print json_encode(
array("result" => "error",
"form" => $form->__toString()));
}
}

/**
* Display an existing comment.
* @todo Set proper Content-Type in a central place (REST_Controller::dispatch?).
* @see REST_Controller::_show($resource)
*/
public function _show($comment) {
$item = ORM::factory("item", $comment->item_id);
access::required("view", $item);
if ($comment->state != "published") {
return;
}

if (rest::output_format() == "json") {
print json_encode(
array("result" => "success",
"data" => array(
"id" => $comment->id,
"author_name" => html::clean($comment->author_name()),
"created" => $comment->created,
"text" => nl2br(html::purify($comment->text)))));
} else {
$view = new Theme_View("comment.html", "other", "comment-fragment");
$view->comment = $comment;
print $view;
}
}

/**
* Change an existing comment.
* @see REST_Controller::_update($resource)
*/
public function _update($comment) {
$item = ORM::factory("item", $comment->item_id);
access::required("view", $item);
access::required("edit", $item);

$form = comment::get_edit_form($comment);
if ($form->validate()) {
$comment->guest_name = $form->edit_comment->inputs["name"]->value;
$comment->guest_email = $form->edit_comment->email->value;
$comment->url = $form->edit_comment->url->value;
$comment->text = $form->edit_comment->text->value;
$comment->save();

print json_encode(
array("result" => "success",
"resource" => url::site("comments/{$comment->id}")));
"view" => $view->__toString(),
"form" => $form->__toString()));
} else {
print json_encode(
array("result" => "error",
"html" => $form->__toString()));
"form" => $form->__toString()));
}
}

/**
* Delete existing comment.
* @see REST_Controller::_delete($resource)
*/
public function _delete($comment) {
$item = ORM::factory("item", $comment->item_id);
access::required("view", $item);
access::required("edit", $item);

$comment->delete();
print json_encode(array("result" => "success"));
}

/**
* Present a form for adding a new comment to this item or editing an existing comment.
* @see REST_Controller::form_add($resource)
*/
public function _form_add($item_id) {
public function form_add($item_id) {
$item = ORM::factory("item", $item_id);
access::required("view", $item);

print comment::get_add_form($item);
}

/**
* Present a form for editing an existing comment.
* @see REST_Controller::form_edit($resource)
*/
public function _form_edit($comment) {
if (!identity::active_user()->admin) {
access::forbidden();
}
print comment::get_edit_form($comment);
}
}
26 changes: 1 addition & 25 deletions modules/comment/helpers/comment.php
Expand Up @@ -65,7 +65,7 @@ static function create($item, $author, $text, $guest_name=null,
}

static function get_add_form($item) {
$form = new Forge("comments", "", "post", array("id" => "g-comment-form"));
$form = new Forge("comments/create/{$item->id}", "", "post", array("id" => "g-comment-form"));
$group = $form->group("add_comment")->label(t("Add comment"));
$group->input("name") ->label(t("Name")) ->id("g-author");
$group->input("email") ->label(t("Email (hidden)")) ->id("g-email");
Expand All @@ -87,29 +87,5 @@ static function get_add_form($item) {

return $form;
}

static function get_edit_form($comment) {
$form = new Forge("comments/{$comment->id}?_method=put", "", "post",
array("id" => "g-edit-comment-form"));
$group = $form->group("edit_comment")->label(t("Edit comment"));
$group->input("name") ->label(t("Author")) ->id("g-author");
$group->input("email") ->label(t("Email (hidden)")) ->id("g-email");
$group->input("url") ->label(t("Website (hidden)"))->id("g-url");
$group->textarea("text")->label(t("Comment")) ->id("g-text");
$group->submit("")->value(t("Edit"));

$group->text = $comment->text;
$author = $comment->author();
if ($author->guest) {
$group->inputs["name"]->value = $comment->guest_name;
$group->email = $comment->guest_email;
$group->url = $comment->guest_url;
} else {
$group->inputs["name"]->value($author->full_name)->disabled("disabled");
$group->email->value($author->email)->disabled("disabled");
$group->url->value($author->url)->disabled("disabled");
}
return $form;
}
}

21 changes: 10 additions & 11 deletions modules/comment/js/comment.js
Expand Up @@ -28,17 +28,16 @@ function ajaxify_comment_form() {
$("#g-comments form").ajaxForm({
dataType: "json",
success: function(data) {
if (data.form) {
$("#g-comments form").replaceWith(data.form);
ajaxify_comment_form();
}
if (data.result == "success" && data.resource) {
$.get(data.resource, function(data, textStatus) {
$("#g-comments .g-block-content ul:first").append("<li>"+data+"</li>");
$("#g-comments .g-block-content ul:first li:last").effect("highlight", {color: "#cfc"}, 8000);
$("#g-comment-form").hide(2000).remove();
$("#g-no-comments-yet").hide(2000);
});
if (data.result == "success") {
$("#g-comments #g-comment-detail ul").append(data.view);
$("#g-comments #g-comment-detail ul li:last").effect("highlight", {color: "#cfc"}, 8000);
$("#g-comment-form").hide(2000).remove();
$("#g-no-comments-yet").hide(2000);
} else {
if (data.form) {
$("#g-comments form").replaceWith(data.form);
ajaxify_comment_form();
}
}
}
});
Expand Down
4 changes: 2 additions & 2 deletions modules/comment/views/comment.html.php
Expand Up @@ -8,9 +8,9 @@ class="g-avatar"
width="40"
height="40" />
</a>
<?= t("on %date_time, %author_name said",
<?= t("on %date_time, <a href=\"#\">%name</a> said",
array("date_time" => gallery::date_time($comment->created),
"author_name" => html::clean($comment->author_name()))) ?>
"name" => html::clean($comment->author_name()))) ?>
</p>
<div>
<?= nl2br(html::purify($comment->text)) ?>
Expand Down
91 changes: 9 additions & 82 deletions modules/gallery/controllers/albums.php
Expand Up @@ -18,10 +18,6 @@
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
*/
class Albums_Controller extends Items_Controller {

/**
* @see REST_Controller::_show($resource)
*/
public function _show($album) {
$page_size = module::get_var("gallery", "page_size", 9);
if (!access::can("view", $album)) {
Expand Down Expand Up @@ -83,27 +79,9 @@ public function _show($album) {
print $template;
}

/**
* @see REST_Controller::_create($resource)
*/
public function _create($album) {
public function create($parent_id) {
access::verify_csrf();
access::required("view", $album);
access::required("add", $album);

switch ($this->input->post("type")) {
case "album":
return $this->_create_album($album);

case "photo":
return $this->_create_photo($album);

default:
access::forbidden();
}
}

private function _create_album($album) {
$album = ORM::factory("item", $parent_id);
access::required("view", $album);
access::required("add", $album);

Expand All @@ -124,8 +102,7 @@ private function _create_album($album) {

print json_encode(
array("result" => "success",
"location" => $new_album->url(),
"resource" => $new_album->url()));
"location" => $new_album->url()));
} else {
print json_encode(
array(
Expand All @@ -134,43 +111,9 @@ private function _create_album($album) {
}
}

private function _create_photo($album) {
access::required("view", $album);
access::required("add", $album);

// If we set the content type as JSON, it triggers saving the result as
// a document in the browser (well, in Chrome at least).
// @todo figure out why and fix this.
$form = photo::get_add_form($album);
if ($form->validate()) {
$photo = photo::create(
$album,
$this->input->post("file"),
$_FILES["file"]["name"],
$this->input->post("title", $this->input->post("name")),
$this->input->post("description"),
identity::active_user()->id);

log::success("content", "Added a photo", html::anchor("photos/$photo->id", "view photo"));
message::success(t("Added photo %photo_title",
array("photo_title" => html::purify($photo->title))));

print json_encode(
array("result" => "success",
"resource" => $photo->url(),
"location" => $photo->url()));
} else {
print json_encode(
array("result" => "error",
"form" => $form->__toString()));
}
}

/**
* @see REST_Controller::_update($resource)
*/
public function _update($album) {
public function update($album_id) {
access::verify_csrf();
$album = ORM::factory("item", $album_id);
access::required("view", $album);
access::required("edit", $album);

Expand Down Expand Up @@ -230,32 +173,16 @@ public function _update($album) {
}
}

/**
* @see REST_Controller::_form_add($parameters)
*/
public function _form_add($album_id) {
public function form_add($album_id) {
$album = ORM::factory("item", $album_id);
access::required("view", $album);
access::required("add", $album);

switch ($this->input->get("type")) {
case "album":
print album::get_add_form($album);
break;

case "photo":
print photo::get_add_form($album);
break;

default:
kohana::show_404();
}
print album::get_add_form($album);
}

/**
* @see REST_Controller::_form_add($parameters)
*/
public function _form_edit($album) {
public function form_edit($album_id) {
$album = ORM::factory("item", $album_id);
access::required("view", $album);
access::required("edit", $album);

Expand Down
12 changes: 7 additions & 5 deletions modules/gallery/controllers/items.php
Expand Up @@ -17,14 +17,16 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
*/
class Items_Controller extends REST_Controller {
protected $resource_type = "item";

public function _show($item) {
class Items_Controller extends Controller {
public function __call($function, $args) {
$item = ORM::factory("item", (int)$function);
if (!$item->loaded) {
return Kohana::show_404();
}
// Redirect to the more specific resource type, since it will render
// differently. We could also just delegate here, but it feels more appropriate
// to have a single canonical resource mapping.
access::required("view", $item);
return url::redirect($item->abs_url());
return $this->_show($item);
}
}

0 comments on commit f50dbd9

Please sign in to comment.