diff options
Diffstat (limited to 'modules/gallery/controllers')
-rw-r--r-- | modules/gallery/controllers/admin_theme_options.php | 2 | ||||
-rw-r--r-- | modules/gallery/controllers/albums.php | 100 | ||||
-rw-r--r-- | modules/gallery/controllers/file_proxy.php | 2 | ||||
-rw-r--r-- | modules/gallery/controllers/items.php | 16 | ||||
-rw-r--r-- | modules/gallery/controllers/movies.php | 23 | ||||
-rw-r--r-- | modules/gallery/controllers/photos.php | 24 | ||||
-rw-r--r-- | modules/gallery/controllers/rest.php | 183 |
7 files changed, 51 insertions, 299 deletions
diff --git a/modules/gallery/controllers/admin_theme_options.php b/modules/gallery/controllers/admin_theme_options.php index 27a67bdb..9de54c78 100644 --- a/modules/gallery/controllers/admin_theme_options.php +++ b/modules/gallery/controllers/admin_theme_options.php @@ -58,6 +58,8 @@ class Admin_Theme_Options_Controller extends Admin_Controller { module::set_var("gallery", "footer_text", $form->edit_theme->footer_text->value); module::set_var("gallery", "show_credits", $form->edit_theme->show_credits->value); + module::event("theme_edit_form_completed", $form); + message::success(t("Updated theme details")); url::redirect("admin/theme_options"); } else { diff --git a/modules/gallery/controllers/albums.php b/modules/gallery/controllers/albums.php index e67df6f6..3c1a0adf 100644 --- a/modules/gallery/controllers/albums.php +++ b/modules/gallery/controllers/albums.php @@ -18,11 +18,16 @@ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ class Albums_Controller extends Items_Controller { + public function index() { + $this->show(ORM::factory("item", 1)); + } - /** - * @see REST_Controller::_show($resource) - */ - public function _show($album) { + public function show($album) { + if (!is_object($album)) { + // show() must be public because we route to it in url::parse_url(), so make + // sure that we're actually receiving an object + Kohana::show_404(); + } $page_size = module::get_var("gallery", "page_size", 9); if (!access::can("view", $album)) { if ($album->id == 1) { @@ -82,27 +87,9 @@ class Albums_Controller extends Items_Controller { 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); @@ -123,8 +110,7 @@ class Albums_Controller extends Items_Controller { print json_encode( array("result" => "success", - "location" => $new_album->url(), - "resource" => $new_album->url())); + "location" => $new_album->url())); } else { print json_encode( array( @@ -133,43 +119,9 @@ class Albums_Controller extends Items_Controller { } } - 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); @@ -229,32 +181,16 @@ class Albums_Controller extends Items_Controller { } } - /** - * @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); diff --git a/modules/gallery/controllers/file_proxy.php b/modules/gallery/controllers/file_proxy.php index acfd6eb9..8fde1132 100644 --- a/modules/gallery/controllers/file_proxy.php +++ b/modules/gallery/controllers/file_proxy.php @@ -112,7 +112,7 @@ class File_Proxy_Controller extends Controller { Session::abort_save(); // Dump out the image. If the item is a movie, then its thumbnail will be a JPG. - if (in_array($item->mime_type, array("video/x-flv", "video/mp4"))) { + if ($item->is_movie() && $type != "albums") { header("Content-type: image/jpeg"); } else { header("Content-Type: $item->mime_type"); diff --git a/modules/gallery/controllers/items.php b/modules/gallery/controllers/items.php index 7f60f2b7..b350c5a2 100644 --- a/modules/gallery/controllers/items.php +++ b/modules/gallery/controllers/items.php @@ -17,14 +17,18 @@ * 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"; +class Items_Controller extends Controller { + public function __call($function, $args) { + $item = ORM::factory("item", (int)$function); + if (!$item->loaded) { + return Kohana::show_404(); + } - public function _show($item) { // 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. + // differently. We can't delegate here because we may have gotten to this + // page via /items/<id> which means that we don't have a type-specific controller. Also, we + // want to drive a single canonical resource mapping where possible. access::required("view", $item); - return url::redirect($item->abs_url()); + url::redirect($item->abs_url()); } } diff --git a/modules/gallery/controllers/movies.php b/modules/gallery/controllers/movies.php index 2e2e837c..575b2b60 100644 --- a/modules/gallery/controllers/movies.php +++ b/modules/gallery/controllers/movies.php @@ -18,11 +18,12 @@ * 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) { + public function show($movie) { + if (!is_object($movie)) { + // show() must be public because we route to it in url::parse_url(), so make + // sure that we're actually receiving an object + Kohana::show_404(); + } access::required("view", $movie); $where = array("type != " => "album"); @@ -53,11 +54,9 @@ class Movies_Controller extends Items_Controller { 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); @@ -120,10 +119,8 @@ class Movies_Controller extends Items_Controller { } } - /** - * @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); diff --git a/modules/gallery/controllers/photos.php b/modules/gallery/controllers/photos.php index 0c2ff6ee..ba4cfb83 100644 --- a/modules/gallery/controllers/photos.php +++ b/modules/gallery/controllers/photos.php @@ -18,11 +18,12 @@ * 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) { + public function show($photo) { + if (!is_object($photo)) { + // show() must be public because we route to it in url::parse_url(), so make + // sure that we're actually receiving an object + Kohana::show_404(); + } access::required("view", $photo); $where = array("type != " => "album"); @@ -53,12 +54,9 @@ class Photos_Controller extends Items_Controller { 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); @@ -125,10 +123,8 @@ class Photos_Controller extends Items_Controller { } } - /** - * @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); diff --git a/modules/gallery/controllers/rest.php b/modules/gallery/controllers/rest.php deleted file mode 100644 index 2edf079f..00000000 --- a/modules/gallery/controllers/rest.php +++ /dev/null @@ -1,183 +0,0 @@ -<?php defined("SYSPATH") or die("No direct script access."); -/** - * 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. - */ -/** - * This abstract controller makes it easy to create a RESTful controller. To use it, create a - * subclass which defines the resource type and implements get/post/put/delete methods, like this: - * - * class Comment_Controller extends REST_Controller { - * protected $resource_type = "comment"; // this tells REST which model to use - * - * public function _index() { - * // Handle GET request to /controller - * } - * - * public function _show(ORM $comment) { - * // Handle GET request to /comments/{comment_id} - * } - * - * public function _update(ORM $comment) { - * // Handle PUT request to /comments/{comment_id} - * } - * - * public function _create(ORM $comment) { - * // Handle POST request to /comments - * } - * - * public function _delete(ORM $comment) { - * // Handle DELETE request to /comments/{comments_id} - * } - * - * public function _form_add($parameters) { - * // Handle GET request to /form/add/comments - * // Show a form for creating a new comment - * } - * - * public function _form_edit(ORM $comment) { - * // Handle GET request to /form/edit/comments - * // Show a form for editing an existing comment - * } - * - * A request to http://example.com/gallery3/comments/3 will result in a call to - * REST_Controller::__call(3) which will load up the comment associated with id 3. If there's - * no such comment, it returns a 404. Otherwise, it will then delegate to - * Comment_Controller::get() with the ORM instance as an argument. - */ -class REST_Controller extends Controller { - protected $resource_type = null; - - public function __construct() { - if ($this->resource_type == null) { - throw new Exception("@todo ERROR_MISSING_RESOURCE_TYPE"); - } - parent::__construct(); - } - - /** - * Handle dispatching for all REST controllers. - */ - public function __call($function, $args) { - // If no parameter was provided after the controller name (eg "/albums") then $function will - // be set to "index". Otherwise, $function is the first parameter, and $args are all - // subsequent parameters. - $request_method = rest::request_method(); - if ($function == "index" && $request_method == "get") { - return $this->_index(); - } - - $resource = ORM::factory($this->resource_type, (int)$function); - if (!$resource->loaded && $request_method != "post") { - return Kohana::show_404(); - } - - switch ($request_method) { - case "get": - return $this->_show($resource); - - case "put": - access::verify_csrf(); - return $this->_update($resource); - - case "delete": - access::verify_csrf(); - return $this->_delete($resource); - - case "post": - access::verify_csrf(); - return $this->_create($resource); - } - } - - /* We're editing an existing item, load it from the database. */ - public function form_edit($resource_id) { - if ($this->resource_type == null) { - throw new Exception("@todo ERROR_MISSING_RESOURCE_TYPE"); - } - - $resource = ORM::factory($this->resource_type, $resource_id); - if (!$resource->loaded) { - return Kohana::show_404(); - } - - // Security checks must be performed in _form_edit - return $this->_form_edit($resource); - } - - /* We're adding a new item, pass along any additional parameters. */ - public function form_add($parameters) { - // Security checks must be performed in _form_add - return $this->_form_add($parameters); - } - - /** - * Perform a GET request on the controller root - * (e.g. http://www.example.com/gallery3/comments) - */ - public function _index() { - throw new Exception("@todo _create NOT IMPLEMENTED"); - } - - /** - * Perform a POST request on this resource - * @param ORM $resource the instance of this resource type - */ - public function _create($resource) { - throw new Exception("@todo _create NOT IMPLEMENTED"); - } - - /** - * Perform a GET request on this resource - * @param ORM $resource the instance of this resource type - */ - public function _show($resource) { - throw new Exception("@todo _show NOT IMPLEMENTED"); - } - - /** - * Perform a PUT request on this resource - * @param ORM $resource the instance of this resource type - */ - public function _update($resource) { - throw new Exception("@todo _update NOT IMPLEMENTED"); - } - - /** - * Perform a DELETE request on this resource - * @param ORM $resource the instance of this resource type - */ - public function _delete($resource) { - throw new Exception("@todo _delete NOT IMPLEMENTED"); - } - - /** - * Present a form for adding a new resource - * @param string part of the URI after the controller name - */ - public function _form_add($parameter) { - throw new Exception("@todo _form_add NOT IMPLEMENTED"); - } - - /** - * Present a form for editing an existing resource - * @param ORM $resource the resource container for instances of this resource type - */ - public function _form_edit($resource) { - throw new Exception("@todo _form_edit NOT IMPLEMENTED"); - } -} |