function update_congregation_rows($KeyArray, $RawDataFile)
{
    $UpdateResults['errors'] = 0;
    $UpdateResults['updates'] = 0;
    $UpdateResults['ignored'] = 0;
    $UpdateResults['deleted'] = 0;
    while (!feof($RawDataFile)) {
        $newRow = fgetcsv($RawDataFile);
        // loop through the arrays to create a new
        // associative array
        $newArray = array();
        $LoopCount = count($KeyArray);
        for ($loop = 0; $loop < $LoopCount; $loop++) {
            $newArray = array_push_assoc($newArray, $KeyArray[$loop], $newRow[$loop]);
        }
        // create the congregation class
        $UpdateCongregation = new Congregation();
        // check to see if we need to delete it first
        if (strtolower($newArray['updatecode']) == 'd') {
            // check to see if it even exists, otherwise, ignore it.
            if ($UpdateCongregation->get_congregation_by_PIN($newArray['pin'])) {
                // check to see if it has been flagged for no auto updates
                if ($UpdateCongregation->DoNotAutoUpdate) {
                    // marked to not update - log it and stop.
                    add_congregation_search_update_log('INFO', 'Congregation ' . $newArray['pin'] . ' is locked to not allow updates. Delete request ignored.');
                    $UpdateResults['ignored']++;
                } elseif (delete_congregation($newArray['pin'])) {
                    add_congregation_search_update_log('DELETE', 'Congregation ' . $newArray['pin'] . ' was deleted.');
                    $UpdateResults['deleted']++;
                } else {
                    add_congregation_search_update_log('ERROR', 'Congregation ' . $newArray['pin'] . ' could not be deleted.');
                    $UpdateResults['errors']++;
                }
            }
        } else {
            // first see if the congregation exists and make certain we d
            if (!$UpdateCongregation->get_congregation_by_PIN($newArray['pin'])) {
                // if false, this is a new congregation - lets create it
                $UpdateCongregation->PIN = $newArray['pin'];
                $UpdateCongregation->CongregationName = $newArray['congregationname'];
                $UpdateCongregation->EIN = $newArray['ein'];
                $UpdateCongregation->Address1 = $newArray['address1'];
                $UpdateCongregation->Address2 = $newArray['address2'];
                $UpdateCongregation->City = $newArray['city'];
                $UpdateCongregation->State = $newArray['state'];
                $UpdateCongregation->PostalCode = $newArray['postalcode'];
                $UpdateCongregation->Phone = $newArray['phone'];
                $UpdateCongregation->Email = $newArray['email'];
                $UpdateCongregation->Website = $newArray['website'];
                $UpdateCongregation->Region = $newArray['region'];
                if (array_key_exists('latitude', $newArray) && array_key_exists('longitude', $newArray)) {
                    // if latitude and longitude were provided then add them
                    $UpdateCongregation->Latitude = $newArray['latitude'];
                    $UpdateCongregation->Longitude = $newArray['longitude'];
                } else {
                    if ($UpdateCongregation->Address1 != '' && $UpdateCongregation->City != '' && $UpdateCongregation->State != '') {
                        //geocode the address and add it to the record
                        $newLatLong = get_congregation_city_state_latlong($UpdateCongregation->Address1 . ', ' . $UpdateCongregation->City . ', ' . $UpdateCongregation->State);
                        if ($newLatLong != null) {
                            $UpdateCongregation->Latitude = $newLatLong['location']['lat'];
                            $UpdateCongregation->Longitude = $newLatLong['location']['long'];
                            add_congregation_search_update_log('GEOCODE_SUCCESS', 'Congregation ' . $newArray['pin'] . ' was successfully geocoded.');
                        } else {
                            add_congregation_search_update_log('GEOCODE_FAILURE', 'Congregation ' . $newArray['pin'] . ' could not be geocoded.');
                        }
                    }
                }
                // save and log the result
                $SavedCongregationResults = $UpdateCongregation->save_new_congregation();
                if ($SavedCongregationResults) {
                    add_congregation_search_update_log('ADDED', 'Congregation ' . $newArray['pin'] . ' was added.');
                    $UpdateResults['updates']++;
                } else {
                    add_congregation_search_update_log('ERROR', 'Congregation ' . $newArray['pin'] . ' could not be added: ' . $SavedCongregationResults);
                    $UpdateResults['errors']++;
                }
            } else {
                // first checked to see if it's been flagged to not update,
                // then check to see if the updated data is more recent
                // than the current data in the database
                // We do this because update file contain updated for the last 7 days,
                // in case a previous update execution created an error that
                // precented some data from being updated.
                // check for no auto updates to this congregation
                if ($UpdateCongregation->DoNotAutoUpdate) {
                    add_congregation_search_update_log('INFO', 'Congregation ' . $newArray['pin'] . ' is locked to not allow updated. Update request ignored.');
                    $UpdateResults['ignored']++;
                } elseif (strtotime($UpdateCongregation->DateUpdated) < strtotime($newArray['lastupdated'])) {
                    $UpdateCongregation->CongregationName = $newArray['congregationname'];
                    $UpdateCongregation->EIN = $newArray['ein'];
                    $UpdateCongregation->Address1 = $newArray['address1'];
                    $UpdateCongregation->Address2 = $newArray['address2'];
                    $UpdateCongregation->City = $newArray['city'];
                    $UpdateCongregation->State = $newArray['state'];
                    $UpdateCongregation->PostalCode = $newArray['postalcode'];
                    $UpdateCongregation->Phone = $newArray['phone'];
                    $UpdateCongregation->Email = $newArray['email'];
                    $UpdateCongregation->Website = $newArray['website'];
                    $UpdateCongregation->Region = $newArray['region'];
                    if (array_key_exists('latitude', $newArray) && array_key_exists('longitude', $newArray)) {
                        // now check to see if they exist in the database. If not, don't overwrite the data
                        // as site-side lat/long data might be overwritten
                        if ($newArray['latitude'] != '' && $newArray['longitude'] != '') {
                            $UpdateCongregation->Latitude = $newArray['latitude'];
                            $UpdateCongregation->Longitude = $newArray['longitude'];
                        }
                    } else {
                        if ($UpdateCongregation->Latitude == 0.0 || $UpdateCongregation->Longitude == 0.0) {
                            //geocode the address and add it to the record
                            $newLatLong = get_congregation_city_state_latlong($UpdateCongregation->Address1 . ', ' . $UpdateCongregation->City . ', ' . $UpdateCongregation->State);
                            if ($newLatLong != null) {
                                $UpdateCongregation->Latitude = $newLatLong['location']['lat'];
                                $UpdateCongregation->Longitude = $newLatLong['location']['long'];
                                add_congregation_search_update_log('GEOCODE_SUCCESS', 'Congregation ' . $newArray['pin'] . ' was successfully geocoded.');
                            } else {
                                add_congregation_search_update_log('GEOCODE_FAILURE', 'Congregation ' . $newArray['pin'] . ' could not be geocoded.');
                            }
                        }
                    }
                    $UpdateCongregationResults = $UpdateCongregation->save_current_congregation();
                    if ($UpdateCongregationResults) {
                        add_congregation_search_update_log('UPDATED', 'Congregation ' . $newArray['pin'] . ' was updated.');
                        $UpdateResults['updates']++;
                    } else {
                        add_congregation_search_update_log('ERROR', 'Congregation ' . $newArray['pin'] . ' could not be updated:' . $UpdateCongregationResults);
                        $UpdateResults['errors']++;
                    }
                }
            }
        }
    }
    // end while
    // close the data file
    fclose($RawDataFile);
    return $UpdateResults;
}
function get_congregations_by_location_search($searchQuery, $searchDistance)
{
    $options = get_option('congregation_search_settings');
    // first see if the search query already exisit in the history
    // this is to avoid hitting the Google geocoding API, thus increasing performance
    $QueryItem = new CongregationSearchQueryItem();
    $QueryItem->get_query($searchQuery);
    $Latitude = 0.0;
    $Longitude = 0.0;
    // if the query is not new and the caching is up to date...
    if (!$QueryItem->IsNew && get_congregation_cache_backdate() <= $QueryItem->LastUpdated) {
        // use the lat/long info from the query
        $Latitude = $QueryItem->Latitude;
        $Longitude = $QueryItem->Longitude;
        // increment the usage count
        $QueryItem->increment_usage();
    } else {
        // hit the Geocoding service for lat/long information
        $searchResults = get_congregation_city_state_latlong($searchQuery);
        if ($searchResults) {
            $Latitude = $searchResults['location']['lat'];
            $Longitude = $searchResults['location']['long'];
            // update the query history
            $QueryItem->Latitude = $Latitude;
            $QueryItem->Longitude = $Longitude;
            $QueryItem->LastUpdated = date('Y-m-d H:i:s');
            if ($QueryItem->IsNew) {
                $QueryItem->save_query();
            } else {
                $QueryItem->increment_usage();
                // also saves the query
            }
        }
    }
    $congregationResults = null;
    if ($Latitude != 0.0 && $Longitude != 0.0) {
        // get congregations that match the results
        $congregationResults = get_congregations_by_latlong($Latitude, $Longitude, $searchDistance);
    }
    return $congregationResults;
}