Esempio n. 1
0
 public function run()
 {
     $request = Yii::$app->getRequest();
     $post = BlogPost::findOne(['surname' => Yii::$app->request->get('surname')]);
     $post_id = $post->id;
     //$post = BlogPost::findOne($post_id);
     $this->modelComment = new BlogComment();
     if ($request->isAjax && $this->modelComment->load($request->post())) {
         Yii::$app->response->format = Response::FORMAT_JSON;
         $this->modelComment->status = Status::STATUS_INACTIVE;
         $errors = ActiveForm::validate($this->modelComment);
         if (count($errors) != 0) {
             Yii::$app->getSession()->setFlash('blog.commentAddError', json_encode($errors));
         } else {
             $transaction = Yii::$app->db->beginTransaction();
             try {
                 if (!$this->modelComment->save()) {
                     Yii::$app->getSession()->setFlash('blog.commentAddError', 'Ошибка');
                     //throw new ErrorException('Error!');
                 }
                 $transaction->commit();
                 Yii::$app->getSession()->setFlash('blog.commentAddSuccess', 'Отправлено на модерацию');
             } catch (ErrorException $e) {
                 $transaction->rollBack();
                 Yii::$app->getSession()->setFlash('blog.commentAddError', $e->getMessage());
             }
         }
         $this->modelComment = new BlogComment();
     }
     $commentsList = BlogComment::find()->where(['post_id' => $post_id, 'status' => Status::STATUS_ACTIVE])->orderBy(['created_at' => SORT_DESC])->all();
     $this->modelComment->post_id = $post_id;
     if (!Yii::$app->user->isGuest) {
         $this->modelComment->email = Yii::$app->params['blogUserEMail'];
         $this->modelComment->author = Yii::$app->params['blogUserDisplayName'];
         $this->modelComment->user_id = Yii::$app->params['blogUserId'];
     }
     return $this->render('@sircovsw/blog/widgets/Comment/views/index', array('modelComment' => $this->modelComment, 'commentsList' => $commentsList));
 }
 public function actionView()
 {
     $this->layout = 'mainView';
     if (Yii::$app->request->get('surname')) {
         $post = BlogPost::findOne(['surname' => Yii::$app->request->get('surname')]);
         if ($post) {
             $post->updateCounters(['click' => 1]);
         } else {
             $this->redirect(['blog']);
         }
     } else {
         $this->redirect(['blog']);
     }
     $cacheId = Yii::$app->params["domain"] . '_blog_catalog';
     $catalogs = Yii::$app->cache->get($cacheId);
     if ($catalogs === false) {
         $queryCatalogs = BlogCatalog::find();
         $catalogs = $queryCatalogs->all();
         Yii::$app->cache->set($cacheId, $catalogs, $this->cacheDuration);
     }
     return $this->render('view', ['catalogs' => $catalogs, 'post' => $post]);
 }
 /**
  * Finds the BlogPost model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return BlogPost the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = BlogPost::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }