/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Accident::find(); $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['pageSize' => 100]]); $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; } $query->andFilterWhere(['id' => $this->id, 'dateCreate' => $this->dateCreate, 'date' => $this->date, 'status' => $this->status, 'lng' => $this->lng, 'lat' => $this->lat]); $query->andFilterWhere(['like', 'description', $this->description]); return $dataProvider; }
/** * Подготовить аварии * @param float $startLat * @param float $startLng */ public function prepareAccident($startLat, $startLng) { $coordinates = []; foreach ($this->routeCoordinates as $route) { $coordinate = ['lat' => ['from' => $startLat, 'to' => $route->lat], 'lng' => ['from' => $startLng, 'to' => $route->lng]]; $coordinates[] = $coordinate; $startLat = $route->lat; $startLng = $route->lng; } $accidents = Accident::find()->getAccidents($coordinates); foreach ($accidents as $accident) { $accidentModel = new \app\models\api\Accident(); $accidentModel->lat = (double) $accident['lat']; $accidentModel->lng = (double) $accident['lng']; $accidentModel->victimsCount = (int) $accident['victimsCount']; $accidentModel->deathsCount = (int) $accident['deathsCount']; $accidentModel->accidentsCount = $accidentModel->victimsCount + $accidentModel->deathsCount; $this->accidents[] = $accidentModel; $this->victimsCount += $accidentModel->victimsCount; $this->deathsCount += $accidentModel->deathsCount; } $this->accidentsCount += $this->victimsCount + $this->deathsCount; }
/** * Finds the Accident model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Accident the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Accident::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
/** * Обработать файл */ public function processFile() { $file = file_get_contents($this->getAddress()); $coding = mb_detect_encoding($file, mb_detect_order(), true); if (!$coding) { $file = iconv("WINDOWS-1251", "UTF-8", $file); file_put_contents($this->getAddress(), $file); } $file = fopen($this->getAddress(), 'r'); $isFirst = true; $accidents = []; while (($data = fgetcsv($file, null, ';')) !== FALSE) { if ($isFirst) { $isFirst = false; continue; } $item = ['description' => $data[1], 'lng' => str_replace(',', '.', $data[2]), 'lat' => str_replace(',', '.', $data[3]), 'date' => \DateTime::createFromFormat('d.m.Y H:m', $data[0])->format('Y-m-d H:m:s')]; if ($item['description'] == 'ДТП БЕЗ ПОТЕРПIЛИХ') { $item['status'] = Enum::ACCIDENT_STATUS_VICTIMS; } elseif ($item['description'] == 'ДТП З ПОТЕРПIЛИМИ') { $item['status'] = Enum::ACCIDENT_STATUS_DEATHS; } else { continue; } $accidents[] = $item; } return Accident::find()->insetBatchAccidents($accidents); }