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) »
  • Location API
Pages: [1]

Author Topic: Location API  (Read 2047 times)

xcf33

  • I post frequently
  • ***
  • Posts: 181
  • Karma: 7
  • CiviCRM version: 3.3.2
  • CMS version: Drupal 6.19/6.20
  • MySQL version: 5.x
  • PHP version: 5.2.6
Location API
May 25, 2010, 08:19:38 am
Hi guys, I been playing with the Location API a little bit trying to add mainly address, phone numbers to contacts and I'm running into a few walls.

On the wiki for location API it states:
http://wiki.civicrm.org/confluence/display/CRMDOC/Location+APIs

Quote
'params' (array) Associative array of property name/value pairs to insert in new location. Minimum required data values are:

    * 'location_type' - a valid location_type string (e.g. 'Home')
    * 'contact_id' - a valid contact_id whom location to be added.
    * At least one other location property name/value pair

However, on the example shown on the wiki page, there was no array element 'location_type' in the base (parent) 'params' array, instead they are in the nested array for phone, address.

It makes since because you want to be able to specify location type for each of the location in the nested array and just make one civicrm_location_add API call.

And it works for the sample code.

However, I'm using almost the same array structures (omit location_type_id) but when I make the API call I get an error telling me that required field 'location_type' is not find in the 'params' array. If I change my code slightly to add say  

Code: [Select]
$params = array(
'contact_id' => $contact['contact_id'],
'phone'      => $phones,
'address'    => $address,
                'location_type' => 'Home',

);

It will return no error and add the location, however, the problem is that now all the nested array locations (phone  #, addresses are all listed as Home Address). I can always make several API calls to add the locations individually but it would be costly in my situation and I do see a reason for doing it. Is there any explanation to this?


Cheers!

Chang

Below is my code snippet (Drupal 6.16/CiviCRM 3.13)

Code: [Select]
civicrm_initialize( );
require_once 'CRM/Core/Config.php';
$config =& CRM_Core_Config::singleton();

require_once 'api/v2/Contact.php';
require_once 'api/v2/Location.php';

$contact_param = array(
'contact_type' => 'Individual', //
'first_name' => 'John', //
'middle_name' => 'Williams', //
'last_name' => 'Smith', //
'sort_name' => 'Smith, John',
'display_name'=> 'Mr. John Smith',
'email' => 'johnwsmith@thisemailhere.com', //
'do_not_email' => 1, //
'do_not_phone' => 1, //
'do_not_mail' => 1, //
'do_not_sms' => 1, //
'do_not_trade' => 1, //
'is_opt_out' => 1, //
'home_URL' => 'http://www.yahoo.com', //
'preferred_mail_format' => 'HTML', //
'job_title' => 'Tester', //
'birth_date' => '12-12-1988', //
'is_deceased' => 1, //
'gender' => 'Male', //
'current_employer' => 'XYZ Company', //
'custom_1' => 'Free text create here',
'custom_2' => 0,
'custom_3' => 20,

);

// create the contact
$contact = &civicrm_contact_create($contact_param);

$homePhone = array(
'phone' => '91-20-276048',
'location_type' => 'Home'
);
 
$faxPhone = array(
'phone' => '91-20-234-657686',
'location_type' => 'Work',
);

$address = array(
1 => array(
'city'                   => 'San Francisco',
'state_province'         => 'California',
'street_address'         => '123, FC Road',
'supplemental_address_1' => 'Near Wenna Lake',
'postal_code' => '91020',
'location_type' => 'Home',
),
);

$phones = array( $homePhone, $faxPhone);

$params = array(
'contact_id' => $contact['contact_id'],
'phone'      => $phones,
'address'    => $address,
);

$newLocation =& civicrm_location_add($params);

print_r($newLocation);

xcf33

  • I post frequently
  • ***
  • Posts: 181
  • Karma: 7
  • CiviCRM version: 3.3.2
  • CMS version: Drupal 6.19/6.20
  • MySQL version: 5.x
  • PHP version: 5.2.6
Re: Location API
May 25, 2010, 08:26:54 am
Never mind,

The problem is solved with the following added to the 'param'

'version' => '3.0'


This should be added to the wiki >_<

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Location API
May 25, 2010, 02:21:24 pm

chang:

can you please edit the wiki and do the needful

thanx

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

xcf33

  • I post frequently
  • ***
  • Posts: 181
  • Karma: 7
  • CiviCRM version: 3.3.2
  • CMS version: Drupal 6.19/6.20
  • MySQL version: 5.x
  • PHP version: 5.2.6
Re: Location API
May 27, 2010, 01:10:37 pm
Another question with location API

in the sample code

Code: [Select]
$address = array( 1 => array( 'location_type_id'       => 1,
                                      'is_primary'             => 1,
                                      'city'                   => 'San Francisco',
                                      'state_province'         => 'California',
                                      'street_address'         => '123, FC Road',
                                      'supplemental_address_1' => 'Near Wenna Lake',
                                      'country_id'             => 1228
                                     )
                        );

This is weird, I'm trying to add 2 addresses in one location API call so I have my code look like this

Code: [Select]
$address = array( 1 => array(
                                      'city'                   => 'San Francisco',
                                      'state_province'         => 'California',
                                      'street_address'         => '123, FC Road',
                                      'supplemental_address_1' => 'Near Wenna Lake',
                                      'country_id'             => 1228,
                                     ),
                          2 => array(
                                      'city'                   => 'Seattle',
                                      'state_province'         => 'Washington',
                                      'street_address'         => '456 Starbucks RD',
                                      'supplemental_address_1' => 'STE 200',
                                      'country_id'             => 1228,
                                     ),
                        );

It seems to only create one address, more specifically it only creates the address with array key 1


I now that you can nest phone, email, im in one location API call. Is this true for address as well?

If not, it's weird to have the address param as a nested array with a specific array key.



Any confirmation would be great, I'm looking to piece together more information to be added to the location API wiki too >_<


Cheers


Chang

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Location API
June 17, 2010, 04:57:14 am
Hi,

Isn't your last sample missing the location_type?

BTW - I've found this thread helpful- thanks
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

xcf33

  • I post frequently
  • ***
  • Posts: 181
  • Karma: 7
  • CiviCRM version: 3.3.2
  • CMS version: Drupal 6.19/6.20
  • MySQL version: 5.x
  • PHP version: 5.2.6
Re: Location API
June 17, 2010, 08:30:24 am
Quote from: Eileen on June 17, 2010, 04:57:14 am
Hi,

Isn't your last sample missing the location_type?

BTW - I've found this thread helpful- thanks

I was thinking of including location_type in the $param array,


In anyways, the conclusion I had from using the Location API was that:


You can nest phone number, IM, etc but you cannot nest addresses to be called in one API call.


So if you had to use the Location API to insert a residential address and billing address, you would need to make 2 API call, which in most cases are fine. It just becomes inefficient when I have to double the number of location API call on a larger scale

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Location API
June 19, 2010, 12:57:46 am
Yeah - I found it hard-coded into the API code - only recognise array [1] for address
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

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Location API
June 19, 2010, 03:44:16 am


It would be a welcome addition to have something more modular. Ping us on IRC if you're willing to give it a go and need help.

X+
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Location API
July 07, 2010, 02:12:08 am
What I am finding is that using location_add for a phone and an address - like the code in the wiki:

1) the address will be updated
2) an additional phone number will be created - even if a home mobile already exists

Is this by design?
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

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Location API
July 08, 2010, 09:44:11 pm
More questions on location api - I only seem to be able to limit my search by contact id - passing 'is_primary' in doesn't seem to prevent me getting all addresses. I have been passing in this array

(
    [contact_id] => 72
    [version] => 3.0
    [location_type] => address
    [is_primary] => 1
)


and getting all addresses of all types (phone, email, address) I also tried location_type = Home & 1 but no change
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

chirayu

  • Guest
Re: Location API
August 19, 2010, 08:09:11 am
I had a similar problem, was getting errors while adding/updating locations. The error said that location_type_id was invalid or missing.

The solution is to add location_type_id key for each phone, address and email. Adding location_type does not work for civicrm_location_update. However, civicrm_location_add works if you have location_type. So this code below, is the code that works:

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

This forum was archived on 2017-11-26.