ADD COMMENTS

This commit is contained in:
Krivchikov Dmitry 2015-10-02 14:50:38 +03:00
parent bb152d028b
commit 7868c21fd6
9 changed files with 155 additions and 0 deletions

View File

@ -15,6 +15,18 @@ class Markdown
return $string; return $string;
} }
static function liteProcessing($string)
{
// $string = self::replaceHeaders($string);
$string = self::replaceBold($string);
$string = self::replaceItalic($string);
$string = self::replaceUnderline($string);
$string = self::replaceChars($string);
$string = self::replaceUrls($string);
return $string;
}
static private function replaceHeaders($string) static private function replaceHeaders($string)
{ {
$pr = '(### (.{1,}))'; $pr = '(### (.{1,}))';

View File

@ -190,6 +190,37 @@ 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();
}
$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']) ){

18
ground/models/Comment.php Normal file
View File

@ -0,0 +1,18 @@
<?php
class Comment extends Model
{
function __construct($fromArray = array())
{
$this->_tableName_ = 'comments';
parent::__construct($fromArray);
$this->setRelation('refUser', 'userId', 'User', 'id', self::TO_ONE);
}
static function model()
{
return new self();
}
}

View File

@ -16,6 +16,12 @@
</div> </div>
</div> </div>
<hr/> <hr/>
<div class="row">
<div class="col-md-12">
<?= self::renderPartial('comments/all.php',['parentId'=>$article->id, 'parentType' => 'article'],TRUE); ?>
</div>
</div>
<hr/>
<div class="row"> <div class="row">
<div class="col-md-12"> <div class="col-md-12">
<a href="/point/<?= $randomPoint->id ?>" style="float: left; margin: 0 20px 0 0;"><img src="<?= $randomPoint->getImg() ?>" style="width: 200px;"/></a> <a href="/point/<?= $randomPoint->id ?>" style="float: left; margin: 0 20px 0 0;"><img src="<?= $randomPoint->getImg() ?>" style="width: 200px;"/></a>

View File

@ -0,0 +1,22 @@
<h3>Комментарии:</h3>
<?php
self::addStyle('/css/comments.css');
$comments = Comment::model()->getAll(array('where'=>'`parentType`="'.$parentType.'" AND `parentId`="'.$parentId.'"'));
if($comments) {
foreach ($comments as $comment) {
self::renderPartial('comments/comment.php',['comment'=>$comment]);
}
} else {
print 'Ваш комментарий может стать первым!';
}
if (App::$user) {
self::renderPartial('comments/form.php',['parentId'=>$parentId, 'parentType' => $parentType]);
} else {
self::renderPartial('comments/deny.php');
self::addScript('//ulogin.ru/js/ulogin.js');
self::renderPartial('modals/login.php');
}

View File

@ -0,0 +1,12 @@
<div class="singleComment">
<a name="comment<?= $comment->id ?>"></a>
<img src="/img/avatars/<?= $comment->refUser->id ?>.jpg" class="avatar"/>
<div class="info">
<b><?= $comment->refUser->nick ?></b>&nbsp;
<?= date('H:i d-m-Y',$comment->addDate) ?>
<a href="#comment<?= $comment->id ?>" name="#comment<?= $comment->id ?>" class="anchor">#</a>
</div>
<br/>
<?= Helper::replaceLinks($comment->html, 'html') ?><br/>
<br style="clear: both;">
</div>

View File

@ -0,0 +1,3 @@
<div class="bg-danger deny">
Чтобы написать комментарий, вам необходимо <a href="#" onclick="$('#loginModal').modal('show');">авторизоваться</a>.
</div>

View File

@ -0,0 +1,12 @@
<div class="commentForm">
<form action="/article/addComment/" method="POST">
<input type="hidden" name="answerTo" value="0" />
<input type="hidden" name="parentType" value="<?= $parentType ?>" />
<input type="hidden" name="parentId" value="<?= $parentId ?>" />
<textarea name="text" style="width: 100%;" rows="5"></textarea>
<br/>
<div class="buttons">
<button type="submit" class="btn btn-success">Сохранить комментарий</button>
</div>
</form>
</div>

39
public/css/comments.css Normal file
View File

@ -0,0 +1,39 @@
.singleComment {
border: solid 1px #ddd;
border-radius: 3px;
padding: 8px;
margin-bottom: 5px;
background-color: rgba(255,255,255, 0.8);
}
.singleComment .info {
background-color: #eee;
color: #777;
height: 50px;
padding: 15px 10px 15px 60px;
}
.singleComment .info .anchor{
float: right;
}
.singleComment .avatar {
width: 50px;
height: 50px;
float: left;
}
div.commentForm {
margin-top: 20px;
}
div.commentForm .buttons {
text-align: center;
}
div.deny {
padding: 10px 20px;
border-radius: 3px;
border: solid 1px #ea4444;
}