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) »
  • Multiple names to one address on mailing labels
Pages: [1]

Author Topic: Multiple names to one address on mailing labels  (Read 6149 times)

mlampard

  • I post occasionally
  • **
  • Posts: 42
  • Karma: 3
Multiple names to one address on mailing labels
June 09, 2008, 05:44:08 pm
This references a post to the Feature Requests and Suggestions forum (http://forum.civicrm.org/index.php/topic,3573.0.html). I moved it here, as I suspect that there will be a bit of back and forth with the code.

I have written the code that checks for duplicate addresses in a search results list and only prints one mailing label per unique address (with multiple names listed). A few points worth noting:
1. "Use household name" must be in use. If it isn't, there's no real way of ensuring duplication - e.g., "626 Stewart Road" and "626 Stewart Rd" will NOT match!
2. As "Use household name" is assumed, I have used a case sensitive array_search function to check for duplicate addresses. If at some point we want to do duplicate checking on non-household addresses, then we'll need to change this to a case insensitive method.
3. The mailing label format must include {contact_name} in Administer Civicrm/Global Settings/Address Settings (it's not there by default), otherwise only the address gets printed
4. This was put together rather quickly, for a need with a client. I know it can be written in a more CiviCRM "compliant" way. For example, the reference to an input field directly from Label.tpl probably needs re-work. Also catching the value of $_POST['uniqueaddress'] probably needs to go elsewhere within the framework. I work almost exclusively with zend's MVC framework, so haven't quite wrapped my head around where everything is in CiviCRM yet!

All that aside, the following works great for us:
Three files to update (I am working on a Drupal install):
1. sites/all/modules/civicrm/CRM/Utils/Address.php
2. sites/all/modules/civicrm/templates/CRM/Contact/Form/Task/Label.tpl
3. sites/all/modules/civicrm/CRM/Contact/Form/Task/Label.php

Here's the code and locations of change - I have included some surrounding code, where necessary, so it's more obvious where it goes:

<!-- ML - insert this at line 10 of sites/all/modules/civicrm/templates/CRM/Contact/Form/Task/Label.tpl
          The following line displays the radio button to determine if only one label should be
          printed per unique address -->
<dt>One label per address?</dt><dd><input type="radio" name="uniqueaddress" value="yes">Yes<br><input type="radio" name="uniqueaddress" value="no">No</dd>
<!-- ML - end -->



// ML - Added the uniqueAddresses flag to the function header in:
//      sites/all/modules/civicrm/CRM/Utils/Address.php
    static function format($fields, $format = null, $microformat = false, $mailing = false, $individualFormat = false, $uniqueAddresses = false )
    {
// ML - end


// ML - The next few lines have been inserted into:
//      sites/all/modules/civicrm/CRM/Utils/Address.php at line 93
//      to handle multiple names to one address
            if ( $type == 'Individual' ) {
                $format = CRM_Core_BAO_Preferences::value( 'individual_name_format' );
                if ($uniqueAddresses)
                  $contactName = CRM_Utils_Array::value( 'display_name', $fields );
                else

                  $contactName = self::format($fields, $format, null, null, true);
// ML - end


// ML - insert this at line 154 of sites/all/modules/civicrm/CRM/Contact/Form/Task/Label.php
//      This tests if the radio button to check for unique addresses has been set
//      if so, it parses the search results and removes duplicate addresses. Before removing,
//      it takes the deisplay_name and pre-pends it to the display_name of the found record
if ($this->getUniqueFlag())
{
  $rows = $this->parseDuplicateAddresses( $rows );
  $uniqueAddresses = TRUE; // this is for the CRM_Utils_Address::format method
}


        // format the addresses according to CIVICRM_ADDRESS_FORMAT (CRM-1327)
        require_once 'CRM/Utils/Address.php';
        foreach ($rows as $id => $row) {
            $row['id'] = $id;

//      the line below gets replaced with the new line that has extra function parameters
//      line 170 odd of sites/all/modules/civicrm/CRM/Contact/Form/Task/Label.php
            // $formatted = CRM_Utils_Address::format( $row, null, null, true );
            $formatted = CRM_Utils_Address::format( $row, null, null, true, false, $uniqueAddresses );


            $rows[$id]= array( $formatted );
        }
// ML - end


// ML - The following functions support mailing address label uniqueness and are at the bottom of:
//      sites/all/modules/civicrm/CRM/Contact/Form/Task/Label.php
//      Ensure they remain within the class scope.

      // simply checks if the uniqueaddress radio button was set to yes and returns true or false
    function getUniqueFlag()
    {
        if ($_POST['uniqueaddress'] == "yes")
          return true;
        else
          return false;
    } // end getUniqueFlag function

      // checks for duplicate addresses (case sensitive) and returns an array without duplicates
      // Any found duplicate has the display_name extracted and appended to the display_name
      // of it's match. The array passed in is multi-dimensional and passed by reference so
      // it doesn't allocate more memory to variable space

      // This really assumes that "Use household address" has been checked!
    function parseDuplicateAddresses( &$contactRows )
    {
          // initialise arrays
        $retArr = array();
        $uniqueArr = array();

          // parse the search results and format the return array
        foreach ($contactRows as $key => $val)
        {
          $uniqueAddress = $val["street_address"] . $val["city"] . $val["state_province"] . $val["postal_code"] . $val["country"];

            // see if the above string matches an already parsed result
          $offset = array_search($uniqueAddress,$uniqueArr);

            // no match, so add to return array and unique array
          if ($offset === false)
          {
            $uniqueArr[$key] = $uniqueAddress;
            $retArr[$key] = $val;
          }

            // Match! Prepend display_name to the end of the matched result and throw the rest away!
          else
          {
            $retArr[$offset]["display_name"] .= "\n".$val["display_name"];
          } // end else
        } // end foreach

        // now return the newly formatted array
      return $retArr;
    } // end parseDuplicateAddresses function

// ML - end





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: Multiple names to one address on mailing labels
June 09, 2008, 05:57:00 pm

ML:

Awesome, thanx for doing this. Can i ask you to do a couple more things:

1. Can you please file an issue for this on the issue tracker (http://issues.civicrm.org/)

2. Can you please generate the below as a patch and attach it to the issue. Makes it much easier to integrate patches. We'll CiviCRM'ize it along the way

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

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: Multiple names to one address on mailing labels
June 09, 2008, 11:28:04 pm
Will be keen to give this a whirl. As chris mentioned on the other post we had a more manual work around to generate an export we could use for a large mailout.

Both systems require 'use household address'. (and chris has posted a script here http://forum.civicrm.org/index.php/topic,2735.0.html that we used to help us build households from individuals - though it missed lots but mostly because addresses weren't identical i expect)

Anyone with a 'is household member of' relationship is put in a smart group.

We can then grab all the 'individuals' we want to mail to, create a group, remove the above 'smart group' members, so we end up with one group of individuals, the other of householders.

We then just have to return a search for both and can export a single list. (Sounds easier than it felt as we had other issues to try and tackle in the same process).
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

mlampard

  • I post occasionally
  • **
  • Posts: 42
  • Karma: 3
Re: Multiple names to one address on mailing labels
June 10, 2008, 12:07:04 pm
OK, patch files and instructions are here:
http://issues.civicrm.org/jira/browse/CRM-3195

Good luck - email me if you have issues running.

Cheers,
Marty.

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • Multiple names to one address on mailing labels

This forum was archived on 2017-11-26.