Пример #1
0
 public function actionView($id, $page = null, $pageSize = null)
 {
     $pageSize = $pageSize ?: Yii::$app->params['pageSize'];
     $page = $page ?: 1;
     $offset = ($page - 1) * $pageSize;
     /** @var iLinkoScope $api */
     $api = Yii::$app->linko->getApi();
     $form = new CommentForm();
     if ($form->load(Yii::$app->request->post()) && $form->validate()) {
         $comment = new Comment(['postId' => $id, 'content' => $form->comment, 'authorId' => Yii::$app->user->id, 'authorName' => Yii::$app->user->getIdentity()->username]);
         $api->addComment($comment);
         Yii::$app->session->setFlash('info', 'Your comment was added.');
         $this->redirect(['view', 'id' => $id]);
     }
     if ($form->hasErrors()) {
         Yii::$app->session->setFlash('error', implode('<br />', $form->getFirstErrors()));
     }
     $linkObject = $api->getLink($id);
     $req = new GetCommentsRequest();
     $req->maxResults = $pageSize;
     $req->offset = $offset;
     $req->linkId = $id;
     $comments = $api->getComments($req);
     $data = new ArrayDataProvider(['totalCount' => $comments->totalResults, 'models' => $comments->comments, 'pagination' => ['totalCount' => $comments->totalResults, 'defaultPageSize' => $pageSize]]);
     return $this->render('view', ['link' => $linkObject, 'comments' => $data, 'commentForm' => $form]);
 }