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
Re: Chained relationships with Smarty?
September 14, 2011, 02:04:22 am
x for Xavier, x for 'previous', x for a kiss, x for a mystery
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: Chained relationships with Smarty?
September 14, 2011, 02:05:20 am
x marks the spot
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: Chained relationships with Smarty?
September 14, 2011, 02:35:43 am
xxxx is a brand of Aussie beer
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

totten

  • Administrator
  • Ask me questions
  • *****
  • Posts: 695
  • Karma: 64
Re: Chained relationships with Smarty?
September 14, 2011, 06:18:10 pm
x is also marketing speak for "Extra-Extremely Expandably Excellent Extension."

{crmAPIx} was sort of arbitrary... basically, the current {crmAPI} is a Smarty "function", and we need a "block" to use end-tags with capturable content, and I don't think you can use the same names for functions and blocks. "x" seems like something you can tack on to mean "somewhat bigger than the original". (We could do worse... we could go the LISP route and say "crmAAAPI".)

I think one important note about recursive blocks... Smarty will invoke the plugin once for each start and end tag in the intuitive order; using these callbacks, we can build up a global variable which represents the desired array-tree. (Each start-tag corresponds to adding a new subtree; each end-tag corresponds to finishing a subtree.) I'm a bit rusty so I can't spit out the code right now, but it shouldn't be very hard.

jimurl

  • I post occasionally
  • **
  • Posts: 70
  • Karma: 0
  • CiviCRM version: 3.4.6
  • CMS version: drupal 6.22
  • MySQL version: 5+
  • PHP version: 5+
Re: Chained relationships with Smarty?
September 15, 2011, 11:52:05 am
Most Xcellent!

I needed to do almost eXactly the same thing as Eileen- On the Contact Summary page,  we wanted the Related Contacts listed right there; along with the Notes associated with each of those contacts. We ended up what I would call nested smarty crmAPI calls- the first pulls the list of related contacts; the second pulls the Notes for each of THOSE Contacts; and the third we use to find the display_name of the person that created the Note.

Thanks, Eileen, I hammer-and-tonged your code into this:

Code: [Select]
        <table id="current_relationship" class="display">
        <thead>
        <tr>
            <th>{ts}Relationship{/ts}</th>
            <th></th>
            <th>{ts}City{/ts}</th>
            <th>{ts}State/Prov{/ts}</th>
            <th>{ts}Email{/ts}</th>
            <th>{ts}Phone{/ts}</th>
            <th></th>
        </tr>
        </thead>
        {foreach from=$RelationshipS.values item=rel}
            <tr id="rel_{$rel.id}" class="{cycle values="odd-row,even-row"} row-relationship {if $rel.is_permission_a_b eq 1 or $rel.is_permission_b_a eq 1}row-highlight{/if}">
     
            <!---    IN here- I just scanvenged the code from Relationship.tpl to output name, city, state, phone, email, etc; of each of the related contacts. I took it out for this post, for readability.  -->
            </tr>
         
            {crmAPI var="NoteS" entity="Note" action="get" version="3" entity_id=$rel.cid sort='modified_date DESC'}
            {if $NoteS.count > 0}
              {foreach from=$NoteS.values item=Note}
              {crmAPI var="ContactS" entity="Contact" action="get" version="3" id=$Note.contact_id }
              <tr>
                 <td> 
                 </td>             
                 <td  colspan ="5">               
                   Note by {foreach from=$ContactS.values item=Contact}{$Contact.display_name}{/foreach} on {$Note.modified_date} <b>: {$Note.subject}</b> <br />
                   {$Note.note}           
                </td>
              </tr>
                {/foreach}
              {/if}
            {/foreach}
        </table>

The  thing that is kinda wierd is the foreach loop for the name of the person who created the Note- there is always only one, so it seems unnecessary, but works. I didn't know enough Smarty to fiXX that.


Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Chained relationships with Smarty?
September 15, 2011, 11:57:51 am
If you pass in 'sequential' => 1 to a civicrm_api call the results will be indexed from 0 so you can avoid the foreach loop. Alternatively, if only 1 result is returned it's index will be set in the returned array as 'id'

ie. $result = array('count' =>1. 'id' => idofonlyreturnedentity')
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: Chained relationships with Smarty?
September 15, 2011, 11:59:52 am
Also, if you hadn't passed 'sort' into your note params you could have done nesting simply by using api_note_get => 1 - at the relationship level
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

jimurl

  • I post occasionally
  • **
  • Posts: 70
  • Karma: 0
  • CiviCRM version: 3.4.6
  • CMS version: drupal 6.22
  • MySQL version: 5+
  • PHP version: 5+
Re: Chained relationships with Smarty?
September 16, 2011, 11:13:13 am
Hi Eileen,

Thanks on number 20, I think I get the api part of it; I don't know smarty well enough- I couldn't figure out how to refer to the display_name of the creator of the note. I tried lots of things, and found this worked:

Code: [Select]
....
{assign var=notecreatorid value=$Note.contact_id}
...
{crmAPI var="ContactS" entity="Contact" action="get" version="3" id=$notecreatorid }
...
{$Contacts.values.$notecreatorid.display_name}

Thanks for your help!

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.