Commit 24e4a414 authored by parvez's avatar parvez
Browse files

Merge pull request #4 from rajeshrhino/master

Format Address lines based on rules in the Royal Mail programmers guide
parents 590433b4 e75dd51d
......@@ -86,15 +86,7 @@ class CRM_Civicrmpostcodelookup_Page_Civipostcode extends CRM_Core_Page {
$addressRow = array();
$AddressListItem = $simpleJSONData->results;
foreach ($AddressListItem as $key => $addressItem) {
$addressLineArray = array();
$addressLineArray[] = $addressItem->building_number;
$addressLineArray[] = $addressItem->organisation_name;
$addressLineArray[] = $addressItem->building_name;
$addressLineArray[] = $addressItem->sub_building_name;
$addressLineArray[] = $addressItem->thoroughfare_descriptor;
$addressLineArray[] = $addressItem->post_town;
$addressLineArray[] = $addressItem->postcode;
$addressLineArray = self::formatAddressLines($addressItem, TRUE);
$addressLineArray = array_filter($addressLineArray);
$addressRow["id"] = (string) $addressItem->id;
......@@ -140,40 +132,57 @@ class CRM_Civicrmpostcodelookup_Page_Civipostcode extends CRM_Core_Page {
$filetoparse = fopen("$querystring","r") or die("Error reading JSON data.");
$data = stream_get_contents($filetoparse);
$simpleJSONData = json_decode($data);
$addressObj = $simpleJSONData->results[0];
$address = array('id' => $moniker);
$addressItem = $simpleJSONData->results[0];
$addressLineArray = $addressLine1Array = array();
$address = self::formatAddressLines($addressObj);
##Close the JSON source##
fclose($filetoparse);
//$addressLineArray[] = (string) $addressItem->organisation_name;
//$addressLineArray[] = (string) $addressItem->building_name;
return $address;
}
if (!empty($addressItem->organisation_name)) {
$addressLineArray[] = (string) $addressItem->organisation_name;
} else {
$addressLineArray[] = (string) $addressItem->building_number;
$addressLineArray[] = (string) $addressItem->thoroughfare_descriptor;
private static function formatAddressLines($addressObj, $forList = FALSE) {
if (empty($addressObj)) {
return;
}
$addressLineArray = array_filter($addressLineArray);
$address["street"] = @implode(' ', $addressLineArray);
//$addressLine1Array[] = (string) $addressItem->sub_building_name;
if (!empty($addressItem->organisation_name)) {
$addressLine1Array[] = (string) $addressItem->building_number;
$addressLine1Array[] = (string) $addressItem->building_name;
$addressLine1Array[] = (string) $addressItem->thoroughfare_descriptor;
// Format address lines based on Royal Mail PAF address assembler (https://github.com/AllenJB/PafUtils)
require_once 'CRM/PafUtils/Address.php';
$addressLineObj = new Address();
$addressLineObj->setUdprn()
->setPostCode($addressObj->postcode)
->setPostTown($addressObj->post_town)
->setDependentLocality($addressObj->dependent_locality)
->setDoubleDependentLocality($addressObj->double_dependent_locality)
->setThoroughfare($addressObj->thoroughfare_descriptor)
->setDependentThoroughfare($addressObj->dependent_thoroughfare_descriptor)
->setBuildingNumber($addressObj->building_number)
->setBuildingName($addressObj->building_name)
->setSubBuildingName($addressObj->sub_building_name)
->setPoBox($addressObj->po_box)
->setDepartmentName($addressObj->department_name)
->setOrganizationName($addressObj->organisation_name)
->setPostcodeType($addressObj->postcode_type)
->setSuOrganizationIndicator($addressObj->su_organisation_indicator)
->setDeliveryPointSuffix($addressObj->delivery_point_suffix);
$addressLines = $addressLineObj->getAddressLines();
if ($forList == FALSE) {
$address = array('id' => $addressObj->id);
}
$addressLine1Array = array_filter($addressLine1Array);
$address["locality"] = @implode(' ', $addressLine1Array);
$address["town"] = (string) $addressItem->post_town;
$address["postcode"] = (string) $addressItem->postcode;
##Close the JSON source##
fclose($filetoparse);
if (!empty($addressLines[0])) {
$address["street_address"] = $addressLines[0];
}
if (!empty($addressLines[1])) {
$address["supplemental_address_1"] = $addressLines[1];
}
if (!empty($addressLines[2])) {
$address["supplemental_address_2"] = $addressLines[2];
}
$address["town"] = (string) $addressObj->post_town;
$address["postcode"] = (string) $addressObj->postcode;
return $address;
}
......
This diff is collapsed.
......@@ -19,8 +19,8 @@ For having postcode lookup feature in CiviCRM backend and Front end profiles.
### Usage ###
* For backend, postcode lookup features is automatically enabled for address fields when adding/editing contacts and configuring event location.
* For front end profiles, postcode lookup feature is enabled only if 'Street Address' field of type 'Primary' or 'Billing' is added to the profile.
* For front end profiles, postcode lookup feature is enabled only if 'Street Address' field of type 'Primary' or 'Billing' is added to the profile. Include 'Supplemental Address 1' and 'Supplemental Address 2' fields in the profile for address lines based on the rules in the Royal Mail programmers guide.
### Support ###
support (at) vedaconsulting.co.uk
\ No newline at end of file
support (at) vedaconsulting.co.uk
{literal}
<style type="text/css">
.ui-autocomplete { height: 200px; overflow-y: scroll; overflow-x: hidden;}
</style>
<script type="text/javascript">
cj(document).ready(function(){
if (cj('#editrow-street_address-Primary').length > 0 ) {
var blockId = 'Primary';
var blockNo = 'Primary';
var targetHtml = '';
var postCodeHtml = '<div class="crm-section addressLookup form-item"><div class="label"><label for="addressLookup">Search for an address</label></div><div class="edit-value content"><div class="postcodelookup-textbox-wrapper"><input placeholder="Start typing a postcode" name="inputPostCode_' + blockId + '" id ="inputPostCode_' + blockId + '"></div><div class="loader-image"><img id="loaderimage_' + blockId + '" src="{/literal}{$config->resourceBase}{literal}i/loading.gif" style="width:15px;height:15px; display: none" /></div></div><div class="clear"></div></div>';
var postCodeHtml = '<div class="crm-section addressLookup form-item"><div class="label"><label for="addressLookup">Search for an address</label></div><div class="edit-value content"><div class="postcodelookup-textbox-wrapper"><input placeholder="Start typing a postcode" name="inputPostCode_' + blockId + '" id ="inputPostCode_' + blockId + '" style="width: 25em;"></div><div class="loader-image"><img id="loaderimage_' + blockId + '" src="{/literal}{$config->resourceBase}{literal}i/loading.gif" style="width:15px;height:15px; display: none" /></div></div><div class="clear"></div></div>';
cj('#editrow-street_address-Primary').before(postCodeHtml);
}
......@@ -14,10 +17,23 @@ cj(document).ready(function(){
var blockNo = '5';
var targetHtml = '';
var divHtml = cj('#editrow-street_address-5').html();
var postCodeHtml = '<div class="crm-section addressLookup form-item"><div class="label"><label for="addressLookup">Search for an address</label></div><div class="edit-value content"><div class="postcodelookup-textbox-wrapper"><input placeholder="Start typing a postcode" name="inputPostCode_' + blockId + '" id ="inputPostCode_' + blockId + '"></div><div class="loader-image"><img id="loaderimage_' + blockId + '" src="{/literal}{$config->resourceBase}{literal}i/loading.gif" style="width:15px;height:15px; display: none" /></div></div><div class="clear"></div></div>';
var postCodeHtml = '<div class="crm-section addressLookup form-item"><div class="label"><label for="addressLookup">Search for an address</label></div><div class="edit-value content"><div class="postcodelookup-textbox-wrapper"><input placeholder="Start typing a postcode" name="inputPostCode_' + blockId + '" id ="inputPostCode_' + blockId + '" style="width: 25em;"></div><div class="loader-image"><img id="loaderimage_' + blockId + '" src="{/literal}{$config->resourceBase}{literal}i/loading.gif" style="width:15px;height:15px; display: none" /></div></div><div class="clear"></div></div>';
cj('#editrow-street_address-5').before(postCodeHtml);
}
// Include lookup in billing section as well
if (cj('#billing_street_address-5').length > 0 ) {
var billingblockId = '5';
var billingblockNo = '5';
var billingtargetHtml = '';
var billingdivHtml = cj('#billing_street_address-5').html();
var billingpostCodeHtml = '<div class="crm-section addressLookup form-item"><div class="label"><label for="addressLookup">Search for an address</label></div><div class="edit-value content"><div class="postcodelookup-textbox-wrapper"><input placeholder="Start typing a postcode" name="inputPostCodeBillingSection_' + billingblockId + '" id ="inputPostCodeBillingSection_' + billingblockId + '" style="width: 25em;"></div><div class="loader-image"><img id="loaderimage_' + billingblockId + '" src="{/literal}{$config->resourceBase}{literal}i/loading.gif" style="width:15px;height:15px; display: none" /></div></div><div class="clear"></div></div>';
cj('.billing_street_address-5-section').before(billingpostCodeHtml);
var billingPostcodeElement = '#inputPostCodeBillingSection_'+billingblockNo;
}
var buttonElement = '#postcodeLookupButton_'+blockNo;
var houseElement = '#inputNumber_'+blockNo;
var postcodeElement = '#inputPostCode_'+blockNo;
......@@ -52,11 +68,35 @@ cj(document).ready(function(){
}
}
}).result(function(event, data, formatted) {
findAddressValues(data[1], blockNo);
findAddressValues(data[1], blockNo, blockPrefix = '');
cj(postcodeElement).val('');
return false;
});
// Postcode lookup in billing section
if (cj('#billing_street_address-5').length > 0 ) {
cj( billingPostcodeElement ).autocomplete( sourceUrl, {
width: 400,
selectFirst: false,
minChars: minCharacters,
matchContains: true,
delay: 400,
max: 1000,
extraParams:{
term:function () {
return cj( billingPostcodeElement ).val();
},
number:function () {
return cj(houseElement).val();
}
}
}).result(function(event, data, formatted) {
findAddressValues(data[1], '5', blockPrefix = 'billing_');
cj(billingPostcodeElement).val('');
return false;
});
}
{/literal}{else}{literal}
cj(postcodeElement).autocomplete({
......@@ -64,15 +104,15 @@ cj(document).ready(function(){
minLength: minCharacters,
data: {postcode: cj( postcodeElement ).val(), number: cj(houseElement).val(), mode: '0'},
search: function( event, ui ) {
cj('#loaderimage_'+blockNo).show();
//cj('#loaderimage_'+blockNo).show();
},
response: function( event, ui ) {
cj('#loaderimage_'+blockNo).hide();
//cj('#loaderimage_'+blockNo).hide();
},
select: function(event, ui) {
if (ui.item.id != '') {
findAddressValues(ui.item.id, blockNo);
cj('#loaderimage_'+blockNo).show();
findAddressValues(ui.item.id, blockNo, blockPrefix = '');
//cj('#loaderimage_'+blockNo).show();
}
return false;
},
......@@ -85,33 +125,62 @@ cj(document).ready(function(){
}
});
// Postcode lookup in billing section
if (cj('#billing_street_address-5').length > 0 ) {
cj(billingPostcodeElement).autocomplete({
source: sourceUrl,
minLength: minCharacters,
data: {postcode: cj( billingPostcodeElement ).val(), number: cj(houseElement).val(), mode: '0'},
search: function( event, ui ) {
//cj('#loaderimage_'+blockNo).show();
},
response: function( event, ui ) {
//cj('#loaderimage_'+blockNo).hide();
},
select: function(event, ui) {
if (ui.item.id != '') {
findAddressValues(ui.item.id, '5', blockPrefix = 'billing_');
//cj('#loaderimage_'+blockNo).show();
}
return false;
},
html: true, // optional (jquery.ui.autocomplete.html.js required)
//optional (if other layers overlap autocomplete list)
open: function(event, ui) {
cj(".ui-autocomplete").css("z-index", 1000);
}
});
}
{/literal}{/if}{literal}
});
});
function findAddressValues(id , blockNo) {
function findAddressValues(id , blockNo, blockPrefix) {
cj('#loaderimage_'+blockNo).show();
setAddressFields(false, blockNo);
setAddressFields(false, blockNo, blockPrefix);
var sourceUrl = CRM.url('civicrm/{/literal}{$config->CiviPostCodeLookupProvider}{literal}/ajax/get', {"json": 1});
cj.ajax({
dataType: 'json',
data: {id: id, mode: '0'},
url: sourceUrl,
success: function (data) {
setAddressFields(data.address, blockNo);
setAddressFields(true, blockNo);
setAddressFields(data.address, blockNo, blockPrefix);
setAddressFields(true, blockNo, blockPrefix);
cj('#loaderimage_'+blockNo).hide();
}
});
}
function setAddressFields(address, blockNo) {
var postcodeElement = '#postal_code-'+ blockNo;
var streetAddressElement = '#street_address-'+ blockNo;
var AddstreetAddressElement = '#supplemental_address_1-'+ blockNo;
var AddstreetAddressElement1 = '#supplemental_address_2-'+ blockNo;
var cityElement = '#city-'+ blockNo;
function setAddressFields(address, blockNo, blockPrefix) {
var postcodeElement = '#' + blockPrefix + 'postal_code-'+ blockNo;
var streetAddressElement = '#' + blockPrefix + 'street_address-'+ blockNo;
var AddstreetAddressElement = '#' + blockPrefix + 'supplemental_address_1-'+ blockNo;
var AddstreetAddressElement1 = '#' + blockPrefix + 'supplemental_address_2-'+ blockNo;
var cityElement = '#' + blockPrefix + 'city-'+ blockNo;
var allFields = {
postcode: postcodeElement,
......@@ -138,8 +207,9 @@ function setAddressFields(address, blockNo) {
cj(cityElement).val('');
cj(postcodeElement).val('');
cj(streetAddressElement).val(address.street);
cj(AddstreetAddressElement).val(address.locality);
cj(streetAddressElement).val(address.street_address);
cj(AddstreetAddressElement).val(address.supplemental_address_1);
cj(AddstreetAddressElement1).val(address.supplemental_address_2);
cj(cityElement).val(address.town);
cj(postcodeElement).val(address.postcode);
}
......
......@@ -33,6 +33,9 @@
</tr>
{literal}
<style type="text/css">
.ui-autocomplete { height: 200px; overflow-y: scroll; overflow-x: hidden;}
</style>
<script type="text/javascript">
var info = []; //Needed to access data from outside the JSON processing function
cj(document).ready(function() {
......@@ -84,15 +87,15 @@ cj(document).ready(function() {
data: {postcode: cj( postcodeElement ).val(), number: cj(houseElement).val(), mode: '0'},
//max: {/literal}{crmSetting name="search_autocomplete_count" group="Search Preferences"}{literal},
search: function( event, ui ) {
cj('#loaderimage_'+blockNo).show();
//cj('#loaderimage_'+blockNo).show();
},
response: function( event, ui ) {
cj('#loaderimage_'+blockNo).hide();
//cj('#loaderimage_'+blockNo).hide();
},
select: function(event, ui) {
if (ui.item.id != '') {
findAddressValues(ui.item.id, blockNo);
cj('#loaderimage_'+blockNo).show();
//cj('#loaderimage_'+blockNo).show();
}
return false;
},
......@@ -162,8 +165,9 @@ function setAddressFields(address, blockNo) {
cj(cityElement).val('');
cj(postcodeElement).val('');
cj(streetAddressElement).val(address.street);
cj(AddstreetAddressElement).val(address.locality);
cj(streetAddressElement).val(address.street_address);
cj(AddstreetAddressElement).val(address.supplemental_address_1);
cj(AddstreetAddressElement1).val(address.supplemental_address_2);
cj(cityElement).val(address.town);
cj(postcodeElement).val(address.postcode);
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment