From 8f7ee3fec13a2e4a5ddc816c3e051c35f3abf097 Mon Sep 17 00:00:00 2001 From: Diego Najar Date: Wed, 30 Jan 2019 23:15:36 +0100 Subject: [PATCH] Improves in Media manager and Ajax responses --- bl-kernel/admin/themes/booty/html/media.php | 29 ++++++++++++++------- bl-kernel/ajax/delete-image.php | 10 ++----- bl-kernel/ajax/list-images.php | 15 +++-------- bl-kernel/ajax/upload-images.php | 16 +++--------- bl-kernel/functions.php | 6 +++++ 5 files changed, 36 insertions(+), 40 deletions(-) diff --git a/bl-kernel/admin/themes/booty/html/media.php b/bl-kernel/admin/themes/booty/html/media.php index 1dce774f..4249f865 100644 --- a/bl-kernel/admin/themes/booty/html/media.php +++ b/bl-kernel/admin/themes/booty/html/media.php @@ -99,6 +99,10 @@ function cleanTable() { // Show the files in the table function displayFiles(files) { + if (!Array.isArray(files)) { + return false; + } + // Clean table cleanTable(); @@ -134,10 +138,12 @@ function getFiles(pageNumber) { { tokenCSRF: tokenCSRF, pageNumber: pageNumber, uuid: "", - path: "thumbnails" // the paths are defined in the list-images.php + path: "thumbnails" // the paths are defined in ajax/list-images }, - function(data) { - displayFiles(data.files); + function(data) { // success function + if (data.status==0) { + displayFiles(data.files); + } } ); } @@ -149,8 +155,10 @@ function deleteMedia(filename) { filename: filename, uuid: "" }, - function(data) { - getFiles(1); + function(data) { // success function + if (data.status==0) { + getFiles(1); + } } ); } @@ -171,7 +179,7 @@ $(document).ready(function() { // Check file size ? // Check file type/extension ? - $("#jsbluditProgressBar").width("1%"); + $("#jsbluditProgressBar").width("0"); // Data to send via AJAX var uuid = $("#jsuuid").val(); @@ -198,9 +206,12 @@ $(document).ready(function() { } return xhr; } - }).done(function() { - // Get the files of the first page, this include the files uploaded - getFiles(1); + }).done(function(data) { + if (data.status==0) { + $("#jsbluditProgressBar").width("0"); + // Get the files for the first page, this include the files uploaded + getFiles(1); + } }); }); }); diff --git a/bl-kernel/ajax/delete-image.php b/bl-kernel/ajax/delete-image.php index d52a5547..6bdcd4f7 100644 --- a/bl-kernel/ajax/delete-image.php +++ b/bl-kernel/ajax/delete-image.php @@ -11,10 +11,7 @@ $uuid = empty($_POST['uuid']) ? false : $_POST['uuid']; // ---------------------------------------------------------------------------- if ($filename==false) { - exit (json_encode(array( - 'status'=>1, - 'message'=>'The filename is empty.' - ))); + ajaxResponse(1, 'The filename is empty.'); } if ($uuid && IMAGE_RESTRICT) { @@ -35,9 +32,6 @@ if (Sanitize::pathFile($thumbnailPath.$filename)) { Filesystem::rmfile($thumbnailPath.$filename); } -exit (json_encode(array( - 'status'=>0, - 'message'=>'Image deleted.' -))); +ajaxResponse(0, 'Image deleted.'); ?> \ No newline at end of file diff --git a/bl-kernel/ajax/list-images.php b/bl-kernel/ajax/list-images.php index 93b9996f..49c75c7f 100644 --- a/bl-kernel/ajax/list-images.php +++ b/bl-kernel/ajax/list-images.php @@ -22,10 +22,7 @@ if ($path=='thumbnails') { $path = PATH_UPLOADS_THUMBNAILS; } } else { - exit (json_encode(array( - 'status'=>1, - 'files'=>'Invalid path.' - ))); + ajaxResponse(1, 'Invalid path.'); } // Get all files from the directory $path, also split the array by numberOfItems @@ -44,16 +41,12 @@ if (isset($listOfFilesByPage[$pageNumber])) { // Returns the number of chunks for the paginator // Returns the files inside the chunk - exit (json_encode(array( - 'status'=>0, + ajaxResponse(0, 'List of files and number of chunks.', array( 'numberOfPages'=>count($listOfFilesByPage), 'files'=>$files - ))); + )); } -exit (json_encode(array( - 'status'=>1, - 'files'=>'Out of index.' -))); +ajaxResponse(1, 'Chunks out of index'); ?> \ No newline at end of file diff --git a/bl-kernel/ajax/upload-images.php b/bl-kernel/ajax/upload-images.php index 5748c0a1..9d799795 100644 --- a/bl-kernel/ajax/upload-images.php +++ b/bl-kernel/ajax/upload-images.php @@ -36,10 +36,7 @@ foreach ($_FILES['bluditInputFiles']['name'] as $key=>$filename) { if ($_FILES['bluditInputFiles']['error'][$key] != 0) { $message = 'Error occurred uploading the image, max file size allowed: '.ini_get('upload_max_filesize'); Log::set($message, LOG_TYPE_ERROR); - exit (json_encode(array( - 'status'=>1, - 'message'=>$message - ))); + ajaxResponse(1, $message); } // Convert URL characters such as spaces or quotes to characters @@ -51,10 +48,7 @@ foreach ($_FILES['bluditInputFiles']['name'] as $key=>$filename) { if (!in_array($fileExtension, $allowedExtensions) ) { $message = 'Extension file not supported.'; Log::set($message, LOG_TYPE_ERROR); - exit (json_encode(array( - 'status'=>1, - 'message'=>$message - ))); + ajaxResponse(1, $message); } // Generate the next filename to not overwrite the original file @@ -75,10 +69,8 @@ foreach ($_FILES['bluditInputFiles']['name'] as $key=>$filename) { } } -exit (json_encode(array( - 'status'=>0, - 'message'=>'Image uploaded success.', +ajaxResponse(0, 'List of files and number of chunks.', array( 'filename'=>$nextFilename -))); +)); ?> \ No newline at end of file diff --git a/bl-kernel/functions.php b/bl-kernel/functions.php index 9c414088..7f276c44 100644 --- a/bl-kernel/functions.php +++ b/bl-kernel/functions.php @@ -794,3 +794,9 @@ function activateTheme($themeDirectory) { } return false; } + +function ajaxResponse($status=0, $message="", $data=array()) { + $default = array('status'=>$status, 'message'=>$message); + $output = array_merge($default, $data); + exit (json_encode($output)); +} \ No newline at end of file