/**
  * Profile
  */
 public function actionProfile()
 {
     /** @var \common\modules\user\models\Profile $profile */
     if (Yii::getAlias('@app') == Yii::getAlias('@backend')) {
         throw new NotFoundHttpException('Страница не найдена.');
     }
     $profile = Yii::$app->user->identity->profile;
     // blogPostsDataProvider
     $query = Post::find()->where(['is_public' => 1, 'user_id' => $profile->user->id, 'content_category_id' => Post::CATEGORY_BLOG]);
     $query->orderBy(['created_at' => SORT_DESC]);
     $blogPostsDataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['pageSize' => 5, 'pageParam' => 'bpage', 'pageSizeParam' => 'bpsize']]);
     $connection = Yii::$app->db;
     $countSql = 'SELECT COUNT(*) as count  
         FROM comments c1 
         LEFT JOIN posts p ON p.id = c1.commentable_id 
         WHERE c1.user_id = :user_id AND c1.id IN (
             SELECT c2.parent_id 
             FROM comments c2 
             WHERE c2.parent_id = c1.id
         )';
     $cmd = $connection->createCommand($countSql);
     $cmd->bindValue(':user_id', $profile->user->id);
     $commentsCountData = $cmd->queryAll();
     $commentsCount = $commentsCountData[0]['count'];
     $commentsPagination = new Pagination(['totalCount' => $commentsCount, 'pageSize' => 10, 'pageParam' => 'cpage', 'pageSizeParam' => 'cpsize']);
     // AND c1.parent_id IS NULL
     $sql = 'SELECT c1.id 
         FROM comments c1 
         LEFT JOIN posts p ON p.id = c1.commentable_id 
         WHERE c1.user_id = :user_id AND c1.id IN (
             SELECT c2.parent_id 
             FROM comments c2 
             WHERE c2.parent_id = c1.id
         ) 
         ORDER BY c1.created_at DESC 
         LIMIT :offset, :rows';
     $cmd = $connection->createCommand($sql);
     $cmd->bindValue(':user_id', $profile->user->id);
     $cmd->bindValue(':offset', $commentsPagination->offset);
     $cmd->bindValue(':rows', $commentsPagination->limit);
     $commentsData = $cmd->queryAll();
     $ids = [];
     foreach ($commentsData as $data) {
         $ids[] = $data['id'];
     }
     $initialComments = Comment::find()->where(['id' => $ids])->orderBy(['created_at' => SORT_DESC])->all();
     $comments = $initialComments;
     $ids = [];
     foreach ($comments as $comment) {
         $ids[] = $comment->id;
     }
     $childComments = Comment::find()->where(['parent_id' => $ids])->orderBy(['created_at' => SORT_ASC])->all();
     if (count($childComments) > 0) {
         $initialComments = array_merge($initialComments, $childComments);
     }
     $parentIDs = [];
     foreach ($initialComments as $comment) {
         if ($comment->parent_id != null) {
             $parentIDs[] = $comment->parent_id;
         }
     }
     $sortedComments = [];
     foreach ($initialComments as $comment) {
         if ($comment->parent_id == null || $comment->user_id == $profile->user->id && in_array($comment->id, $parentIDs)) {
             $index = 0;
         } else {
             $index = $comment->parent_id;
         }
         $sortedComments[$index][] = $comment;
     }
     $commentForm = new CommentForm();
     $additionalBlocks = ['fisrtBanner' => SiteBlock::getBanner(\common\models\Banner::REGION_FIRST_COLUMN), 'secondBanner' => SiteBlock::getBanner(\common\models\Banner::REGION_FIRST_COLUMN), 'photo_news' => SiteBlock::getPhotoNews(), 'video_news' => SiteBlock::getVideoNews(), 'subscribing' => SiteBlock::getSubscribingForm(), 'questionBlock' => SiteBlock::getQuestionBlock()];
     // render
     return $this->render('@frontend/views/site/index', ['templateType' => 'col2', 'title' => 'Профиль', 'columnFirst' => ['user_comments' => ['view' => '@frontend/views/profile/user_comments', 'data' => ['comments' => $sortedComments, 'pagination' => $commentsPagination, 'commentForm' => $commentForm]], 'profile' => ['view' => '@frontend/views/profile/profile_view', 'data' => compact('profile')], 'blog_column' => ['view' => '@frontend/views/profile/blog_posts', 'data' => ['blogPostsDataProvider' => $blogPostsDataProvider]], 'additional_data' => ['view' => '@frontend/views/profile/additional_data', 'data' => ['blocks' => $additionalBlocks]]], 'columnSecond' => ['blogs' => SiteBlock::getBlogPosts()]]);
 }