Being able to pick one module over the other is always nice. Due to recent developments with the Addresses module, I'm migrating my data to the Location CCK module. Any tips/tricks would be appreciated.

Comments

mikeytown2’s picture

This is what I have for migrating a field that contains only 1 value.

  // Setup database names
  $old_field = 'field_address';
  $new_field = 'field_location';
  $node_name = 'page';

  // Copy Data
  $result = db_query("
    SELECT
      n.nid,
      n.vid,
      ${old_field}_aname       AS name,
      ${old_field}_street      AS street,
      ${old_field}_additional  AS additional,
      ${old_field}_city        AS city,
      ${old_field}_province    AS province,
      ${old_field}_postal_code AS postal_code,
      ${old_field}_country     AS country
    FROM {node} n
    INNER JOIN {content_type_${node_name}} USING (nid)
    WHERE n.type = '%s'
  ", $node_name);

  $node = new stdClass;
  $field['field_name'] = $new_field;
  while ($location = db_fetch_array($result)) {
    // Set some data
    $node->nid = $location['nid'];
    $node->vid = $location['vid'];
    $items[] = $location;

    // save data
    location_cck_field('insert', $node, $field, $items, NULL, NULL);
    // get location ID from insert opperation
    $lid = (int)db_result(db_query("
      SELECT lid
      FROM {location}
      WHERE street LIKE '%%%s%%'
      ORDER BY lid DESC
      LIMIT 0 , 1
    ", trim($location['street'])));

    // link location to cck field
    db_query("
      UPDATE {content_type_${node_name}}
      SET ${new_field}_lid = %d
      WHERE nid = %d
    ", $lid , $node->nid);
  }
mikeytown2’s picture

This will not save the data if only a state/country is defined in address. It only saves new info to location if one of name, street, city, or zip is defined.

  // Setup node/field/location names
  include_once(drupal_get_path('module', 'addresses') . '/addresses.inc');
  $states = _addresses_province_get();
  $countries = _addresses_country_get();
  $old_field = 'field_address';
  $new_field = 'field_location';
  $node_name = 'page';

  // Get list of nodes
  $result = db_query("SELECT nid FROM {node} WHERE type = '%s'", $node_name);

  // Update node data
  $counter = 0;
  while ($nid = db_result($result)) {
    $node = node_load($nid, NULL, TRUE);
    $new_data = FALSE;
    if (!empty($node->{$new_field}[0]['lid'])) {
      echo 'node ' . $nid . " already has a location<br />\n";
      continue;
    }
    foreach ($node->{$old_field} as $key => $address) {
      // Name
      $address['name'] = $address['aname'];
      unset($address['aname']);

      // Country
      if (empty($address['country'])) {
        $address['country'] = variable_get('addresses_country_default', 'us');
      }
      $address['country_name'] = $countries[$address['country']];

      // State
      if (!empty($address['province'])) {
        $state = array_search($address['province'], $states[$address['country']]);
        if ($state !== FALSE) {
          $address['province_name'] = $address['province'];
          $address['province'] = $state;
        }
        else {
          $address['province_name'] = $states[$address['country']][$address['province']];
        }
      }

      // Insert data into node object
      if (   !empty($address['name'])
          || !empty($address['street'])
          || !empty($address['additional'])
          || !empty($address['city'])
          || !empty($address['postal_code'])
        ) {
        $node->{$new_field}[$key] = $address;
        $new_data = TRUE;
      }
    }
    if ($new_data) {
      // performs validation
      $node = node_submit($node);
      if ($node->validated) {
        // save the node
        node_save($node);
        echo 'node ' . $nid . " updated<br />\n";
        $counter++;
      }
      else {
        echo 'node ' . $nid . " failed validation<br />\n";
      }
    }
    else {
      echo 'node ' . $nid . " has no location information<br />\n";
    }
  }

YesCT’s picture

Issue tags: +location importing

tagging.

mikeytown2’s picture

Status: Active » Needs review

"Weaponized" Version For update.php. Works for single and multi value CCK fields. Fill in the blanks ;) & set parameters.

/**
 * Migrate address module to location module
 */
function *_update_N() {
  $ret = array();

  // Enable modules
  $modules = array('location', 'location_cck', 'gmap', 'gmap_location');
  module_enable($modules);
  // Call Install hook for modules
  foreach ($modules as $module) {
    module_invoke($module, 'install');
  }

  // Import CCK field data
  /* You must supply this code, or already have the CCK fields setup */


  // Check that imported CCK table exists, if not error out.
  if (!db_table_exists('content_field_secondary_locations')) {
    $ret['#abort'] = array('success' => FALSE, 'query' => "Location CCK fields have not be updated. Could not find the Secondary Location DB table.");
    return $ret;
  }

  // Enable Geocoding for locations; USA, CAN
  variable_set('location_geocode_us', 'google');
  variable_set('location_geocode_ca', 'google');

  // Set Google Maps Key; domain alias support.
  global $base_root
  $domain = variable_get('domain_root', $base_root);
  $keys = array(
    'example.com'            => '',
  );
  if (!empty($keys[$domain])) {
    variable_set('googlemap_api_key', $keys[$domain]);
  }
  else {
    $ret['#abort'] = array('success' => FALSE, 'query' => "Could not find Google Maps api key for $domain");
    return $ret;
  }

  // Setup node/field/location names
  include_once(drupal_get_path('module', 'addresses') . '/addresses.inc');
  $states = _addresses_province_get();
  $countries = _addresses_country_get();
  $node_name = 'page';

  // Get list of nodes
  $result = db_query("SELECT nid FROM {node} WHERE type = '%s'", $node_name);

  // Update node data
  while ($nid = db_result($result)) {
    $node = node_load($nid, NULL, TRUE);
    $ret[] = install_migrate_address_location($node, 'field_primary_address', 'field_primary_location', $countries, $states);
    $ret[] = install_migrate_address_location($node, 'field_secondary_addresses', 'field_secondary_locations', $countries, $states);
  }
  return $ret;
}

function install_migrate_address_location($node, $old_field, $new_field, $countries, $states) {
  $new_data = FALSE;
  if (!empty($node->{$new_field}[0]['lid'])) {
    return array('success' => TRUE, 'query' => 'node ' . $node->nid . ' already has a location');
  }
  foreach ($node->{$old_field} as $key => $address) {
    // Name
    $address['name'] = $address['aname'];
    unset($address['aname']);

    // Country
    if (empty($address['country'])) {
      $address['country'] = variable_get('addresses_country_default', 'us');
    }
    $address['country_name'] = $countries[$address['country']];

    // State
    if (!empty($address['province'])) {
      $state = array_search($address['province'], $states[$address['country']]);
      if ($state !== FALSE) {
        $address['province_name'] = $address['province'];
        $address['province'] = $state;
      }
      else {
        $address['province_name'] = $states[$address['country']][$address['province']];
      }
    }

    // Insert data into node object
    if (   !empty($address['name'])
        || !empty($address['street'])
        || !empty($address['additional'])
        || !empty($address['city'])
        || !empty($address['postal_code'])
      ) {
      $node->{$new_field}[$key] = $address;
      $new_data = TRUE;
    }
  }

  // Save data
  if ($new_data) {
    // performs validation
    $node = node_submit($node);
    if ($node->validated) {
      // save the node
      node_save($node);
      return array('success' => TRUE, 'query' => 'node ' . $node->nid . ' updated');
      $counter++;
    }
    else {
      return array('success' => FALSE, 'query' => 'node ' . $node->nid . ' failed validation');
    }
  }
  else {
    return array('success' => TRUE, 'query' => 'node' . $node->nid . ' has no location information');
  }
}
rooby’s picture

This issue has some info on migrating from a regular cck field.
It might be of interest to you but you seem to already have something that works for you.

#570816: Convert from CCK field to Location CCK field

bryancasler’s picture

subscribe

podarok’s picture

Version: 6.x-3.x-dev » 7.x-3.x-dev
Status: Needs review » Patch (to be ported)

true status and version

legolasbo’s picture

Issue summary: View changes
Status: Patch (to be ported) » Needs work

I'm in doubt between 'Active' and 'Needs work' because there is no actual patch present. Setting to needs work because there is a lot of code in this issue which could be made into (part of) a patch