コード例 #1
0
 protected function _installStates($xml)
 {
     if (isset($xml->states->state)) {
         foreach ($xml->states->state as $data) {
             $attributes = $data->attributes();
             if (!($id_state = State::getIdByName($attributes['name']))) {
                 $state = new State();
                 $state->name = strval($attributes['name']);
                 $state->iso_code = strval($attributes['iso_code']);
                 $state->id_country = Country::getByIso(strval($attributes['country']));
                 $state->id_zone = (int) Zone::getIdByName(strval($attributes['zone']));
                 if (!$state->validateFields()) {
                     $this->_errors[] = Tools::displayError('Invalid state properties.');
                     return false;
                 }
                 $country = new Country($state->id_country);
                 if (!$country->contains_states) {
                     $country->contains_states = 1;
                     if (!$country->update()) {
                         $this->_errors[] = Tools::displayError('Cannot update the associated country: ') . $country->name;
                     }
                 }
                 if (!$state->add()) {
                     $this->_errors[] = Tools::displayError('An error occurred while adding the state.');
                     return false;
                 }
             } else {
                 $state = new State($id_state);
                 if (!Validate::isLoadedObject($state)) {
                     $this->_errors[] = Tools::displayError('An error occurred while fetching the state.');
                     return false;
                 }
             }
             // Add counties
             foreach ($data->county as $xml_county) {
                 $county_attributes = $xml_county->attributes();
                 if (!($id_county = County::getIdCountyByNameAndIdState($county_attributes['name'], $state->id))) {
                     $county = new County();
                     $county->name = $county_attributes['name'];
                     $county->id_state = (int) $state->id;
                     $county->active = 1;
                     if (!$county->validateFields()) {
                         $this->_errors[] = Tools::displayError('Invalid County properties');
                         return false;
                     }
                     if (!$county->save()) {
                         $this->_errors[] = Tools::displayError('An error has occurred while adding the county');
                         return false;
                     }
                 } else {
                     $county = new County((int) $id_county);
                     if (!Validate::isLoadedObject($county)) {
                         $this->_errors[] = Tools::displayError('An error occurred while fetching the county.');
                         return false;
                     }
                 }
                 // add zip codes
                 foreach ($xml_county->zipcode as $xml_zipcode) {
                     $zipcode_attributes = $xml_zipcode->attributes();
                     $zipcodes = $zipcode_attributes['from'];
                     if (isset($zipcode_attributes['to'])) {
                         $zipcodes .= '-' . $zipcode_attributes['to'];
                     }
                     if ($county->isZipCodeRangePresent($zipcodes)) {
                         continue;
                     }
                     if (!$county->addZipCodes($zipcodes)) {
                         $this->_errors[] = Tools::displayError('An error has occurred while adding zipcodes');
                         return false;
                     }
                 }
             }
         }
     }
     return true;
 }