Beispiel #1
0
 /**
  * Finds the CommentRecord model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return CommentRecord the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = CommentRecord::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
Beispiel #2
0
 public function actionCreate()
 {
     $validator = new \yii\captcha\CaptchaValidator();
     $post = Yii::$app->request->post();
     $res = $validator->validate($post['captcha'], $err);
     if ($res) {
         $comment = new CommentRecord();
         $comment->parent_id = $post['parent_id'];
         $comment->type = $post['type'];
         $comment->text = $post['text'];
         $comment->user_name = $post['user_name'];
         $comment->active = CommentRecord::STATUS_IS_NOT_ACTIVE;
         $comment->save();
         Yii::$app->getSession()->setFlash('success', 'Ваш комментарий добавлен и после модерации появится на сайте');
     } else {
         Yii::$app->getSession()->setFlash('error', 'Код введен не верно');
     }
     $this->redirect($post['back_url']);
 }
Beispiel #3
0
 public function run()
 {
     $type = false;
     if ($this->type == self::TYPE_ARTICLE) {
         $type = 'article';
     } elseif ($this->type == self::TYPE_PAGE) {
         $type = 'page';
     } else {
         throw new Exception("Error CommentsWidget: unknown type [" . $this->type . "]");
     }
     $models = CommentRecord::find()->where(['parent_id' => (int) $this->parent_id, 'type' => $type, 'active' => CommentRecord::STATUS_IS_ACTIVE])->orderBy(['sort' => SORT_ASC])->all();
     return $this->render('comments', ['parent_id' => $this->parent_id, 'type' => $this->type, 'models' => $models]);
 }
Beispiel #4
0
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     if (!isset($params['parent_id'])) {
         throw new \Exception('Property parent_id must be setted');
     }
     // only for comments this way
     $query = CommentRecord::find()->where($this->getParentParams());
     $dataProvider = new ActiveDataProvider(['query' => $query, 'sort' => ['defaultOrder' => ['created_at' => SORT_DESC]], 'pagination' => ['pageSize' => 3]]);
     $this->load($params);
     if (!$this->validate()) {
         // uncomment the following line if you do not want to any records when validation fails
         // $query->where('0=1');
         return $dataProvider;
     }
     $query->andFilterWhere(['id' => $this->id, 'parent_id' => $this->parent_id, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at, 'sort' => $this->sort, 'user_id' => $this->user_id, 'active' => $this->active]);
     $query->andFilterWhere(['like', 'type', $this->type])->andFilterWhere(['like', 'text', $this->text]);
     return $dataProvider;
 }