Add very basic product import via CSV

This commit is contained in:
wrongecho
2026-08-03 18:58:53 +01:00
parent ad234c5e46
commit f87d43881e
3 changed files with 148 additions and 0 deletions

View File

@@ -0,0 +1,32 @@
<?php
require_once '../../../includes/modal_header.php';
ob_start();
?>
<div class="modal-header bg-dark">
<h5 class="modal-title"><i class="fa fa-fw fa-box-open mr-2"></i>Import Products</h5>
<button type="button" class="close text-white" data-dismiss="modal">
<span>&times;</span>
</button>
</div>
<form action="post.php" method="post" enctype="multipart/form-data" autocomplete="off">
<input type="hidden" name="csrf_token" value="<?= $_SESSION['csrf_token'] ?>">
<div class="modal-body">
<p><strong>Format csv file with headings & data:</strong><br>Product Name, Description, Price</p>
<hr>
<div class="form-group my-4">
<input type="file" class="form-control-file" name="file" accept=".csv" required>
</div>
<hr>
<div>Download: <a class="text-bold" href="post.php?download_products_csv_template">sample csv template</a></div>
</div>
<div class="modal-footer">
<button type="submit" name="import_products_csv" class="btn btn-primary text-strong"><i class="fas fa-upload mr-2"></i>Import</button>
<button type="button" class="btn btn-light" data-dismiss="modal"><i class="fa fa-times mr-2"></i>Cancel</button>
</div>
</form>
<?php
require_once '../../../includes/modal_footer.php';

View File

@@ -353,3 +353,112 @@ if (isset($_POST['add_product_stock'])) {
redirect();
}
if (isset($_POST["import_products_csv"])) {
validateCSRFToken();
enforceUserPermission('module_sales', 2);
$error = false;
if (!empty($_FILES["file"]["tmp_name"])) {
$file_name = $_FILES["file"]["tmp_name"];
} else {
flashAlert("Please select a file to upload.", 'error');
redirect();
}
//Check file is CSV
$file_extension = strtolower(end(explode('.',$_FILES['file']['name'])));
$allowed_file_extensions = array('csv');
if (in_array($file_extension,$allowed_file_extensions) === false) {
$error = true;
flashAlert("Bad file extension", 'error');
}
//Check file isn't empty
elseif ($_FILES["file"]["size"] < 1) {
$error = true;
flashAlert("Bad file size (empty?)", 'error');
}
//(Else)Check column count
$f = fopen($file_name, "r");
$f_columns = fgetcsv($f, 1000, ",");
if (!$error & count($f_columns) != 3) {
$error = true;
flashAlert("Bad column count.", 'error');
}
//Else, parse the file
if (!$error) {
$file = fopen($file_name, "r");
fgetcsv($file, 1000, ","); // Skip first line
$row_count = 0;
while(($column = fgetcsv($file, 1000, ",")) !== false) {
$name = '';
if (isset($column[0])) {
$name = escapeSql(substr($column[0], 0, 200));
}
$description = '';
if (isset($column[1])) {
$description = escapeSql($column[1]);
}
$price = 0;
if (isset($column[2])) {
$price = floatval($column[2]);
}
if (!empty($name)) {
mysqli_query($mysqli, "INSERT INTO products SET product_name = '$name', product_type = 'product', product_description = '$description', product_price = '$price', product_currency_code = '$session_company_currency', product_category_id = 0");
$row_count++;
}
}
fclose($file);
logAudit("Product", "Import", "$session_name imported $row_count product(s) via CSV file");
flashAlert("<strong>$row_count</strong> Product(s) added");
redirect();
}
//Check for any errors, if there are notify user and redirect
if ($error) {
redirect();
}
}
if (isset($_GET['download_products_csv_template'])) {
$delimiter = ",";
$enclosure = '"';
$escape = '\\'; // backsla
$filename = "Products-Template.csv";
//create a file pointer
$f = fopen('php://memory', 'w');
//set column headers
$fields = array('Product Name', 'Description', 'Price');
fputcsv($f, $fields, $delimiter, $enclosure, $escape);
//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);
exit;
}

View File

@@ -69,6 +69,13 @@ $num_rows = mysqli_fetch_row(mysqli_query($mysqli, "SELECT FOUND_ROWS()"));
<?php } ?>
<button type="button" class="btn btn-primary dropdown-toggle dropdown-toggle-split" data-toggle="dropdown"></button>
<div class="dropdown-menu">
<?php if ($type_display == 'Products') { ?>
<a class="dropdown-item text-dark ajax-modal" href="#"
data-modal-url="modals/product/product_import.php">
<i class="fa fa-fw fa-upload mr-2"></i>Import
</a>
<div class="dropdown-divider"></div>
<?php } ?>
<a class="dropdown-item text-dark ajax-modal"
data-modal-url="<?= buildExportModalUrl('modals/product/product_export.php', ['type', 'category', 'archived', 'q']) ?>">
<i class="fa fa-fw fa-download mr-2"></i>Export