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 (Moderator: Donald Lobo) »
  • Trying to print CiviCRM_Address fields for Contact Refernce Via a NODE
Pages: [1] 2

Author Topic: Trying to print CiviCRM_Address fields for Contact Refernce Via a NODE  (Read 3809 times)

miko

  • I post occasionally
  • **
  • Posts: 75
  • Karma: 2
  • CiviCRM version: 3.4.5
  • CMS version: Drupal 6.22
  • MySQL version: 5.0.77
  • PHP version: 5.2.11
Trying to print CiviCRM_Address fields for Contact Refernce Via a NODE
April 24, 2011, 02:01:09 am
Please see below for the UPDATED description of this issue that I've provide for Xavier.  Thanks for the heads up Xavier. 

What I really just need to do is to print the addresses of sports facilities for practice  events.  I can't use Civi-Event for the Practice Entry because
a) CiviEvent doesn't have Profiles and
b) CiviCRM calendaring doesn't handle repeating dates very well, but Drupal does an amazing job, and practices take on certain days and often skips weeks.

So i'm just trying to get the address to print when the /content/ form of the node opens- and I know that I need to clue into something about the contact reference fields before I can figure this out.

THANKS in advance.  I know I have zero points- I don't know how to GET points.  I'm NEW here, and I am working for a wonderful non-profit, the Special Olympics.
« Last Edit: April 24, 2011, 01:49:52 pm by miko »

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Can someone tell me who developed the CiviCRM CCK node contact reference field?
April 24, 2011, 02:26:26 am
Hi,

No idea what happened to your karma to drop, but that's certainly not the criteria to decide if I'll answer a post, nor is that one for others in the community. On the other hand posting on a week-end and going all "No one will tell me, but apparently it is stopping ANYONE from answering my questions, and that is WRONG" conspiracy theory 3 minutes after your post is unlikely to motivate contributors to jump in ;)

ESR is providing some useful tips on how to seek for help, you might want to read it http://www.catb.org/~esr/faqs/smart-questions.html

Moreover, put as the subject what is your problem. Finding someone that is going to tell you who developed the CCK node contact reference is not your problem, and no matter who wrote it, might not be the person that will help you solving your problem

Ok, let's rewind shall we?

Can you start by explain what you're trying to achieve, what do you mean by print, what are the criteria to fetch addresses ? Not sure I understood based from the code you posted (eg what is the contact_id= 7 hardcoded ?) nor how you plan to use it.

Anyway, I would suggest you to use the api instead of going directly at the mysql layer, your code will be much more robust (and easier to maintain).

i'd suggest to start fresh on a new thread.

X+
« Last Edit: April 24, 2011, 02:29:31 am by xavier »
-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: Can someone tell me who developed the CiviCRM CCK node contact reference field?
April 24, 2011, 01:29:25 pm
I believe the smarts for this are in this function

function theme_civicrm_cck_formatter_default($element)

under civicrm/drupal/modules/civicrm_cck/civicrm_cck.module

- that is just a view handler. I presume you can override that with your own views handler. If you have the contact id you can get other details using the api:

require_once 'api/api.php';
$contact = civicrm_api('Contact','Get','version'=>3, 'contact_id' = x);

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

miko

  • I post occasionally
  • **
  • Posts: 75
  • Karma: 2
  • CiviCRM version: 3.4.5
  • CMS version: Drupal 6.22
  • MySQL version: 5.0.77
  • PHP version: 5.2.11
Re: Can someone tell me who developed the CiviCRM CCK node contact reference field?
April 24, 2011, 01:41:50 pm
Thanks so much Xavier!

First- regarding using the "API".  I have never used an API and have no idea how to use one.  The learning curve simply terrifies me.   Is it easy?  How would I even learn how to use it?  I have absolutely no clue how I would learn about using the API.  I don't even know where the API is, and the term has always scared me since I tried to figure out the PayPal SOAP/XML api  and couldn't figure it out.  No one's ever given me any good instruction about APIs.

Second, I didn't realize that you can't tell what I'm trying to achieve.  I'll try to be really clear and explain what I've tried.

The Special Olympics holds training/practices for the athletes.  The practices get posted and placed on a calendar, as do competitions.  This is not a problem in Drupal.  However, the Practice node content page should list the location ADDRESS, not just the name.  With the current accessiblity of CiviCRM in the node, I ONLY can get a contact reference, which displays ONLY the name of the location on the Practice screen.  Also it's a link to the CiviCRM contact screen and I don't really want that screen exposed to Anonymous users because of the secondary tabs and extra information that's exposed.

So the Practice Listing reads like this:

Oakland Aquatics

Sport
Aquatics

Head Coach
Coach To Be Announced   <--link to CiviCRM contact (not desired but not an issue in this case)

Practice Location
Live Oak Memorial Pool  <--link to CiviCRM contact (not desired)

Dates

Repeats every week until Tue Jun 21 2011 except Mon Mar 28 2011.
Sat, 03/26/2011 - 10:00am
Sat, 04/02/2011 - 10:00am
Sat, 04/09/2011 - 10:00am
Sat, 04/16/2011 - 10:00am
Sat, 04/23/2011 - 10:00am
Sat, 04/30/2011 - 10:00am
Sat, 05/07/2011 - 10:00am
Sat, 05/14/2011 - 10:00am
Sat, 05/21/2011 - 10:00am
Sat, 05/28/2011 - 10:00am
Sat, 06/04/2011 - 10:00am
Sat, 06/11/2011 - 10:00am
Sat, 06/18/2011 - 10:00am


And it SHOULD show the address also, like this, ideally:

Oakland Aquatics

Sport
Aquatics

Head Coach
Coach To Be Announced

Practice Location
Live Oak Memorial Pool
1055 MacArthur Boulevard
Oakland, CA  94610


Dates

Repeats every week until Tue Jun 21 2011 except Mon Mar 28 2011.
Sat, 03/26/2011 - 10:00am
Sat, 04/02/2011 - 10:00am
Sat, 04/09/2011 - 10:00am
Sat, 04/16/2011 - 10:00am
Sat, 04/23/2011 - 10:00am
Sat, 04/30/2011 - 10:00am
Sat, 05/07/2011 - 10:00am
Sat, 05/14/2011 - 10:00am
Sat, 05/21/2011 - 10:00am
Sat, 05/28/2011 - 10:00am
Sat, 06/04/2011 - 10:00am
Sat, 06/11/2011 - 10:00am
Sat, 06/18/2011 - 10:00am


Note that the calendar module creates an automatic link, so I cannot redirect this to a page made from a node View mixed with a Civi View. When I use DEVEL's Execute Php Code tool, I can get the address, perfectly, when I submit this code:

db_set_active('cividb');

$data = db_query('SELECT DISTINCTROW street_address, supplemental_address_1, city, county_id, civicrm_state_province.abbreviation, postal_code FROM civicrm_address INNER JOIN civicrm_state_province ON state_province_id = civicrm_state_province.id WHERE is_primary =1 AND contact_id =7') or die(mysql_error());

 while($info = mysqli_fetch_array( $data ))
 {
 Print $info['street_address']."\n";
 if ($info['supplemental_address_1']) {Print $info['supplemental_address_1']."\n";};
 Print $info['city'].", ".$info['abbreviation']."  ".$info['postal_code']."\n";
 }

db_set_active('default');


This code works in that screen, within the databse, within Drupal.

I have successfully stored this code in the Content Template now, replacing "=7" with the node variable:

<?php
db_set_active('cividb');

$sql = "SELECT DISTINCTROW street_address, supplemental_address_1, city, county_id, civicrm_state_province.abbreviation, postal_code FROM civicrm_address INNER JOIN civicrm_state_province ON state_province_id = civicrm_state_province.id WHERE is_primary =1 AND contact_id =".print $node->field_prac_location[0]['contact_id'];

$data = db_query($sql) or die(mysql_error());

 while($info = mysqli_fetch_array( $data ))
 {
 Print $info['street_address']."\n";
 if ($info['supplemental_address_1']) {Print $info['supplemental_address_1']."\n";};
 Print $info['city'].", ".$info['abbreviation']."  ".$info['postal_code']."\n";
 }

db_set_active('default');
?>


But when I run this it simply adds the number 7 to the screen, not the address:

Oakland Aquatics

Sport
Aquatics

Head Coach
Coach To Be Announced

Practice Location
Live Oak Memorial Pool

7


Dates

Repeats every week until Tue Jun 21 2011 except Mon Mar 28 2011.
Sat, 03/26/2011 - 10:00am
Sat, 04/02/2011 - 10:00am
Sat, 04/09/2011 - 10:00am
Sat, 04/16/2011 - 10:00am
Sat, 04/23/2011 - 10:00am
Sat, 04/30/2011 - 10:00am
Sat, 05/07/2011 - 10:00am
Sat, 05/14/2011 - 10:00am
Sat, 05/21/2011 - 10:00am
Sat, 05/28/2011 - 10:00am
Sat, 06/04/2011 - 10:00am
Sat, 06/11/2011 - 10:00am
Sat, 06/18/2011 - 10:00am


7 *IS* the contact ID, so it's just printing the node variable correctly but ignoring all my other php.

I am also trying this in node-practice.tpl.php but Drupal 6 seems to be ignoring the custom module.

If the aPI method is pretty easy to learn, it'd be great to see a no-brainer tutorial, if one exists.
« Last Edit: April 24, 2011, 01:51:03 pm by miko »

miko

  • I post occasionally
  • **
  • Posts: 75
  • Karma: 2
  • CiviCRM version: 3.4.5
  • CMS version: Drupal 6.22
  • MySQL version: 5.0.77
  • PHP version: 5.2.11
Re: Can someone tell me who developed the CiviCRM CCK node contact reference field?
April 24, 2011, 01:46:49 pm
Eileen, you're wonderful.  Thanks.  That doesn't look so hard.

Where would I put the require_once statement?

I'm going to look for some documentation on the API because even with the useful information you've provided, I don't know where to put the statements and I don't know what $contact is here- is it an array or the standard contact data fields?  I actually only need address fields.  I don;t want to be a burden to anyone but I am SO under a deadline that if I just try to learn everything myself, we;'ll go past the deadline or over budget.

Thanks again Eileen.

Quote from: Eileen on April 24, 2011, 01:29:25 pm
I believe the smarts for this are in this function

function theme_civicrm_cck_formatter_default($element)

under civicrm/drupal/modules/civicrm_cck/civicrm_cck.module

- that is just a view handler. I presume you can override that with your own views handler. If you have the contact id you can get other details using the api:

require_once 'api/api.php';
$contact = civicrm_api('Contact','Get','version'=>3, 'contact_id' = x);

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Can someone tell me who developed the CiviCRM CCK node contact reference field?
April 24, 2011, 01:57:22 pm
Quote from: miko on April 24, 2011, 01:41:50 pm
Thanks so much Xavier!  Yes, I tend to be a bit paranoid because Don Lobo yelled at me the VERY first day I was here, saying that I did several things wrong.  I'm not a perfect person, just a good geek.

I don't think I ever heard or seen lobo yelled, and believe me, I screwed up my fair share of code in civi ;) Both in the forums and in real life, he genuinely tries to help and understand that we don't have his knowledge of the software.

As for the API, that's fairly simple since api v3

http://civicrm.org/API_version_3
http://wiki.civicrm.org/confluence/display/CRMDOC40/CiviCRM+Public+APIs

Try using (assuming you have updated to civicrm 3.4) /civicrm/ajax/doc  (choose address, get)

$results=civicrm_api("Address","get", array ('version' =>'3', 'contact_id' =>'7'));
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

miko

  • I post occasionally
  • **
  • Posts: 75
  • Karma: 2
  • CiviCRM version: 3.4.5
  • CMS version: Drupal 6.22
  • MySQL version: 5.0.77
  • PHP version: 5.2.11
Re: Trying to print CiviCRM_Address fields for Contact Refernce Via a NODE
April 24, 2011, 02:11:47 pm
Thanks again.

Xavier, this looks fairly straightforward. 

Can this code be IN the custom node template, or in the Contemplate screen?  When can I put this?

I cannot hardwire the "7", I absolutely must use the $node->field_prac_location[0]['contact_id'] variable within the node.

Also, do I need the "require_once?"  For version two, would I code this way?:

require_once 'sites/all/modules/civicrm/api/api.php';
$contid = $node->field_prac_location[0]['contact_id']
$results=civicrm_api("Address","get", array ('version' =>'2', 'contact_id' =>$contid));
Print $results[street_address];


Can it all happen in the node template?

miko

  • I post occasionally
  • **
  • Posts: 75
  • Karma: 2
  • CiviCRM version: 3.4.5
  • CMS version: Drupal 6.22
  • MySQL version: 5.0.77
  • PHP version: 5.2.11
Re: Trying to print CiviCRM_Address fields for Contact Refernce Via a NODE
April 24, 2011, 02:13:56 pm
Also, we're at CiviCRM 3.3.6 and I'm scared of trying to upgrade during this high development time and screw everything up.  What do you think- it's a super-easy upgrade?  Thanks again.

miko

  • I post occasionally
  • **
  • Posts: 75
  • Karma: 2
  • CiviCRM version: 3.4.5
  • CMS version: Drupal 6.22
  • MySQL version: 5.0.77
  • PHP version: 5.2.11
Re: Trying to print CiviCRM_Address fields for Contact Refernce Via a NODE
April 24, 2011, 02:41:12 pm
Well I am having trouble even getting pas the require_once  :(

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Trying to print CiviCRM_Address fields for Contact Refernce Via a NODE
April 24, 2011, 02:45:10 pm
If you go to the link Xavier posted there is also instructions to backport api v3 to 3.3.6 or earlier - that is a no risk option (actually - the instructions might be on this board somewhere - you basically just get the latest version of api.php & the v3 folder out of the CiviCRM code base)

You can use the code given in any module but you may need to add

    if ( ! civicrm_initialize( ) ) {
        return;
    }

first to ensure it can find the api.php file
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: Trying to print CiviCRM_Address fields for Contact Refernce Via a NODE
April 24, 2011, 02:47:58 pm
OK - so here is the backport instruction

from the api directory in code base
svn co http://svn.civicrm.org/civicrm/trunk/api/api.php
svn co http://svn.civicrm.org/civicrm/trunk/api/v3

(and then use write code per http://svn.civicrm.org/civicrm/trunk/api/v3/examples)

If you have this first

  if ( ! civicrm_initialize( ) ) {
        return;
    }

then you should only need

require_once 'api/api.php'; because the civicrm root should be added to the path
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

miko

  • I post occasionally
  • **
  • Posts: 75
  • Karma: 2
  • CiviCRM version: 3.4.5
  • CMS version: Drupal 6.22
  • MySQL version: 5.0.77
  • PHP version: 5.2.11
Re: Trying to print CiviCRM_Address fields for Contact Refernce Via a NODE
April 24, 2011, 03:17:15 pm
Thank you Eileen.  I hope I can work through it.  But it's like visiting Mars.

When you write "you can use in any module" you mean node templates as well?  I'm not sure where modules bgin and end- I didn't think a template was a module.

I'll try that backport.  I guess you're both implying that I do NOT want to use V2.

Thanks, we'll see how this goes.

So this is probably what I'll code then?

  if ( ! civicrm_initialize( ) ) {
        return;

        require_once 'api/api.php';   <!-- <- does all my code go inside these brackets so that it won't run if there's no civicrn_initialize? -->

        $contid = $node->field_prac_location[0]['contact_id']
        $results=civicrm_api("Address","get", array ('version' =>'2', 'contact_id' =>$contid));
        Print $results[street_address];
    }

Or is it like this?

  if ( ! civicrm_initialize( ) ) {
        return;
    }

        require_once 'api/api.php';   <!-- <- does all my code go inside these brackets so that it won't run if there's no civicrn_initialize? -->

        $contid = $node->field_prac_location[0]['contact_id']
        $results=civicrm_api("Address","get", array ('version' =>'2', 'contact_id' =>$contid));
        Print $results[street_address];

But then it seems like the code will still run regardless of the result of the function call to initialize.  Or maybe nothing else on the page will render.

When I tried the require once in the DEVEL code tester, it never worked.  :(

But thanks again!

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Trying to print CiviCRM_Address fields for Contact Refernce Via a NODE
April 24, 2011, 03:21:30 pm
The second looks correct. The initialise call just says - give up gracefully if you can't initialise civicrm. If you don't have this then if you disable civicrm for any reason (maybe while upgrading) you might start getting fatal errors as the code relies on it


  if ( ! civicrm_initialize( ) ) {
        return;
    }

        require_once 'api/api.php'; 

        $contid = $node->field_prac_location[0]['contact_id']
        $results=civicrm_api("Address","get", array ('version' =>'3', 'contact_id' =>$contid));
       print $results[$contid]['values'][street_address];

should work - the result array returned is a few levels deep
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

miko

  • I post occasionally
  • **
  • Posts: 75
  • Karma: 2
  • CiviCRM version: 3.4.5
  • CMS version: Drupal 6.22
  • MySQL version: 5.0.77
  • PHP version: 5.2.11
Re: Trying to print CiviCRM_Address fields for Contact Refernce Via a NODE
April 24, 2011, 03:25:33 pm
Thanks, and I just checked my api folder- there's no actual api.php in there!  So no wonder the require_once didn't work!  Now I'm off to do the backport.

miko

  • I post occasionally
  • **
  • Posts: 75
  • Karma: 2
  • CiviCRM version: 3.4.5
  • CMS version: Drupal 6.22
  • MySQL version: 5.0.77
  • PHP version: 5.2.11
Re: Trying to print CiviCRM_Address fields for Contact Refernce Via a NODE
April 24, 2011, 04:05:39 pm
Eilen, I got it working in PHP executor of DEVEL (not a node yet), but here is how the code ACTUALLY needs to look:

  if ( ! civicrm_initialize( ) ) {
        return;
    }

        require_once 'api/api.php'; 

        $results=civicrm_api("Address","get", array ('version' =>'3', 'contact_id' => '7'));
       print_r($results);
       
       $temp_id=$results['id'];


       print $results['values'][$temp_id]['street_address'];[/font]

Still- you got me a long way there!  I gave you an "applaud"!  :)

Pages: [1] 2
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • Trying to print CiviCRM_Address fields for Contact Refernce Via a NODE

This forum was archived on 2017-11-26.