6

OK so I am interested in displaying social media account information for my contacts. Attentiv.ly looks awesome but very expensive :((

So I have been investigating other options and can now successfully pass a rest API via a php file to retrieve json from PIPL for a contact. Within civicrm I can create a link to the json using the email variable. I have temporarily (for exploratory purposes only don't worry!) hacked the email.tpl with this

<a href="https://api.fullcontact.com/v2/person.html?email={$item.email}&apiKey=XXXXXX"> social profile</alink

And when clicked it takes me to the correct social mediaprofile. It also works with the Fullcontact API. Hooray!!!

However what I really need is to somehow substitute the link for the script - but I can't put the PHP in the smarty template so now I'm stumped.

 //    $curl = curl_init();
 //   curl_setopt ($curl, CURLOPT_URL, "https://api.pipl.com/search/v4/?email=email@email.com&key=xxxxxxxxxxxxxx ");
 //   curl_exec ($curl);
 //  curl_close ($curl);
?>

Or this

//include_once '../creds.php';
//include_once '../Services/FullContact.php';

//initialize our FullContact API object
//get your api key here:  http://fullcontact.com/getkey
//$fullcontact = new Services_FullContact_API($apikey);

//do a lookup
//$result = $fullcontact->doLookup('bart@fullcontact.com');

//dump our results
//echo "<br/>----------------<br/><pre>";
//print_r($result);
//echo "</pre><br/>----------------<br/>\n";

Obviously substituting the email for the civicrm email variable.

Or indeed if I am going about this entirely the wrong way I'm all ears!!!! All input welcome! Thank you!

PS if anyone can also tell me how to copy and paste code without it actually rendering that would be great

  • See the "help" button in the StackExchange rich text editor when editing questions and answers for instructions on how to format code for legibility. – Chris Burgess Dec 28 '15 at 21:42
  • It'd be a shame if your work didn't get into an extension at some point, this sounds like an excellent and generally useful thing. I notice that PIPL has a non-profit free option, which also makes it really affordable. Please feel free to contact me if you want some help with that. – Alan Dixon Jan 4 '16 at 18:13
  • Hi Alan I'd like to contact you about this - I'm not sure there is a way through this mechanism - can you send me your email address or twitter handle? – Caroline B Jan 10 '16 at 23:20
6

You can run php inside smarty, you just need to include it within {php} tags.

{php}
  $result = doSomethingCool();
  $this->assign('coolStuff', $result);
{/php}

<div>Your social media profile is: {$coolStuff}</div>

PS instead of hacking the template directly you can add a .tpl.extra file. See https://civicrm.org/blogs/dave-greenberg/now-its-easier-add-custom-behaviors-templates

  • Ok - this is good to know I shall try this! – Caroline B Dec 30 '15 at 12:28
4

Since you are wanting to do more than just a "little bit" of php in your smarty template I would suggest this alternate approach:

  1. Implement a hook that fires when you want, such as hook_civicrm_pageRun.
  2. In your hook, run the php code you want to retrieve the remote data (also add a conditional to only run it in the specific circumstance you want, since this is a rather ubiquitous hook - check the page name).
  3. I might also suggest some form of local caching so you are not firing external rest requests all the time.
  4. From your hook, inject the content you want into a region of the page.
  • Great thanks everyone I've got a lot of pointers here – Caroline B Jan 2 '16 at 21:46
2

Second the solution from Coleman. If however you do not want to include PHP in a Smarty template you could also create an API in an extension that does what you want to do, and then use CRM.api to retrieve the data in jQuery/JS.

  • I think this is probably beyond my "talents" at the moment however I like a challenge so I may well give it a try! Thank you :) – Caroline B Dec 30 '15 at 12:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.