adding autocomplete postcode

This commit is contained in:
Marion Muszynski 2016-05-19 13:13:47 +02:00
parent 05ed10fcc1
commit eb441d8edf
4 changed files with 375 additions and 0 deletions

24
check_address.php Normal file
View File

@ -0,0 +1,24 @@
<?php
include dirname(__FILE__).'/config/config.inc.php';
include dirname(__FILE__).'/init.php';
if(!$cookie->isLogged()) {
exit;
}
$token = Tools::getValue('token');
if($token && $token === Tools::encrypt(date('Y-m-d').$cookie->id_customer.'Wb62nI1mcPdwyqqt')) {
if((int) Tools::getValue('id_country') === 8) {
$postcode = Tools::getValue('postcode');
$city = Tools::getValue('city');
header('Content-Type: application/json; charset=utf-8');
echo json_encode(Db::getInstance()->ExecuteS('
SELECT `city`, `postcode`
FROM `'._DB_PREFIX_.'france_postcode`
WHERE 1
'.($postcode? ' AND `postcode` LIKE "%'.pSQL($postcode).'%"': '').'
'.($city? ' AND `city` LIKE "%'.pSQL(preg_replace('/ C(I|E)DEX(.*)$/i', '', $city)).'%"': '').'
LIMIT 46
'));
}
}

View File

@ -0,0 +1,53 @@
<?php
class AddressController extends AddressControllerCore {
public function process()
{
parent::process();
/* Secure restriction for guest */
if (self::$cookie->is_guest)
Tools::redirect('addresses.php');
if (Tools::isSubmit('id_country') AND Tools::getValue('id_country') != NULL AND is_numeric(Tools::getValue('id_country')))
$selectedCountry = (int)Tools::getValue('id_country');
elseif (isset($this->_address) AND isset($this->_address->id_country) AND !empty($this->_address->id_country) AND is_numeric($this->_address->id_country))
$selectedCountry = (int)$this->_address->id_country;
elseif (isset($_SERVER['HTTP_ACCEPT_LANGUAGE']))
{
$array = preg_split('/,|-/', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
if (!Validate::isLanguageIsoCode($array[0]) OR !($selectedCountry = Country::getByIso($array[0])))
$selectedCountry = (int)Configuration::get('PS_COUNTRY_DEFAULT');
}
else
$selectedCountry = (int)Configuration::get('PS_COUNTRY_DEFAULT');
if (Configuration::get('PS_RESTRICT_DELIVERED_COUNTRIES'))
$countries = Carrier::getDeliveredCountries((int)self::$cookie->id_lang, true, true);
else
$countries = Country::getCountries((int)self::$cookie->id_lang, true);
$countriesList = '';
foreach ($countries AS $country)
$countriesList .= '<option value="'.(int)($country['id_country']).'" '.($country['id_country'] == $selectedCountry ? 'selected="selected"' : '').'>'.htmlentities($country['name'], ENT_COMPAT, 'UTF-8').'</option>';
if ((Configuration::get('VATNUMBER_MANAGEMENT') AND file_exists(_PS_MODULE_DIR_.'vatnumber/vatnumber.php')) && VatNumber::isApplicable(Configuration::get('PS_COUNTRY_DEFAULT')))
self::$smarty->assign('vat_display', 2);
elseif (Configuration::get('VATNUMBER_MANAGEMENT'))
self::$smarty->assign('vat_display', 1);
else
self::$smarty->assign('vat_display', 0);
self::$smarty->assign('ajaxurl', _MODULE_DIR_);
self::$smarty->assign('vatnumber_ajax_call', (int)file_exists(_PS_MODULE_DIR_.'vatnumber/ajax.php'));
self::$smarty->assign(array(
'address_ac_token' => (self::$cookie->isLogged()? Tools::encrypt(date('Y-m-d').self::$cookie->id_customer.'Wb62nI1mcPdwyqqt'): ''),
'countries_list' => $countriesList,
'countries' => $countries,
'errors' => $this->errors,
'token' => Tools::getToken(false),
'select_address' => (int)(Tools::getValue('select_address'))
));
}
}

View File

@ -273,5 +273,272 @@ $(function(){ldelim}
<input type="submit" name="submitAddress" id="submitAddress" value="{l s='Save'}" class="exclusive" /> <input type="submit" name="submitAddress" id="submitAddress" value="{l s='Save'}" class="exclusive" />
<br class="clear"/> <br class="clear"/>
</p> </p>
<script type="text/javascript">
<!--{literal}
var ctimeout = null;
var creq = null;
$(document).ready(function() {
$("#postcode, #city").keydown(function(e) {
if($("#address_ac").length > 0 && (e.keyCode == 13 || e.keyCode == 38 || e.keyCode == 40)) {
e.preventDefault();
return false;
}
});
$("#postcode").keyup(function(e) {
if($("#id_country").val() == 8) {
switch($("#postcode").val().substring(0, 3)) {
case "971":
try {
creq.abort();
} catch(e) {}
$("#address_ac").remove();
$("#id_country").val(98);
$("#postcode, #city").attr("autocomplete", "on");
return;
break;
case "972":
try {
creq.abort();
} catch(e) {}
$("#address_ac").remove();
$("#id_country").val(141);
$("#postcode, #city").attr("autocomplete", "on");
return;
break;
case "973":
try {
creq.abort();
} catch(e) {}
$("#address_ac").remove();
$("#id_country").val(241);
$("#postcode, #city").attr("autocomplete", "on");
return;
break;
case "974":
try {
creq.abort();
} catch(e) {}
$("#address_ac").remove();
$("#id_country").val(176);
$("#postcode, #city").attr("autocomplete", "on");
return;
break;
case "975":
try {
creq.abort();
} catch(e) {}
$("#address_ac").remove();
$("#id_country").val(183);
$("#postcode, #city").attr("autocomplete", "on");
return;
break;
case "976":
try {
creq.abort();
} catch(e) {}
$("#address_ac").remove();
$("#id_country").val(144);
$("#postcode, #city").attr("autocomplete", "on");
return;
break;
case "987":
try {
creq.abort();
} catch(e) {}
$("#address_ac").remove();
$("#id_country").val(242);
$("#postcode, #city").attr("autocomplete", "on");
return;
break;
case "988":
try {
creq.abort();
} catch(e) {}
$("#address_ac").remove();
$("#id_country").val(158);
$("#postcode, #city").attr("autocomplete", "on");
return;
break;
case "980":
try {
creq.abort();
} catch(e) {}
$("#address_ac").remove();
$("#id_country").val(148);
$("#postcode, #city").attr("autocomplete", "on");
return;
break;
}
switch($("#postcode").val().substring(0, 2)) {
case "AD":
case "ad":
case "Ad":
try {
creq.abort();
} catch(e) {}
$("#address_ac").remove();
$("#id_country").val(40);
$("#postcode, #city").attr("autocomplete", "on");
return;
break;
case "SP":
case "sp":
case "Sp":
case "00":
try {
creq.abort();
} catch(e) {}
$("#address_ac").remove();
$("#id_country").val(245);
$("#postcode, #city").attr("autocomplete", "on");
return;
break;
}
if($("#address_ac").length > 0 && (e.keyCode == 27)) {
$("#address_ac").remove();
} else if(e.keyCode == 13) {
e.preventDefault();
if($("#address_ac .active").length > 0) {
$("#address_ac .active").trigger("click");
}
return false;
} else if($("#address_ac").length > 0 && (e.keyCode == 38 || e.keyCode == 40)) {
if(e.keyCode == 38) {
$("#address_ac .active:not(:first-child)").removeClass("active").prev().addClass("active");
} else {
if($("#address_ac .active").length > 0) {
$("#address_ac .active:not(:last-child)").removeClass("active").next().addClass("active");
} else {
$("#address_ac li:first-child").addClass("active");
}
}
} else {
ctimeout = setTimeout(function() {
try {
creq.abort();
} catch(e) {}
$("#address_ac").remove();
$("#city").val("");
clearTimeout(ctimeout);
var inval = $("<div />").text($("#postcode").val()).html();
if(inval != "") {
creq = $.getJSON("{/literal}{$base_dir_ssl}{literal}check_address.php?token={/literal}{$address_ac_token}{literal}&id_country=" + parseInt($("#id_country").val()) + "&postcode=" + inval, function(result) {
console.log(result);
if(result) {
if(result.length == 1) {
$("#postcode").val(result[0].postcode);
$("#city").val(result[0].city);
} else {
var ac = $("<ul />").attr("id", "address_ac").css({
position: "absolute",
top: $("#postcode").position().top + $("#postcode").outerHeight(),
left: $("#postcode").position().left
});
for(i in result) {
$(ac).append($("<li />")
.attr("data-city", result[i].city)
.attr("data-postcode", result[i].postcode)
.html((result[i].postcode + " - " + result[i].city).replace(inval, "<strong>" + inval + "</strong>"))
.click(function() {
$("#postcode").val($(this).attr("data-postcode"));
$("#city").val($(this).attr("data-city"));
$("#address_ac").remove();
})
);
}
$("#postcode").parent().after($(ac));
}
}
});
}
}, 300);
}
}
});
$("#city").keyup(function(e) {
if($("#id_country").val() == 8) {
if($("#address_ac").length > 0 && (e.keyCode == 27)) {
$("#address_ac").remove();
} else if(e.keyCode == 13) {
e.preventDefault();
if($("#address_ac .active").length > 0) {
$("#address_ac .active").trigger("click");
}
return false;
} else if($("#address_ac").length > 0 && (e.keyCode == 38 || e.keyCode == 40)) {
if(e.keyCode == 38) {
$("#address_ac .active:not(:first-child)").removeClass("active").prev().addClass("active");
} else {
if($("#address_ac .active").length > 0) {
$("#address_ac .active:not(:last-child)").removeClass("active").next().addClass("active");
} else {
$("#address_ac li:first-child").addClass("active");
}
}
} else {
ctimeout = setTimeout(function() {
try {
creq.abort();
} catch(e) {}
$("#address_ac").remove();
$("#postcode").val("");
clearTimeout(ctimeout);
var inval = $("<div />").text($("#city").val()).html();
if(inval != "") {
creq = $.getJSON("{/literal}{$base_dir_ssl}{literal}check_address.php?token={/literal}{$address_ac_token}{literal}&id_country=" + parseInt($("#id_country").val()) + "&city=" + inval, function(result) {
console.log(result);
if(result) {
if(result.length == 1) {
$("#postcode").val(result[0].postcode);
var cedex = $("#city").val().match(/ C(I|E)DEX.*$/i);
$("#city").val(result[0].city + (cedex? cedex[0]: ""));
} else {
var ac = $("<ul />").attr("id", "address_ac").css({
position: "absolute",
top: $("#city").position().top + $("#city").outerHeight(),
left: $("#city").position().left
});
for(i in result) {
$(ac).append($("<li />")
.attr("data-city", result[i].city)
.attr("data-postcode", result[i].postcode)
.html((result[i].postcode + " - " + result[i].city).replace(inval, "<strong>" + inval + "</strong>"))
.click(function() {
var cedex = $("#city").val().match(/ C(I|E)DEX.*$/i);
$("#postcode").val($(this).attr("data-postcode"));
$("#city").val($(this).attr("data-city") + (cedex? cedex[0]: ""));
$("#address_ac").remove();
})
);
}
$("#city").parent().after($(ac));
}
}
});
}
}, 300);
}
}
});
$("#id_country").change(function() {
$("#postcode, #city").val("");
if($("#id_country").val() == 8) {
$("#postcode, #city").attr("autocomplete", "off");
} else {
$("#postcode, #city").attr("autocomplete", "on");
}
});
if($("#id_country").val() == 8) {
$("#postcode, #city").attr("autocomplete", "off");
} else {
$("#postcode, #city").attr("autocomplete", "on");
}
});
{/literal}-->
</script>
</form> </form>

View File

@ -5675,6 +5675,37 @@ table#carrierTable tbody td {
display: none!important display: none!important
} }
/******** Address autocomplete **********/
#address_ac {
z-index: 5;
background: #FFF;
border-left: 1px solid #e4e4e4;
border-right: 1px solid #e4e4e4;
border-bottom: 1px solid #e4e4e4;
margin: 1px 0px 0px 0px;
min-width: 278px;
-moz-box-shadow: 2px 10px 10px -8px rgba(0, 0, 0, .5);
-webkit-box-shadow: 2px 10px 10px -8px rgba(0, 0, 0, .5);
-ms-box-shadow: 2px 10px 10px -8px rgba(0, 0, 0, .5);
-o-box-shadow: 2px 10px 10px -8px rgba(0, 0, 0, .5);
box-shadow: 2px 10px 10px -8px rgba(0, 0, 0, .5));
list-style: outside none;
overflow: auto;
max-height: 300px;
}
#address_ac li {
font-size: 12px;
line-height: 1.4em;
padding: 5px 20px 5px 10px;
cursor: pointer;
}
#address_ac li:hover {
background: #EEE;
}
#address_ac strong {
padding: 0px 1px;
}
@keyframes slideInDown { @keyframes slideInDown {
0% { 0% {