Skip to content

Commit

Permalink
Merge pull request #262 from TransbankDevelopers/feat/add-download-lo…
Browse files Browse the repository at this point in the history
…gs-function

feat: add download logs function
  • Loading branch information
Matiasnickolas authored Nov 8, 2024
2 parents b3da229 + 1b77e0c commit ff974a2
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 8 deletions.
25 changes: 21 additions & 4 deletions plugin/css/tbk.css
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,8 @@
}

.tbk-box {
--tbk-red: #D5006C;
--tbk-red-2: #C00063;
background: #fff;
border-radius: 10px;
padding: 15px;
Expand Down Expand Up @@ -316,20 +318,35 @@
}

.button.tbk-button-primary {
background: #D5006C;
border-color: #D5006C;
background: var(--tbk-red);
border-color: var(--tbk-red);
color: white;
transition: background-color 0.3s ease;
}

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

.button.tbk-button-secondary {
background: white;
border-color: var(--tbk-red);
color: var(--tbk-red);
transition: background-color 0.3s ease;
}

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

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

function checkPermission(fileToDownload) {
return $.ajax({
url: ajaxurl,
type: 'POST',
data: {
action: 'check_can_download_file',
file: fileToDownload
}
}).then(function (response) {
return response;
}).catch(function (error) {
return { success: false, data: {
error: error.message || "Error en la solicitud de descarga"
} };
});
}

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.success) {
window.location.href = checkResponse.data.downloadUrl;
return;
}
showNotice('Error en la descarga', checkResponse.data.error, 'error');

});
});



})
32 changes: 32 additions & 0 deletions plugin/shared/Helpers/PluginLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,36 @@ private function saveLogFileNameInCache(string $logFileName, int $expireTime)
{
set_transient(self::CACHE_LOG_NAME, $logFileName, $expireTime);
}

private static 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));
}

public static function checkCanDownloadLogFile()
{
if (!is_user_logged_in()) {
wp_send_json_error(['error' => 'Debes iniciar sesión para poder descargar']);
}

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

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

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

wp_send_json_success(['downloadUrl' => $baseUploadDir['baseurl'] . $tbkLogsFolder . $logName]);
}
}
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
6 changes: 4 additions & 2 deletions plugin/webpay-rest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Transbank\WooCommerce\WebpayRest\Blocks\WCGatewayTransbankOneclickBlocks;
use Transbank\WooCommerce\WebpayRest\Utils\ConnectionCheck;
use Transbank\WooCommerce\WebpayRest\Utils\TableCheck;
use Transbank\Plugin\Helpers\PluginLogger;

if (!defined('ABSPATH')) {
return;
Expand Down Expand Up @@ -41,8 +42,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_check_can_download_file', PluginLogger::class . '::checkCanDownloadLogFile');
add_action('wp_ajax_get_transaction_status', [new TransactionStatusController(), 'getStatus']);
add_action('woocommerce_before_cart', 'transbank_rest_before_cart');

Expand Down

0 comments on commit ff974a2

Please sign in to comment.