From 655f1230c2b29374ff5c098d44f6c237c6f837d8 Mon Sep 17 00:00:00 2001 From: johnnyq Date: Mon, 10 Jun 2024 15:53:26 -0400 Subject: [PATCH] Added Export Products CSV --- post/product.php | 44 ++++++++++++++++++++++++++++++++++++++++ product_export_modal.php | 23 +++++++++++++++++++++ products.php | 1 + 3 files changed, 68 insertions(+) create mode 100644 product_export_modal.php diff --git a/post/product.php b/post/product.php index bc2f106a..ae7ee71b 100644 --- a/post/product.php +++ b/post/product.php @@ -84,3 +84,47 @@ if (isset($_GET['delete_product'])) { header("Location: " . $_SERVER["HTTP_REFERER"]); } + +if (isset($_POST['export_products_csv'])) { + + //get records from database + $sql = mysqli_query($mysqli,"SELECT * FROM products + LEFT JOIN categories ON product_category_id = category_id + LEFT JOIN taxes ON product_tax_id = tax_id + WHERE product_archived_at IS NULL + ORDER BY product_name DESC + "); + + if (mysqli_num_rows($sql) > 0) { + $delimiter = ","; + $filename = "$session_company_name-Products.csv"; + + //create a file pointer + $f = fopen('php://memory', 'w'); + + //set column headers + $fields = array('Product', 'Description', 'Price', 'Currency', 'Category', 'Tax'); + fputcsv($f, $fields, $delimiter); + + //output each row of the data, format line as csv and write to file pointer + while($row = mysqli_fetch_assoc($sql)) { + $lineData = array($row['product_name'], $row['product_description'], $row['product_price'], $row['product_currency_code'], $row['category_name'], $row['tax_name']); + fputcsv($f, $lineData, $delimiter); + } + + //move back to beginning of file + fseek($f, 0); + + //set headers to download file rather than displayed + header('Content-Type: text/csv'); + header('Content-Disposition: attachment; filename="' . $filename . '";'); + + //output all remaining data on a file pointer + fpassthru($f); + } + + //Logging + mysqli_query($mysqli,"INSERT INTO logs SET log_type = 'Product', log_action = 'Export', log_description = '$session_name exported products to CSV File', log_ip = '$session_ip', log_user_agent = '$session_user_agent', log_user_id = $session_user_id"); + + exit; +} \ No newline at end of file diff --git a/product_export_modal.php b/product_export_modal.php new file mode 100644 index 00000000..117f00d5 --- /dev/null +++ b/product_export_modal.php @@ -0,0 +1,23 @@ + diff --git a/products.php b/products.php index c6bf601e..f0e8557d 100644 --- a/products.php +++ b/products.php @@ -211,5 +211,6 @@ $num_rows = mysqli_fetch_row(mysqli_query($mysqli, "SELECT FOUND_ROWS()"));