Skip to content

Commit

Permalink
Merge pull request #72 from VSLCatena/development
Browse files Browse the repository at this point in the history
Add data export
  • Loading branch information
Kipjr authored Sep 12, 2024
2 parents 73d3c01 + 93e8721 commit 7f4f176
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ ENV DEPLOY_ENV=production
RUN mkdir /docker-entrypoint-initdb.d && \
cp /var/www/html/data/marktwerking.structure.sql /docker-entrypoint-initdb.d/ && \
cp /usr/local/etc/php/php.ini-${DEPLOY_ENV} /usr/local/etc/php/php.ini && \
chown www-data:root /var/www/html/bar/tmp && \
sed -ri ' \
s!^(\s*CustomLog)\s+\S+!\1 /proc/self/fd/1!g; \
s!^(\s*ErrorLog)\s+\S+!\1 /proc/self/fd/2!g; \
Expand Down
75 changes: 75 additions & 0 deletions src/html/bar/download.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php include './password_protect.php'; ?>
<?php


require_once '../core.php';

if (MW_DEBUG == true) {
error_reporting(E_ALL);
ini_set('display_errors', 1);
}



// SQL queries for both tables
$sql = Array(
"SELECT * FROM drinks",
"SELECT * FROM orders",
"SELECT * FROM order_history"
);

$csvFiles = [];

echo "<h2>CSV Data Preview</h2>";
echo "<ul>";

foreach($sql as $query){

// Match table name from query
preg_match('/from\s+(\S+)/i', $query, $matches);
$tableName = $matches[1];

// Create a temporary file
$filepath = tempnam(__DIR__ . "/tmp",$tableName . "_csv_");
$tmpFile = basename($filepath);

// Store temporary file path to download later
$csvFiles[] = $tmpFile;

// Open the temporary file for writing
$output = fopen($filepath, 'w');

// Execute query
$stmt = $pdo->prepare($query);
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);

// Write column headers to the CSV
if (!empty($result)) {
fputcsv($output, array_keys($result[0]));
}

// Write each row to the CSV
foreach ($result as $row) {
fputcsv($output, $row);
}

// Close the temporary file
fclose($output);

// Display the CSV content in the browser as a preview
echo "<h3>Table: $tableName</h3>";
echo "<pre>";

// Open the file for reading and output its contents
$csvData = file_get_contents($filepath);
echo nl2br($csvData); // Display CSV data

echo "</pre>";

// Provide a link to download the file
echo '<li><a href="./tmp/' . $tmpFile . '">Download ' . $tableName . ' CSV</a></li>';
}

echo "</ul>";
?>
1 change: 1 addition & 0 deletions src/html/bar/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@
<div id="navbar" class="navbar-right navbar-collapse collapse">
<ul class="nav navbar-nav navbar-right">
<li><a href="../index.php" type="button">Beamer</a></li>
<li><a href="download.php" type="button">Export data</a></li>
<li><a data-target="#financial" data-toggle="modal" type="button">Financial</a></li>
<li><a data-target="#settings" data-toggle="modal" type="button">Settings</a></li>
<li><a href="./index.php?logout">Uitloggen</a></li>
Expand Down

0 comments on commit 7f4f176

Please sign in to comment.