Пример #1
0
 public function actionComment()
 {
     if (isset($_POST['user_id']) && isset($_POST['post_id']) && isset($_POST['content'])) {
         $comment = new Comment();
         $comment['user_id'] = $_POST['user_id'];
         $comment['post_id'] = $_POST['post_id'];
         $comment['content'] = $_POST['content'];
         $comment['create_at'] = $_POST['create_at'];
         //$comment['create_at'] = Yii::$app->formatter->asDatetime("Y-m-d");
         $comment->save();
         $user = User::findOne(['id' => $_POST['user_id']]);
         if ($user['image'] != "") {
             $image = Yii::$app->request->baseUrl . "/images/" . $user['image'];
         } else {
             $image = Yii::$app->request->baseUrl . "/images/avatar-default.jpg";
         }
         $id = $_POST['post_id'];
         $receiver = Post::findOne(['id' => $id])['user_id'];
         if ($receiver != Yii::$app->user->getId()) {
             $newNotify = new PostNotification();
             $newNotify['post_id'] = $id;
             $newNotify['type'] = 1;
             $newNotify['status'] = 0;
             $newNotify['action_id'] = Yii::$app->user->getId();
             $newNotify['receiver_id'] = $receiver;
             $newNotify['create_at'] = date("Y/m/d H:i");
             $newNotify->save();
         }
         $listComment = Comment::find()->where(['post_id' => $id])->asArray()->all();
         foreach ($listComment as $oneComment) {
             $isAdded[$oneComment['user_id']] = 0;
         }
         $isAdded[$receiver] = 1;
         foreach ($listComment as $oneComment) {
             $receiver = $oneComment['user_id'];
             if ($receiver != Yii::$app->user->getId() && $isAdded[$receiver] != 1) {
                 $newNotify = new PostNotification();
                 $newNotify['post_id'] = $id;
                 $newNotify['type'] = 1;
                 $newNotify['status'] = 0;
                 $newNotify['action_id'] = Yii::$app->user->getId();
                 $newNotify['receiver_id'] = $receiver;
                 $newNotify['create_at'] = date("Y/m/d H:i");
                 $newNotify->save();
             }
             $isAdded[$receiver] = 1;
         }
         echo '<div class="box-comment">' . '<img class="img-circle img-sm" src="' . $image . '" alt="user image">' . '<div class="comment-text">' . '<span class="username">' . $user['username'] . '<span class="text-muted pull-right">' . $comment['create_at'] . '</span>' . '</span>' . $comment['content'] . '</div>' . '</div>';
     } else {
         echo 'NO';
     }
 }