diff options
-rw-r--r-- | core/controllers/album.php | 6 | ||||
-rw-r--r-- | core/controllers/item.php | 6 | ||||
-rw-r--r-- | core/controllers/photo.php | 6 | ||||
-rw-r--r-- | core/controllers/rest.php | 13 | ||||
-rw-r--r-- | core/helpers/xml.php | 35 | ||||
-rw-r--r-- | modules/comment/controllers/comment.php | 23 | ||||
-rw-r--r-- | modules/comment/controllers/comments.php | 6 | ||||
-rw-r--r-- | modules/comment/helpers/comment.php | 36 | ||||
-rw-r--r-- | modules/user/controllers/user.php | 4 |
9 files changed, 98 insertions, 37 deletions
diff --git a/core/controllers/album.php b/core/controllers/album.php index 1701e283..a5853fa7 100644 --- a/core/controllers/album.php +++ b/core/controllers/album.php @@ -23,13 +23,13 @@ class Album_Controller extends Item_Controller { * @see Rest_Controller::_form($resource) */ public function _form($comment) { - throw new Exception("@todo Comment_Controller::_get NOT IMPLEMENTED"); + throw new Exception("@todo Album_Controller::_form NOT IMPLEMENTED"); } /** - * @see Rest_Controller::_get($resource) + * @see Rest_Controller::_get($resource, $output_format) */ - public function _get($item) { + public function _get($item, $output_format) { // @todo: these need to be pulled from the database $theme_name = "default"; $page_size = 9; diff --git a/core/controllers/item.php b/core/controllers/item.php index 053acc92..70af9d95 100644 --- a/core/controllers/item.php +++ b/core/controllers/item.php @@ -23,11 +23,11 @@ class Item_Controller extends REST_Controller { /** * @see Rest_Controller::_form($resource) */ - public function _form($comment) { - throw new Exception("@todo Comment_Controller::_get NOT IMPLEMENTED"); + public function _form($item) { + throw new Exception("@todo Comment_Controller::_form NOT IMPLEMENTED"); } - public function _get($item) { + public function _get($item, $format) { // 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. diff --git a/core/controllers/photo.php b/core/controllers/photo.php index 2a0153a2..819af133 100644 --- a/core/controllers/photo.php +++ b/core/controllers/photo.php @@ -23,13 +23,13 @@ class Photo_Controller extends Item_Controller { * @see Rest_Controller::_form($resource) */ public function _form($comment) { - throw new Exception("@todo Comment_Controller::_get NOT IMPLEMENTED"); + throw new Exception("@todo Comment_Controller::_form NOT IMPLEMENTED"); } /** - * @see Rest_Controller::_get($resource) + * @see Rest_Controller::_get($resource, $output_format) */ - public function _get($item) { + public function _get($item, $output_format) { $template = new View("page.html"); // @todo: this needs to be data-driven diff --git a/core/controllers/rest.php b/core/controllers/rest.php index 82262f3b..3b630f2d 100644 --- a/core/controllers/rest.php +++ b/core/controllers/rest.php @@ -24,19 +24,19 @@ * class Comment_Controller extends REST_Controller { * protected $resource_type = "comment"; // this tells REST which model to use * - * public function _get(ORM $comment) { + * public function _get(ORM $comment, $output_format) { * // Handle GET request * } * - * public function _put(ORM $comment) { + * public function _put(ORM $comment, $output_format) { * // Handle PUT request * } * - * public function _post(ORM $comment) { + * public function _post(ORM $comment, $output_format) { * // Handle POST request * } * - * public function _delete(ORM $comment) { + * public function _delete(ORM $comment, $output_format) { * // Handle DELETE request * } * @@ -67,8 +67,9 @@ abstract class REST_Controller extends Controller { * We're expecting to run in an environment that only supports GET/POST, so expect to tunnel * PUT/DELETE through POST. */ + $output_format = $this->input->get("_format", $this->input->post("_format", "html")); if (request::method() == "get") { - $this->_get($resource); + $this->_get($resource, $output_format); if (Session::instance()->get("use_profiler", false)) { $profiler = new Profiler(); @@ -107,7 +108,7 @@ abstract class REST_Controller extends Controller { * Perform a GET request on this resource * @param ORM $resource the instance of this resource type */ - abstract public function _get($resource); + abstract public function _get($resource, $output_format); /** * Perform a PUT request on this resource diff --git a/core/helpers/xml.php b/core/helpers/xml.php new file mode 100644 index 00000000..2700b5ae --- /dev/null +++ b/core/helpers/xml.php @@ -0,0 +1,35 @@ +<?php defined("SYSPATH") or die("No direct script access."); +/** + * Gallery - a web based photo album viewer and editor + * Copyright (C) 2000-2008 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 Xml_Core { + public static function to_xml($array, $element_names) { + $xml = "<$element_names[0]>\n"; + foreach ($array as $key => $value) { + if (is_array($value)) { + $xml .= xml::to_xml($value, array_slice($element_names, 1)); + } else if (is_object($value)) { + $xml .= xml::to_xml($value->as_array(), array_slice($element_names, 1)); + } else { + $xml .= "<$key>$value</$key>\n"; + } + } + $xml .= "</$element_names[0]>\n"; + return $xml; + } +} diff --git a/modules/comment/controllers/comment.php b/modules/comment/controllers/comment.php index 9a9ce84e..546f3d67 100644 --- a/modules/comment/controllers/comment.php +++ b/modules/comment/controllers/comment.php @@ -31,14 +31,26 @@ class Comment_Controller extends REST_Controller { /** * Get an existing comment. - * @see Rest_Controller::_get($resource) + * @see Rest_Controller::_get($resource, $output_format) */ - public function _get($comment) { - $v = new View("comment.html"); - $v->comment = $comment; - print $v; + public function _get($comment, $output_format) { + switch ($output_format) { + case "xml": + print xml::to_xml($comment->as_array(), array("comment")); + break; + + case "json": + print json_encode($comment->as_array()); + break; + + default: + $v = new View("comment.$output_format"); + $v->comment = $comment; + print $v; + } } + /** * Update existing comment. * @see Rest_Controller::_put($resource) @@ -46,7 +58,6 @@ class Comment_Controller extends REST_Controller { public function _put($comment) { $form = comment::get_edit_form($comment); if ($form->validate()) { - $comment = ORM::factory('comment'); $comment->author = $this->input->post('author'); $comment->email = $this->input->post('email'); $comment->text = $this->input->post('text'); diff --git a/modules/comment/controllers/comments.php b/modules/comment/controllers/comments.php index 60ab9b5d..ab3be76f 100644 --- a/modules/comment/controllers/comments.php +++ b/modules/comment/controllers/comments.php @@ -31,10 +31,10 @@ class Comments_Controller extends REST_Controller { /** * Show the comment collection - * @see Rest_Controller::_get($resource) + * @see Rest_Controller::_get($resource, $format) */ - public function _get($item) { - print comment::get_comments($item); + public function _get($item, $output_format) { + print comment::get_comments($item, $output_format); } /** diff --git a/modules/comment/helpers/comment.php b/modules/comment/helpers/comment.php index b5d57829..0bfd08d0 100644 --- a/modules/comment/helpers/comment.php +++ b/modules/comment/helpers/comment.php @@ -96,7 +96,7 @@ class Comment_Core { $block = new Block; $block->id = "gComment"; $block->title = _("Comments"); - $block->content = comment::get_comments($theme->item()); + $block->content = comment::get_comments($theme->item(), "html"); if ($show_add_form) { $block->content .= comment::get_add_form($theme->item())->render("form.html"); @@ -104,17 +104,31 @@ class Comment_Core { return $block; } - static function get_comments($item) { - $comments = array("<ul>"); - foreach (ORM::factory('comment')->where('item_id', $item->id) - ->orderby('datetime', 'asc') - ->find_all() as $comment) { - $v = new View("comment.html"); - $v->comment = $comment; - $comments[] = $v; + static function get_comments($item, $output_format) { + $comments = ORM::factory('comment')->where('item_id', $item->id) + ->orderby('datetime', 'asc') + ->find_all(); + + switch ($output_format) { + case "xml": + return xml::to_xml($comments, array("comments", "comment")); + break; + + case "json": + foreach ($comments as $comment) { + $data[] = $comment->as_array(); + } + return json_encode($data); + + default: + $html = array("<ul>"); + foreach ($comments as $comment) { + $v = new View("comment.html"); + $v->comment = $comment; + $html[] = $v; + } + return "<ul>\n" . implode("\n", $html) . "</ul>\n"; } - $comments[] = "</ul>"; - return implode("\n", $comments); } /** diff --git a/modules/user/controllers/user.php b/modules/user/controllers/user.php index 35498a04..bc7fb6ff 100644 --- a/modules/user/controllers/user.php +++ b/modules/user/controllers/user.php @@ -30,9 +30,9 @@ class User_Controller extends REST_Controller { } /** - * @see Rest_Controller::_get($resource) + * @see Rest_Controller::_get($resource, $format) */ - public function _get($user) { + public function _get($user, $format) { throw new Exception("@todo User_Controller::_get NOT IMPLEMENTED"); } |