public function actionCreatesingle() { $model = new NRESProperty(['scenario' => 'create']); // if (!\Yii::$app->request->isPost) { // $this->performAjaxValidation($model); // } if ($model->load(Yii::$app->request->post()) && $model->save()) { $googleHelper = new GoogleAPIHelper(); $coordinates = $googleHelper->identifyLatLon([$model->streetAddress, $model->city, $model->state, $model->zipcode]); if (!$coordinates) { echo json_encode(['error' => 'Wrong google coordinates']); Yii::$app->end(); } $model->latitude = $coordinates->lat; $model->longitude = $coordinates->lng; if ($model->save()) { Yii::$app->session->setFlash('success', 'New Property added.'); } else { Yii::$app->session->setFlash('warning', 'Error while saving property'); } if (Yii::$app->request->isAjax && $model->addNew == 1) { Yii::$app->session->getAllFlashes(); echo json_encode(Yii::$app->session->getAllFlashes()); Yii::$app->end(); } return $this->redirect(['index']); // Yii::$app->end(); } else { if ($model->load(Yii::$app->request->post()) && !$model->validate()) { echo json_encode(['errors' => $model->getErrors()]); Yii::$app->end(); } return $this->renderAjax('create', ['model' => $model]); } }
private function _createAffectedProperties($weatherAlertAreaId) { $areaDefinition = AreaDefinition::find()->where(['WeatherAlertArea_id' => $weatherAlertAreaId])->all(); $pointChecker = new PointInPolygon(); if (count($areaDefinition) && count($areaDefinition) == 1) { $needToInsertProperties = (int) $this->countPostAffected; $alertCircle = $areaDefinition[0]; while ($needToInsertProperties) { $property = new \stdClass(); $property->latitude = $alertCircle->latitude + $this->_generateRand(false, rand(300, 1000)); $property->longitude = $alertCircle->longitude + $this->_generateRand(false, rand(300, 1000)); if ($pointChecker->pointInsideCircle($alertCircle->latitude, $alertCircle->longitude, $alertCircle->radius, $property->latitude, $property->longitude)) { //insert property $newProperty = new NRESProperty(); $newProperty->attributes = (array) $property; $newProperty->name = 'Generated'; $newProperty->zipcode = '12345-6789'; $newProperty->city = 'Test'; $newProperty->state = 'CA'; $newProperty->streetAddress = 'Test'; $newProperty->client = 'Test'; $newProperty->status = 2; $newProperty->save(); if ($newProperty->getErrors()) { var_dump($newProperty->getErrors()); die; } $needToInsertProperties--; } } } else { $allCoordinates = []; $needToInsertProperties = $this->isStormPath ? $this->countAffectedInStormPath : (int) $this->countPreAffected; foreach ($areaDefinition as $point) { $allCoordinates[] = $point->latitude . ' ' . $point->longitude; } while ($needToInsertProperties) { $propertyToGet = rand(0, count($areaDefinition) - 1); $property = new \stdClass(); $property->latitude = $areaDefinition[$propertyToGet]->latitude + $this->_generateRand(); $property->longitude = $areaDefinition[$propertyToGet]->longitude + $this->_generateRand(); if ($pointChecker->pointInPolygon($property->latitude . ' ' . $property->longitude, $allCoordinates) !== "outside") { //insert property $newProperty = new NRESProperty(); $newProperty->attributes = (array) $property; $newProperty->name = 'Generated'; $newProperty->zipcode = '12345-6789'; $newProperty->city = 'Test'; $newProperty->state = 'CA'; $newProperty->streetAddress = 'Test'; $newProperty->client = 'Test'; $newProperty->status = 2; $newProperty->save(); $needToInsertProperties--; } } } }
public function processCsv($file) { ini_set('auto_detect_line_endings', true); try { $uploadedFile = Yii::$app->basePath . '/uploads/' . $file->baseName . '.' . $file->extension; $fileSize = $file->size; $delimiter = ","; $propertiesFromCSV = []; if (($handle = fopen($uploadedFile, 'r')) !== FALSE) { $csvFirstLine = fgetcsv($handle, 0, $delimiter); if (!$csvFirstLine) { $this->_log(1, 'Wrong CSV file format (can\'t read header information)'); echo json_encode($this->log); Yii::$app->end(); } if (count($csvFirstLine) != 7) { $this->_log(1, 'Wrong CSV file format (count of headers should be equal 7)'); echo json_encode($this->log); Yii::$app->end(); } $lineNumber = 2; while (($row = fgetcsv($handle, $fileSize, $delimiter)) !== FALSE) { $newProperty = new \stdClass(); $newProperty->line = $lineNumber; foreach ($row as $key => $element) { $newProperty->{$csvFirstLine}[$key] = $element; } $propertiesFromCSV[] = $newProperty; $lineNumber++; } } //Now we need to identify lat and lon for each record $googleAPIHelper = new GoogleAPIHelper(); foreach ($propertiesFromCSV as $property) { if (!isset($property->streetAddress) || !isset($property->city) || !isset($property->state) || !isset($property->zipcode)) { throw new Exception('CSV has missing parameters (Check headers line)'); } //Special check for our QA $propertyCheck = new NRESProperty(); $propertyCheck->attributes = (array) $property; $propertyCheck->status = NRESProperty::STATUS_ACTIVE; if (!$propertyCheck->validate()) { $modelValidationErrors = []; foreach ($propertyCheck->getErrors() as $err) { $modelValidationErrors[] = $err[0]; } $this->_log($property->line, "ERROR, NOT inserted (" . implode(',', $modelValidationErrors) . ")"); continue; } $coordinates = $googleAPIHelper->identifyLatLon([$property->streetAddress, $property->city, $property->state, $property->zipcode]); if ($coordinates) { $property->latitude = $coordinates->lat; $property->longitude = $coordinates->lng; if ($property->id) { //It's a record that we already have in our database $propertyFromTheDatabase = new NRESProperty(); $recordInDb = $propertyFromTheDatabase->findOne($property->id); if ($recordInDb) { $recordInDb->attributes = (array) $property; if ($recordInDb->save()) { $this->_log($property->line, "SUCCESS, Updated in the database, ID #" . $recordInDb->id); } else { $errorArray = []; foreach ($recordInDb->getErrors() as $error) { $errorArray[] = $error[0]; } $this->_log($property->line, "ERROR, NOT updated, ID #" . $recordInDb->id . ", Errors: (" . implode(',', $errorArray) . ")"); } } else { $this->_log($property->line, "ERROR, Record with ID #" . $property->id . ", does not exist."); } } else { //absolutelly new one $propertyNewModel = new NRESProperty(); $propertyNewModel->attributes = (array) $property; $propertyNewModel->status = NRESProperty::STATUS_ACTIVE; unset($propertyNewModel->id); if ($propertyNewModel->save()) { $this->_log($property->line, "SUCCESS, Added in the database, ID #" . $propertyNewModel->id); } else { $errorArray = []; foreach ($propertyNewModel->getErrors() as $error) { $errorArray[] = $error[0]; } $this->_log($property->line, "ERROR, NOT inserted (" . implode(',', $errorArray) . ")"); } } } else { // We need to reject such properties $this->_log($property->line, "ERROR, Can't detect geolocation information about provided address"); } } // var_dump($this->log);die; echo json_encode((object) $this->log); // die; } catch (Exception $e) { $this->_log(0, $e->getMessage()); echo json_encode((object) $this->log); } Yii::$app->end(); }