diff --git a/core/config/routes.php b/core/config/routes.php index 24e2d73c..de66b8fb 100644 --- a/core/config/routes.php +++ b/core/config/routes.php @@ -27,3 +27,8 @@ $config['^(\w+)/(\d+)$'] = '$1/dispatch/$2'; // For now our default page is the scaffolding. $config['_default'] = 'welcome'; + +// Special routes for the comment module. +// @todo Dynamically load this. +$config['photo/(\d+)/comments/add$'] = 'comment/add/$1'; +$config['photo/(\d+)/comments$'] = 'comment/get_item_comments/$1'; diff --git a/modules/comment/controllers/comment.php b/modules/comment/controllers/comment.php index dd2b44f9..fab18498 100644 --- a/modules/comment/controllers/comment.php +++ b/modules/comment/controllers/comment.php @@ -17,6 +17,104 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ -class Comment_Controller extends Controller { +class Comment_Controller extends REST_Controller { + protected $resource_type = "comment"; + /** + * Return the form for adding comments. + */ + public function _get_form($comment) { + $form = new Forge(url::current(true), "", "post", array("id" => "gComment")); + $group = $form->group(_("Add Comment")); + $group->input("author") + ->label(_("Author")) + ->id("gAuthor") + ->class(null) + ->value($comment->author); + $group->input("email") + ->label(_("Email")) + ->id("gEmail") + ->class(null) + ->value($comment->email); + $group->textarea("text") + ->label(_("Text")) + ->id("gText") + ->class(null) + ->value($comment->text); + $group->hidden("item_id") + ->value($comment->item_id); + $group->submit(_("Add")); + + $this->_add_validation_rules(ORM::factory("comment")->validation_rules, $form); + + return $form; + } + + /** + * @todo Refactor this into a more generic location + */ + private function _add_validation_rules($rules, $form) { + foreach ($form->inputs as $name => $input) { + if (isset($input->inputs)) { + $this->_add_validation_rules($rules, $input); + } + if (isset($rules[$name])) { + $input->rules($rules[$name]); + } + } + } + + public function add($item_id) { + $comment = ORM::factory('comment'); + $comment->item_id = $item_id; + + $form = $this->_get_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'); + $comment->datetime = time(); + $comment->item_id = $this->input->post('item_id'); + $comment->save(); + } else { + print $form->render("form.html"); + } + } + + public function get_item_comments($item_id) { + $v = comment::show_comment_list($item_id); + print $v; + } + + /** + * Get an existing comment. + * @see Rest_Controller::_get($resource) + */ + public function _get($user) { + throw new Exception("@todo Comment_Controller::_get NOT IMPLEMENTED"); + } + + /** + * Update existing comment. + * @see Rest_Controller::_put($resource) + */ + public function _put($resource) { + throw new Exception("@todo Comment_Controller::_put NOT IMPLEMENTED"); + } + + /** + * @see Rest_Controller::_post($resource) + */ + public function _post($user) { + throw new Exception("@todo Comment_Controller::_post NOT IMPLEMENTED"); + } + + /** + * Delete existing comment. + * @see Rest_Controller::_delete($resource) + */ + public function _delete($resource) { + throw new Exception("@todo Comment_Controller::_delete NOT IMPLEMENTED"); + } } diff --git a/modules/comment/models/comment.php b/modules/comment/models/comment.php index 4db09a5b..2ed2b83c 100644 --- a/modules/comment/models/comment.php +++ b/modules/comment/models/comment.php @@ -18,4 +18,8 @@ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ class Comment_Model extends ORM { + var $validation_rules = array( + "author" => "required", + "email" => "required|valid_email", + "text" => "required"); } diff --git a/modules/comment/views/comment_form.html.php b/modules/comment/views/comment_form.html.php index 1561035f..cc2c4028 100644 --- a/modules/comment/views/comment_form.html.php +++ b/modules/comment/views/comment_form.html.php @@ -1,25 +1,40 @@ defined("SYSPATH") or die("No direct script access."); ?> -
+ + + ')"> + = _("Add Comment") ?> + + + diff --git a/modules/comment/views/comment_list.html.php b/modules/comment/views/comment_list.html.php index ea824597..81c5691e 100644 --- a/modules/comment/views/comment_list.html.php +++ b/modules/comment/views/comment_list.html.php @@ -1,15 +1,13 @@ defined("SYSPATH") or die("No direct script access."); ?> -- = $comment->author ?> - = comment::format_elapsed_time($comment->datetime) ?>, - = strftime('%c', $comment->datetime) ?> -
-+ = $comment->author ?> + = comment::format_elapsed_time($comment->datetime) ?>, + = strftime('%c', $comment->datetime) ?> +
+