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]); } }
public function checkGoogleCoordinates() { if ($this->streetAddress && $this->city && $this->state && $this->zipcode) { $googleHelper = new GoogleAPIHelper(); $coordinates = $googleHelper->identifyLatLon([$this->streetAddress, $this->city, $this->state, $this->zipcode]); if (!$coordinates) { $this->addError('latitude', 'Wrong response from Google Maps, please check your address'); $this->addError('longitude', 'Wrong response from Google Maps, please check your address'); } } }
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(); }