Skip to content

Commit

Permalink
comment
Browse files Browse the repository at this point in the history
  • Loading branch information
xutl committed Nov 19, 2017
1 parent 46c6b9f commit 9c387e9
Show file tree
Hide file tree
Showing 9 changed files with 364 additions and 4 deletions.
57 changes: 57 additions & 0 deletions frontend/models/CommentForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
/**
* @link http://www.tintsoft.com/
* @copyright Copyright (c) 2012 TintSoft Technology Co. Ltd.
* @license http://www.tintsoft.com/license/
*/
namespace yuncms\question\frontend\models;

use Yii;
use yii\base\Model;
use yuncms\question\models\QuestionComment;

/**
* Class CommentForm
* @package yuncms\comment\frontend\models
*/
class CommentForm extends Model
{
/**
* @var int
*/
public $model_id;

/**
* @var string
*/
public $content;

private $_user;

/**
* @return \yii\web\IdentityInterface
*/
public function getUser()
{
if ($this->_user == null) {
$this->_user = Yii::$app->user->identity;
}
return $this->_user;
}

/**
* 保存评论
*/
public function save()
{
if ($this->validate()) {
$model = new QuestionComment([
'model_id' => $this->model_id,
'user_id' => Yii::$app->user->id,
'content' => $this->content,
]);
return $model->save();
}
return false;
}
}
8 changes: 4 additions & 4 deletions frontend/views/question/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
use yii\bootstrap\Modal;
use yii\helpers\HtmlPurifier;
use yii\bootstrap\ActiveForm;
use yuncms\question\models\Answer;
use yuncms\question\models\Question;
use yuncms\question\models\QuestionAnswer;
use yuncms\question\frontend\assets\Asset;

/**
Expand Down Expand Up @@ -103,7 +103,7 @@ class="fa fa-thumbs-o-up"></i>
</div>

<!-- 评论 -->
<?= \yuncms\comment\frontend\widgets\Comment::widget(['source_type' => 'answer', 'source_id' => $bestAnswer->id, 'hide_cancel' => false]) ?>
<?= \yuncms\question\frontend\widgets\Comment::widget(['source_type' => 'answer', 'source_id' => $bestAnswer->id, 'hide_cancel' => false]) ?>
<!-- 评论结束 -->
<div class="media user-info border-top">
<div class="media-left">
Expand Down Expand Up @@ -175,7 +175,7 @@ class="text-muted"> - <?= $bestAnswer->user->profile->city ?></span>
</div>
<?php if ($model->status != Question::STATUS_END): ?>
<div class="widget-answer-form mt-15">
<?php if (!Yii::$app->user->isGuest && ($model->user_id != Yii::$app->user->id && Answer::isAnswered(Yii::$app->user->id, $model->id))): ?>
<?php if (!Yii::$app->user->isGuest && ($model->user_id != Yii::$app->user->id && QuestionAnswer::isAnswered(Yii::$app->user->id, $model->id))): ?>
<h4>我来回答</h4>
<?= \yuncms\question\widgets\Answer::widget(['questionId' => $model->id]); ?>
<?php endif; ?>
Expand Down Expand Up @@ -299,7 +299,7 @@ class="no-stress"><?= $model->views ?></strong> 浏览
</select> 个积分
</div>
<div class="form-group">
(您目前共有 <span class="text-gold"><?= Yii::$app->user->identity->extend->coins ?></span> 个积分)
(您目前共有 <span class="text-gold"><?= Yii::$app->user->identity->extra->coins ?></span> 个积分)
</div>
<?php ActiveForm::end() ?>
<?php
Expand Down
91 changes: 91 additions & 0 deletions frontend/widgets/Comment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php
/**
* @link http://www.tintsoft.com/
* @copyright Copyright (c) 2012 TintSoft Technology Co. Ltd.
* @license http://www.tintsoft.com/license/
*/
namespace yuncms\question\frontend\widgets;

use Yii;
use yii\base\Widget;
use yii\helpers\Url;
use yii\base\InvalidConfigException;
use yuncms\question\frontend\models\CommentForm;

/**
* Class Comment
* @package yuncms\question\widgets
*/
class Comment extends Widget
{

/**
* @var int 关系ID
*/
public $model_id;

/**
* @var bool 隐藏取消
*/
public $hide_cancel = false;

/** @var bool */
public $validate = true;

/** @inheritdoc */
public function init()
{
parent::init();
$this->registerTranslations();
if (empty ($this->model_class)) {
throw new InvalidConfigException ('The "model_class" property must be set.');
}
if (empty ($this->model_id)) {
throw new InvalidConfigException ('The "model_id" property must be set.');
}
Url::remember('', 'actions-redirect');
}

/**
* 注册翻译
*/
public function registerTranslations()
{
if (!isset(Yii::$app->i18n->translations['widgets/comment/*'])) {
Yii::$app->i18n->translations['widgets/comment/*'] = [
'class' => 'yii\i18n\PhpMessageSource',
'sourceLanguage' => 'en-US',
'basePath' => __DIR__ . '/messages',
'fileMap' => [
'widgets/comment/comment' => 'comment.php',
],
];
}
}

/**
* 显示语言包
* @param string $category
* @param string $message
* @param array $params
* @param null $language
* @return string
*/
public static function t($category, $message, $params = [], $language = null)
{
return Yii::t('widgets/comment/' . $category, $message, $params, $language);
}

/** @inheritdoc */
public function run()
{
$model = new CommentForm([
'model_id' => $this->model_id
]);
return $this->render('comment', [
'model' => $model,
'model_id' => $this->model_id,
'hide_cancel' => $this->hide_cancel,
]);
}
}
28 changes: 28 additions & 0 deletions frontend/widgets/assets/BootstrapPluginAsset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/

namespace yuncms\article;

use yii\web\AssetBundle;

/**
* Asset bundle for the Twitter bootstrap javascript files.
*
* @author Qiang Xue <[email protected]>
* @since 2.0
*/
class BootstrapPluginAsset extends AssetBundle
{
public $sourcePath = '@bower/bootstrap/dist';
public $js = [
'js/bootstrap.js',
];
public $depends = [
'yii\web\JqueryAsset',
'yii\bootstrap\BootstrapAsset',
];
}
26 changes: 26 additions & 0 deletions frontend/widgets/assets/CommentAsset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
/**
* @link http://www.tintsoft.com/
* @copyright Copyright (c) 2012 TintSoft Technology Co. Ltd.
* @license http://www.tintsoft.com/license/
*/
namespace yuncms\comment\frontend\widgets\assets;

use yii\web\AssetBundle;

/**
* Class CommentAsset
* @package yuncms\comment
*/
class CommentAsset extends AssetBundle
{
public $sourcePath = '@yuncms/comment/frontend/widgets/views/assets';

public $js = [
'js/comment.js',
];
public $depends = [
'yii\web\JqueryAsset',
'yii\bootstrap\BootstrapAsset'
];
}
44 changes: 44 additions & 0 deletions frontend/widgets/messages/config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
/**
* Configuration file for 'yii message/extract' command.
*
* This file is automatically generated by 'yii message/config' command.
* It contains parameters for source code messages extraction.
* You may modify this file to suit your needs.
*
* You can use 'yii message/config-template' command to create
* template configuration file with detaild description for each parameter.
*/
return [
'color' => null,
'interactive' => true,
// string, required, root directory of all source files
'sourcePath' => __DIR__ . '/..',
// string, required, root directory containing message translations.
'messagePath' => __DIR__,
'languages' => ['zh-CN'],
'translator' => 'Comment::t',
'sort' => false,
'overwrite' => true,
'removeUnused' => false,
'markUnused' => true,
'except' => [
'.svn',
'.git',
'.gitignore',
'.gitkeep',
'.hgignore',
'.hgkeep',
'/messages',
'/BaseYii.php',
],
'only' => [
'*.php',
],
'format' => 'php',
'db' => 'db',
'sourceMessageTable' => '{{%source_message}}',
'messageTable' => '{{%message}}',
'catalog' => 'messages',
'ignoreCategories' => ['yii', 'app'],
];
23 changes: 23 additions & 0 deletions frontend/widgets/messages/zh-CN/comment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
/**
* Message translations.
*
* This file is automatically generated by 'yii message/extract' command.
* It contains the localizable messages extracted from source code.
* You may modify this file by translating the extracted messages.
*
* Each array element represents the translation (value) of a message (key).
* If the value is empty, the message is considered as not translated.
* Messages that no longer need translation will have their translations
* enclosed between a pair of '@@' marks.
*
* Message string can be used with plural forms format. Check i18n section
* of the guide for details.
*
* NOTE: this file must be saved in UTF-8 encoding.
*/
return [
'Clean' => '取消',
'Submit Comment' => '提交评论',
'Write your comment' => '写下你的评论',
];
49 changes: 49 additions & 0 deletions frontend/widgets/views/assets/js/comment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* 发布评论
* @param model_class
* @param model_id
* @param content
* @param to_user_id
*/
function add_comment(model_class, model_id, content, to_user_id) {
var postData = {model_id: model_id, model_class: model_class, content: content};
if (to_user_id > 0) {
postData.to_user_id = to_user_id;
}
jQuery.post('/comment/default/store', postData, function (html) {
jQuery("#comments-" + model_class + "-" + model_id + " .widget-comment-list").append(html);
jQuery("#comment-" + model_class + "-content-" + model_id).val('');
});
}

/**
* 加载评论
* @param model_class
* @param model_id
*/
function load_comments(model_class, model_id) {
jQuery.get('/comment/default/list', {
model_id: model_id, model_class: model_class
}, function (html) {
jQuery("#comments-" + model_class + "-" + model_id + " .widget-comment-list").append(html);
});
}

/**
* 清除评论
* @param model_class
* @param model_id
*/
function clear_comments(model_class, model_id) {
jQuery("#comments-" + model_class + "-" + model_id + " .widget-comment-list").empty();
}

/*评论提交*/
jQuery(".comment-btn").click(function () {
var model_id = jQuery(this).data('model_id');
var model_class = jQuery(this).data('model_class');
var to_user_id = jQuery(this).data('to_user_id');
var content = jQuery("#comment-" + model_class + "-content-" + model_id).val();
add_comment(model_class, model_id, content, to_user_id);
jQuery("#comment-content-" + model_id + "").val('');
});
Loading

0 comments on commit 9c387e9

Please sign in to comment.