/** * Create comment common api controller * * @access protected * @param string $parent_table target parent table * @param int $parent_id target parent record id * @param string $member_related related table for get member_id * @return Response (json) * @throws Exception in Controller_Base::controller_common_api * @see Controller_Base::controller_common_api */ protected function api_create_comment_common($parent_table, $parent_id = null, $member_related = null) { $this->controller_common_api(function () use($parent_table, $parent_id, $member_related) { $parent_id = intval(\Input::post('id') ?: $parent_id); $parent_model = Site_Model::get_model_name($parent_table); $parent_obj = $parent_model::check_authority($parent_id); $this->check_browse_authority($parent_obj->public_flag, $member_related ? $parent_obj->{$member_related}->member_id : $parent_obj->member_id); $parent_id_prop = $parent_table . '_id'; $model = Site_Model::get_model_name($parent_table . '_comment'); // Lazy validation $body = trim(\Input::post('body', '')); if (!strlen($body)) { throw new \ValidationFailedException(sprintf('%sの入力は%sです。', term('form.comment'), term('form.required'))); } if ($parent_table == 'timeline' && \Timeline\Site_Util::check_type_for_post_foreign_table_comment($parent_obj->type)) { throw new \HttpInvalidInputException(); } \DB::start_transaction(); // Create a new comment $comment = $model::forge(array('body' => $body, $parent_id_prop => $parent_id, 'member_id' => $this->u->id)); $result = (int) $comment->save(); \DB::commit_transaction(); $this->set_response_body_api(array('result' => $result, 'id' => $comment->id)); }); }