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) »
  • Displaying first name
Pages: [1] 2

Author Topic: Displaying first name  (Read 2434 times)

jsimonis

  • I post frequently
  • ***
  • Posts: 316
  • Karma: 4
    • Forward Support, Inc.
  • CiviCRM version: 4.4-4.5
  • CMS version: Drupal 7
  • MySQL version: 5.5.37-cll
  • PHP version: 5.3.29
Displaying first name
April 11, 2012, 10:04:38 am
We'd like to have a block in Drupal that shows up whenever you're logged in (know how to do this). Inside that block we'd like it to say:

Welcome [First Name]

So I need it to pull the person's first name from CiviCRM.

I am assuming I need to use the APIs to do this. However, I have never used the APIs before. Could someone help me on this?

Thanks!

Michael McAndrew

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1274
  • Karma: 55
    • Third Sector Design
  • CiviCRM version: various
  • CMS version: Nearly always Drupal
  • MySQL version: 5.5
  • PHP version: 5.3
Re: Displaying first name
April 11, 2012, 10:15:33 am
Try http://book.civicrm.org/developer/techniques/api and other chapters in that book, and also http://wiki.civicrm.org/confluence/display/CRMDOC41/CiviCRM+Public+APIs
Service providers: Grow your business, build your reputation and support CiviCRM. Become a partner today

jsimonis

  • I post frequently
  • ***
  • Posts: 316
  • Karma: 4
    • Forward Support, Inc.
  • CiviCRM version: 4.4-4.5
  • CMS version: Drupal 7
  • MySQL version: 5.5.37-cll
  • PHP version: 5.3.29
Re: Displaying first name
April 16, 2012, 10:25:33 am
Yea, I had looked at them, but it's so general that it's difficult to tell what to do.

It appears I'd use something like this:

Code: [Select]
require_once 'api/api.php';
$result = civicrm_api (Contact,Get,first_name);

If I understand correctly, that would pull the first name? But is it the first name of the person who is logged in? And what do I do with it after that?

Hershel

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4640
  • Karma: 176
    • CiviHosting
  • CiviCRM version: Latest
  • CMS version: Mostly WordPress and Drupal
Re: Displaying first name
April 16, 2012, 11:05:17 am
Actually it's quite a bit more complicated than that. It's a two step process and the code would look something like:

Code: [Select]
global $user;
civicrm_initialize(TRUE);
require_once 'api/api.php';
$UFMatch_results=civicrm_api("UFMatch","get", array ('version' =>'3', 'uf_id' =>$user->uid));
$contact_id = $UFMatch_results['values'][$user->uid]['contact_id'];
$results=civicrm_api("Contact","get", array ('version' =>'3', 'contact_id' =>$contact_id));
echo $results['values'][$contact_id]['display_name'];

This code is completely untested and not guaranteed. :)

That's the basic idea anyhow...
CiviHosting and CiviOnline -- The CiviCRM hosting experts, since 2007

See here for the official: What to do if you think you've found a bug.

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Displaying first name
April 16, 2012, 01:26:02 pm
Just an aside

require_once 'api/api.php';

is no longer required as civicrm_initialize(); does that for you (pretty sure the True in initialize is also irrelevent now as the API turns on exception handling which is what the TRUE did).

There are a couple of api functions that just change the format of the result - they are only applicable when only one results can be returned. So, Hershel's code could also look like

$UFMatch_results=civicrm_api("UFMatch","getsingle", array ('version' =>'3', 'uf_id' =>$user->uid));
$contact_id = $UFMatch_results['contact_id'];

OR

$contact_id=civicrm_api("UFMatch","getvalue", array ('version' =>'3', 'uf_id' =>$user->uid, 'return' => 'contact_id'));

(return is a required param for the second option)

If you were really keen you could chain it

$contact = civicrm_api("UFMatch","get", array ('sequential' => 1, 'version' =>'3', 'uf_id' =>$user->uid, 'api.contact.get' => 1));

$contact_id = $contact['values'][0]['api.contact.get']['values'][0]['display_name'];


(note that I used 0 because I set sequential so it is re-indexed. Chained API calls set sequential by default)
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

Hershel

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4640
  • Karma: 176
    • CiviHosting
  • CiviCRM version: Latest
  • CMS version: Mostly WordPress and Drupal
Re: Displaying first name
April 16, 2012, 01:41:57 pm
Thanks for the tips. :)

I just starting wondering now why we don't use module_invoke instead of calling civicrm_initialize directly. Is there a reason?
CiviHosting and CiviOnline -- The CiviCRM hosting experts, since 2007

See here for the official: What to do if you think you've found a bug.

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Displaying first name
April 16, 2012, 02:02:07 pm
I'm guessing that it's because that is a drupal only command?
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

Coleman Watts

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 2346
  • Karma: 183
  • CiviCRM version: The Bleeding Edge
  • CMS version: Various
Re: Displaying first name
April 16, 2012, 02:15:21 pm
Here's a utility function I wrote for D6. It makes all this stuff super easy, and is way more efficient than hitting the API twice on every page load. Just paste it into any .module on your site (a custom module, that is, not views.module or something else that will get overwritten by upgrades!).

Code: [Select]
/**
 * Quick way to retrieve a contact's name given their uid or cid
 * Cached as static for performance
 * A user's own name is cached in session for even better performance
 * @param $ret: 'first', 'last', 'nick', 'middle', 'display', or 'sort'
 * @param $id: optional contact or user id. Defaults to current user
 * @param $id_type: is this a contact or user id?
 */
function contact_name($ret, $id = NULL, $id_type = 'contact') {
  static $info = array();
  global $user;
  // Retrieve user's own name
  if (!$id) {
    $id_type = 'user';
    $id = $user->uid;
    if (!isset($info['user'][$id]) && !empty($_SESSION['contact_name'])) {
      $info['user'][$id] = $_SESSION['contact_name'];
    }
  }
  if (!$id) {
    return 'Visitor';
  }
  if (empty($info[$id_type][$id])) {
    $query = 'SELECT first_name, nick_name, middle_name, last_name, display_name, sort_name
    FROM {civicrm_contact} WHERE id = ';
    if ($id_type == 'contact') {
      $query .= '%d';
    }
    else {
      $query .= '(SELECT contact_id FROM {civicrm_uf_match} WHERE uf_id = %d)';
    }
    $db = db_query($query, $id);
    if ($contact = db_fetch_array($db)) {
      $contact['nick_name'] = $contact['nick_name'] ? $contact['nick_name'] : $contact['first_name'];
      $contact['full_name'] = $contact['nick_name'] . ' ' . $contact['last_name'];
      $info[$id_type][$id] = $contact;
      if ($id_type == 'user' && $id == $user->uid) {
        $_SESSION['contact_name'] = $contact;
      }
    }
    else {
      return 'Visitor';
    }
  }
  else {
    $contact = $info[$id_type][$id];
  }
  return $contact[$ret . '_name'];
}


Then to get the first name of the logged-in user just use:
Code: [Select]
print contact_name('first');
« Last Edit: April 16, 2012, 02:22:06 pm by colemanw »
Try asking your question on the new CiviCRM help site.

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Displaying first name
April 16, 2012, 03:16:46 pm
While we are nitpicking,

You should always specify what you want to get from the api.contact get. The syntax post 4.2 is return = array ('first_name'), and on 4.1 return.first_name=1.

With that and coelmanw wise suggestion of storing in the session the first name, I'd recommend taking the (small) penalty hit once per session to fetch the first name using two api calls (or one chained) instead of going the sql road, as you are less likely to have problems when you upgrade if the database schema changes (and it will at one point or another).

If I really were nitpicking, I'd point out that using the api for instance avoid fetching a contact that is deleted, something that the sql would fetch anyway ;)

So in a nutshell: store in session, defines what fields you want to fetch when you use api.contact.get and... there is more than one way to skin a cat.

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: Displaying first name
April 16, 2012, 03:52:45 pm
Ever wish you'd never asked  ;D

Quote
You should always specify what you want to get from the api.contact get. The syntax post 4.2 is return = array ('first_name'), and on 4.1 return.first_name=1.

Actually - 4.1 the syntax depends on the api call .... sigh.

4.2 supports a wierd & wonderful array of syntaxes that we have developed at different times. But we are pushing a single preferred syntax - one question (for you X) should we promote return = array ('first_name', 'last_name') or return = 'first_name,last_name'; ie. the second works in urls & I think it works in php too, not sure about smarty....
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: Displaying first name
April 16, 2012, 11:39:55 pm
Hi,

On the rest/ajax interface, &return=first_name,last_name has been working for a while.

for php, I think that return = array is more php syntaxy (and would avoid issues if you put a space before after the ','), but we can move down the return string parsing if it's right now only for ajax/rest so it supports both.

Basically, don't have a lot of opinion about string vs. array. Can't we all love each other?

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

Hershel

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4640
  • Karma: 176
    • CiviHosting
  • CiviCRM version: Latest
  • CMS version: Mostly WordPress and Drupal
Re: Displaying first name
April 17, 2012, 02:28:17 am
Quote from: xavier on April 16, 2012, 03:16:46 pm
If I really were nitpicking, I'd point out that using the api for instance avoid fetching a contact that is deleted, something that the sql would fetch anyway ;)

In this case it's nitpicking, but generally speaking, this is a serious issue and a very good reason to use the API and not SQL. Coleman's suggestion of a function and cache is very good, but definitely using the API to populate the cache initially is recommended.

Quote from: xavier on April 16, 2012, 11:39:55 pm
Can't we all love each other?

Yes. Please add that to the agenda.
CiviHosting and CiviOnline -- The CiviCRM hosting experts, since 2007

See here for the official: What to do if you think you've found a bug.

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Displaying first name
April 17, 2012, 02:55:26 pm
Agenda
1) All love each other
2) Drink Warm Beer
3) Drink Flat Coffee
4) discuss API
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

jsimonis

  • I post frequently
  • ***
  • Posts: 316
  • Karma: 4
    • Forward Support, Inc.
  • CiviCRM version: 4.4-4.5
  • CMS version: Drupal 7
  • MySQL version: 5.5.37-cll
  • PHP version: 5.3.29
Re: Displaying first name
April 19, 2012, 04:26:24 pm
Well, I thought I was doing good, but I think I got lost in all the responses. Hopefully I can figure out which parts of this to use or not to use so I can implement this.

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Displaying first name
April 19, 2012, 04:30:39 pm
Just the last post  ;D
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

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

This forum was archived on 2017-11-26.