Skip to content

Commit

Permalink
Select province even if its name has been altered in CiviCRM. E.g. fr…
Browse files Browse the repository at this point in the history
…om Limburg to Limburg (NL)
  • Loading branch information
jaapjansma committed May 23, 2022
1 parent 1d19687 commit 5b108e9
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 38 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## Version 1.38

* Select province even if its name has been altered in CiviCRM. E.g. from Limburg to Limburg (NL)

## Version 1.36

* Added progressbar to importer.
Expand Down
22 changes: 11 additions & 11 deletions CRM/Postcodenl/Updater.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public static function storetreetUnitFromFormSubmission(CRM_Core_Form $form) {

/**
* Updates an address to their corresponding information from the postcode table.
*
*
* @param int $id
* @param array $params
* @return void|array
Expand Down Expand Up @@ -162,7 +162,7 @@ protected function updateAddressFields($id, &$params) {
$update_params['city'] = $official_city->city;
}
}

if (isset($params['country_id']) && $params['country_id'] == 1152 && isset($params['street_number']) && isset($params['street_name']) && isset($params['city']) && !empty($params['street_number']) && !empty($params['street_name']) && !empty($params['city']) && (!isset($params['postal_code']) || empty($params['postal_code']))) {
$info = civicrm_api3('PostcodeNL', 'get', array('adres' => $params['street_name'], 'huisnummer' => $params['street_number'], 'woonplaats' => $params['city']));
if (isset($info['values']) && is_array($info['values']) && count($info['values']) > 0) {
Expand Down Expand Up @@ -258,14 +258,14 @@ protected function updateAddressFields($id, &$params) {
return $update_params;
}

protected function getProvinceIdByDutchName($province) {
public function getProvinceIdByDutchName($province) {
$result = civicrm_api3('Address', 'getoptions', array(
'sequential' => 1,
'field' => "state_province_id",
'country_id' => 1152,
));
foreach($result['values'] as $state_province) {
if ($state_province['value'] == $province) {
if ($state_province['value'] == $province || $state_province['value'] == ($province .' (NL)') ) {
return $state_province['key'];
}
}
Expand All @@ -276,9 +276,9 @@ protected function getProvinceIdByDutchName($province) {
* Parse the address for Dutch addresses
* Glues together the different parts of an address or explode
* the the street_adress into the different parts
*
*
* Returns an array with the changed parts of the address
*
*
* @param array $params
* @return array
*/
Expand Down Expand Up @@ -319,7 +319,7 @@ protected function parseAddress(&$params) {
$update_params['street_address'] = $this->glueStreetAddressNl($params);
}
}

return $update_params;
}

Expand Down Expand Up @@ -370,7 +370,7 @@ protected function splitStreetAddressNl($streetAddress) {
/*
* if the part is numeric, there are several possibilities:
* - if the partKey is 0 so it is the first element, it is
* assumed it is part of the street_name to cater for
* assumed it is part of the street_name to cater for
* situation like 2e Wormenseweg
* - if not the first part and there is no street_number yet (foundStreetNumber
* is false), it is assumed this numeric part contains the street_number
Expand Down Expand Up @@ -432,12 +432,12 @@ protected function splitStreetAddressNl($streetAddress) {

/**
* Update the custom values for an address
*
*
* The address data is given in params. The custom values are the community
* cbs_area code etc...
*
*
* Returns true when the custom values are updated
*
*
* @param int $address_id
* @param array $params
* @return boolean
Expand Down
27 changes: 16 additions & 11 deletions api/v3/PostcodeNL/Get.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

/**
* PostcodeNL.Get API
*
*
* Returns the found postcode, woonplaats, gemeente with the queried paramaters
*
* @param array $params
Expand Down Expand Up @@ -49,9 +49,11 @@ function civicrm_api3_postcode_n_l_get($params) {
'latitude',
'longitude'
);

/*
* check if at least one parameter is valid

$updater = CRM_Postcodenl_Updater::singleton();

/*
* check if at least one parameter is valid
* Also break up an postcode into postcode number (4 digits) and postcode letter (2 letters).
*
*/
Expand All @@ -74,9 +76,9 @@ function civicrm_api3_postcode_n_l_get($params) {
}
}
}

$sql = "SELECT * FROM `civicrm_postcodenl` WHERE 1";

/**
* Build the where clausule of the postcode
*/
Expand All @@ -91,19 +93,19 @@ function civicrm_api3_postcode_n_l_get($params) {
$where .= " AND (`even` = %".$i." XOR `adres` Like 'Postbus')";
$values[$i] = array($even, 'Integer');
$i++;

$where .= " AND ((%".$i." BETWEEN `huisnummer_van` AND `huisnummer_tot`) XOR (`adres` Like 'Postbus'))";
$values[$i] = array($value, 'Integer');
$i++;
$i++;
} else {
$where .= " AND `".$field."` = %".$i;
$values[$i] = array($value, 'String');
$i++;
}
}
}
$sql .= $where . " LIMIT 0, 25";
$dao = CRM_Core_DAO::executeQuery($sql, $values);

$returnValues = array();
while($dao->fetch()) {
$row = array();
Expand All @@ -112,14 +114,17 @@ function civicrm_api3_postcode_n_l_get($params) {
$row[$field] = $dao->$field;
}
}
if (isset($row['provincie'])) {
$row['state_province_id'] = $updater->getProvinceIdByDutchName($row['provincie']);
}
$returnValues[$dao->id] = $row;
}

CRM_Postcodenl_Utils_Hook::invoke(1,
$returnValues, CRM_Utils_Hook::$_nullObject, CRM_Utils_Hook::$_nullObject, CRM_Utils_Hook::$_nullObject, CRM_Utils_Hook::$_nullObject, CRM_Utils_Hook::$_nullObject,
'civicrm_postcodenl_get'
);

return civicrm_api3_create_success($returnValues, $params, 'PostcodeNL', 'get');
}

4 changes: 2 additions & 2 deletions info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
<url desc="Support">http://civicoop.org</url>
<url desc="Licensing">http://www.gnu.org/licenses/agpl-3.0.html</url>
</urls>
<releaseDate>2021-01-08</releaseDate>
<version>1.37</version>
<releaseDate>2022-05-23</releaseDate>
<version>1.38</version>
<develStage>stable</develStage>
<compatibility>
<ver>4.4</ver>
Expand Down
28 changes: 14 additions & 14 deletions postcodenl.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
* Function to retrieve the postcode and fill the fields
*/
function postcodenl_retrieve(blockId, housenumber, postcode, toevoeging) {
Expand All @@ -15,8 +15,8 @@ function postcodenl_retrieve(blockId, housenumber, postcode, toevoeging) {
//run only when a postcode is present
if (postcode.length == 0) {
return;
}
}

cj('#address_' + blockId + '_street_name').addClass('ac_loading');
cj('#address_' + blockId + '_city').addClass('ac_loading');

Expand All @@ -33,7 +33,7 @@ function postcodenl_retrieve(blockId, housenumber, postcode, toevoeging) {
cj('.crm-address-custom-set-block-' + blockId + ' input[data-crm-custom="Adresgegevens:Gemeente"]').val(obj.gemeente);
cj('.crm-address-custom-set-block-' + blockId + ' input[data-crm-custom="Adresgegevens:Buurt"]').val(obj.cbs_buurtnaam);
cj('#address_'+blockId+'_state_province_id option').filter(function() {
return cj(this).html() == obj.provincie;
return cj(this).html() == obj.provincie || cj(this).val() == obj.state_province_id;
}).prop('selected', true);
cj('#address_'+blockId+'_state_province_id').trigger('change');

Expand All @@ -49,7 +49,7 @@ function postcodenl_retrieve(blockId, housenumber, postcode, toevoeging) {
cj('#address_'+blockId+'_state_province_id option:selected').prop('selected', false);
cj('#address_'+blockId+'_state_province_id').trigger('change');
}

cj('#address_' + blockId + '_street_name').removeClass('ac_loading');
cj('#address_' + blockId + '_city').removeClass('ac_loading');
}
Expand All @@ -58,7 +58,7 @@ function postcodenl_retrieve(blockId, housenumber, postcode, toevoeging) {

function postcodenl_init_addressBlock(blockId, address_table_id) {
var first_row = cj(address_table_id + ' tbody tr:first');

first_row.before('<tr class="hiddenElement postcodenl_input_row" id="postcodenl_row_' + blockId + '"><td>Postcode<br /><input class="form-text" id="postcodenl_postcode_' + blockId + '" /></td><td>Huisnummer<br /><input id="postcodenl_huisnummer_' + blockId + '" class="form-text six"></td><td>Toevoeging<br /><input id="postcodenl_huisnummer_toev_' + blockId + '" class="form-text six"></td></tr>');

var postcode_field = cj('#postcodenl_postcode_' + blockId);
Expand All @@ -73,7 +73,7 @@ function postcodenl_init_addressBlock(blockId, address_table_id) {
cj('#address_' + blockId + '_postal_code').val(postcode_field.val());
postcodenl_retrieve(blockId, housenumber_field.val(), postcode_field.val(), housenumber_toev_field.val());
});

postcode_field.keyup(function(e) {
cj('#address_' + blockId + '_postal_code').val(postcode_field.val());
postcodenl_retrieve(blockId, housenumber_field.val(), postcode_field.val(), housenumber_toev_field.val());
Expand All @@ -83,30 +83,30 @@ function postcodenl_init_addressBlock(blockId, address_table_id) {
cj('#address_' + blockId + '_street_number').val(housenumber_field.val());
postcodenl_retrieve(blockId, housenumber_field.val(), postcode_field.val(), housenumber_toev_field.val());
});

housenumber_field.keyup(function(e) {
cj('#address_' + blockId + '_street_number').val(housenumber_field.val());
postcodenl_retrieve(blockId, housenumber_field.val(), postcode_field.val(), housenumber_toev_field.val());
});

housenumber_toev_field.change(function(e) {
postcodenl_retrieve(blockId, housenumber_field.val(), postcode_field.val(), housenumber_toev_field.val());
});

housenumber_toev_field.keyup(function(e) {
postcodenl_retrieve(blockId, housenumber_field.val(), postcode_field.val(), housenumber_toev_field.val());
});

cj('#address_' + blockId + '_country_id').change(function(e) {
if ((cj('#address_' + blockId + '_country_id').val()) == 1152) {
if (typeof processAddressFields == 'function' && cj('#addressElements_'+blockId).length > 0) {
processAddressFields('addressElements', blockId, 1);
cj('#addressElements_' + blockId).show();
cj('#streetAddress_' + blockId).hide();
}

cj('#postcodenl_row_' + blockId).show();

street_number_td.hide();
street_unit_td.hide();
postalcode_td.hide();
Expand Down Expand Up @@ -139,7 +139,7 @@ function postcodenl_init_addressBlock(blockId, address_table_id) {
cj('div.crm-address-custom-set-block-'+blockId+' input[data-crm-custom="Adresgegevens:cbs_manual_entry"]').change(function (e) {
cj('#address_' + blockId + '_country_id').trigger('change');
})

cj('#address_' + blockId + '_country_id').trigger('change');
}

Expand Down

0 comments on commit 5b108e9

Please sign in to comment.