Skip to content

Commit

Permalink
Refactor the album, movie and photo handling to remove the REST_Contr…
Browse files Browse the repository at this point in the history
…oller. Partial fix for ticket #917
  • Loading branch information
Tim Almdal committed Nov 25, 2009
1 parent 0bf81f0 commit 2d5c232
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 119 deletions.
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 @@ -82,27 +78,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 @@ -123,8 +101,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 @@ -133,43 +110,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 @@ -229,32 +172,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);
}
}
16 changes: 4 additions & 12 deletions modules/gallery/controllers/movies.php
Expand Up @@ -18,10 +18,6 @@
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
*/
class Movies_Controller extends Items_Controller {

/**
* @see REST_Controller::_show($resource)
*/
public function _show($movie) {
access::required("view", $movie);

Expand Down Expand Up @@ -53,11 +49,9 @@ public function _show($movie) {
print $template;
}

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

Expand Down Expand Up @@ -120,10 +114,8 @@ public function _update($movie) {
}
}

/**
* @see REST_Controller::_form_edit($resource)
*/
public function _form_edit($movie) {
public function form_edit($movie_id) {
$movie = ORM::factory("item", $movie_id);
access::required("view", $movie);
access::required("edit", $movie);
print movie::get_edit_form($movie);
Expand Down
17 changes: 4 additions & 13 deletions modules/gallery/controllers/photos.php
Expand Up @@ -18,10 +18,6 @@
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
*/
class Photos_Controller extends Items_Controller {

/**
* @see REST_Controller::_show($resource)
*/
public function _show($photo) {
access::required("view", $photo);

Expand Down Expand Up @@ -53,12 +49,9 @@ public function _show($photo) {
print $template;
}


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

Expand Down Expand Up @@ -125,10 +118,8 @@ public function _update($photo) {
}
}

/**
* @see REST_Controller::_form_edit($resource)
*/
public function _form_edit($photo) {
public function form_edit($photo_id) {
$photo = ORM::factory("item", $photo_id);
access::required("view", $photo);
access::required("edit", $photo);

Expand Down
4 changes: 2 additions & 2 deletions modules/gallery/helpers/album.php
Expand Up @@ -92,7 +92,7 @@ static function create($parent, $name, $title, $description=null, $owner_id=null
}

static function get_add_form($parent) {
$form = new Forge("albums/{$parent->id}", "", "post", array("id" => "g-add-album-form"));
$form = new Forge("albums/create/{$parent->id}", "", "post", array("id" => "g-add-album-form"));
$group = $form->group("add_album")
->label(t("Add an album to %album_title", array("album_title" => $parent->title)));
$group->input("title")->label(t("Title"));
Expand All @@ -114,7 +114,7 @@ static function get_add_form($parent) {
}

static function get_edit_form($parent) {
$form = new Forge("albums/{$parent->id}", "", "post", array("id" => "g-edit-album-form"));
$form = new Forge("albums/update/{$parent->id}", "", "post", array("id" => "g-edit-album-form"));
$form->hidden("_method")->value("put");
$group = $form->group("edit_item")->label(t("Edit Album"));

Expand Down
2 changes: 1 addition & 1 deletion modules/gallery/helpers/movie.php
Expand Up @@ -129,7 +129,7 @@ static function create($parent, $filename, $name, $title,
}

static function get_edit_form($movie) {
$form = new Forge("movies/$movie->id", "", "post", array("id" => "g-edit-movie-form"));
$form = new Forge("movies/update/$movie->id", "", "post", array("id" => "g-edit-movie-form"));
$form->hidden("_method")->value("put");
$group = $form->group("edit_item")->label(t("Edit Movie"));
$group->input("title")->label(t("Title"))->value($movie->title);
Expand Down
2 changes: 1 addition & 1 deletion modules/gallery/helpers/photo.php
Expand Up @@ -157,7 +157,7 @@ static function get_add_form($parent) {
}

static function get_edit_form($photo) {
$form = new Forge("photos/$photo->id", "", "post", array("id" => "g-edit-photo-form"));
$form = new Forge("photos/update/$photo->id", "", "post", array("id" => "g-edit-photo-form"));
$form->hidden("_method")->value("put");
$group = $form->group("edit_item")->label(t("Edit Photo"));
$group->input("title")->label(t("Title"))->value($photo->title);
Expand Down
3 changes: 2 additions & 1 deletion modules/gallery/tests/Albums_Controller_Test.php
Expand Up @@ -48,7 +48,8 @@ public function change_album_test() {
access::allow(identity::everybody(), "edit", $root);

ob_start();
$controller->_update($this->_album);
$controller->update($this->_album->id);
$this->_album->reload();
$results = ob_get_contents();
ob_end_clean();

Expand Down
3 changes: 2 additions & 1 deletion modules/gallery/tests/Photos_Controller_Test.php
Expand Up @@ -44,7 +44,8 @@ public function change_photo_test() {
access::allow(identity::everybody(), "edit", $root);

ob_start();
$controller->_update($photo);
$controller->update($photo->id);
$photo->reload();
$results = ob_get_contents();
ob_end_clean();

Expand Down
1 change: 0 additions & 1 deletion modules/gallery/tests/controller_auth_data.txt
Expand Up @@ -4,7 +4,6 @@ modules/digibug/controllers/digibug.php print_proxy
modules/digibug/controllers/digibug.php close_window DIRTY_AUTH
modules/gallery/controllers/admin.php __call DIRTY_AUTH
modules/gallery/controllers/albums.php _show DIRTY_CSRF
modules/gallery/controllers/albums.php _form_add DIRTY_CSRF
modules/gallery/controllers/combined.php javascript DIRTY_AUTH
modules/gallery/controllers/combined.php css DIRTY_AUTH
modules/gallery/controllers/file_proxy.php __call DIRTY_CSRF|DIRTY_AUTH
Expand Down

0 comments on commit 2d5c232

Please sign in to comment.