Commit 8a34dff046a6a6378216bbcab57655834bee8fb6
1 parent
68133386
+ коментарии (front + back)
Showing
11 changed files
with
586 additions
and
0 deletions
Show diff stats
1 | +<?php | ||
2 | + | ||
3 | +namespace backend\controllers; | ||
4 | + | ||
5 | +use Yii; | ||
6 | +use common\models\Comment; | ||
7 | +use yii\data\ActiveDataProvider; | ||
8 | +use yii\web\Controller; | ||
9 | +use yii\web\NotFoundHttpException; | ||
10 | +use yii\filters\VerbFilter; | ||
11 | + | ||
12 | +/** | ||
13 | + * CommentController implements the CRUD actions for Comment model. | ||
14 | + */ | ||
15 | +class CommentController extends Controller | ||
16 | +{ | ||
17 | + public function behaviors() | ||
18 | + { | ||
19 | + return [ | ||
20 | + 'verbs' => [ | ||
21 | + 'class' => VerbFilter::className(), | ||
22 | + 'actions' => [ | ||
23 | + 'delete' => ['post'], | ||
24 | + ], | ||
25 | + ], | ||
26 | + ]; | ||
27 | + } | ||
28 | + | ||
29 | + /** | ||
30 | + * Lists all Comment models. | ||
31 | + * @return mixed | ||
32 | + */ | ||
33 | + public function actionIndex() | ||
34 | + { | ||
35 | + $dataProvider = new ActiveDataProvider([ | ||
36 | + 'query' => Comment::find(), | ||
37 | + ]); | ||
38 | + | ||
39 | + return $this->render('index', [ | ||
40 | + 'dataProvider' => $dataProvider, | ||
41 | + ]); | ||
42 | + } | ||
43 | + | ||
44 | + /** | ||
45 | + * Displays a single Comment model. | ||
46 | + * @param integer $id | ||
47 | + * @return mixed | ||
48 | + */ | ||
49 | + public function actionView($id) | ||
50 | + { | ||
51 | + return $this->render('view', [ | ||
52 | + 'model' => $this->findModel($id), | ||
53 | + ]); | ||
54 | + } | ||
55 | + | ||
56 | + /** | ||
57 | + * Creates a new Comment model. | ||
58 | + * If creation is successful, the browser will be redirected to the 'view' page. | ||
59 | + * @return mixed | ||
60 | + */ | ||
61 | + public function actionCreate() | ||
62 | + { | ||
63 | + $model = new Comment(); | ||
64 | + | ||
65 | + if ($model->load(Yii::$app->request->post()) && $model->save()) { | ||
66 | + return $this->redirect(['view', 'id' => $model->comment_id]); | ||
67 | + } else { | ||
68 | + return $this->render('create', [ | ||
69 | + 'model' => $model, | ||
70 | + ]); | ||
71 | + } | ||
72 | + } | ||
73 | + | ||
74 | + /** | ||
75 | + * Updates an existing Comment model. | ||
76 | + * If update is successful, the browser will be redirected to the 'view' page. | ||
77 | + * @param integer $id | ||
78 | + * @return mixed | ||
79 | + */ | ||
80 | + public function actionUpdate($id) | ||
81 | + { | ||
82 | + $model = $this->findModel($id); | ||
83 | + | ||
84 | + if ($model->load(Yii::$app->request->post()) && $model->save()) { | ||
85 | + return $this->redirect(['view', 'id' => $model->comment_id]); | ||
86 | + } else { | ||
87 | + return $this->render('update', [ | ||
88 | + 'model' => $model, | ||
89 | + ]); | ||
90 | + } | ||
91 | + } | ||
92 | + | ||
93 | + /** | ||
94 | + * Deletes an existing Comment model. | ||
95 | + * If deletion is successful, the browser will be redirected to the 'index' page. | ||
96 | + * @param integer $id | ||
97 | + * @return mixed | ||
98 | + */ | ||
99 | + public function actionDelete($id) | ||
100 | + { | ||
101 | + $this->findModel($id)->delete(); | ||
102 | + | ||
103 | + return $this->redirect(['index']); | ||
104 | + } | ||
105 | + | ||
106 | + /** | ||
107 | + * Finds the Comment model based on its primary key value. | ||
108 | + * If the model is not found, a 404 HTTP exception will be thrown. | ||
109 | + * @param integer $id | ||
110 | + * @return Comment the loaded model | ||
111 | + * @throws NotFoundHttpException if the model cannot be found | ||
112 | + */ | ||
113 | + protected function findModel($id) | ||
114 | + { | ||
115 | + if (($model = Comment::findOne($id)) !== null) { | ||
116 | + return $model; | ||
117 | + } else { | ||
118 | + throw new NotFoundHttpException('The requested page does not exist.'); | ||
119 | + } | ||
120 | + } | ||
121 | +} |
1 | +<?php | ||
2 | + | ||
3 | +use yii\helpers\Html; | ||
4 | +use yii\widgets\ActiveForm; | ||
5 | + | ||
6 | +/* @var $this yii\web\View */ | ||
7 | +/* @var $model common\models\Comment */ | ||
8 | +/* @var $form yii\widgets\ActiveForm */ | ||
9 | +?> | ||
10 | + | ||
11 | +<div class="comment-form"> | ||
12 | + | ||
13 | + <?php $form = ActiveForm::begin(); ?> | ||
14 | + | ||
15 | + <?= $form->field($model, 'comment_pid')->textInput() ?> | ||
16 | + | ||
17 | + <?= $form->field($model, 'content')->textarea(['rows' => 6]) ?> | ||
18 | + | ||
19 | + <?= $form->field($model, 'created_at')->textInput() ?> | ||
20 | + | ||
21 | + <?= $form->field($model, 'entity_id')->textInput() ?> | ||
22 | + | ||
23 | + <?= $form->field($model, 'user_id')->textInput() ?> | ||
24 | + | ||
25 | + <div class="form-group"> | ||
26 | + <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?> | ||
27 | + </div> | ||
28 | + | ||
29 | + <?php ActiveForm::end(); ?> | ||
30 | + | ||
31 | +</div> |
1 | +<?php | ||
2 | + | ||
3 | +use yii\helpers\Html; | ||
4 | + | ||
5 | + | ||
6 | +/* @var $this yii\web\View */ | ||
7 | +/* @var $model common\models\Comment */ | ||
8 | + | ||
9 | +$this->title = 'Create Comment'; | ||
10 | +$this->params['breadcrumbs'][] = ['label' => 'Comments', 'url' => ['index']]; | ||
11 | +$this->params['breadcrumbs'][] = $this->title; | ||
12 | +?> | ||
13 | +<div class="comment-create"> | ||
14 | + | ||
15 | + <h1><?= Html::encode($this->title) ?></h1> | ||
16 | + | ||
17 | + <?= $this->render('_form', [ | ||
18 | + 'model' => $model, | ||
19 | + ]) ?> | ||
20 | + | ||
21 | +</div> |
1 | +<?php | ||
2 | + | ||
3 | +use yii\helpers\Html; | ||
4 | +use yii\grid\GridView; | ||
5 | + | ||
6 | +/* @var $this yii\web\View */ | ||
7 | +/* @var $dataProvider yii\data\ActiveDataProvider */ | ||
8 | + | ||
9 | +$this->title = 'Comments'; | ||
10 | +$this->params['breadcrumbs'][] = $this->title; | ||
11 | +?> | ||
12 | +<div class="comment-index"> | ||
13 | + | ||
14 | + <h1><?= Html::encode($this->title) ?></h1> | ||
15 | + | ||
16 | + <p> | ||
17 | + <?= Html::a('Create Comment', ['create'], ['class' => 'btn btn-success']) ?> | ||
18 | + </p> | ||
19 | + | ||
20 | + <?= GridView::widget([ | ||
21 | + 'dataProvider' => $dataProvider, | ||
22 | + 'columns' => [ | ||
23 | + ['class' => 'yii\grid\SerialColumn'], | ||
24 | + | ||
25 | + 'comment_id', | ||
26 | + 'comment_pid', | ||
27 | + 'content:ntext', | ||
28 | + 'created_at', | ||
29 | + 'entity_id', | ||
30 | + // 'user_id', | ||
31 | + | ||
32 | + ['class' => 'yii\grid\ActionColumn'], | ||
33 | + ], | ||
34 | + ]); ?> | ||
35 | + | ||
36 | +</div> |
1 | +<?php | ||
2 | + | ||
3 | +use yii\helpers\Html; | ||
4 | + | ||
5 | +/* @var $this yii\web\View */ | ||
6 | +/* @var $model common\models\Comment */ | ||
7 | + | ||
8 | +$this->title = 'Update Comment: ' . ' ' . $model->comment_id; | ||
9 | +$this->params['breadcrumbs'][] = ['label' => 'Comments', 'url' => ['index']]; | ||
10 | +$this->params['breadcrumbs'][] = ['label' => $model->comment_id, 'url' => ['view', 'id' => $model->comment_id]]; | ||
11 | +$this->params['breadcrumbs'][] = 'Update'; | ||
12 | +?> | ||
13 | +<div class="comment-update"> | ||
14 | + | ||
15 | + <h1><?= Html::encode($this->title) ?></h1> | ||
16 | + | ||
17 | + <?= $this->render('_form', [ | ||
18 | + 'model' => $model, | ||
19 | + ]) ?> | ||
20 | + | ||
21 | +</div> |
1 | +<?php | ||
2 | + | ||
3 | +use yii\helpers\Html; | ||
4 | +use yii\widgets\DetailView; | ||
5 | + | ||
6 | +/* @var $this yii\web\View */ | ||
7 | +/* @var $model common\models\Comment */ | ||
8 | + | ||
9 | +$this->title = $model->comment_id; | ||
10 | +$this->params['breadcrumbs'][] = ['label' => 'Comments', 'url' => ['index']]; | ||
11 | +$this->params['breadcrumbs'][] = $this->title; | ||
12 | +?> | ||
13 | +<div class="comment-view"> | ||
14 | + | ||
15 | + <h1><?= Html::encode($this->title) ?></h1> | ||
16 | + | ||
17 | + <p> | ||
18 | + <?= Html::a('Update', ['update', 'id' => $model->comment_id], ['class' => 'btn btn-primary']) ?> | ||
19 | + <?= Html::a('Delete', ['delete', 'id' => $model->comment_id], [ | ||
20 | + 'class' => 'btn btn-danger', | ||
21 | + 'data' => [ | ||
22 | + 'confirm' => 'Are you sure you want to delete this item?', | ||
23 | + 'method' => 'post', | ||
24 | + ], | ||
25 | + ]) ?> | ||
26 | + </p> | ||
27 | + | ||
28 | + <?= DetailView::widget([ | ||
29 | + 'model' => $model, | ||
30 | + 'attributes' => [ | ||
31 | + 'comment_id', | ||
32 | + 'comment_pid', | ||
33 | + 'content:ntext', | ||
34 | + 'created_at', | ||
35 | + 'entity_id', | ||
36 | + 'user_id', | ||
37 | + ], | ||
38 | + ]) ?> | ||
39 | + | ||
40 | +</div> |
1 | +<?php | ||
2 | + | ||
3 | +namespace common\models; | ||
4 | + | ||
5 | +use Yii; | ||
6 | + | ||
7 | +/** | ||
8 | + * This is the model class for table "comment". | ||
9 | + * | ||
10 | + * @property integer $comment_id | ||
11 | + * @property integer $comment_pid | ||
12 | + * @property string $content | ||
13 | + * @property string $created_at | ||
14 | + * @property integer $entity_id | ||
15 | + * @property integer $user_id | ||
16 | + */ | ||
17 | +class Comment extends \yii\db\ActiveRecord | ||
18 | +{ | ||
19 | + /** | ||
20 | + * @inheritdoc | ||
21 | + */ | ||
22 | + public static function tableName() | ||
23 | + { | ||
24 | + return 'comment'; | ||
25 | + } | ||
26 | + | ||
27 | + /** | ||
28 | + * @inheritdoc | ||
29 | + */ | ||
30 | + public function rules() | ||
31 | + { | ||
32 | + return [ | ||
33 | + [['comment_pid', 'entity_id', 'user_id'], 'integer'], | ||
34 | + [['content'], 'string'], | ||
35 | + [['created_at'], 'safe'] | ||
36 | + ]; | ||
37 | + } | ||
38 | + | ||
39 | + /** | ||
40 | + * @inheritdoc | ||
41 | + */ | ||
42 | + public function attributeLabels() | ||
43 | + { | ||
44 | + return [ | ||
45 | + 'comment_id' => 'Comment ID', | ||
46 | + 'comment_pid' => 'Comment Pid', | ||
47 | + 'content' => 'Content', | ||
48 | + 'created_at' => 'Created At', | ||
49 | + 'entity_id' => 'Entity ID', | ||
50 | + 'user_id' => 'User ID', | ||
51 | + ]; | ||
52 | + } | ||
53 | + | ||
54 | + public function getUser() | ||
55 | + { | ||
56 | + return $this->hasOne(User::className(), ['id' => 'user_id']); | ||
57 | + } | ||
58 | +} |
1 | +<?php | ||
2 | + | ||
3 | +namespace common\widgets; | ||
4 | + | ||
5 | +use yii\base\Widget; | ||
6 | +use yii\helpers\Html; | ||
7 | + | ||
8 | +class CommentWidget extends Widget | ||
9 | +{ | ||
10 | + | ||
11 | + public $comments; | ||
12 | + public $reload; | ||
13 | + | ||
14 | + private function html ($d, $lvl) | ||
15 | + { | ||
16 | + echo ' | ||
17 | + <li class="comment" level="' . $lvl . '"> | ||
18 | + <div class="comment_body" comment_id="'. $d['id'] .'"> | ||
19 | + <div class="comment_info"> | ||
20 | + <span class="comment_username">' . $d['username'] . '</span> | ||
21 | + <span class="comment_created_time">' . date('F j Y h:i', strtotime($d['created_at'])) . '</span> | ||
22 | + <div class="dropdown"> | ||
23 | + <button class="btn btn-default btn-xs dropdown-toggle" type="button" id="" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true"> | ||
24 | + Manage | ||
25 | + <span class="caret"></span> | ||
26 | + </button> | ||
27 | + <ul class="dropdown-menu" aria-labelledby="dropdownMenu1"> | ||
28 | + <li><a href="#">Edit</a></li> | ||
29 | + <li><a href="#">Hide</a></li> | ||
30 | + <li><a href="#">Delete</a></li> | ||
31 | + </ul> | ||
32 | + </div> | ||
33 | + </div> | ||
34 | + <div class="message"> | ||
35 | + <p>' . $d['content'] . '</p> | ||
36 | + </div> | ||
37 | + <button type="button" class="btn btn-default btn-xs btn-reply">Reply</button> | ||
38 | + </div> | ||
39 | + </li>'; | ||
40 | + } | ||
41 | + | ||
42 | + private function display(array $comments, $level = 0) | ||
43 | + { | ||
44 | + foreach ($comments as $info) | ||
45 | + { | ||
46 | + echo '<ul>'; | ||
47 | + | ||
48 | + $this->html($info, $level + 1); | ||
49 | + | ||
50 | + if (!empty($info['childs'])) | ||
51 | + { | ||
52 | + $this->display($info['childs'], $level + 1); | ||
53 | + } | ||
54 | + | ||
55 | + echo '</ul>'; | ||
56 | + } | ||
57 | + | ||
58 | + if (isset($this->reload) && $this->reload == false) | ||
59 | + { | ||
60 | + | ||
61 | + } | ||
62 | + | ||
63 | + } | ||
64 | + | ||
65 | + private function field() | ||
66 | + { | ||
67 | + echo '<div class="container new_comment"> | ||
68 | + <div class="form-group"> | ||
69 | + <label for="comment">Comment:</label> | ||
70 | + <textarea class="form-control" rows="5" id="comment"></textarea> | ||
71 | + <button type="button" class="btn btn-info btn-md btn-send">Send</button> | ||
72 | + </div> | ||
73 | + </div>'; | ||
74 | + } | ||
75 | + | ||
76 | + public function init() | ||
77 | + { | ||
78 | + | ||
79 | + } | ||
80 | + public function run() | ||
81 | + { | ||
82 | + echo '<div class="container" id="comment_wrapper">'; | ||
83 | + $this->display($this->comments); | ||
84 | + echo '</div>'; | ||
85 | + $this->field(); | ||
86 | + } | ||
87 | +} |
1 | +<?php | ||
2 | + | ||
3 | +namespace frontend\controllers; | ||
4 | + | ||
5 | +use Yii; | ||
6 | +use common\models\Comment; | ||
7 | + | ||
8 | +class CommentController extends \yii\web\Controller | ||
9 | +{ | ||
10 | + public function actionIndex() | ||
11 | + { | ||
12 | + $post = Comment::find()->with('user')->orderBy('created_at ASC')->all(); | ||
13 | + | ||
14 | + $comments = array(); | ||
15 | + | ||
16 | + foreach($post as $p) | ||
17 | + { | ||
18 | + $comments[$p['comment_id']] = array( | ||
19 | + 'id' => $p['comment_id'], | ||
20 | + 'parent' => $p['comment_pid'], | ||
21 | + 'content' => $p['content'], | ||
22 | + 'username' => $p->user->username, | ||
23 | + 'created_at' => $p['created_at'], | ||
24 | + 'childs' => array() | ||
25 | + ); | ||
26 | + } | ||
27 | + | ||
28 | + foreach ($comments as $key => &$val) | ||
29 | + { | ||
30 | + if ($val['parent'] != 0) | ||
31 | + { | ||
32 | + $comments[$val['parent']]['childs'][] =& $val; | ||
33 | + } | ||
34 | + } | ||
35 | + unset($val); | ||
36 | + | ||
37 | + foreach ($comments as $key => $val) | ||
38 | + { | ||
39 | + if ($val['parent'] != 0) | ||
40 | + { | ||
41 | + unset($comments[$key]); | ||
42 | + } | ||
43 | + } | ||
44 | + | ||
45 | + if (Yii::$app->request->get('action') == 'reload') | ||
46 | + { | ||
47 | + return $this->renderAjax('index', array('data' => $comments)); | ||
48 | + } | ||
49 | + else | ||
50 | + { | ||
51 | + return $this->render('index', array('data' => $comments, 'reload' => false)); | ||
52 | + } | ||
53 | + } | ||
54 | + | ||
55 | + public function actionCreate() | ||
56 | + { | ||
57 | + $comment = new Comment; | ||
58 | + | ||
59 | + $comment->user_id = Yii::$app->user->identity->id; | ||
60 | + $comment->content = Yii::$app->request->post('content'); | ||
61 | + $comment->entity_id = 3;//Yii::$app->request->post('entity_id'); | ||
62 | + $comment->comment_pid = Yii::$app->request->post('parent_id'); | ||
63 | + $comment->created_at = date('Y-m-d H:i:s', time()); | ||
64 | + | ||
65 | + $comment->save(); | ||
66 | + | ||
67 | + } | ||
68 | + | ||
69 | +} |
1 | +$(document).ready(function(){ | ||
2 | + | ||
3 | + applyScript(); | ||
4 | + | ||
5 | + /*var pubnub = PUBNUB({ | ||
6 | + subscribe_key: 'demo', | ||
7 | + publish_key: 'demo' | ||
8 | + }); | ||
9 | + | ||
10 | + pubnub.subscribe({ | ||
11 | + channel: 'comment', | ||
12 | + message: function(m){console.log(m)}, | ||
13 | + error: function (error) { | ||
14 | + // Handle error here | ||
15 | + console.log(JSON.stringify(error)); | ||
16 | + } | ||
17 | + }); */ | ||
18 | +}); | ||
19 | + | ||
20 | +function applyScript() | ||
21 | +{ | ||
22 | + | ||
23 | + var parent_id = 0; | ||
24 | + | ||
25 | + var textarea = $(".new_comment").find("textarea"); | ||
26 | + var replyName; | ||
27 | + | ||
28 | + $(".comment_reply").each(function() | ||
29 | + { | ||
30 | + $(this).hide(); | ||
31 | + }); | ||
32 | + | ||
33 | + $(".btn-reply").each(function() | ||
34 | + { | ||
35 | + $(this).on("click", function() | ||
36 | + { | ||
37 | + replyName = $(this).siblings().find(".comment_username").text(); | ||
38 | + parent_id = $(this).parents(".comment_body").attr("comment_id"); | ||
39 | + | ||
40 | + textarea.val(replyName + ', '); | ||
41 | + | ||
42 | + }); | ||
43 | + | ||
44 | + }); | ||
45 | + | ||
46 | + $(".btn-send").on('click', function() | ||
47 | + { | ||
48 | + var content = textarea.val(); | ||
49 | + | ||
50 | + if (content.indexOf(replyName) > -1 == false) | ||
51 | + { | ||
52 | + parent_id = 0; | ||
53 | + } | ||
54 | + createComment(content, parent_id); | ||
55 | + }); | ||
56 | + | ||
57 | + function reload_comments() | ||
58 | + { | ||
59 | + var path = window.location.protocol + "//" + window.location.host + "/"; | ||
60 | + $.ajax({ | ||
61 | + type: 'GET', | ||
62 | + url : path + 'comment', | ||
63 | + data : { | ||
64 | + 'action' : 'reload' | ||
65 | + }, | ||
66 | + success: function(res) | ||
67 | + { | ||
68 | + $("#comment_wrapper").html(res); | ||
69 | + console.log('reloaded'); | ||
70 | + | ||
71 | + applyScript(); | ||
72 | + } | ||
73 | + }); | ||
74 | + } | ||
75 | + function createComment(content, parent_id) | ||
76 | + { | ||
77 | + $.ajax({ | ||
78 | + type: 'POST', | ||
79 | + url : '/comment/create', | ||
80 | + data: { | ||
81 | + 'content' : content, | ||
82 | + 'parent_id' : parent_id | ||
83 | + }, | ||
84 | + success: function(res) | ||
85 | + { | ||
86 | + //reload_comments(); | ||
87 | + alert('Comment sent'); | ||
88 | + } | ||
89 | + }); | ||
90 | + } | ||
91 | + | ||
92 | +} |