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) »
  • Using api v3...more concise
Pages: [1]

Author Topic: Using api v3...more concise  (Read 891 times)

emilyf

  • Ask me questions
  • ****
  • Posts: 696
  • Karma: 54
  • CiviCRM version: 2.x - 4.x
  • CMS version: Drupal 5, 6, 7
Using api v3...more concise
March 20, 2014, 10:53:03 am
I'm trying to experiment with the new api and am not sure if I'm writing overkill on the code, seems like it is more work than the old calls.
Below I am looking up a contact id from a drupal id, then looking up the contact and displaying the phone and display name. Can this be written in a shorter form? I believe I should be able to chain these two functions? Do I need the error call on both?
Code: [Select]
 
<?php
if (module_exists('civicrm')) {

  
civicrm_initialize();

  
//find the contact record
  
try{
      
$userID = civicrm_api3( 'uf_match','get', array(
   
    'uf_id' => $user->uid,
      ));
    }
   

   catch (
CiviCRM_API3_Exception $e) {
      
$error = $e->getMessage();
   }

    if (!
$userID['values']) {
      return;
    }
    
    
$uf_user = $userID['values'];
    foreach(
$uf_user as $User){
       
// Get Contact ID
       
$user_id = $User['contact_id'];
     }
    
    try{
      
$contacts = civicrm_api3('contact', 'get', array(
         
'contact_id'  =>  $user_id,
      ));
     }

     catch (
CiviCRM_API3_Exception $e) {
      
$error = $e->getMessage();
     }

    if (!isset(
$contacts['values'])) {
return;
    }

    
$contact = $contacts['values'];

     
// We will only have one return
     
foreach($contact as $contact){
           
// update username and phone
           
$username = $contact['display_name'];
           
$phone = $contact['phone'];
     }
  }
?>

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Using api v3...more concise
March 20, 2014, 11:33:35 am
Hi,

In general, I use sequential=>1 as a param, so instead of

Code: [Select]
$uf_user = $userID['values'];
    foreach($uf_user as $User){
       // Get Contact ID
       $user_id = $User['contact_id'];
     }
   

I can do $user_id = $uf_user["values"][0];

same goes when you fetch the contacts.

You can as well use getsingle, where if you have 0 or more than 1 results, it will raise an error. (it saves you a test isset($bla["values"])

On both calls, you don't see how you process your errors. I don't think there is any "normal" case where a user hasn't a uf_match or a contact in uf_match hasn't a contact record. I wouldn't try to catch it and violently crash if it happens, unless you have a dead solid error handler further down the code.

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

Jeremy Proffitt

  • I post occasionally
  • **
  • Posts: 63
  • Karma: 2
    • Mobius New Media
  • CiviCRM version: 4.4.x
  • CMS version: Joomla 2.5.x/3.x / D7
  • MySQL version: 5.1.x
  • PHP version: 5.3.10+, 5.4.x
Re: Using api v3...more concise
March 20, 2014, 11:47:10 am
It would still be good to use the try/catch container, but this could be done this way instead...

try
{
    $contactId = civicrm_api3('UFMatch','getsingle',array('uf_id'=>$user->uid, 'return'=>'contact_id'));
    $contact = civicrm_api3('Contact','getsingle',array('id'=>$contactId));
    if ($contact['count'] > 0) {
        $username = $contact['display_name'];
        $phone = $contact['phone'];
    } else return;
}
 catch (CiviCRM_API3_Exception $e)
{
      $error = $e->getMessage();
}

   

Jeremy Proffitt
Mobius New Media
IRC: JP_EzoD

Jeremy Proffitt

  • I post occasionally
  • **
  • Posts: 63
  • Karma: 2
    • Mobius New Media
  • CiviCRM version: 4.4.x
  • CMS version: Joomla 2.5.x/3.x / D7
  • MySQL version: 5.1.x
  • PHP version: 5.3.10+, 5.4.x
Re: Using api v3...more concise
March 20, 2014, 11:50:01 am
You could test on the contactID, but the try/catch block should handle that. I also think the object-oriented calls are now easier to use, but that's a matter of preference.
Jeremy Proffitt
Mobius New Media
IRC: JP_EzoD

emilyf

  • Ask me questions
  • ****
  • Posts: 696
  • Karma: 54
  • CiviCRM version: 2.x - 4.x
  • CMS version: Drupal 5, 6, 7
Re: Using api v3...more concise
March 20, 2014, 01:20:08 pm
thanks to you both; I couldn't get the 'return' param to work, but getsingle definitely decreased my code. Here's what I ended up with:

Code: [Select]
  if (module_exists('civicrm')) {

civicrm_initialize();

    //find the contact record
    try{     
      $userID = civicrm_api3( 'uf_match','getsingle', array(
        'uf_id' => $user->uid,
      ));
     
  $contacts = civicrm_api3('contact', 'getsingle', array(
        'contact_id'  =>  $userID['contact_id'],
      ));
 
      // Set username and phone based on Civi information
      $username = $contacts['display_name'];
      $phone = $contacts['phone'];   
    }
   
catch (CiviCRM_API3_Exception $e) {   
      $error = $e->getMessage();
}

  }

One question - is my error clause correct? That will output a civi-based error if it gets triggered?

totten

  • Administrator
  • Ask me questions
  • *****
  • Posts: 695
  • Karma: 64
Re: Using api v3...more concise
March 20, 2014, 01:58:54 pm
The error clause looks good.

A couple things which may go without saying, but since you wanted confirmation:

1. The good thing is that there's one try{...}catch{...} block, and all the calls to civicrm_api3() are in the try{...} section.
2. The indentation looks a bit wonky in my browser.
3. The "output" is that $error contains the error message, but the code snippet doesn't show how it gets onto the screen (eg via "echo", "print", "drupal_set_message()", "theme()", etc). It shouldn't matter how it gets displayed, but it should get displayed somehow.

emilyf

  • Ask me questions
  • ****
  • Posts: 696
  • Karma: 54
  • CiviCRM version: 2.x - 4.x
  • CMS version: Drupal 5, 6, 7
Re: Using api v3...more concise
March 20, 2014, 04:03:58 pm
thank you, totten, that cleared it up. the patch i ended up submitting for a particular drupal module add-on:
https://drupal.org/files/issues/reservations_contract_civicrm_call-2222613-1.patch

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Using api v3...more concise

This forum was archived on 2017-11-26.