/**
  * Import data from CSV file into database
  */
 public function import()
 {
     $API = new PerchAPI(1.0, 'root_locator');
     $Lang = $API->get('Lang');
     $Template = $API->get('Template');
     $Template->set('locator/address.html', 'locator');
     $Addresses = new RootLocator_Addresses($API);
     $Tasks = new RootLocator_Tasks($API);
     $data = $this->reader->fetchAssoc();
     foreach ($data as $row) {
         $errors = $this->getRowErrors($row);
         $warnings = $this->getRowWarnings($row);
         if ($errors) {
             $this->addError($row, $Lang->get('‘%s’ columns are missing required data', $errors));
             continue;
         }
         if ($warnings) {
             $this->addWarning($row, $Lang->get('‘%s’ columns are recommended to prevent geocoding errors.', $warnings));
         }
         $imported = $Addresses->create(['addressTitle' => $row['addressTitle'], 'addressBuilding' => $row['addressBuilding'], 'addressStreet' => $row['addressStreet'], 'addressTown' => $row['addressTown'], 'addressRegion' => $row['addressRegion'], 'addressPostcode' => $row['addressPostcode'], 'addressCountry' => $row['addressCountry']]);
         $imported->index($Template);
         $Tasks->add('address.geocode', $imported->id());
         $this->addSuccess($row);
     }
 }
 if (isset($data['force'])) {
     $force = true;
     unset($data['force']);
 }
 // Dynamic fields
 $previous_values = false;
 if (isset($details['addressDynamicFields'])) {
     $previous_values = PerchUtil::json_safe_decode($details['addressDynamicFields'], true);
 }
 $dynamic_fields = $Form->receive_from_template_fields($Template, $previous_values, $Addresses, $Address);
 $data['addressDynamicFields'] = PerchUtil::json_safe_encode($dynamic_fields);
 // Save
 if (is_object($Address)) {
     $requeue = $Address->shouldQueue($data);
     if (!$force && $requeue) {
         $Tasks->add('address.geocode', $Address->id());
         $data['addressLatitude'] = null;
         $data['addressLongitude'] = null;
     }
     $result = $Address->update($data, $force);
     $details = $Address->to_array();
     $Address->index($Template);
 } else {
     $new_address = $Addresses->create($data);
     if ($new_address) {
         if ($force) {
             $new_address->geocode();
         } else {
             $Tasks->add('address.geocode', $new_address->id());
         }
         $new_address->index($Template);
예제 #3
0
    $db->execute($sql);
}
if ($Settings->get('root_locator_update')->val() != '2.0.0') {
    $legacy = $Addresses->getLegacyData($Paging);
    if (PerchUtil::count($legacy)) {
        foreach ($legacy as $row) {
            // Ok, we have an error and it's not a quota issue, so just save as is.
            if (isset($row['errorMessage']) && !empty($row['errorMessage']) && $row['errorMessage'] == 'The address could not be found.') {
                $legacyAddress = $Addresses->create(['addressTitle' => $row['locationTitle'], 'addressBuilding' => $row['locationBuilding'], 'addressStreet' => $row['locationStreet'], 'addressTown' => $row['locationTown'], 'addressRegion' => $row['locationRegion'], 'addressPostcode' => $row['locationPostcode'], 'addressCountry' => $row['locationPostcode'], 'addressDynamicFields' => $row['locationDynamicFields'], 'addressError' => 'no_results']);
                $legacyAddress->index();
                continue;
            }
            // Do we have some existing location data we can just simply shift over?
            if (isset($row['markerLatitude']) && isset($row['markerLongitude'])) {
                $legacyAddress = $Addresses->create(['addressTitle' => $row['locationTitle'], 'addressBuilding' => $row['locationBuilding'], 'addressStreet' => $row['locationStreet'], 'addressTown' => $row['locationTown'], 'addressRegion' => $row['locationRegion'], 'addressPostcode' => $row['locationPostcode'], 'addressCountry' => $row['locationPostcode'], 'addressDynamicFields' => $row['locationDynamicFields'], 'addressLatitude' => $row['markerLatitude'], 'addressLongitude' => $row['markerLongitude']]);
                $legacyAddress->index();
                continue;
            }
            // Ok, default action is to just save the row and queue it for later.
            $legacyAddress = $Addresses->create(['addressTitle' => $row['locationTitle'], 'addressBuilding' => $row['locationBuilding'], 'addressStreet' => $row['locationStreet'], 'addressTown' => $row['locationTown'], 'addressRegion' => $row['locationRegion'], 'addressPostcode' => $row['locationPostcode'], 'addressCountry' => $row['locationPostcode'], 'addressDynamicFields' => $row['locationDynamicFields']]);
            $legacyAddress->index();
            $Tasks->add('address.geocode', $legacyAddress->id());
        }
    } else {
        $Settings->set('root_locator_update', '2.0.0');
        PerchUtil::redirect($API->app_path());
    }
}
if ($Paging->is_last_page()) {
    $Settings->set('root_locator_update', '2.0.0');
}