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) »
  • Chained relationships with Smarty?
Pages: [1] 2

Author Topic: Chained relationships with Smarty?  (Read 4326 times)

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Chained relationships with Smarty?
September 11, 2011, 09:37:57 pm
I want to add something to the bottom of the contribution dashboard to show contributions (& pledges) by related contacts. I'm not sure the smarty syntax for accessing chained APIs... i.e

{crmAPI entity="relationship" action="get" var="relationships" contact_id=$contactId api_contribution_get=array('contact_id'=>2262) }

nb:: the contact_id is probably going to be '$value.contact_id_a' rather than a number. To make relationship api useful in this context I'd almost need to receive back

'cid'=>             (the id I passed in - this happens now)
'contact_id_a'   (per the table - could be either way around)
'contact_id_b'   (per the table - could be either way around)
'contact_id'       (the Other contact id - ie. the one that isn't what I passed in  - otherwise I have to compare 'cid' against ;contact_id_a & b to find this out. Could be called something different but contact_id at least would chain in nicely & then api_contribution_get would just work....
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: Chained relationships with Smarty?
September 12, 2011, 12:12:28 am
Hi,

Haven't tested it, but think that if you

{$contrib_params.contact_id=2262}
{crmAPI entity="relationship" action="get" var="relationships" contact_id=$contactId api_contribution_get=$contrib_params }

Should work.
-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: Chained relationships with Smarty?
September 12, 2011, 02:04:31 am
Nah it didn't work. Looks like assigning arrays within smarty templates is even more painful than I realised

http://www.hackvalue.nl/en/article/125/static%20assignment%20of%20arrays%20in%20smarty%20templates
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: Chained relationships with Smarty?
September 12, 2011, 03:01:59 am
Ah, the easiest is then probably to modify  CRM/Core/Smarty/plugins/function.crmAPI.php

to test if one of the param is a json (eg starts with "{" ) and json_decode it, so you can call it like that:

{crmAPI entity="relationship" action="get" var="relationships" contact_id=$contactId api_contribution_get="{'contact_id':2262}" }

What do you think?
-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: Chained relationships with Smarty?
September 12, 2011, 03:17:42 am
You're determined to get me using JSON aren't you ;)

I think that something to assist with using arrays in smarty is definitely needed. Not sure if the { brackets would cause problems? But yeah, seems like a good approach.

In the meantime I got my code working without using nesting - this is CRM/Pledge/Page/tab.extra.tpl & shows pledges from related contacts on the contact's pledge tab (have also done for user dashboard)

Code: [Select]
{crmAPI entity="relationship" action="get" var="relationships" contact_id=$contactId  }
{if $relationships.values}
{foreach from=$relationships.values item=rel}
{if $rel.is_active == 0 }
{else}
{/if}
{if $contact_id EQ $rel.contact_id_a}
{crmAPI entity="pledge" action="get" var="pledges" contact_id=$rel.contact_id_b sort='create_date DESC'}
{assign var=relatedContact value=`$rel.contact_id_b`}
{else}
 {crmAPI entity="pledge" action="get" var="pledges" contact_id=$rel.contact_id_a sort='create_date DESC'}
{assign var=relatedContact value=`$rel.contact_id_a`}
{/if}



 {if $pledges.values} 
           
            {strip}
            <div class="header-dark" id="related-pledges">
                {ts}Pledges made by Related People{/ts}:
            </div>
            <table class="selector">
                <tr class="columnheader" id='relatedcontheader'>
                    <th>{ts}Contributor{/ts}</th>
                    <th>{ts}Amount{/ts}</th>
                    <th>{ts}Contribution Type{/ts}</th>
                    <th>{ts}Start date{/ts}</th>
                    <th>{ts}Status{/ts}</th>
                    <th></th>
                </tr>
                {foreach from=$pledges.values item=row}
                    <tr id='rowid{$row.honorId}' class="{cycle values="odd-row,even-row"}">
                        <td><a href="{crmURL p="civicrm/contact/view" q="reset=1&cid=`$row.contact_id`"}" id="view_contact">{$row.display_name}</a></td>
                        <td>{$row.pledge_amount}</td>
                        <td>{$row.pledge_contribution_type}</td>
                        <td>{$row.pledge_create_date|truncate:10:''|crmDate}</td>
                        <td>{$row.pledge_status}</td>
                        <td>{if $rel.is_active EQ 0}inactive relationship{/if}</td>
                    </tr>
                {/foreach}
            </table>
            {/strip}
 {/if}


{/foreach}
{/if}
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: Chained relationships with Smarty?
September 12, 2011, 03:22:05 am
Looks good. May I lobo you and ask for a blog post?

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: Chained relationships with Smarty?
September 12, 2011, 03:53:06 am
new verb? to Lobo
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

petednz

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4899
  • Karma: 193
    • Fuzion
  • CiviCRM version: 3.x - 4.x
  • CMS version: Drupal 6 and 7
Re: Chained relationships with Smarty?
September 12, 2011, 03:56:01 am
bit like being lobbied? or lobbed? loboed?
Sign up to StackExchange and get free expert advice: https://civicrm.org/blogs/colemanw/get-exclusive-access-free-expert-help

pete davis : www.fuzion.co.nz : connect + campaign + communicate

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Chained relationships with Smarty?
September 12, 2011, 05:54:44 am
Quote from: Eileen on September 12, 2011, 03:53:06 am
new verb? to Lobo

In my experience, much much more common in the passive form:

"I asked a simple question but I've been loboed to write a test unit and a blog post".
See also "patch welcomed".

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

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: Chained relationships with Smarty?
September 12, 2011, 06:50:05 am

making it more specific:

loboed == please do the right thing :)

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

totten

  • Administrator
  • Ask me questions
  • *****
  • Posts: 695
  • Karma: 64
Re: Chained relationships with Smarty?
September 13, 2011, 02:30:32 pm
Quote from: Eileen on September 11, 2011, 09:37:57 pm
{crmAPI entity="relationship" action="get" var="relationships" contact_id=$contactId api_contribution_get=array('contact_id'=>2262) }

...

Quote from: xavier on September 12, 2011, 03:01:59 am
{crmAPI entity="relationship" action="get" var="relationships" contact_id=$contactId api_contribution_get="{'contact_id':2262}" }

So, if I'm reading correctly, the problem is that {crmAPI} accepts a flat parameter list, but we need to pass a tree of params into civicrm_api(). One approach is to treat each parameter-name as a path expression with some delimiter ("/" or "." or "-" -- note that "_" won't work because we have too many CiviCRM API symbols which use "_"). For example, if ":" is the delimiter:

Code: [Select]
{crmAPI entity="relationship" action="get" var="relationships" contact_id=$contactId api.contribution.get:contact_id=2262}

To translate the flat key-value array into a tree, you can use CRM_Utils_Array::unflatten(). However, it may be hard to find a delimiter that satisfies the naming constraints of both Smarty and CiviCRM API.

If we're willing to add an extra Smarty helper, we could do something like:

Code: [Select]
{crmAPIx entity="relationship" action="get" var="relationships" contact_id=$contactId}
    {crmAPIx entity="contribution" action="get" contact_id=2262}
    {/crmAPIx}
{/crmAPIx}

(In that snippet, "crmAPIx" needs to be a Smarty "block" instead of a Smarty "function"; it's renamed to avoid conflicting with the current crmAPI.)

[Note: Comment edited to use "crmAPIx" instead of "crmAPI".]
« Last Edit: September 13, 2011, 02:49:39 pm by totten »

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Chained relationships with Smarty?
September 13, 2011, 02:50:47 pm
There are a few other areas where crmAPI wants to receive data as an array - like options, filters, return so maybe we do need a more general array conversion (not sure if that's instead or as well as Tim's suggestion)
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: Chained relationships with Smarty?
September 13, 2011, 11:21:52 pm
return="field,field,field" works.

It works because you don't have a meaningful key.

For an associative array, I'd stick with json. I't's known, got the converter to array no matter the depth, you stick it in a string and it just works, without having to find a magic separator that is going to be not available on smarty 3;)

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

totten

  • Administrator
  • Ask me questions
  • *****
  • Posts: 695
  • Karma: 64
Re: Chained relationships with Smarty?
September 14, 2011, 01:35:21 am
Quote from: xavier on September 12, 2011, 03:01:59 am
{crmAPI entity="relationship" action="get" var="relationships" contact_id=$contactId api_contribution_get="{'contact_id':2262}" }

Quote from: xavier on September 13, 2011, 11:21:52 pm
For an associative array, I'd stick with json. I't's known, got the converter to array no matter the depth, you stick it in a string and it just works, without having to find a magic separator...

JSON is problematic. :(

1. The example isn't legal JSON -- http://www.json.org/ mandates the use of double-quotes. JSON is a handy wire protocol, but it's not very pleasant for human-interaction.

2. Although the example looks reasonable, Smarty barfs on the inner "}":

Code: [Select]
// Smarty Code
{assign var=test value="{'contact_id':2262}" }
Test=<{$test}>

// Output
" }
Test=<{\'contact_id\':2262>

// Smarty Code
{assign var=test value="\{'contact_id':2262\}" }
Test=<{$test}>

// Output
" }
Test=<\{\'contact_id\':2262\>

3. Finally, even if we got past #1 and #2, we'd still be making an open-invitation for user's to create subtle escaping bugs, e.g.

Code: [Select]
// Safe
contribution_type=$variable

// Unsafe
api_foo_bar="{\"contribution_type\":\"{$variable}\"}"

// Safe
api_foo_bar="{\"contribution_type\":\"{$variable|escape:javascript}\"}"

AFAICS, anything that pushes the limits of Smarty's syntax will break. We can avoid the battle by working with Smarty's parser rather than against it, e.g.

Code: [Select]
{crmAPIx entity="relationship" action="get" var="relationships" contact_id=$contactId}
    {crmAPIx entity="contribution" action="get" contact_id=2262}
    {/crmAPIx}
{/crmAPIx}

or

Code: [Select]
{crmAPIx entity="relationship" action="get" var="relationships" contact_id=$contactId}
  {key name="api.contribution.get"}
     {key name="contactId"}2262{/key}
  {/key}
{/crmAPIx}

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Chained relationships with Smarty?
September 14, 2011, 01:59:31 am
You are right, starting to look really ugly if you have to backslash everything.

blocks look like an option, have to say I really never played with them, so not sure if they are limitations with it.

Why crmAPIx ? (I mean the x as a convention for the name)

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

Pages: [1] 2
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Chained relationships with Smarty?

This forum was archived on 2017-11-26.