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) »
  • Auto-populating Address Fields
Pages: [1]

Author Topic: Auto-populating Address Fields  (Read 2228 times)

MKorostoff

  • Guest
Auto-populating Address Fields
July 12, 2009, 12:21:03 pm
Hello Friends,

I work for a political group that does a good deal of canvasing.  We need to store our addresses with separate House #s and Street Names so that we can properly sort walk on a street by street basis.  For that purpose, the single string street address field currently employed by civiCRM is insufficient.

Until now, we've been using custom fields for house number and street name.  We would like to start using the elegant built in geocoding, which means we have to have the address fields filled in.  What I would like to is set the default address field to hook-in to the custom fields so that its value would be something to the effect of:

Code: [Select]
$Street_Address = $custom_house_number . " " . $customm_street_name . " " . "Apt " . $custom_apartment_number
But I can't for the life of me figure out where these values are stored.  Someone on another thread suggested that the better way to go would be a parsing script which pulls out the values from a single line of text.  We played around with that a bit, but ultimately decided that it was just not reliable enough.  So I'm here seeking advice on how I might populate the address fields from my custom fields.  In exchange, I've included the parsing script I wrote, that comes very close to producing the effect we want--maybe it will be useful to someone here.

Code: [Select]
<html>
<form action="Address Parcing003.php" method="post">
Address:</br>
<input type="text" name="Address" /><br />
<input type="submit" value="Submit" />
</form>
<?php
/*
This script parses humun written Street addresses into a standardized form as follows: House#, Apt#, Street Name.
It should sill be accompanied by instrucitions to the user as to formatting, because it has only limited capabilities.
It is mostly intended for correcting for human error in data entry.
*/

//First we set an array of regex search patterns.
$string = $_POST ['Address'];
$preg = array ('/,/','/\./', //Strips commas and period.
'/(\d{1,})\s(.{3,})\s(RD|CT|ST|CIR|ST|AVE|BLVD|SQ|LN|PL|road|Court|Street|Circle|Avenue|boulevard|Square|Lane|Place|Way)\s(NORTH|SOUTH|EAST|WEST|N|S|E|W)\s(APT|APARTMENT|#|SUITE|SUIT|ROOM|RM)\s?(.{1,})/mis',
//finds addresses of the format "21 Robinson Street West Apt 2B" and converts to "21, 2B, Robinson Street West".  Understands "Apt", "Apartment" "#", "Suite", "Suit" (a common missspelling), "Room", and "Rm" to be equivalent.
'/(\d{1,})\s(.{3,})\s(RD|CT|ST|CIR|ST|AVE|BLVD|SQ|LN|PL|road|Court|Street|Circle|Avenue|boulevard|Square|Lane|Place|Way)\s(APT|APARTMENT|#|SUITE|SUIT|ROOM|RM)\s?(.{1,})/mis',
//same as above, but without the North/South/East/West capabilities.  This is the standard USPS address format.
'/(\d{1,})\s(.{3,})\s(RD|CT|ST|CIR|ST|AVE|BLVD|SQ|LN|PL|road|Court|Street|Circle|Avenue|boulevard|Square|Lane|Place|Way)\s(.{1,3})/mis',
//finds addresses of the format "21 Robinson St 2B".  Apartment number is capped at 3 characters to prevent an error on street names containing common street suffixes (i.e. 21 Way High Road).  This cap does not exist if Apt number is precedent by the letters "Apt".
'/(\d{1,})\s(.{3,})\s(RD|CT|ST|CIR|ST|AVE|BLVD|SQ|LN|PL|road|Court|Street|Circle|Avenue|boulevard|Square|Lane|Place|Way)\s(NORTH|SOUTH|EAST|WEST|N|S|E|W)\s(.{1,3})/mis',
//same as pattern #1 but without the need for the "Apt" label.  Max apt number characters is again 3.
'/(\d{1,})\s(.{3,})\s(NORTH|SOUTH|EAST|WEST|N|S|E|W)\s(APT|APARTMENT|#|SUITE|SUIT|ROOM|RM)\s?(.{1,})/mis',
//I sort of forget what this one does.
'/(\d{1,})\s(.{3,})\s(APT|APARTMENT|#|SUITE|SUIT|ROOM|RM)\s?(.{1,})/mis',
//Finds addresses of the format "21 hartwell apt. 2"
'/(\d{1,})\s(.{1,2})\s/mis',
//finds addresses of the format "22 C Duke Street"
'/(\d{1,})(\s\w{1,})/mis',
//A catchall that finds the classic "25 Hartwell".  Also catches addresses of the format "66 apt. 5 Hartwell St" but not "66 apt. B Hartwell"
'/(^\d{1,})(\w{1,2})(\s\w{1,})/mis'
//Finds 2B Robinson.
);
$replace = array ('','',
'\1, \6, \2 \3 \4',
'\1, \5, \2 \3',
'\1, \4, \2 \3',
'\1, \5, \2 \3 \4',
'\1, \5, \2 \3',
'\1, \4, \2',
'\1, \2, ',
'\1,\2',
'\1, \2,\3'
);
$comma_delim = preg_replace($preg, $replace, $string);
echo 
$comma_delim;
/*

############################################################################
This next part is supposed to split the standardized comma delimited form which we created above and distribute the different values accross an array.  
It currently does not work and in fact breaks the rest of the script, so it is commented out for the time being.
############################################################################

if preg_match('/.{1,},.{1,},/', $comma_delim)
{
$address_array = preg_split(',', $comma_delim);
echo House Number: $address_array[0]</br>Street Name: $address_array[2]</br>Apartment: $address_array[1];
}

################################
If this part worked as designed it would take the values represented by 1, 2, and 3 in the regex replace strings and put them into an array called "address_array" where
 "$address_array[0]" = the house #, "$address_array[1]" = the apt number, etc.  These could then, of course, be assigned to their own variables "$House_Number", "$Apt_Number", 
and "$Street_Name"
################################

*/
?>

</html>

Test version here:
http://eden.rutgers.edu/~mattkoro/Address%20Parcing003.php
« Last Edit: July 12, 2009, 02:13:05 pm by MKorostoff »

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: Auto-populating Address Fields
July 12, 2009, 07:57:36 pm

the custom fields are stored in a new table, check your db for newly created tables of the form: civicrm_value_*

the custom field names will be the column names.

Not sure how/where/when you are getting this information. might be easier to discuss this on IRC

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

MKorostoff

  • Guest
Re: Auto-populating Address Fields
July 13, 2009, 07:29:11 am
I'm sorry if I was unclear before, but my ultimate question is actually very simple.  Once I've written the PHP to auto populate the address fields, where do I put it?

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: Auto-populating Address Fields
July 13, 2009, 08:40:21 am

i would put it in the contact edit/create POST hook and also the places where u update the street # etc (not sure how/where u do this). so maybe a the custom hook also

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

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • Auto-populating Address Fields

This forum was archived on 2017-11-26.