diff --git a/src/bulk_action.php b/src/bulk_action.php index e59d302..d97ccf7 100644 --- a/src/bulk_action.php +++ b/src/bulk_action.php @@ -6,52 +6,96 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { $db = getDbInstance(); $json = json_decode(file_get_contents('php://input'), true); - $params = $json['params']; - $files = []; + if($json["action"] == "download") { + $params = $json['params']; + $files = []; + + if (isset($json['type'])) { + $type = filter_var($json['type'], FILTER_SANITIZE_FULL_SPECIAL_CHARS); + } else { + echo json_encode([ + 'data' => 'Type action field in the request.', + 'status' => 400 + ]); + exit(); + } + + if (count($params) == 0) { + echo json_encode([ + 'data' => 'No qrcodes were selected.', + 'status' => 400 + ]); + exit(); + } + + foreach ($params as $param) { + $row = $db->where('id', $param); + $row = $db->getOne("{$type}_qrcodes"); + @$files[] = SAVED_QRCODE_FOLDER . $row['qrcode']; + } + + $zip = new ZipArchive(); + $uniqid = uniqid(); + $relative_dir = SAVED_QRCODE_FOLDER . 'zip/qrcodes_' . $uniqid . '.zip'; + @unlink($relative_dir); + $url_path = SAVED_QRCODE_URL . 'zip/qrcodes_' . $uniqid . '.zip'; + $zip->open($relative_dir, ZipArchive::CREATE); + + foreach ($files as $file) { + $download_file = @file_get_contents($file, true); + $zip->addFromString(basename($file), $download_file); + } + + $zip->close(); - if (isset($json['type'])) { - $type = filter_var($json['type'], FILTER_SANITIZE_FULL_SPECIAL_CHARS); - } else { echo json_encode([ - 'data' => 'Type action field in the request.', - 'status' => 400 + 'data' => $url_path, + 'status' => 200 ]); exit(); - } + } else if($json["action"] == "delete") { + $params = $json['params']; + $files = []; + + if (isset($json['type'])) { + $type = filter_var($json['type'], FILTER_SANITIZE_FULL_SPECIAL_CHARS); + } else { + echo json_encode([ + 'data' => 'Type action field in the request.', + 'status' => 400 + ]); + exit(); + } + + if (count($params) == 0) { + echo json_encode([ + 'data' => 'No qrcodes were selected.', + 'status' => 400 + ]); + exit(); + } + + if($type == "dynamic") + $instance = new DynamicQrcode(); + else if($type == "static") + $instance = new StaticQrcode(); + else + die("Type not allowed"); + + foreach ($params as $param) { + $a = 0; + $instance->deleteQrcode($param, true); + } - if (count($params) == 0) { echo json_encode([ - 'data' => 'No qrcodes were selected.', - 'status' => 400 + 'action' => "delete", + 'data' => "Qrcode deleted", + 'status' => 200 ]); exit(); - } - foreach ($params as $param) { - $row = $db->where('id', $param); - $row = $db->getOne("{$type}_qrcodes"); - @$files[] = SAVED_QRCODE_FOLDER . $row['qrcode']; - } - - $zip = new ZipArchive(); - $uniqid = uniqid(); - $relative_dir = SAVED_QRCODE_FOLDER. 'zip/qrcodes_'. $uniqid .'.zip'; - @unlink($relative_dir); - $url_path = SAVED_QRCODE_URL . 'zip/qrcodes_'. $uniqid .'.zip'; - $zip->open($relative_dir, ZipArchive::CREATE); - - foreach ($files as $file) { - $download_file = @file_get_contents($file, true); - $zip->addFromString(basename($file), $download_file); - } - - $zip->close(); - - echo json_encode([ - 'data' => $url_path, - 'status' => 200 - ]); - exit(); + } else + exit("Action not allowed"); } else { exit('Direct access to this script not allowed.'); } diff --git a/src/dist/js/custom.js b/src/dist/js/custom.js index 4ebb9d3..7653209 100644 --- a/src/dist/js/custom.js +++ b/src/dist/js/custom.js @@ -459,7 +459,7 @@ }); const data = { - action: 'download', + action: $('select[name=action]').val(), params: params, type: $('input[name=type]').val() } @@ -472,8 +472,12 @@ contentType: 'application/json', success: (res) => { if (res.status == 200) { - window.location.href = res.data; - $('#err-msg').css('display', 'none'); + if(data["action"] === "download") { + window.location.href = res.data; + $('#err-msg').css('display', 'none'); + } else if(data["action"] === "delete") { + location.reload(); + } } if (res.status == 400) { diff --git a/src/forms/table_dynamic.php b/src/forms/table_dynamic.php index 472c5e6..24e590f 100644 --- a/src/forms/table_dynamic.php +++ b/src/forms/table_dynamic.php @@ -3,8 +3,20 @@