Ejemplo n.º 1
0
 public function actionBlog($id = null)
 {
     $user = Yii::$app->user->id;
     /** @var $profile \app\models\Profile */
     if ($id === null) {
         $profile = Profile::findOne(['user_id' => $user]);
         $queryPosts = Posts::find()->where(['user_id' => $user]);
     } else {
         $queryPosts = Posts::find()->where(['user_id' => $id]);
         $profile = Profile::findOne(['user_id' => $id]);
         if (!isset($profile)) {
             Yii::$app->getSession()->setFlash('error', 'that profile not found');
             return $this->goHome();
         }
     }
     $pagination = new Pagination(['defaultPageSize' => 5, 'totalCount' => $queryPosts->count()]);
     $posts = $queryPosts->orderBy(['id' => SORT_DESC])->offset($pagination->offset)->limit($pagination->limit)->all();
     $model = new Posts();
     if ($model->load(Yii::$app->request->post()) && $model->validate()) {
         if ($model->createPost()) {
             $this->refresh();
         } else {
             $errors = $model->errors;
             Yii::$app->session->setFlash('error', 'Error: check the fields');
         }
     }
     return $this->render('blog', ['posts' => $posts, 'profile' => $profile, 'activePage' => Yii::$app->request->get('page', 1), 'countPages' => $pagination->getPageCount(), 'pagination' => $pagination, 'model' => $model]);
 }