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) »
  • Support »
  • Using CiviCRM »
  • Using Core CiviCRM Functions (Moderator: Yashodha Chaku) »
  • Custom Token: Joint greeting and year to date donations
Pages: [1]

Author Topic: Custom Token: Joint greeting and year to date donations  (Read 563 times)

YossiCrm

  • I’m new here
  • *
  • Posts: 4
  • Karma: 0
  • CiviCRM version: 4.4.7
  • CMS version: Drupal 7
  • MySQL version: 5.5
  • PHP version: 5.3
Custom Token: Joint greeting and year to date donations
January 09, 2015, 02:50:06 pm
So we've been trying to make a joint greeting token for mailings as well as a table displaying all year to date donations.
At the moment when I insert the Tokens and create the PDF nothing gets displayed.
Here is the code. Any ideas what might be the issue?
We are running Drupal 7 and CiviCrm 4.4.7

<?php

function fcberkTokens_civicrm_tokens(&$tokens) {
  $tokens['rGreeting'] = array(
    'rGreeting.full' => 'Relationship Greeting: Full',
    'rGreeting.short' => 'Relationship Greeting: Short'
  );

  $tokens['donations'] = array(
    'donations.ytd' => 'Donations: YTD'
  );
}


function fcberkTokens_civicrm_tokenValues(&$values, $cids, $job = null, $tokens = array(), $context = null) {
   $relationship_type_spouse = 2;
   $relationship_type_partner = 3;
   $contacts = implode(',', $cids);

   //
   if (!empty($tokens['rGreeting'])) {
      $sql = "SELECT c1.id id1, c1.prefix_id pref1, c1.first_name fname1, c1.last_name lname1, c2.prefix_id pref2, c2.first_name fname2, c2.last_name lname2, r.relationship_type_id, r.contact_id_a, r.contact_id_b
      FROM civicrm_contact c1
      LEFT JOIN civicrm_relationship r ON c1.id = r.contact_id_a OR c1.id = r.contact_id_b
         AND r.relationship_type_id IN($relationship_type_spouse,$relationship_type_partner)
         AND r.is_active = 1
         AND (r.end_date IS NULL OR r.end_date > CURDATE())
      LEFT JOIN civicrm_contact c2 ON (c1.id = r.contact_id_a AND c2.id = r.contact_id_b) OR (c2.id = r.contact_id_a AND c1.id = r.contact_id_b)
      WHERE c1.id IN($contacts)
      ";

      $dao = &CRM_Core_DAO::executeQuery($sql);
      while ($dao->fetch()) {

         $pref1  = $dao->id1 == $dao->contact_id_a ? $dao->pref1  : $dao->pref2;
         $fname1 = $dao->id1 == $dao->contact_id_a ? $dao->fname1 : $dao->fname2;
         $lname1 = $dao->id1 == $dao->contact_id_a ? $dao->lname1 : $dao->lname2;
         $pref2  = $dao->id1 == $dao->contact_id_a ? $dao->pref2  : $dao->pref1;
         $fname2 = $dao->id1 == $dao->contact_id_a ? $dao->fname2 : $dao->fname1;
         $lname2 = $dao->id1 == $dao->contact_id_a ? $dao->lname2 : $dao->lname1;

         $cid = $dao->id1;

         if (empty($dao->relationship_type_id)) {
            if (in_array('rGreeting.full', $tokens['rGreeting'])) {
               $values[$cid]['rGreeting.full'] = $dao->pref1 . " " . $dao->fname1 . " " . $dao->lname1;
            }
            if (in_array('rGreeting.short', $tokens['rGreeting'])) {
               $values[$cid]['rGreeting.short'] = $dao->pref1 . " " . $dao->lname1;
            }
         }
         elseif ($dao->relationship_type_id == $relationship_type_spouse) {
            if (in_array('rGreeting.full', $tokens['rGreeting'])) {
               $values[$cid]['rGreeting.full'] = $pref1 ." & ". $pref2 ." ". $fname1 ." & ". $fname2 ." ". $lname1;
            }
            if (in_array('rGreeting.short', $tokens['rGreeting'])) {
               $values[$cid]['rGreeting.short'] = $pref1 ." & ". $pref2 ." ". $lname1;
            }
         }
         elseif ($dao->relationship_type_id == $relationship_type_partner) {
            if (in_array('rGreeting.full', $tokens['rGreeting'])) {
               $values[$cid]['rGreeting.full'] = $pref1 ." & ". $pref2 ." ". $fname1 ." ". $lname1 ." & ". $fname2 ." ". $lname2;
            }
            if (in_array('rGreeting.short', $tokens['rGreeting'])) {
               $values[$cid]['rGreeting.short'] = $pref1 ." ". $lname1 ." & ". $pref2 ." ". $lname2;
            }
         }
      }
   }

   if (!empty($tokens['donations'])) {
      if (in_array('donations.ytd', $tokens['donations'])){

         $spouses = array();
         $contacts_and_spouses = $cids;
         $dao = &CRM_Core_DAO::executeQuery("
           SELECT contact_id_a, contact_id_b
           FROM civicrm_relationship
           WHERE relationship_type_id IN($relationship_type_spouse,$relationship_type_partner)
           AND is_active = 1
           AND (end_date IS NULL OR end_date > CURDATE())
           AND (contact_id_a IN ($contacts) OR contact_id_b IN ($contacts))
         ");
         while ($dao->fetch()) {
           if (!in_array($dao->contact_id_a, $contacts_and_spouses)) {
            $contacts_and_spouses[] = $dao->contact_id_a;
           }
           if (!in_array($dao->contact_id_b, $contacts_and_spouses)) {
            $contacts_and_spouses[] = $dao->contact_id_b;
           }
           if (in_array($dao->contact_id_a, $cids)) {
            $spouses[$dao->contact_id_b] = $dao->contact_id_a;
           }
           if (in_array($dao->contact_id_b, $cids)) {
            $spouses[$dao->contact_id_a] = $dao->contact_id_b;
           }
         }
         $contacts_and_spouses = implode(',', $contacts_and_spouses);


         $dao = &CRM_Core_DAO::executeQuery("
            SELECT cc.contact_id, cc.total_amount, cc.receive_date
            FROM civicrm_contribution cc
            INNER JOIN civicrm_contact con ON con.id = cc.contact_id
            WHERE cc.is_test = 0 AND cc.financial_type_id = 1 AND cc.contribution_status_id = 1
            AND cc.contact_id IN ($contacts_and_spouses)
            ORDER BY cc.receive_date");

         $header = '
            <table class="donations">
              <thead><tr>
               <th>Date</th>
               <th>Amount</th>
              </tr></thead>
              <tbody>';
         while ($dao->fetch()) {
            $cid = $dao->contact_id;
            $row = '
              <tr>
               <td>' . date('m/d/Y', strtotime($dao->receive_date)) . '</td>
               <td>$' . $dao->total_amount . '</td>
              </tr>';
            if (in_array($cid, $cids)) {
              $values[$cid]['donations.ytd'] = (!empty($values[$cid]['donations.ytd']) ? $values[$cid]['donations.ytd'] : $header) . $row;
            }
            if (isset($spouses[$cid])) {
              $values[$spouses[$cid]]['donations.ytd'] = (!empty($values[$spouses[$cid]]['donations.ytd']) ? $values[$spouses[$cid]]['donations.ytd'] : $header) . $row;
            }
         }
           foreach ($cids as $cid) {
            if (!empty($values[$cid]['donations.ytd'])) {
              $values[$cid]['donations.ytd'] .= '</tbody></table>';
            }
           }

      }
   }
}

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: Custom Token: Joint greeting and year to date donations
January 12, 2015, 03:00:37 pm
has the joint greeting issue already been covered by others eg https://civicrm.org/blogs/sgladstone/dealing-joint-greetings-part-2-code-provided
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

YossiCrm

  • I’m new here
  • *
  • Posts: 4
  • Karma: 0
  • CiviCRM version: 4.4.7
  • CMS version: Drupal 7
  • MySQL version: 5.5
  • PHP version: 5.3
Re: Custom Token: Joint greeting and year to date donations
January 22, 2015, 03:37:21 pm
Quote from: petednz on January 12, 2015, 03:00:37 pm
has the joint greeting issue already been covered by others eg https://civicrm.org/blogs/sgladstone/dealing-joint-greetings-part-2-code-provided

The link to the installation instructions doesn't work and I haven't been able to figure it out.
Also, would this work for civi 4.4?

Thanks

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: Custom Token: Joint greeting and year to date donations
January 22, 2015, 05:05:56 pm
maybe add a comment on the blog to that effect
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

YossiCrm

  • I’m new here
  • *
  • Posts: 4
  • Karma: 0
  • CiviCRM version: 4.4.7
  • CMS version: Drupal 7
  • MySQL version: 5.5
  • PHP version: 5.3
Re: Custom Token: Joint greeting and year to date donations
January 22, 2015, 09:21:38 pm
Thanks,
Hope it works out!  :)

ucHH

  • I’m new here
  • *
  • Posts: 9
  • Karma: 0
  • CiviCRM version: 4.5
  • CMS version: Drupal 7.34
  • MySQL version: 5.6
  • PHP version: 5.6.2
Re: Custom Token: Joint greeting and year to date donations
February 24, 2015, 07:01:30 am
Why not make it an extension and put in on GitHub? That way, providing you with help for your code would be much easier! :)

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using Core CiviCRM Functions (Moderator: Yashodha Chaku) »
  • Custom Token: Joint greeting and year to date donations

This forum was archived on 2017-11-26.