Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add download logs function #262

Merged
merged 11 commits into from
Nov 8, 2024
15 changes: 15 additions & 0 deletions plugin/css/tbk.css
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,21 @@
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.button.tbk-button-secondary {
background: white;
border-color: #D5006C;
color: #D5006C;
transition: background-color 0.3s ease;
}

.button.tbk-button-secondary:hover,
.button.tbk-button-secondary:focus {
background: white;
border-color: #D5006C;
color: #D5006C;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.log-container {
width: calc(100vw - 480px);
max-height: 600px;
Expand Down
61 changes: 61 additions & 0 deletions plugin/js/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,65 @@ jQuery(function($) {
notice_id: noticeId
});
});

function checkPermission(fileToDownload) {
return $.ajax({
url: ajaxurl,
type: 'POST',
data: {
action: 'checkCanDownloadLogFile',
file: fileToDownload
}
}).then(function (response) {
return response.data;
}).catch(function () {
return false;
});
}

function showNotice(title, message, type = 'success') {
const notice = $('<div>')
.addClass(`is-dismissible notice notice-${type}`)
.prepend(`<p><strong>${title}</strong><br>${message}</p>`);

const dismissButton = $('<button>')
.addClass('notice-dismiss');

notice.append(dismissButton);


notice.find('.notice-dismiss').on('click', function () {
notice.fadeOut(300, function () {
notice.remove();
});
});

$('#logs-container').prepend(notice);
}

$('#btnDownload').on('click', function (e) {
e.preventDefault();
const logFileSelected = $('#log_file').val();

if (!logFileSelected) {
showNotice('Error en la descarga', 'Debes seleccionar un archivo', 'error');
return;
}

checkPermission(logFileSelected).then(function (checkResponse) {
if (!checkResponse) {
showNotice('Error en la descarga', 'No es posible descargar el archivo', 'error');
return;
}
if (checkResponse?.canDownload) {
window.location.href = checkResponse.downloadUrl;
return;
}
showNotice('Error en la descarga', checkResponse.error, 'error');

});
});



})
5 changes: 3 additions & 2 deletions plugin/templates/admin/log.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
$lineCountTitle = "Cantidad de líneas que posee el último archivo de registro creado";
?>

<div class="tbk-box">
<div class="tbk-box" id="logs-container">
<h3 class="tbk_title_h3">Información de Registros</h3>
<div class="tbk-plugin-info-container">
<div class="info-column">
Expand Down Expand Up @@ -35,7 +35,7 @@
<input type="hidden" name="page" value="transbank_webpay_plus_rest">
<input type="hidden" name="tbk_tab" value="logs">

<select class="select label" name="log_file" <?= !$folderHasLogs ? "disabled" : '' ?>>
<select class="select label" name="log_file" id="log_file" <?= !$folderHasLogs ? "disabled" : '' ?>>
<?php
$options = '';

Expand All @@ -58,6 +58,7 @@
<input type="submit" class="button button-primary tbk-button-primary"
<?= !$folderHasLogs ? 'disabled' : '' ?>
value="Ver">
<button class="button button-secondary tbk-button-secondary" id="btnDownload">Descargar</button>
</form>
</div>
</div>
Expand Down
49 changes: 47 additions & 2 deletions plugin/webpay-rest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@
add_action('wp_loaded', 'woocommerceTransbankInit');
add_action('admin_init', 'on_transbank_rest_webpay_plugins_loaded');

add_action('wp_ajax_check_connection', ConnectionCheck::class.'::check');
add_action('wp_ajax_check_exist_tables', TableCheck::class.'::check');
add_action('wp_ajax_check_connection', ConnectionCheck::class . '::check');
add_action('wp_ajax_check_exist_tables', TableCheck::class . '::check');
add_action('wp_ajax_checkCanDownloadLogFile', 'checkCanDownloadLogFile');
Matiasnickolas marked this conversation as resolved.
Show resolved Hide resolved
add_action('wp_ajax_get_transaction_status', [new TransactionStatusController(), 'getStatus']);
add_action('woocommerce_before_cart', 'transbank_rest_before_cart');

Expand Down Expand Up @@ -274,3 +275,47 @@ function () {
}
);
}

function fileExistsInFolder($fileName, $folderPath)
{
$filesInFolder = array_filter(scandir($folderPath), function ($file) use ($folderPath) {
return is_file($folderPath . '/' . $file);
});

return in_array($fileName, array_values($filesInFolder));
}

function checkCanDownloadLogFile()
{
$response = [
'canDownload' => false,
'downloadUrl' => '',
'error' => ''
];

if (!is_user_logged_in()) {
$response['error'] = 'Debes iniciar sesión para poder descargar';
wp_send_json_error($response);
}

if (!current_user_can('manage_options')) {
$response['error'] = 'No tienes permisos para descargar';
wp_send_json_error($response);
}

$baseUploadDir = wp_upload_dir();
$tbkLogsFolder = '/transbank_webpay_plus_rest/logs/';
$logName = sanitize_text_field($_POST['file']);
$folderPath = $baseUploadDir['basedir'] . $tbkLogsFolder;
$fileExists = fileExistsInFolder($logName, $folderPath);

if (!$fileExists) {
$response['error'] = 'No existe el archivo solicitado';
wp_send_json_error($response);
}

$response['canDownload'] = true;
$response['downloadUrl'] = $baseUploadDir['baseurl'] . $tbkLogsFolder . $logName;

wp_send_json_success($response);
}
Loading