From a2230b0161b1a984a56a78f331bcb82ae190deda Mon Sep 17 00:00:00 2001 From: vSchoener Date: Mon, 5 Dec 2011 10:54:01 +0000 Subject: [PATCH] [-] MO : Carriercompare (shipping estimation), merge 1.4 => 1.5 git-svn-id: http://dev.prestashop.com/svn/v1/branches/1.5.x@10900 b9a71923-0436-4b27-9f14-aed3839534dd --- modules/carriercompare/carriercompare.js | 113 ++++++++++++++++------ modules/carriercompare/carriercompare.php | 79 ++++++++++++--- modules/carriercompare/style.css | 25 ++++- 3 files changed, 172 insertions(+), 45 deletions(-) diff --git a/modules/carriercompare/carriercompare.js b/modules/carriercompare/carriercompare.js index efa41a89a..6a73d2ef6 100644 --- a/modules/carriercompare/carriercompare.js +++ b/modules/carriercompare/carriercompare.js @@ -24,39 +24,65 @@ * International Registered Trademark & Property of PrestaShop SA */ +var ajaxQueries = new Array(); + +function resetAjaxQueries() +{ + for (i = 0; i < ajaxQueries.length; ++i) + ajaxQueries[i].abort(); + + ajaxQueries = new Array(); +} + +function displayWaitingAjax(type, message) +{ + + $('#SE_AjaxDisplay').find('p').html(message); + $('#SE_AjaxDisplay').css('display', type); +} + function updateStateByIdCountry() { - $.ajax({ + $('#id_state').children().remove(); + $('#availableCarriers').slideUp('fast'); + $('#states').slideUp('fast'); + displayWaitingAjax('block', SE_RefreshStateTS); + + var query = $.ajax({ type: 'POST', url: baseDir + 'modules/carriercompare/ajax.php', data: 'method=getStates&id_country=' + $('#id_country').val(), dataType: 'json', success: function(json) { - $('#id_state').children().remove(); - $('#states').slideUp('slow'); if (json.length) { for (state in json) { $('#id_state').append(''); } - $('#states').slideDown('slow'); + $('#states').slideDown('fast'); } + if (SE_RefreshMethod == 0) + updateCarriersList(); + displayWaitingAjax('none', ''); } }); + ajaxQueries.push(query); } function updateCarriersList() { - $.ajax({ + $('#carriers_list').children().remove(); + $('#availableCarriers').slideUp('fast'); + $('#noCarrier').slideUp('fast'); + displayWaitingAjax('block', SE_RetrievingInfoTS); + + var query = $.ajax({ type: 'POST', url: baseDir + 'modules/carriercompare/ajax.php', data: 'method=getCarriers&id_country=' + $('#id_country').val() + '&id_state=' + $('#id_state').val() + '&zipcode=' + $('#zipcode').val(), dataType: 'json', success: function(json) { - $('#carriers_list').children().remove(); - $('#availableCarriers').slideUp('slow'); - $('#noCarrier').slideUp('slow'); if (json.length) { for (carrier in json) @@ -70,7 +96,7 @@ function updateCarriersList() (json[carrier].img ? ''+json[carrier].name+'' : json[carrier].name)+ ''+ ''+ - ''+json[carrier].delay+''+ + ''+((json[carrier].delay != null) ? json[carrier].delay : '') +''+ ''; if (json[carrier].price) @@ -85,54 +111,77 @@ function updateCarriersList() ''; $('#carriers_list').append(html); } - $('#availableCarriers').slideDown('slow'); + $('#availableCarriers').slideDown('fast'); } else - $('#noCarrier').slideDown('slow'); + $('#noCarrier').slideDown('fast'); + displayWaitingAjax('none', ''); } }); + ajaxQueries.push(query); } function saveSelection() { - $.ajax({ + $('#carriercompare_errors_list').children().remove(); + $('#carriercompare_errors').slideUp('fast'); + displayWaitingAjax('block', SE_RedirectTS); + $('#carriercompare_submit').attr('disabled', true); + + var query = $.ajax({ type: 'POST', url: baseDir + 'modules/carriercompare/ajax.php', data: 'method=saveSelection&' + $('#compare_shipping_form').serialize(), dataType: 'json', success: function(json) { - $('#carriercompare_errors_list').children().remove(); - $('#carriercompare_errors').slideUp('slow'); if (json.length) { for (error in json) - { $('#carriercompare_errors_list').append('
  • '+json[error]+'
  • '); - } - $('#carriercompare_errors').slideDown('slow'); + $('#carriercompare_errors').slideDown('fast'); + displayWaitingAjax('none', ''); + $('#carriercompare_submit').removeAttr('disabled'); } else { - $('#carriercompare_submit').fadeOut('slow'); - document.location.href = document.location.href; + $('.SE_SubmitRefreshCard').fadeOut('fast'); + location.reload(true); } } }); + ajaxQueries.push(query); return false; } -$(document).ready(function() { - $('#id_country').change(function(){ +function PS_SE_HandleEvent() +{ + $(document).ready(function() { + $('#id_country').change(function() { + resetAjaxQueries(); + updateStateByIdCountry(); + }); + + if (SE_RefreshMethod == 0) + { + $('#id_state,#zipcode').change(function() { + resetAjaxQueries(); + updateCarriersList(); + }); + $('#zipcode').keyup(function() { + resetAjaxQueries(); + updateCarriersList(); + }); + } + + $('#update_carriers_list').click(function() { + updateCarriersList(); + }); + + $('#carriercompare_submit').click(function() { + resetAjaxQueries(); + saveSelection(); + return false; + }); updateStateByIdCountry(); - updateCarriersList(); }); - $('#id_state').change(function(){ - updateCarriersList(); - }); - $('#carriercompare_submit').click(function(){ - saveSelection(); - return false; - }); - updateStateByIdCountry(); - updateCarriersList(); -}); +} diff --git a/modules/carriercompare/carriercompare.php b/modules/carriercompare/carriercompare.php index c38a8be89..71cdb18e5 100755 --- a/modules/carriercompare/carriercompare.php +++ b/modules/carriercompare/carriercompare.php @@ -30,11 +30,14 @@ if (!defined('_PS_VERSION_')) class CarrierCompare extends Module { + public $template_directory = ''; + public $smarty; + public function __construct() { $this->name = 'carriercompare'; $this->tab = 'shipping_logistics'; - $this->version = '1.1'; + $this->version = '1.2'; $this->author = 'PrestaShop'; $this->need_instance = 0; @@ -42,6 +45,19 @@ class CarrierCompare extends Module $this->displayName = $this->l('Shipping Estimation'); $this->description = $this->l('Module to compare carrier possibilities before using the checkout process'); + $this->template_directory = dirname(__FILE__).'/template/'; + $this->initRetroCompatibilityVar(); + } + + // Retro-compatibiliy 1.4/1.5 + private function initRetroCompatibilityVar() + { + if (class_exists('Context')) + $smarty = Context::getContext()->smarty; + else + global $smarty; + + $this->smarty = $smarty; } public function install() @@ -50,6 +66,29 @@ class CarrierCompare extends Module return false; return true; } + + public function getContent() + { + if (!empty($_POST)) + $this->postProcess(); + + $this->smarty->assign('refresh_method', Configuration::get('SE_RERESH_METHOD')); + return $this->smarty->fetch($this->template_directory .'configuration.tpl'); + } + + public function postProcess() + { + $errors = array(); + + if (Tools::isSubmit('setGlobalConfiguration')) + { + $method = (int)Tools::getValue('refresh_method'); + Configuration::updateValue('SE_RERESH_METHOD', $method); + } + + $this->smarty->assign(array( + 'display_error' => count($errors) ? $errors : false)); + } public function hookHeader($params) { @@ -66,9 +105,21 @@ class CarrierCompare extends Module { if (!$this->isModuleAvailable()) return; - - $this->context->smarty->assign(array( - 'countries' => Country::getCountries($this->context->language->id), + + $protocol = (Configuration::get('PS_SSL_ENABLED') || (!empty($_SERVER['HTTPS']) + && strtolower($_SERVER['HTTPS']) != 'off')) ? 'https://' : 'http://'; + + $endURL = __PS_BASE_URI__.'modules/carriercompare/'; + + if (method_exists('Tools', 'getShopDomainSsl')) + $moduleURL = $protocol.Tools::getShopDomainSsl().$endURL; + else + $moduleURL = $protocol.$_SERVER['HTTP_HOST'].$endURL; + + $refresh_method = Configuration::get('SE_RERESH_METHOD'); + + $this->smarty->assign(array( + 'countries' => Country::getCountries((int)$cookie->id_lang), 'id_carrier' => ($params['cart']->id_carrier ? $params['cart']->id_carrier : Configuration::get('PS_CARRIER_DEFAULT')), 'id_country' => (isset($this->context->customer->geoloc_id_country) ? $this->context->customer->geoloc_id_country : Configuration::get('PS_COUNTRY_DEFAULT')), 'id_state' => (isset($this->context->customer->geoloc_id_state) ? $this->context->customer->geoloc_id_state : 0), @@ -76,10 +127,12 @@ class CarrierCompare extends Module 'currencySign' => $this->context->currency->sign, 'currencyRate' => $this->context->currency->conversion_rate, 'currencyFormat' => $this->context->currency->format, - 'currencyBlank' => $this->context->currency->blank + 'currencyBlank' => $this->context->currency->blank, + 'new_base_dir' => $moduleURL, + 'refresh_method' => ($refresh_method === false) ? 0 : $refresh_method )); - return $this->display(__FILE__, 'carriercompare.tpl'); + return $this->smarty->fetch($this->template_directory.'carriercompare.tpl'); } /* @@ -110,15 +163,17 @@ class CarrierCompare extends Module $id_zone = State::getIdZone($id_state); if (!$id_zone) $id_zone = Country::getIdZone($id_country); + + // Need to set the infos for carrier module ! + $this->context->cookie->id_country = $id_country; + $this->context->cookie->id_state = $id_state; + $this->context->cookie->postcode = $zipcode; $carriers = Carrier::getCarriersForOrder((int)$id_zone); - - return (count($carriers) ? $carriers : array()); + + return (sizeof($carriers) ? $carriers : array()); } - /** - * @todo this function must be modified - id_carrier is now delivery_option - */ public function saveSelection($id_country, $id_state, $zipcode, $id_carrier) { $errors = array(); @@ -191,7 +246,7 @@ class CarrierCompare extends Module * This module is only available on standard order process because * on One Page Checkout the carrier list is already available. */ - if ($fileName != 'order.php') + if (!in_array($fileName, array('order.php', 'cart.php'))) return false; /** * If visitor is logged, the module isn't available on Front office, diff --git a/modules/carriercompare/style.css b/modules/carriercompare/style.css index 1b9f951f7..c49fdbae5 100644 --- a/modules/carriercompare/style.css +++ b/modules/carriercompare/style.css @@ -11,4 +11,27 @@ #compare_shipping ul#carriercompare_errors_list li { margin-left: 30px; -} \ No newline at end of file + text-decoration: none; + list-style: none; +} + +#SE_AjaxDisplay +{ + text-align: center; + display: none; +} + +#SE_AjaxDisplay img +{ + width: 15px; +} + +.SE_SubmitRefreshCard +{ + text-align: center; +} + +.SE_SubmitRefreshCard input +{ + display: inline; +}