From 8a34dff046a6a6378216bbcab57655834bee8fb6 Mon Sep 17 00:00:00 2001 From: furytingle Date: Mon, 18 Jan 2016 10:58:55 +0200 Subject: [PATCH] + коментарии (front + back) --- backend/controllers/CommentController.php | 121 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ backend/views/comment/_form.php | 31 +++++++++++++++++++++++++++++++ backend/views/comment/create.php | 21 +++++++++++++++++++++ backend/views/comment/index.php | 36 ++++++++++++++++++++++++++++++++++++ backend/views/comment/update.php | 21 +++++++++++++++++++++ backend/views/comment/view.php | 40 ++++++++++++++++++++++++++++++++++++++++ common/models/Comment.php | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ common/widgets/CommentWidget.php | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ frontend/controllers/CommentController.php | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ frontend/views/comment/index.php | 10 ++++++++++ frontend/web/js/comments.js | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 11 files changed, 586 insertions(+), 0 deletions(-) create mode 100644 backend/controllers/CommentController.php create mode 100644 backend/views/comment/_form.php create mode 100644 backend/views/comment/create.php create mode 100644 backend/views/comment/index.php create mode 100644 backend/views/comment/update.php create mode 100644 backend/views/comment/view.php create mode 100644 common/models/Comment.php create mode 100644 common/widgets/CommentWidget.php create mode 100644 frontend/controllers/CommentController.php create mode 100644 frontend/views/comment/index.php create mode 100644 frontend/web/js/comments.js diff --git a/backend/controllers/CommentController.php b/backend/controllers/CommentController.php new file mode 100644 index 0000000..f001043 --- /dev/null +++ b/backend/controllers/CommentController.php @@ -0,0 +1,121 @@ + [ + 'class' => VerbFilter::className(), + 'actions' => [ + 'delete' => ['post'], + ], + ], + ]; + } + + /** + * Lists all Comment models. + * @return mixed + */ + public function actionIndex() + { + $dataProvider = new ActiveDataProvider([ + 'query' => Comment::find(), + ]); + + return $this->render('index', [ + 'dataProvider' => $dataProvider, + ]); + } + + /** + * Displays a single Comment model. + * @param integer $id + * @return mixed + */ + public function actionView($id) + { + return $this->render('view', [ + 'model' => $this->findModel($id), + ]); + } + + /** + * Creates a new Comment model. + * If creation is successful, the browser will be redirected to the 'view' page. + * @return mixed + */ + public function actionCreate() + { + $model = new Comment(); + + if ($model->load(Yii::$app->request->post()) && $model->save()) { + return $this->redirect(['view', 'id' => $model->comment_id]); + } else { + return $this->render('create', [ + 'model' => $model, + ]); + } + } + + /** + * Updates an existing Comment model. + * If update is successful, the browser will be redirected to the 'view' page. + * @param integer $id + * @return mixed + */ + public function actionUpdate($id) + { + $model = $this->findModel($id); + + if ($model->load(Yii::$app->request->post()) && $model->save()) { + return $this->redirect(['view', 'id' => $model->comment_id]); + } else { + return $this->render('update', [ + 'model' => $model, + ]); + } + } + + /** + * Deletes an existing Comment model. + * If deletion is successful, the browser will be redirected to the 'index' page. + * @param integer $id + * @return mixed + */ + public function actionDelete($id) + { + $this->findModel($id)->delete(); + + return $this->redirect(['index']); + } + + /** + * Finds the Comment model based on its primary key value. + * If the model is not found, a 404 HTTP exception will be thrown. + * @param integer $id + * @return Comment the loaded model + * @throws NotFoundHttpException if the model cannot be found + */ + protected function findModel($id) + { + if (($model = Comment::findOne($id)) !== null) { + return $model; + } else { + throw new NotFoundHttpException('The requested page does not exist.'); + } + } +} diff --git a/backend/views/comment/_form.php b/backend/views/comment/_form.php new file mode 100644 index 0000000..b8b2c16 --- /dev/null +++ b/backend/views/comment/_form.php @@ -0,0 +1,31 @@ + + +
+ + + + field($model, 'comment_pid')->textInput() ?> + + field($model, 'content')->textarea(['rows' => 6]) ?> + + field($model, 'created_at')->textInput() ?> + + field($model, 'entity_id')->textInput() ?> + + field($model, 'user_id')->textInput() ?> + +
+ isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?> +
+ + + +
diff --git a/backend/views/comment/create.php b/backend/views/comment/create.php new file mode 100644 index 0000000..b3ab4a8 --- /dev/null +++ b/backend/views/comment/create.php @@ -0,0 +1,21 @@ +title = 'Create Comment'; +$this->params['breadcrumbs'][] = ['label' => 'Comments', 'url' => ['index']]; +$this->params['breadcrumbs'][] = $this->title; +?> +
+ +

title) ?>

+ + render('_form', [ + 'model' => $model, + ]) ?> + +
diff --git a/backend/views/comment/index.php b/backend/views/comment/index.php new file mode 100644 index 0000000..01f578c --- /dev/null +++ b/backend/views/comment/index.php @@ -0,0 +1,36 @@ +title = 'Comments'; +$this->params['breadcrumbs'][] = $this->title; +?> +
+ +

title) ?>

+ +

+ 'btn btn-success']) ?> +

+ + $dataProvider, + 'columns' => [ + ['class' => 'yii\grid\SerialColumn'], + + 'comment_id', + 'comment_pid', + 'content:ntext', + 'created_at', + 'entity_id', + // 'user_id', + + ['class' => 'yii\grid\ActionColumn'], + ], + ]); ?> + +
diff --git a/backend/views/comment/update.php b/backend/views/comment/update.php new file mode 100644 index 0000000..1ea8244 --- /dev/null +++ b/backend/views/comment/update.php @@ -0,0 +1,21 @@ +title = 'Update Comment: ' . ' ' . $model->comment_id; +$this->params['breadcrumbs'][] = ['label' => 'Comments', 'url' => ['index']]; +$this->params['breadcrumbs'][] = ['label' => $model->comment_id, 'url' => ['view', 'id' => $model->comment_id]]; +$this->params['breadcrumbs'][] = 'Update'; +?> +
+ +

title) ?>

+ + render('_form', [ + 'model' => $model, + ]) ?> + +
diff --git a/backend/views/comment/view.php b/backend/views/comment/view.php new file mode 100644 index 0000000..59aa83a --- /dev/null +++ b/backend/views/comment/view.php @@ -0,0 +1,40 @@ +title = $model->comment_id; +$this->params['breadcrumbs'][] = ['label' => 'Comments', 'url' => ['index']]; +$this->params['breadcrumbs'][] = $this->title; +?> +
+ +

title) ?>

+ +

+ $model->comment_id], ['class' => 'btn btn-primary']) ?> + $model->comment_id], [ + 'class' => 'btn btn-danger', + 'data' => [ + 'confirm' => 'Are you sure you want to delete this item?', + 'method' => 'post', + ], + ]) ?> +

+ + $model, + 'attributes' => [ + 'comment_id', + 'comment_pid', + 'content:ntext', + 'created_at', + 'entity_id', + 'user_id', + ], + ]) ?> + +
diff --git a/common/models/Comment.php b/common/models/Comment.php new file mode 100644 index 0000000..f6664c9 --- /dev/null +++ b/common/models/Comment.php @@ -0,0 +1,58 @@ + 'Comment ID', + 'comment_pid' => 'Comment Pid', + 'content' => 'Content', + 'created_at' => 'Created At', + 'entity_id' => 'Entity ID', + 'user_id' => 'User ID', + ]; + } + + public function getUser() + { + return $this->hasOne(User::className(), ['id' => 'user_id']); + } +} diff --git a/common/widgets/CommentWidget.php b/common/widgets/CommentWidget.php new file mode 100644 index 0000000..317dcff --- /dev/null +++ b/common/widgets/CommentWidget.php @@ -0,0 +1,87 @@ + +
+
+ ' . $d['username'] . '  + ' . date('F j Y h:i', strtotime($d['created_at'])) . ' + +
+
+

' . $d['content'] . '

+
+ +
+ '; + } + + private function display(array $comments, $level = 0) + { + foreach ($comments as $info) + { + echo ''; + } + + if (isset($this->reload) && $this->reload == false) + { + + } + + } + + private function field() + { + echo '
+
+ + + +
+
'; + } + + public function init() + { + + } + public function run() + { + echo '
'; + $this->display($this->comments); + echo '
'; + $this->field(); + } +} diff --git a/frontend/controllers/CommentController.php b/frontend/controllers/CommentController.php new file mode 100644 index 0000000..fd3a190 --- /dev/null +++ b/frontend/controllers/CommentController.php @@ -0,0 +1,69 @@ +with('user')->orderBy('created_at ASC')->all(); + + $comments = array(); + + foreach($post as $p) + { + $comments[$p['comment_id']] = array( + 'id' => $p['comment_id'], + 'parent' => $p['comment_pid'], + 'content' => $p['content'], + 'username' => $p->user->username, + 'created_at' => $p['created_at'], + 'childs' => array() + ); + } + + foreach ($comments as $key => &$val) + { + if ($val['parent'] != 0) + { + $comments[$val['parent']]['childs'][] =& $val; + } + } + unset($val); + + foreach ($comments as $key => $val) + { + if ($val['parent'] != 0) + { + unset($comments[$key]); + } + } + + if (Yii::$app->request->get('action') == 'reload') + { + return $this->renderAjax('index', array('data' => $comments)); + } + else + { + return $this->render('index', array('data' => $comments, 'reload' => false)); + } + } + + public function actionCreate() + { + $comment = new Comment; + + $comment->user_id = Yii::$app->user->identity->id; + $comment->content = Yii::$app->request->post('content'); + $comment->entity_id = 3;//Yii::$app->request->post('entity_id'); + $comment->comment_pid = Yii::$app->request->post('parent_id'); + $comment->created_at = date('Y-m-d H:i:s', time()); + + $comment->save(); + + } + +} diff --git a/frontend/views/comment/index.php b/frontend/views/comment/index.php new file mode 100644 index 0000000..2c6e7d4 --- /dev/null +++ b/frontend/views/comment/index.php @@ -0,0 +1,10 @@ +registerJsFile(Yii::$app->request->baseUrl . '/js/comments.js', ['yii\web\JqueryAsset']); + +?> + + + + $data, 'reload' => $reload]) ?> diff --git a/frontend/web/js/comments.js b/frontend/web/js/comments.js new file mode 100644 index 0000000..0878021 --- /dev/null +++ b/frontend/web/js/comments.js @@ -0,0 +1,92 @@ +$(document).ready(function(){ + + applyScript(); + + /*var pubnub = PUBNUB({ + subscribe_key: 'demo', + publish_key: 'demo' + }); + + pubnub.subscribe({ + channel: 'comment', + message: function(m){console.log(m)}, + error: function (error) { + // Handle error here + console.log(JSON.stringify(error)); + } + }); */ +}); + +function applyScript() +{ + + var parent_id = 0; + + var textarea = $(".new_comment").find("textarea"); + var replyName; + + $(".comment_reply").each(function() + { + $(this).hide(); + }); + + $(".btn-reply").each(function() + { + $(this).on("click", function() + { + replyName = $(this).siblings().find(".comment_username").text(); + parent_id = $(this).parents(".comment_body").attr("comment_id"); + + textarea.val(replyName + ', '); + + }); + + }); + + $(".btn-send").on('click', function() + { + var content = textarea.val(); + + if (content.indexOf(replyName) > -1 == false) + { + parent_id = 0; + } + createComment(content, parent_id); + }); + + function reload_comments() + { + var path = window.location.protocol + "//" + window.location.host + "/"; + $.ajax({ + type: 'GET', + url : path + 'comment', + data : { + 'action' : 'reload' + }, + success: function(res) + { + $("#comment_wrapper").html(res); + console.log('reloaded'); + + applyScript(); + } + }); + } + function createComment(content, parent_id) + { + $.ajax({ + type: 'POST', + url : '/comment/create', + data: { + 'content' : content, + 'parent_id' : parent_id + }, + success: function(res) + { + //reload_comments(); + alert('Comment sent'); + } + }); + } + +} -- libgit2 0.21.4