Работа с комментариями перенесена в отдельный контроллер

This commit is contained in:
Krivchikov Dmitry 2015-10-03 13:47:23 +03:00
parent 565fd72f15
commit 5cd212ea9f
5 changed files with 61 additions and 37 deletions

View File

@ -192,41 +192,6 @@ class ArticleController extends Controller
App::redirect('/user/'); App::redirect('/user/');
} }
static function actionAddComment()
{
if (!App::$user ||
!isset($_POST['parentType']) ||
!isset($_POST['parentId']) ||
!isset($_POST['answerTo']) ||
!isset($_POST['text']))
{
App::error404();
}
if (trim($_POST['text']) == '') {
App::redirect('/'.$_POST['parentType'].'/'.$_POST['parentId']);
}
$text = str_replace(array('<','>'), array('&lt;','&gt;'), $_POST['text']);
$html = strip_tags($_POST['text']);
$html = str_replace(array('<','>',"'"), array('&lt;','&gt;','&prime;'), $html);
$html = Markdown::liteProcessing($html);
$html = nl2br($html);
$comment = new Comment(array(
'userId' => App::$user->id,
'parentType' => ($_POST['parentType'] == 'article') ? 'article' : 'point',
'parentId' => (int)$_POST['parentId'],
'answerTo' => (int)$_POST['answerTo'],
'addDate' => time(),
'text' => $text,
'html' => $html,
));
$comment->save();
App::redirect('/'.$_POST['parentType'].'/'.$_POST['parentId'].'#comment'.$comment->id);
}
static function actionMarkdown() static function actionMarkdown()
{ {
if (isset($_POST['text']) ){ if (isset($_POST['text']) ){

View File

@ -0,0 +1,55 @@
<?php
class CommentsController extends Controller
{
static function actionAdd()
{
if (!App::$user ||
!isset($_POST['parentType']) ||
!isset($_POST['parentId']) ||
!isset($_POST['answerTo']) ||
!isset($_POST['text'])) {
App::error404();
}
if (trim($_POST['text']) == '') {
App::redirect('/' . $_POST['parentType'] . '/' . $_POST['parentId']);
}
$text = str_replace(array('<', '>'), array('&lt;', '&gt;'), $_POST['text']);
$html = strip_tags($_POST['text']);
$html = str_replace(array('<', '>', "'"), array('&lt;', '&gt;', '&prime;'), $html);
$html = Markdown::liteProcessing($html);
$html = nl2br($html);
$comment = new Comment(array(
'userId' => App::$user->id,
'parentType' => ($_POST['parentType'] == 'article') ? 'article' : 'point',
'parentId' => (int) $_POST['parentId'],
'answerTo' => (int) $_POST['answerTo'],
'addDate' => time(),
'text' => $text,
'html' => $html,
));
$comment->save();
App::redirect('/' . $_POST['parentType'] . '/' . $_POST['parentId'] . '#comment' . $comment->id);
}
static function actionDel()
{
if (!App::$user || !(int)App::getParam('id')) {
App::error404();
}
$id = (int)App::getParam('id');
$comment = Comment::model()->getByPK($id);
if($comment->id && $comment->userId == App::$user->id) {
$comment->delete($id);
}
App::redirect('/'.$comment->parentType.'/'.$comment->parentId);
}
}

View File

@ -2,9 +2,12 @@
<a name="comment<?= $comment->id ?>"></a> <a name="comment<?= $comment->id ?>"></a>
<img src="/img/avatars/<?= $comment->refUser->id ?>.jpg" class="avatar"/> <img src="/img/avatars/<?= $comment->refUser->id ?>.jpg" class="avatar"/>
<div class="info"> <div class="info">
<b><?= $comment->refUser->nick ?></b>&nbsp; <a href="/user/<?= $comment->refUser->id ?>"><b><?= $comment->refUser->nick ?></b></a>&nbsp;
<?= date('H:i d-m-Y',$comment->addDate) ?> <?= date('H:i d-m-Y',$comment->addDate) ?>
<a href="#comment<?= $comment->id ?>" name="#comment<?= $comment->id ?>" class="anchor">#</a> <a href="#comment<?= $comment->id ?>" name="#comment<?= $comment->id ?>" class="anchor">#</a>
<?php if (App::$user && $comment->userId == App::$user->id): ?>
<a href="/comments/del/id/<?= $comment->id ?>" title="Удалить комментарий" class="anchor"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span></a>
<?php endif; ?>
</div> </div>
<br/> <br/>
<?= Helper::replaceLinks($comment->html, 'html') ?><br/> <?= Helper::replaceLinks($comment->html, 'html') ?><br/>

View File

@ -1,5 +1,5 @@
<div class="commentForm"> <div class="commentForm">
<form action="/article/addComment/" method="POST"> <form action="/comments/add" method="POST">
<input type="hidden" name="answerTo" value="0" /> <input type="hidden" name="answerTo" value="0" />
<input type="hidden" name="parentType" value="<?= $parentType ?>" /> <input type="hidden" name="parentType" value="<?= $parentType ?>" />
<input type="hidden" name="parentId" value="<?= $parentId ?>" /> <input type="hidden" name="parentId" value="<?= $parentId ?>" />

View File

@ -15,6 +15,7 @@
} }
.singleComment .info .anchor{ .singleComment .info .anchor{
margin-right: 10px;
float: right; float: right;
} }