public function runAddComment(TBGRequest $request) { $i18n = TBGContext::getI18n(); $comment_applies_type = $request['comment_applies_type']; try { if (!$this->getUser()->canPostComments()) { throw new Exception($i18n->__('You are not allowed to do this')); } if (!trim($request['comment_body'])) { throw new Exception($i18n->__('The comment must have some content')); } $comment = new TBGComment(); $comment->setTitle(''); $comment->setContent($request->getParameter('comment_body', null, false)); $comment->setPostedBy($this->getUser()->getID()); $comment->setTargetID($request['comment_applies_id']); $comment->setTargetType($request['comment_applies_type']); $comment->setReplyToComment($request['reply_to_comment_id']); $comment->setModuleName($request['comment_module']); $comment->setIsPublic((bool) $request['comment_visibility']); $comment->setSyntax($request['comment_body_syntax']); $comment->save(); if ($comment_applies_type == TBGComment::TYPE_ISSUE) { $issue = TBGIssuesTable::getTable()->selectById((int) $request['comment_applies_id']); if (!$request->isAjaxCall() || $request['comment_save_changes']) { $issue->setSaveComment($comment); $issue->save(); } else { TBGEvent::createNew('core', 'TBGComment::createNew', $comment, compact('issue'))->trigger(); } } elseif ($comment_applies_type == TBGComment::TYPE_ARTICLE) { $article = TBGArticlesTable::getTable()->selectById((int) $request['comment_applies_id']); TBGEvent::createNew('core', 'TBGComment::createNew', $comment, compact('article'))->trigger(); } switch ($comment_applies_type) { case TBGComment::TYPE_ISSUE: $comment_html = $this->getTemplateHTML('main/comment', array('comment' => $comment, 'issue' => TBGContext::factory()->TBGIssue($request['comment_applies_id']))); break; case TBGComment::TYPE_ARTICLE: $comment_html = $this->getTemplateHTML('main/comment', array('comment' => $comment)); break; default: $comment_html = 'OH NO!'; } } catch (Exception $e) { if ($request->isAjaxCall()) { $this->getResponse()->setHttpStatus(400); return $this->renderJSON(array('error' => $e->getMessage())); } else { TBGContext::setMessage('comment_error', $e->getMessage()); TBGContext::setMessage('comment_error_body', $request['comment_body']); TBGContext::setMessage('comment_error_title', $request['comment_title']); TBGContext::setMessage('comment_error_visibility', $request['comment_visibility']); } } if ($request->isAjaxCall()) { return $this->renderJSON(array('title' => $i18n->__('Comment added!'), 'comment_data' => $comment_html, 'continue_url' => $request['forward_url'], 'commentcount' => TBGComment::countComments($request['comment_applies_id'], $request['comment_applies_type']))); } if (isset($comment) && $comment instanceof TBGComment) { $this->forward($request['forward_url'] . "#comment_{$request['comment_applies_type']}_{$request['comment_applies_id']}_{$comment->getID()}"); } else { $this->forward($request['forward_url']); } }