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 Profiles (Moderator: Dave Greenberg) »
  • Specify Primary Address in Profile
Pages: [1]

Author Topic: Specify Primary Address in Profile  (Read 707 times)

jaymcgraw

  • I post occasionally
  • **
  • Posts: 106
  • Karma: 6
  • CiviCRM version: 4.1.2
  • CMS version: Drupal 7.12
  • MySQL version: 5.0.92-community
  • PHP version: 5.2.11
Specify Primary Address in Profile
December 29, 2010, 02:48:51 pm
We need to collect both a business and home address from constituents. I've exposed both of these addresses in a profile. I'm not finding a way, however, to allow for the constituent to specify which of these addresses is their primary address (specifically, for mailing). Anyone know of a way to do this? Thanks!

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: Specify Primary Address in Profile
December 29, 2010, 06:19:23 pm

you will need to do this via hooks and customized templates

1. expose a radio button asking for which is primary: home/work. customize the template to show this radio button

2. implement the postProcess hook to mark that location as is_primary for that contact

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

jaymcgraw

  • I post occasionally
  • **
  • Posts: 106
  • Karma: 6
  • CiviCRM version: 4.1.2
  • CMS version: Drupal 7.12
  • MySQL version: 5.0.92-community
  • PHP version: 5.2.11
Re: Specify Primary Address in Profile
January 05, 2011, 07:25:08 am
Thanks much for the quick response! I'm looking into this now.

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: Specify Primary Address in Profile
January 31, 2012, 11:21:08 am
Outr of Lobos 2 above, here is a part of a solution:
Code: [Select]
<?php 
function customModule_civicrm_postProcess($formName, &$form ){
  if ( 
is_a( $form, 'CRM_Profile_Form_Edit' ) ) {
     
$gid = $form->getVar( '_gid' );
     
$cid = $form->getVar( '_id');        
     switch (
$gid){
       case 
'32':      // 32 is the Profile ID
         
customModule_civicrm_contact_primary_address($cid , 4);  // 4 = 'Address type ID' that you want to make the primary address when this profile form is submitted

       
break;
     }
  }
} 
?>



And here is the function that updates $cid 's primary address to $address_type_id :

Code: [Select]
<?php

function customModule_civicrm_contact_primary_address($cid , $address_type_id){
  
civicrm_initialize(true);
  include_once(
'api/api.php');
  
$params = array( 'version' => 3, 
    
'contact_id' => $cid 
    
);
  
$existing = civicrm_api('Address','get',$params);

  foreach ( 
$existing['values'] as $location ){
    if (
$location['location_type_id'] == $address_type_id){
      foreach (
$existing['values'] as $address_id => $address){
        
$is_primary = ($address['location_type_id'] == $address_type_id) ? 1 : 0;

        
$results = civicrm_api('Address','update', array ('version' => 3, 'id'=>$address_id, 'is_primary' => $is_primary));
        
$message = ($results['error'] == 0)? "Address ID: ".$address_id. " updated successfully" : "Error updating address: ".$results['error_message'];
        
watchdog('customModule', $message, NULL, WATCHDOG_WARNING);
      }
    }
  }
}
?>

This takes care to change the existing primary address type to be not primary, and set the newest address to be the primary one.

There is an extra foreach (location ... ) loop in there, in case this gets called to set an address_type_id to be the primary one that isn't set for that contact. That would be bad, it would unset the existing primary address without setting a new one.

In our usage, we just needed to set a contacts primary address to be such and such after they submitted it via a profile form. It's the only address shown on that profile form.

Adding options to the Profile form would be one more step, and this would be done. If I do that, I will post it back here.
« Last Edit: February 15, 2012, 12:34:46 pm by jimurl »

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using Profiles (Moderator: Dave Greenberg) »
  • Specify Primary Address in Profile

This forum was archived on 2017-11-26.