From 661ee72d55a377932068266d271a7e6c88f6ddea Mon Sep 17 00:00:00 2001 From: phf Date: Sun, 20 Mar 2016 15:17:30 +0100 Subject: [PATCH] UPDATE move ajax request in js file and call a new php file to update all rates --- htdocs/admin/multicurrency.php | 91 +----------------- htdocs/multicurrency/ajax/updaterates.php | 35 +++++++ htdocs/multicurrency/js/currencylayer.js.php | 98 ++++++++++++++++++++ 3 files changed, 136 insertions(+), 88 deletions(-) create mode 100644 htdocs/multicurrency/ajax/updaterates.php create mode 100644 htdocs/multicurrency/js/currencylayer.js.php diff --git a/htdocs/admin/multicurrency.php b/htdocs/admin/multicurrency.php index e4f4fc34301..235b16ba100 100644 --- a/htdocs/admin/multicurrency.php +++ b/htdocs/admin/multicurrency.php @@ -42,16 +42,10 @@ if (! $user->admin) { // Parameters $action = GETPOST('action', 'alpha'); -$sync_response = GETPOST('sync_response'); /* * Actions */ -if (!empty($sync_response)) -{ - $sync_response = json_decode($sync_response); - MultiCurrency::syncRates($sync_response); - exit; -} + if (preg_match('/set_(.*)/',$action,$reg)) { @@ -145,8 +139,9 @@ if ($resql) */ $page_name = "MultiCurrency"; +$morejs = array('/multicurrency/js/currencylayer.js.php'); -llxHeader('', $langs->trans($page_name)); +llxHeader('', $langs->trans($page_name), '', '', '', '', $morejs); // Subheader $linkback = '' @@ -228,86 +223,6 @@ print ''; print ''; print '
'; -print ''; - $var=false; print ''; print ''; diff --git a/htdocs/multicurrency/ajax/updaterates.php b/htdocs/multicurrency/ajax/updaterates.php new file mode 100644 index 00000000000..663e68a647c --- /dev/null +++ b/htdocs/multicurrency/ajax/updaterates.php @@ -0,0 +1,35 @@ + + * Copyright (C) 2016 Pierre-Henry Favre + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +if (! defined('NOTOKENRENEWAL')) define('NOTOKENRENEWAL','1'); // Disables token renewal +if (! defined('NOREQUIREMENU')) define('NOREQUIREMENU','1'); +if (! defined('NOREQUIREHTML')) define('NOREQUIREHTML','1'); +if (! defined('NOREQUIREAJAX')) define('NOREQUIREAJAX','1'); +if (! defined('NOREQUIRESOC')) define('NOREQUIRESOC','1'); +if (! defined('NOREQUIREHOOK')) define('NOREQUIREHOOK','1'); + +require '../../main.inc.php'; +require_once DOL_DOCUMENT_ROOT.'/multicurrency/class/multicurrency.class.php'; + +$sync_response = GETPOST('sync_response'); + +if (!empty($sync_response)) +{ + $sync_response = json_decode($sync_response); + MultiCurrency::syncRates($sync_response); +} \ No newline at end of file diff --git a/htdocs/multicurrency/js/currencylayer.js.php b/htdocs/multicurrency/js/currencylayer.js.php new file mode 100644 index 00000000000..0b563aae266 --- /dev/null +++ b/htdocs/multicurrency/js/currencylayer.js.php @@ -0,0 +1,98 @@ + + * Copyright (C) 2016 Pierre-Henry Favre + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +include '../../main.inc.php'; +$langs->load('errors'); + +?> + +function getXMLHttpRequest() +{ + var xhr = null; + if (window.XMLHttpRequest || window.ActiveXObject) + { + if (window.ActiveXObject) + { + try + { + xhr = new ActiveXObject("Msxml2.XMLHTTP"); + } + catch(e) + { + xhr = new ActiveXObject("Microsoft.XMLHTTP"); + } + } + else + { + xhr = new XMLHttpRequest(); + } + } + else + { + if (typeof $ !== "undefined") $.jnotify("transnoentitiesnoconv('multicurrency_error_browser_incompatible'); ?>", "error"); + else alert("transnoentitiesnoconv('multicurrency_error_browser_incompatible'); ?>"); + + return null; + } + + return xhr; +} + +function request(url, callback) +{ + var xhr = getXMLHttpRequest(); + xhr.onreadystatechange = function() + { + if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) + { + callback(xhr.responseText); + } + + }; + + xhr.open("GET", url, true); + xhr.send(null); +} + +function syncronize_rates() +{ + document.getElementById("bt_sync").disabled = true; + var url_sync = "http://apilayer.net/api/live?access_key=global->MULTICURRENCY_APP_ID; ?>&format=1global->MULTICURRENCY_APP_SOURCE)) echo '&source='.$conf->global->MULTICURRENCY_APP_SOURCE; ?>"; + request(url_sync, update_rates); +} + +function update_rates(responseText) +{ + var response = JSON.parse(responseText); + if (response.success) + { + var url = "/multicurrency/ajax/updaterates.php?sync_response="+JSON.stringify(response); + request(url, reloadpage); + } + else + { + if (typeof $ !== "undefined") $.jnotify("transnoentitiesnoconv('multicurrency_syncronize_error'); ?>: "+response.error.info, "error"); + else alert("transnoentitiesnoconv('multicurrency_syncronize_error'); ?>: "+response.error.info); + } +} + +function reloadpage(responseText) +{ + document.getElementById("bt_sync").disabled = false; + window.location.href = window.location.pathname; +}