コード例 #1
0
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = Claim::find();
     $user = Yii::$app->getModule("user")->model("User");
     $claimTable = Claim::tableName();
     $userTable = $user::tableName();
     $query->joinWith(['user' => function ($query) use($userTable) {
         $query->from(['user' => $userTable]);
     }]);
     $query->joinWith(['commentAuthor' => function ($query) use($userTable) {
         $query->from(['commentAuthor' => $userTable]);
     }]);
     $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['pageSize' => 10], 'sort' => ['defaultOrder' => ['created_at' => SORT_DESC]]]);
     // enable sorting for the related columns
     $addSortAttributes = ['user.username', 'commentAuthor.username'];
     foreach ($addSortAttributes as $addSortAttribute) {
         $dataProvider->sort->attributes[$addSortAttribute] = ['asc' => [$addSortAttribute => SORT_ASC], 'desc' => [$addSortAttribute => SORT_DESC]];
     }
     if (!($this->load($params) && $this->validate())) {
         return $dataProvider;
     }
     $query->andFilterWhere(["{$claimTable}.id" => $this->id, 'comment_id' => $this->comment_id, 'user_id' => $this->user_id, 'comment_author' => $this->comment_author]);
     $createdTime = strtotime($this->created_at);
     $startDay = date("Y-m-d 00:00:00", $createdTime);
     $endDay = date("Y-m-d 00:00:00", $createdTime + 60 * 60 * 24);
     if ($this->created_at) {
         $query->andFilterWhere(['between', 'created_at', $startDay, $endDay]);
     }
     $query->andFilterWhere(['like', 'message', $this->message])->andFilterWhere(['like', 'user.username', $this->getAttribute('user.username')])->andFilterWhere(['like', 'commentAuthor.username', $this->getAttribute('commentAuthor.username')]);
     return $dataProvider;
 }
コード例 #2
0
 /**
  * Finds the Claim model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return Claim the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = Claim::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
コード例 #3
0
 /**
  * Url: /complain/{$id}
  * @param int $id Comment id
  * @return mixed
  * @throws BadRequestHttpException
  * @throws NotFoundHttpException
  */
 public function actionComplain($id)
 {
     $comment = Comment::findOne($id);
     if (!isset($comment)) {
         throw new NotFoundHttpException('Страница не найдена.');
     }
     if (Yii::$app->user->isGuest) {
         throw new BadRequestHttpException('Для отправки жалобы авторизируйтесь.');
     }
     $claim = Claim::find()->where(['comment_id' => $comment->id, 'user_id' => Yii::$app->user->id])->one();
     if (!isset($claim->id)) {
         $claim = new Claim();
         $claim->comment_id = $comment->id;
         $claim->comment_author = $comment->user_id;
         $claim->user_id = Yii::$app->user->id;
     }
     if ($claim->load(Yii::$app->request->post())) {
         $claim->save();
     }
     return $this->render('@frontend/views/site/index', ['templateType' => 'col2', 'title' => 'Dynamomania.com | Жалоба на комментарий', 'columnFirst' => ['claim' => ['view' => '@frontend/views/forms/complain_form', 'data' => compact('comment', 'claim')]], 'columnSecond' => ['photo_news' => SiteBlock::getPhotoNews(), 'banner1' => SiteBlock::getBanner(Banner::REGION_NEWS), 'banner2' => SiteBlock::getBanner(Banner::REGION_NEWS), 'banner3' => SiteBlock::getBanner(Banner::REGION_NEWS), 'banner4' => SiteBlock::getBanner(Banner::REGION_NEWS), 'banner5' => SiteBlock::getBanner(Banner::REGION_NEWS)]]);
 }