Commit 41ce81fe authored by parvez's avatar parvez Committed by GitHub
Browse files

Merge pull request #21 from mattwire/mjwconsulting

Format postcode before searching
parents 918accb1 44ca4903
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
require_once 'CRM/Core/Page.php'; require_once 'CRM/Core/Page.php';
class CRM_Civicrmpostcodelookup_Page_Afd extends CRM_Core_Page { class CRM_Civicrmpostcodelookup_Page_Afd extends CRM_Civicrmpostcodelookup_Page_Postcode {
/* /*
* Function to get the Server URL and login credentials * Function to get the Server URL and login credentials
...@@ -41,7 +41,7 @@ class CRM_Civicrmpostcodelookup_Page_Afd extends CRM_Core_Page { ...@@ -41,7 +41,7 @@ class CRM_Civicrmpostcodelookup_Page_Afd extends CRM_Core_Page {
* Function to get address list based on a Post code * Function to get address list based on a Post code
*/ */
public static function search() { public static function search() {
$postcode = CRM_Utils_Request::retrieve('term', 'String', $this, true); $postcode = self::getPostcode(TRUE); // FIXME: Check whether API requires space or not
$number = CRM_Utils_Request::retrieve('number', 'String', $this, false); $number = CRM_Utils_Request::retrieve('number', 'String', $this, false);
$querystring = self::getAFDCredentials(1); $querystring = self::getAFDCredentials(1);
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
require_once 'CRM/Core/Page.php'; require_once 'CRM/Core/Page.php';
class CRM_Civicrmpostcodelookup_Page_Civipostcode extends CRM_Core_Page { class CRM_Civicrmpostcodelookup_Page_Civipostcode extends CRM_Civicrmpostcodelookup_Page_Postcode {
/* /*
* Function to get the Server URL and login credentials * Function to get the Server URL and login credentials
...@@ -40,7 +40,7 @@ class CRM_Civicrmpostcodelookup_Page_Civipostcode extends CRM_Core_Page { ...@@ -40,7 +40,7 @@ class CRM_Civicrmpostcodelookup_Page_Civipostcode extends CRM_Core_Page {
* Function to get address list based on a Post code * Function to get address list based on a Post code
*/ */
public static function search() { public static function search() {
$postcode = CRM_Utils_Request::retrieve('term', 'String', $this, true); $postcode = self::getPostcode(TRUE);
$number = CRM_Utils_Request::retrieve('number', 'String', $this, false); $number = CRM_Utils_Request::retrieve('number', 'String', $this, false);
$querystring = self::getCivipostcodeCredentials(1); $querystring = self::getCivipostcodeCredentials(1);
......
...@@ -5,7 +5,7 @@ require_once 'CRM/Core/Page.php'; ...@@ -5,7 +5,7 @@ require_once 'CRM/Core/Page.php';
// Access the QAS library via the dependency 'llr_qas_library' module // Access the QAS library via the dependency 'llr_qas_library' module
require_once '/lib/QASCapture.php'; require_once '/lib/QASCapture.php';
class CRM_PostcodeLookup_Page_Ajax extends CRM_Core_Page { class CRM_PostcodeLookup_Page_Ajax extends CRM_Civicrmpostcodelookup_Page_Postcode {
static private $qacampture; static private $qacampture;
public static function getQasCredentials($account_type) { public static function getQasCredentials($account_type) {
...@@ -43,7 +43,7 @@ class CRM_PostcodeLookup_Page_Ajax extends CRM_Core_Page { ...@@ -43,7 +43,7 @@ class CRM_PostcodeLookup_Page_Ajax extends CRM_Core_Page {
} }
public static function search() { public static function search() {
$postcode = CRM_Utils_Request::retrieve('term', 'String', $this, true); $postcode = self::getPostcode(TRUE); // FIXME: Check whether API requires space or not
$number = CRM_Utils_Request::retrieve('number', 'String', $this, true); $number = CRM_Utils_Request::retrieve('number', 'String', $this, true);
$qaCapture = self::getQACapture(); $qaCapture = self::getQACapture();
......
<?php
require_once 'CRM/Core/Page.php';
abstract class CRM_Civicrmpostcodelookup_Page_Postcode extends CRM_Core_Page {
/**
* Get the postcode from the submitted values, with or without space.
* @param bool $space
*
* @return string
*/
protected static function getPostcode($space = FALSE) {
$postcode = CRM_Utils_Request::retrieve('term', 'String', $this, true);
return self::format($postcode, $space);
}
/**
* Format a UK postcode so it has a space before the last digit, or doesn't if $space is FALSE
* @param $postcode
* @param bool $space
*
* @return string
*/
protected static function format($postcode, $space) {
// Strip non-alpha characters
$postcode = preg_replace('/\W/', '', $postcode);
if (strlen($postcode) > 4) {
($space) ? $spacerChar = ' ' : $spacerChar = '';
return preg_replace('/^(.*)(\d)(.*)/', "$1{$spacerChar}$2$3", $postcode);
}
return $postcode;
}
}
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
require_once 'CRM/Core/Page.php'; require_once 'CRM/Core/Page.php';
class CRM_Civicrmpostcodelookup_Page_PostcodeAnywhere extends CRM_Core_Page { class CRM_Civicrmpostcodelookup_Page_PostcodeAnywhere extends CRM_Civicrmpostcodelookup_Page_Postcode {
/* /*
* Function to get the Server URL and login credentials * Function to get the Server URL and login credentials
...@@ -41,7 +41,8 @@ class CRM_Civicrmpostcodelookup_Page_PostcodeAnywhere extends CRM_Core_Page { ...@@ -41,7 +41,8 @@ class CRM_Civicrmpostcodelookup_Page_PostcodeAnywhere extends CRM_Core_Page {
* Function to get address list based on a Post code * Function to get address list based on a Post code
*/ */
public static function search() { public static function search() {
$postcode = CRM_Utils_Request::retrieve('term', 'String', $this, true); // PostcodeAnywhere API works with postcodes when they have a space and when they don't.
$postcode = self::getPostcode();
$querystring = self::getPostcodeAnywhereCredentials(1); $querystring = self::getPostcodeAnywhereCredentials(1);
$querystring = $querystring . "&SearchTerm=" . urlencode($postcode); $querystring = $querystring . "&SearchTerm=" . urlencode($postcode);
......
...@@ -16,6 +16,9 @@ For having postcode lookup feature in CiviCRM backend and Front end profiles. ...@@ -16,6 +16,9 @@ For having postcode lookup feature in CiviCRM backend and Front end profiles.
* Install the extension manually in CiviCRM. More details [here](http://wiki.civicrm.org/confluence/display/CRMDOC/Extensions#Extensions-Installinganewextension) about installing extensions in CiviCRM. * Install the extension manually in CiviCRM. More details [here](http://wiki.civicrm.org/confluence/display/CRMDOC/Extensions#Extensions-Installinganewextension) about installing extensions in CiviCRM.
* Configure postcode lookup provider details in Administer >> Postcode Lookup(civicrm/postcodelookup/settings?reset=1) * Configure postcode lookup provider details in Administer >> Postcode Lookup(civicrm/postcodelookup/settings?reset=1)
#### Integration with Drupal Webform
This drupal module provides integration with Drupal Webform: https://github.com/compucorp/webform_civicrm_postcode
### Usage ### ### Usage ###
* For backend, postcode lookup features is automatically enabled for address fields when adding/editing contacts and configuring event location. * For backend, postcode lookup features is automatically enabled for address fields when adding/editing contacts and configuring event location.
......
...@@ -14,8 +14,8 @@ ...@@ -14,8 +14,8 @@
<url desc="Support">https://github.com/veda-consulting/uk.co.vedaconsulting.module.civicrmpostcodelookup</url> <url desc="Support">https://github.com/veda-consulting/uk.co.vedaconsulting.module.civicrmpostcodelookup</url>
<url desc="Licensing">http://www.gnu.org/licenses/agpl-3.0.html</url> <url desc="Licensing">http://www.gnu.org/licenses/agpl-3.0.html</url>
</urls> </urls>
<releaseDate>2017-09-18</releaseDate> <releaseDate>2017-10-14</releaseDate>
<version>1.4</version> <version>1.5</version>
<develStage>stable</develStage> <develStage>stable</develStage>
<compatibility> <compatibility>
<ver>4.4</ver> <ver>4.4</ver>
......
...@@ -55,6 +55,7 @@ cj(document).ready(function(){ ...@@ -55,6 +55,7 @@ cj(document).ready(function(){
var addressResultElement = '#addressResult_'+blockNo; var addressResultElement = '#addressResult_'+blockNo;
var addressResultsElement = '#addressResults_'+blockNo; var addressResultsElement = '#addressResults_'+blockNo;
var minCharacters = 4; var minCharacters = 4;
var delay = 200;
var postcodeProvider = '{/literal}{$civiPostCodeLookupProvider}{literal}'; var postcodeProvider = '{/literal}{$civiPostCodeLookupProvider}{literal}';
if (postcodeProvider !== 'civipostcode') { if (postcodeProvider !== 'civipostcode') {
...@@ -72,7 +73,7 @@ cj(document).ready(function(){ ...@@ -72,7 +73,7 @@ cj(document).ready(function(){
selectFirst: false, selectFirst: false,
minChars: minCharacters, minChars: minCharacters,
matchContains: true, matchContains: true,
delay: 400, delay: delay,
max: 1000, max: 1000,
extraParams:{ extraParams:{
term:function () { term:function () {
...@@ -95,7 +96,7 @@ cj(document).ready(function(){ ...@@ -95,7 +96,7 @@ cj(document).ready(function(){
selectFirst: false, selectFirst: false,
minChars: minCharacters, minChars: minCharacters,
matchContains: true, matchContains: true,
delay: 400, delay: delay,
max: 1000, max: 1000,
extraParams:{ extraParams:{
term:function () { term:function () {
...@@ -119,15 +120,15 @@ cj(document).ready(function(){ ...@@ -119,15 +120,15 @@ cj(document).ready(function(){
minLength: minCharacters, minLength: minCharacters,
data: {postcode: cj( postcodeElement ).val(), number: cj(houseElement).val(), mode: '0'}, data: {postcode: cj( postcodeElement ).val(), number: cj(houseElement).val(), mode: '0'},
search: function( event, ui ) { search: function( event, ui ) {
//cj('#loaderimage_'+blockNo).show(); cj('#loaderimage_'+blockNo).show();
}, },
response: function( event, ui ) { response: function( event, ui ) {
//cj('#loaderimage_'+blockNo).hide(); cj('#loaderimage_'+blockNo).hide();
}, },
select: function(event, ui) { select: function(event, ui) {
if (ui.item.id != '') { if (ui.item.id != '') {
findAddressValues(ui.item.id, blockNo, blockPrefix = ''); findAddressValues(ui.item.id, blockNo, blockPrefix = '');
//cj('#loaderimage_'+blockNo).show(); cj('#loaderimage_'+blockNo).show();
} }
return false; return false;
}, },
...@@ -147,15 +148,15 @@ cj(document).ready(function(){ ...@@ -147,15 +148,15 @@ cj(document).ready(function(){
minLength: minCharacters, minLength: minCharacters,
data: {postcode: cj( billingPostcodeElement ).val(), number: cj(houseElement).val(), mode: '0'}, data: {postcode: cj( billingPostcodeElement ).val(), number: cj(houseElement).val(), mode: '0'},
search: function( event, ui ) { search: function( event, ui ) {
//cj('#loaderimage_'+blockNo).show(); cj('#loaderimage_'+blockNo).show();
}, },
response: function( event, ui ) { response: function( event, ui ) {
//cj('#loaderimage_'+blockNo).hide(); cj('#loaderimage_'+blockNo).hide();
}, },
select: function(event, ui) { select: function(event, ui) {
if (ui.item.id != '') { if (ui.item.id != '') {
findAddressValues(ui.item.id, '5', blockPrefix = 'billing_'); findAddressValues(ui.item.id, '5', blockPrefix = 'billing_');
//cj('#loaderimage_'+blockNo).show(); cj('#loaderimage_'+blockNo).show();
} }
return false; return false;
}, },
...@@ -230,8 +231,9 @@ function setAddressFields(address, blockNo, blockPrefix) { ...@@ -230,8 +231,9 @@ function setAddressFields(address, blockNo, blockPrefix) {
cj(cityElement).val(address.town); cj(cityElement).val(address.town);
cj(postcodeElement).val(address.postcode); cj(postcodeElement).val(address.postcode);
if(typeof(address.state_province_id) != "undefined" && address.state_province_id !== null) { if(typeof(address.state_province_id) != "undefined" && address.state_province_id !== null) {
cj(countyElement).val(address.state_province_id).trigger("change"); cj(countyElement).val(address.state_province_id);
} }
cj(countyElement).trigger("change");
} }
} }
</script> </script>
......
...@@ -46,6 +46,7 @@ cj(document).ready(function() { ...@@ -46,6 +46,7 @@ cj(document).ready(function() {
var addressResultElement = '#addressResult_'+blockNo; var addressResultElement = '#addressResult_'+blockNo;
var addressResultsElement = '#addressResults_'+blockNo; var addressResultsElement = '#addressResults_'+blockNo;
var minCharacters = 4; var minCharacters = 4;
var delay = 200;
var postcodeProvider = '{/literal}{$civiPostCodeLookupProvider}{literal}'; var postcodeProvider = '{/literal}{$civiPostCodeLookupProvider}{literal}';
if (postcodeProvider !== 'civipostcode') { if (postcodeProvider !== 'civipostcode') {
...@@ -63,7 +64,7 @@ cj(document).ready(function() { ...@@ -63,7 +64,7 @@ cj(document).ready(function() {
selectFirst: false, selectFirst: false,
minChars: minCharacters, minChars: minCharacters,
matchContains: true, matchContains: true,
delay: 400, delay: delay,
max: 1000, max: 1000,
extraParams:{ extraParams:{
term:function () { term:function () {
...@@ -84,18 +85,19 @@ cj(document).ready(function() { ...@@ -84,18 +85,19 @@ cj(document).ready(function() {
cj(postcodeElement).autocomplete({ cj(postcodeElement).autocomplete({
source: sourceUrl, source: sourceUrl,
minLength: minCharacters, minLength: minCharacters,
delay: delay,
data: {postcode: cj( postcodeElement ).val(), number: cj(houseElement).val(), mode: '0'}, data: {postcode: cj( postcodeElement ).val(), number: cj(houseElement).val(), mode: '0'},
//max: {/literal}{crmSetting name="search_autocomplete_count" group="Search Preferences"}{literal}, //max: {/literal}{crmSetting name="search_autocomplete_count" group="Search Preferences"}{literal},
search: function( event, ui ) { search: function( event, ui ) {
//cj('#loaderimage_'+blockNo).show(); cj('#loaderimage_'+blockNo).show();
}, },
response: function( event, ui ) { response: function( event, ui ) {
//cj('#loaderimage_'+blockNo).hide(); cj('#loaderimage_'+blockNo).hide();
}, },
select: function(event, ui) { select: function(event, ui) {
if (ui.item.id != '') { if (ui.item.id != '') {
findAddressValues(ui.item.id, blockNo); findAddressValues(ui.item.id, blockNo);
//cj('#loaderimage_'+blockNo).show(); cj('#loaderimage_'+blockNo).show();
} }
return false; return false;
}, },
...@@ -173,8 +175,9 @@ function setAddressFields(address, blockNo) { ...@@ -173,8 +175,9 @@ function setAddressFields(address, blockNo) {
cj(cityElement).val(address.town); cj(cityElement).val(address.town);
cj(postcodeElement).val(address.postcode); cj(postcodeElement).val(address.postcode);
if(typeof(address.state_province_id) != "undefined" && address.state_province_id !== null) { if(typeof(address.state_province_id) != "undefined" && address.state_province_id !== null) {
cj(countyElement).val(address.state_province_id).trigger("change"); cj(countyElement).val(address.state_province_id);
} }
cj(countyElement).trigger("change");
} }
} }
......
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