CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • API v3 - Country to country_id?
Pages: [1]

Author Topic: API v3 - Country to country_id?  (Read 2120 times)

DerekL

  • I post frequently
  • ***
  • Posts: 132
  • Karma: 1
  • CiviCRM version: 4.5.5
  • CMS version: Drupal 7.34
  • MySQL version: 5.1.54
  • PHP version: 5.2.17
API v3 - Country to country_id?
March 23, 2012, 02:24:57 pm
Hi Guys and Gals,

I'm trying to transform country strings (like "US", "FR", "BR", etc) to civicrm country_id's via the v3 API but I can't seem to get any traction.

I've been doing:

CRM_Core_PseudoConstant::country($value);

To accomplish the inverse lookup (country_id to country) but I can't seem to find the right tool for this job.

All I can find in import/contact.php is what looks to be code that validates the country against the allowed list...

Any ideas?

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: API v3 - Country to country_id?
March 23, 2012, 06:37:11 pm
If you pass in

$params['country'] - to the address api (& probably to contact as well)

If will convert a two-digit or full length country string. (we should add this to the _spec function)
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

DerekL

  • I post frequently
  • ***
  • Posts: 132
  • Karma: 1
  • CiviCRM version: 4.5.5
  • CMS version: Drupal 7.34
  • MySQL version: 5.1.54
  • PHP version: 5.2.17
Re: API v3 - Country to country_id?
March 25, 2012, 02:31:00 pm
Hi Eileen,

I'll try that once I get go back to my address import inc and clean it up, adding that to the spec would be great!

Posterity Post:

This is what ended up working for me:  I passed country and province strings in the id fields, then used prepare() to lookup and replace.

Code: [Select]
public function prepare(&$entity, &$row) {
    civicrm_initialize();
   $countryIsoCodes=civicrm_api("Constant","get", array ('version' =>'3', 'name' =>'countryIsoCode'));
   $stateProvinces=civicrm_api("Constant","get", array ('version' =>'3', 'name' =>'stateProvince'));
   $stateProvinceAbbreviations=civicrm_api("Constant","get", array ('version' =>'3', 'name' =>'stateProvinceAbbreviation'));       

    $entity->country_id = array_search($entity->country_id, $countryIsoCodes['values']);

    $entity->state_province_id= array_search($entity->state_province_id, $stateProvinceAbbreviations['values']);

if( !isset($entity->state_province_id) ) {
$entity->state_province_id= array_search($entity->state_province_id, $stateProvinces['values']);
}


Not quite elegant, I'm sure, but it did the trick.

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • API v3 - Country to country_id?

This forum was archived on 2017-11-26.