Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ############################
- // ###### Perform import ######
- // ############################
- // At this point it's reasonably certain that the import can succeed, so
- // start the transaction
- R::begin();
- // Iterate through the data and start importing rows
- foreach($td->data as $index => $dataRow)
- {
- $tableRow = R::dispense(self::TABLE_DATA);
- $tableRow->group = $group;
- // Set title
- if(isset($mapping[self::COLUMN_TITLE]))
- {
- $tableRow->title = $dataRow[$mapping[self::COLUMN_TITLE][1]];
- }
- // If the flag for UTM coordinates is up, convert the UTM
- // coordinates to WGS84 and set them on the table row
- if($flag_useutmcoords)
- {
- // Fetch values for utmx and utmy
- $utmx = $dataRow[$mapping[self::COLUMN_UTMX][1]];
- $utmy = $dataRow[$mapping[self::COLUMN_UTMY][1]];
- $map_utmzone = $mapping[self::COLUMN_UTM_ZONE];
- // Get value for utm_zone depending on whether the import type
- // is set to column or static
- $utmzone = $map_utmzone[0] == 'column' ?
- $dataRow[$map_utmzone[1]] : $map_utmzone[1];
- // Convert UTM to WGS84
- $convert = Helpers::UtmToLatLng($utmx, $utmy, $utmzone);
- $tableRow->lat = $convert['lat'];
- $tableRow->lng = $convert['lng'];
- }
- // Else if the flag for WGS84 coordinates is up, simply set the
- // coordinates to the table row
- else if($flag_usewgscoords)
- {
- $tableRow->lat = $dataRow[$mapping[self::COLUMN_LAT][1]];
- $tableRow->lng = $dataRow[$mapping[self::COLUMN_LNG][1]];
- }
- // Else if the flag for using address coordinates is set, try to
- // Get the coordinates from those
- else if($flag_useaddrcoords)
- {
- // Instantiate an address object
- $address = new Address();
- // If the address is contained in a single field, assume the
- // format "%streetname% %num% %letter%, %postalcode% %city%"
- if($flag_hassingleaddress)
- {
- // Shortcut to the address
- $addr = $dataRow[$mapping[self::COLUMN_ADDR][1]];
- $splitregex = $this->regexes['single_field_address_parser'];
- // Perform the regex match
- $matches = array();
- $match = preg_match($splitregex, $addr, $matches);
- // If no address could be devised from the field
- if($match === 0)
- {
- // Log the failure and continue to the next data row
- $this->importLog[] = array(
- 'code' => self::ERROR_INVALIDADDRESSFORMAT,
- 'message' => sprintf(
- "Bad address format '%s'\n", $addr
- ),
- 'file_name' => sprintf(
- '%s.%s', $file->name, $file->ext
- ),
- 'line_number' => $index,
- 'line_contents' => $dataRow
- );
- // Iterate the rowsNotImported counter
- $this->rowsNotImported += 1;
- continue;
- }
- // If there is a problem with the regex resource then throw
- // an exception about it and rollback.
- else if($match === false)
- {
- R::rollback();
- throw new OmniDataManagerException(
- "An error occurred while parsing an address field",
- 0,
- array(
- 'regex' => $splitregex,
- 'dataRowIndex' => $index,
- 'dataRow' => $dataRow
- )
- );
- }
- // Otherwise the match was a great success and we can map
- // the fields to the address object
- $address->city = $matches['city'];
- $address->postalCode = $matches['postalcode'];
- $address->streetName = $matches['streetname'];
- $address->houseNumber = intval($matches['housenumber']);
- // Even add the letter to the address (if there is one) for
- // future use
- if(!empty($matches['letter']))
- {
- $address->letter = $matches['letter'];
- }
- }
- // Otherwise the address is contained in 4 separate fields,
- // which makes things easy
- else
- {
- $address->city =
- $dataRow[$mapping[self::COLUMN_ADDR_CITY][1]];
- $address->postalCode =
- $dataRow[$mapping[self::COLUMN_ADDR_POSTALCODE][1]];
- $address->streetName =
- $dataRow[$mapping[self::COLUMN_ADDR_STREETNAME][1]];
- $address->houseNumber =
- $dataRow[$mapping[self::COLUMN_ADDR_HOUSENUMBER][1]];
- }
- // With the address object populated, run the validation
- // function
- try
- {
- $address->validate();
- // Check if the matcher had to make any guesses to find a
- // match
- if(!empty($address->result['process']))
- {
- // Log the guess
- $this->importLog[] = array(
- 'code' => self::ERROR_GUESSEDADDRESS,
- 'message' => 'The input address had to be ' .
- 'processed before a match was found',
- 'file_name' => sprintf(
- "%s.%s", $file->name, $file->ext
- ),
- 'line_number' => $index,
- 'line_contents' => $dataRow,
- 'extra' => array(
- 'original_address' =>
- $address->result['input_address'],
- 'matched_address' => (string)$address
- )
- );
- }
- }
- // If the address validation failed, log it and continue to the
- // next row
- catch(AddressException $e)
- {
- if($e->getCode() === Address::ERROR_VALIDATION)
- {
- $this->importLog[] = array(
- 'code' => self::ERROR_UNKNOWNADDRESS,
- 'message' => sprintf(
- "Could not find a match for address '%s'\n",
- $addr
- ),
- 'file_name' => sprintf(
- "%s.%s", $file->name, $file->ext
- ),
- 'line_number' => $index,
- 'line_contents' => $dataRow,
- 'extra' => array(
- 'problem' => $e->extra['problem'],
- 'process' => $e->extra['process'],
- 'extra' => $e->extra['extra']
- )
- );
- // Iterate the rowsNotImported counter
- $this->rowsNotImported += 1;
- continue;
- }
- else
- {
- // Something unexpected happened in this case. Bail out
- // and pass the exception along
- R::rollback();
- throw $e;
- }
- }
- // After the address is validated the WGS84 coordinates will be
- // available
- $tableRow->lat = $address->latitude;
- $tableRow->lng = $address->longitude;
- }
- // If additional data is set to be imported
- if(isset($mapping[self::COLUMN_DATA]))
- {
- $data = array();
- // Iterate through the extra data rows
- foreach($mapping[self::COLUMN_DATA] as $key => $value)
- {
- // If the import type if static, just set the static value
- if($value[0] == 'static')
- {
- $data[$key] = $value[1];
- }
- // If the import type is column, fetch the data from that
- // column
- else if($value[0] == 'column')
- {
- $data[$key] = $dataRow[$value[1]];
- }
- }
- // Serialize the data object and insert it into the table row
- $tableRow->data = serialize($data);
- }
- // Save the row and move on
- R::store($tableRow);
- // Iterate the rowsSuccessfullyImported counter
- $this->rowsSuccessfullyImported += 1;
- }
- // Commit the changes to the database
- R::rollback();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement