UPDATE move ajax request in js file and call a new php file to update all rates

This commit is contained in:
phf 2016-03-20 15:17:30 +01:00
parent 0a533660b8
commit 661ee72d55
3 changed files with 136 additions and 88 deletions

View File

@ -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 = '<a href="' . DOL_URL_ROOT . '/admin/modules.php">'
@ -228,86 +223,6 @@ print '</td></tr>';
print '</table>';
print '<br />';
print '<script type="text/javascript">
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("'.$langs->transnoentitiesnoconv('multicurrency_error_browser_incompatible').'", "error");
else alert("'.$langs->transnoentitiesnoconv('multicurremulticurrency_error_browser_incompatiblency_syncronize_error').'");
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='.$conf->global->MULTICURRENCY_APP_ID.'&format=1'.(!empty($conf->global->MULTICURRENCY_APP_SOURCE) ? '&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.php?sync_response="+JSON.stringify(response);
request(url, reloadpage);
}
else
{
if (typeof $ !== "undefined") $.jnotify("'.$langs->transnoentitiesnoconv('multicurrency_syncronize_error').': "+response.error.info, "error");
else alert("'.$langs->transnoentitiesnoconv('multicurrency_syncronize_error').': "+response.error.info);
}
}
function reloadpage(responseText)
{
document.getElementById("bt_sync").disabled = false;
window.location.href = window.location.pathname;
}
</script>';
$var=false;
print '<table class="noborder" width="100%">';
print '<tr class="liste_titre">';

View File

@ -0,0 +1,35 @@
<?php
/* <one line to give the program's name and a brief idea of what it does.>
* Copyright (C) 2016 Pierre-Henry Favre <phf@atm-consulting.fr>
*
* 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 <http://www.gnu.org/licenses/>.
*/
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);
}

View File

@ -0,0 +1,98 @@
<?php
/* <one line to give the program's name and a brief idea of what it does.>
* Copyright (C) 2016 Pierre-Henry Favre <phf@atm-consulting.fr>
*
* 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 <http://www.gnu.org/licenses/>.
*/
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("<?php echo $langs->transnoentitiesnoconv('multicurrency_error_browser_incompatible'); ?>", "error");
else alert("<?php echo $langs->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=<?php echo $conf->global->MULTICURRENCY_APP_ID; ?>&format=1<?php if (!empty($conf->global->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 = "<?php echo DOL_URL_ROOT; ?>/multicurrency/ajax/updaterates.php?sync_response="+JSON.stringify(response);
request(url, reloadpage);
}
else
{
if (typeof $ !== "undefined") $.jnotify("<?php echo $langs->transnoentitiesnoconv('multicurrency_syncronize_error'); ?>: "+response.error.info, "error");
else alert("<?php echo $langs->transnoentitiesnoconv('multicurrency_syncronize_error'); ?>: "+response.error.info);
}
}
function reloadpage(responseText)
{
document.getElementById("bt_sync").disabled = false;
window.location.href = window.location.pathname;
}