Advertisement
codemonkey

Malfunctioning import function

Jun 1st, 2012
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.80 KB | None | 0 0
  1.         // ############################
  2.         // ###### Perform import ######
  3.         // ############################
  4.  
  5.         // At this point it's reasonably certain that the import can succeed, so
  6.         // start the transaction
  7.         R::begin();
  8.  
  9.         // Iterate through the data and start importing rows
  10.         foreach($td->data as $index => $dataRow)
  11.         {
  12.             $tableRow = R::dispense(self::TABLE_DATA);
  13.             $tableRow->group = $group;
  14.  
  15.             // Set title
  16.             if(isset($mapping[self::COLUMN_TITLE]))
  17.             {
  18.                 $tableRow->title = $dataRow[$mapping[self::COLUMN_TITLE][1]];
  19.             }
  20.  
  21.             // If the flag for UTM coordinates is up, convert the UTM
  22.             // coordinates to WGS84 and set them on the table row
  23.             if($flag_useutmcoords)
  24.             {
  25.                 // Fetch values for utmx and utmy
  26.                 $utmx = $dataRow[$mapping[self::COLUMN_UTMX][1]];
  27.                 $utmy = $dataRow[$mapping[self::COLUMN_UTMY][1]];
  28.                 $map_utmzone = $mapping[self::COLUMN_UTM_ZONE];
  29.  
  30.                 // Get value for utm_zone depending on whether the import type
  31.                 // is set to column or static
  32.                 $utmzone = $map_utmzone[0] == 'column' ?
  33.                     $dataRow[$map_utmzone[1]] : $map_utmzone[1];
  34.  
  35.                 // Convert UTM to WGS84
  36.                 $convert = Helpers::UtmToLatLng($utmx, $utmy, $utmzone);
  37.  
  38.                 $tableRow->lat = $convert['lat'];
  39.                 $tableRow->lng = $convert['lng'];
  40.             }
  41.             // Else if the flag for WGS84 coordinates is up, simply set the
  42.             // coordinates to the table row
  43.             else if($flag_usewgscoords)
  44.             {
  45.                 $tableRow->lat = $dataRow[$mapping[self::COLUMN_LAT][1]];
  46.                 $tableRow->lng = $dataRow[$mapping[self::COLUMN_LNG][1]];
  47.             }
  48.             // Else if the flag for using address coordinates is set, try to
  49.             // Get the coordinates from those
  50.             else if($flag_useaddrcoords)
  51.             {
  52.                 // Instantiate an address object
  53.                 $address = new Address();
  54.  
  55.                 // If the address is contained in a single field, assume the
  56.                 // format "%streetname% %num% %letter%, %postalcode% %city%"
  57.                 if($flag_hassingleaddress)
  58.                 {
  59.                     // Shortcut to the address
  60.                     $addr = $dataRow[$mapping[self::COLUMN_ADDR][1]];
  61.  
  62.                     $splitregex = $this->regexes['single_field_address_parser'];
  63.  
  64.                     // Perform the regex match
  65.                     $matches = array();
  66.                     $match = preg_match($splitregex, $addr, $matches);
  67.  
  68.                     // If no address could be devised from the field
  69.                     if($match === 0)
  70.                     {
  71.                         // Log the failure and continue to the next data row
  72.                         $this->importLog[] = array(
  73.                             'code' => self::ERROR_INVALIDADDRESSFORMAT,
  74.                             'message' => sprintf(
  75.                                 "Bad address format '%s'\n", $addr
  76.                             ),
  77.                             'file_name' => sprintf(
  78.                                 '%s.%s', $file->name, $file->ext
  79.                             ),
  80.                             'line_number' => $index,
  81.                             'line_contents' => $dataRow
  82.                         );
  83.  
  84.                         // Iterate the rowsNotImported counter
  85.                         $this->rowsNotImported += 1;
  86.  
  87.                         continue;
  88.                     }
  89.                     // If there is a problem with the regex resource then throw
  90.                     // an exception about it and rollback.
  91.                     else if($match === false)
  92.                     {
  93.                         R::rollback();
  94.  
  95.                         throw new OmniDataManagerException(
  96.                             "An error occurred while parsing an address field",
  97.                             0,
  98.                             array(
  99.                                 'regex' => $splitregex,
  100.                                 'dataRowIndex' => $index,
  101.                                 'dataRow' => $dataRow
  102.                             )
  103.                         );
  104.                     }
  105.  
  106.                     // Otherwise the match was a great success and we can map
  107.                     // the fields to the address object
  108.                     $address->city = $matches['city'];
  109.                     $address->postalCode = $matches['postalcode'];
  110.                     $address->streetName = $matches['streetname'];
  111.                     $address->houseNumber = intval($matches['housenumber']);
  112.  
  113.                     // Even add the letter to the address (if there is one) for
  114.                     // future use
  115.                     if(!empty($matches['letter']))
  116.                     {
  117.                         $address->letter = $matches['letter'];
  118.                     }
  119.                 }
  120.                 // Otherwise the address is contained in 4 separate fields,
  121.                 // which makes things easy
  122.                 else
  123.                 {
  124.                     $address->city =
  125.                         $dataRow[$mapping[self::COLUMN_ADDR_CITY][1]];
  126.                     $address->postalCode =
  127.                         $dataRow[$mapping[self::COLUMN_ADDR_POSTALCODE][1]];
  128.                     $address->streetName =
  129.                         $dataRow[$mapping[self::COLUMN_ADDR_STREETNAME][1]];
  130.                     $address->houseNumber =
  131.                         $dataRow[$mapping[self::COLUMN_ADDR_HOUSENUMBER][1]];
  132.                 }
  133.  
  134.                 // With the address object populated, run the validation
  135.                 // function
  136.                 try
  137.                 {
  138.                     $address->validate();
  139.  
  140.                     // Check if the matcher had to make any guesses to find a
  141.                     // match
  142.                     if(!empty($address->result['process']))
  143.                     {
  144.                         // Log the guess
  145.                         $this->importLog[] = array(
  146.                             'code' => self::ERROR_GUESSEDADDRESS,
  147.                             'message' => 'The input address had to be ' .
  148.                                 'processed before a match was found',
  149.                             'file_name' => sprintf(
  150.                                 "%s.%s", $file->name, $file->ext
  151.                             ),
  152.                             'line_number' => $index,
  153.                             'line_contents' => $dataRow,
  154.                             'extra' => array(
  155.                                 'original_address' =>
  156.                                     $address->result['input_address'],
  157.                                 'matched_address' => (string)$address
  158.                             )
  159.                         );
  160.                     }
  161.                 }
  162.                 // If the address validation failed, log it and continue to the
  163.                 // next row
  164.                 catch(AddressException $e)
  165.                 {
  166.                     if($e->getCode() === Address::ERROR_VALIDATION)
  167.                     {
  168.                         $this->importLog[] = array(
  169.                             'code' => self::ERROR_UNKNOWNADDRESS,
  170.                             'message' => sprintf(
  171.                                 "Could not find a match for address '%s'\n",
  172.                                 $addr
  173.                             ),
  174.                             'file_name' => sprintf(
  175.                                 "%s.%s", $file->name, $file->ext
  176.                             ),
  177.                             'line_number' => $index,
  178.                             'line_contents' => $dataRow,
  179.                             'extra' => array(
  180.                                 'problem' => $e->extra['problem'],
  181.                                 'process' => $e->extra['process'],
  182.                                 'extra' => $e->extra['extra']
  183.                             )
  184.                         );
  185.  
  186.                         // Iterate the rowsNotImported counter
  187.                         $this->rowsNotImported += 1;
  188.  
  189.                         continue;
  190.                     }
  191.                     else
  192.                     {
  193.                         // Something unexpected happened in this case. Bail out
  194.                         // and pass the exception along
  195.                         R::rollback();
  196.  
  197.                         throw $e;
  198.                     }
  199.                 }
  200.  
  201.                 // After the address is validated the WGS84 coordinates will be
  202.                 // available
  203.                 $tableRow->lat = $address->latitude;
  204.                 $tableRow->lng = $address->longitude;
  205.             }
  206.  
  207.             // If additional data is set to be imported
  208.             if(isset($mapping[self::COLUMN_DATA]))
  209.             {
  210.                 $data = array();
  211.  
  212.                 // Iterate through the extra data rows
  213.                 foreach($mapping[self::COLUMN_DATA] as $key => $value)
  214.                 {
  215.                     // If the import type if static, just set the static value
  216.                     if($value[0] == 'static')
  217.                     {
  218.                         $data[$key] = $value[1];
  219.                     }
  220.                     // If the import type is column, fetch the data from that
  221.                     // column
  222.                     else if($value[0] == 'column')
  223.                     {
  224.                         $data[$key] = $dataRow[$value[1]];
  225.                     }
  226.                 }
  227.  
  228.                 // Serialize the data object and insert it into the table row
  229.                 $tableRow->data = serialize($data);
  230.             }
  231.  
  232.             // Save the row and move on
  233.             R::store($tableRow);
  234.  
  235.             // Iterate the rowsSuccessfullyImported counter
  236.             $this->rowsSuccessfullyImported += 1;
  237.         }
  238.  
  239.         // Commit the changes to the database
  240.         R::rollback();
  241.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement