"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(); $data = array('valid' => true, 'html' => sprintf(comment::show_comment_list($item_id))); } else { $data = array('valid' => false, 'html' => sprintf($form->render())); } if (request::method() == "get") { print $data['html']; } else if (request::method() == "post") { print json_encode($data); } } public function get_item_comments($item_id) { print comment::show_comment_list($item_id); } /** * 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"); } }