public function updateAbbr($id = null) { $Geocoder = new GeocodeLib(); $override = false; if ($id == -1) { $id = ''; $override = true; } if (!empty($id)) { $res = $this->find('first', ['conditions' => [$this->alias() . '.id' => $id], 'contain' => []]); if (!empty($res['ori_name']) && $Geocoder->geocode($res['ori_name']) || $res['name'] != $res['ori_name'] && $Geocoder->geocode($res['name'])) { } } else { $conditions = []; if (!$override) { $conditions = [$this->alias() . '.iso2' => '']; # right now only iso2 } $results = $this->find('all', ['conditions' => $conditions, 'contain' => []]); $count = 0; foreach ($results as $res) { if (!empty($res['ori_name']) && $Geocoder->geocode($res['ori_name']) || $res['name'] != $res['ori_name'] && $Geocoder->geocode($res['name'])) { $data = $Geocoder->getResult(); //echo returns($res); //echo returns($data); die(); # seems to be very problematic: country "Georgien" results in "Georgia, USA" $saveArray = []; if (!empty($data['country_code']) && mb_strlen($data['country_code']) === 3 && preg_match('/^([A-Z])*$/', $data['country_code'])) { $saveArray['iso3'] = $data['country_code']; //die(returns($saveArray)); } elseif (!empty($data['country_code']) && mb_strlen($data['country_code']) === 2 && preg_match('/^([A-Z])*$/', $data['country_code'])) { $saveArray['iso2'] = $data['country_code']; //die(returns($saveArray)); } $this->id = $res['id']; if ($this->save($saveArray, true, ['iso2', 'iso3'])) { $count++; if (!empty($saveArray['iso2']) && $saveArray['iso2'] != $res['iso2']) { $this->log('Iso2 for country \'' . $data['country'] . '\' changed from \'' . $res['iso2'] . '\' to \'' . $saveArray['iso2'] . '\'', LOG_NOTICE); } if (!empty($saveArray['iso3']) && $saveArray['iso3'] != $res['iso3']) { $this->log('Iso3 for country \'' . $data['country'] . '\' changed from \'' . $res['iso3'] . '\' to \'' . $saveArray['iso3'] . '\'', LOG_NOTICE); } } else { //pr($data); pr($Geocoder->debug()); die(); } $Geocoder->pause(); } } return $count; } return false; }
/** * Lat and lng + abbr if available! * * @param id * - NULL: update all records with missing coordinates only * - otherwise: specific update */ public function updateCoordinates($id = null) { $geocoder = new GeocodeLib(); $override = false; if ($id == -1) { $id = ''; $override = true; } if (!empty($id)) { $res = $this->find('first', ['conditions' => [$this->alias() . '.id' => $id], 'contain' => ['Country.name']]); if (!empty($res['name']) && !empty($res[$this->Country->alias]['name']) && $geocoder->geocode($res['name'] . ', ' . $res[$this->Country->alias]['name'])) { $data = $geocoder->getResult(); //pr($data); die(); $saveArray = ['id' => $id, 'lat' => $data['lat'], 'lng' => $data['lng'], 'country_id' => $res['country_id']]; if (!empty($data['country_province_code']) && mb_strlen($data['country_province_code']) <= 3 && preg_match('/^([A-Z])*$/', $data['country_province_code'])) { $saveArray['abbr'] = $data['country_province_code']; } $this->id = $id; if (!$this->save($saveArray, true, ['id', 'lat', 'lng', 'abbr', 'country_id'])) { if ($data['country_province_code'] !== 'DC') { /* echo returns($this->id); pr($res); pr($data); pr($saveArray); die(returns($this->validationErrors)); */ throw new \Exception('Update Error'); } } return true; } } else { $conditions = []; if (!$override) { $conditions = [$this->alias() . '.lat' => 0, $this->alias() . '.lng' => 0]; } $results = $this->find('all', ['conditions' => $conditions, 'contain' => ['Country.name'], 'order' => ['CountryProvince.modified' => 'ASC']]); $count = 0; foreach ($results as $res) { if (!empty($res['name']) && !empty($res[$this->Country->alias]['name']) && $geocoder->geocode($res['name'] . ', ' . $res[$this->Country->alias]['name'])) { $data = $geocoder->getResult(); //pr($data); die(); //pr ($geocoder->debug()); $saveArray = ['id' => $res['id'], 'country_id' => $res['country_id']]; if (isset($data['lat']) && isset($data['lng'])) { $saveArray = array_merge($saveArray, ['lat' => $data['lat'], 'lng' => $data['lng']]); } if (!empty($data['country_province_code']) && mb_strlen($data['country_province_code']) <= 3 && preg_match('/^([A-Z])*$/', $data['country_province_code'])) { $saveArray['abbr'] = $data['country_province_code']; } $this->id = $res['id']; if ($this->save($saveArray, true, ['lat', 'lng', 'abbr', 'country_id'])) { $count++; if (!empty($saveArray['abbr']) && $saveArray['abbr'] != $res['abbr']) { $this->log('Abbr for country province \'' . $data['country_province'] . '\' changed from \'' . $res['abbr'] . '\' to \'' . $saveArray['abbr'] . '\'', LOG_NOTICE); } } else { //pr($data); pr($geocoder->debug()); die(); if ($data['country_province_code'] !== 'DC') { echo returns($this->id); pr($res); pr($data); pr($saveArray); die(returns($this->validationErrors)); } } $geocoder->pause(); } } return $count; } return false; }