/** * Lists all GuestBook models. * @return mixed */ public function actionIndex() { $searchModel = new GuestBookSearch(); $dataProvider = $searchModel->search(Yii::$app->request->queryParams); $blackList = BlackList::find()->count(); return $this->render('index', ['searchModel' => $searchModel, 'dataProvider' => $dataProvider, 'blackList' => $blackList]); }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = BlackList::find()->orderBy('id DESC'); // add conditions that should always apply here $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); return $dataProvider; } // grid filtering conditions $query->andFilterWhere(['id' => $this->id, 'user_id' => $this->user_id]); $query->andFilterWhere(['like', 'email', $this->email])->andFilterWhere(['like', 'ip', $this->ip]); return $dataProvider; }
/** * Lists all GuestBook models. * @return mixed */ public function actionIndex() { $model = new GuestBook(); if ($model->load(Yii::$app->request->post())) { // $url ='http://freegeoip.net/json/'; // $ch = curl_init(); // // Disable SSL verification // curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // // Will return the response, if false it print the response // curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // // Set the url // curl_setopt($ch, CURLOPT_URL,$url); // // Execute // $result=curl_exec($ch); // // Closing // curl_close($ch); // // Will dump a beauty json :3 // $ipDetails = json_decode($result, true); // $ipDetails = $_SERVER["REMOTE_ADDR"]; // if (!is_null($ipDetails)) { // $model->ip = $ipDetails['ip']; // } else { // $model->ip = 'NULL'; // } $ip = $_SERVER['REMOTE_ADDR']; if (isset($ip)) { $model->ip = $ip; } else { $model->ip = 'NULL'; } $query = BlackList::find()->where(['email' => $model->email])->orWhere(['ip' => $model->ip]); if (!Yii::$app->user->isGuest) { $query->orWhere(['user_id' => $model->ip]); } // $query->one(); $blacklistedCheck = $query->one(); if (is_null($blacklistedCheck)) { // $blacklistedCheck = BlackList::find() // ->where(['email'=>$model->email]) // ->orWhere(['ip'=>$model->ip]) // ->orWhere(['user_id'=>$model->ip]) // ->one(); $model->date = time(); $model->status = 'on'; if (Yii::$app->user->isGuest) { $model->user_id = 0; } else { $model->user_id = Yii::$app->user->identity->id; $userDetails = User::findOne($model->user_id); $model->name = $userDetails['username']; $model->email = $userDetails['email']; } if ($model->save()) { // $model = new GuestBook(); return Alert::widget(['options' => ['class' => 'alert-success'], 'body' => '<b>Успешно!</b> Ваша запись опубликованна.']); } else { var_dump($model->errors); exit; } } else { return Alert::widget(['options' => ['class' => 'alert-danger'], 'body' => '<b>Ошибка!</b> Вы в чёрном списке.']); } } $dataProvider = new ActiveDataProvider(['query' => GuestBook::find()->where(['status' => 'on'])->orderBy('date DESC'), 'pagination' => ['pageSize' => 20]]); return $this->render('index', ['dataProvider' => $dataProvider, 'model' => $model]); }