Skip to content

Commit

Permalink
Merge pull request #21 from Ente/TT-45
Browse files Browse the repository at this point in the history
TT-45: Save PDF/CSV exports onto server
  • Loading branch information
Ente authored Nov 12, 2024
2 parents 265c969 + 2dbac7c commit 9cf23ee
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* Added CSV export module class `Arbeitszeit\ExportModule\CSVExportModule` which can be used by clicking on `(CSV)` within All Worktimes
* Renamed all GUI elements from `Calendar` to `Notifications`
* Fixed being unable to edit notifications entries
* PDF and CSV exports are now directly saved onto the server. This is done automatically. Exports are saved within `data/exports/{ExportModuleName}/{username}/`

<!-- Fixed title not set for Plugin Hub -->
<!-- Fixed missing i18n files for `suite/admin/notifications/edit.php` -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function export($args) {
$month = $args["month"];
$user = $args["user"];


$this->saveAsCsv($args);
if (!is_string($year)) {
$year = date("Y");
}
Expand Down Expand Up @@ -58,6 +58,56 @@ public function export($args) {
exit;
}

public function saveAsCsv($args) {
$arbeit = new Arbeitszeit();
$year = $args["year"];
$month = $args["month"];
$user = $args["user"];

if (!is_string($year)) {
$year = date("Y");
}

$sql = "SELECT id, username, schicht_tag, schicht_anfang, schicht_ende, ort, pause_start, pause_end
FROM `arbeitszeiten`
WHERE YEAR(schicht_tag) = ? AND MONTH(schicht_tag) = ? AND username = ?
ORDER BY schicht_tag DESC";
$statement = $arbeit->db()->sendQuery($sql);
$statement->execute([$year, $month, $user]);
$data = $statement->fetchAll(\PDO::FETCH_ASSOC);

// Falls keine Daten vorhanden sind
if (empty($data)) {
return false;
}

// Erstelle den Dateipfad basierend auf den Argumenten
$directory = $_SERVER["DOCUMENT_ROOT"] . "/data/exports/" . $this->getName() . "/$user";
$filename = "$directory/worktimes_{$year}-{$month}.csv";

// Verzeichnis erstellen, falls es nicht existiert
if (!is_dir($directory)) {
mkdir($directory, 0777, true);
}

// Datei öffnen
$output = fopen($filename, 'w');

// Setze die Spaltennamen in die CSV-Datei
$columns = ["ID", "Username", "Shift Date", "Shift Start", "Shift End", "Location/Notes", "Pause Start", "Pause End"];
fputcsv($output, $columns, ';');

// Datenzeilen in CSV schreiben
foreach ($data as $row) {
fputcsv($output, $row, ';');
}

fclose($output);

return $filename;
}


public function getName() {
return "CSVExportModule";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,36 @@ public function export($args) {
DATA;

return $data;
}

public function saveAsPdf($args) {
$html = $this->export($args);
$user = $args['user'] ?? "dummy";
$month = $args['month'] ?? "00";
$year = $args['year'] ?? "0000";


$directory = $_SERVER["DOCUMENT_ROOT"] . "/data/exports/" . $this->getName() . "/$user";
$filename = "$directory/worktimes_{$year}-{$month}.pdf";


if (!is_dir($directory)) {
mkdir($directory, 0777, true);
}


$dompdf = new \Dompdf\Dompdf();
$dompdf->loadHtml($html);
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();


file_put_contents($filename, $dompdf->output());

return $filename;
}

public function getName() {
return "PDFExportModule";
}
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"cweagans/composer-patches": "^1.7",
"ldaptools/ldaptools": "dev-master",

"php": ">=8.0"
"php": ">=8.0",
"dompdf/dompdf": "^3.0"
},
"config": {
"allow-plugins": {
Expand Down
1 change: 1 addition & 0 deletions suite/worktime/view_pdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@

$arbeit->auth()->login_validation();
echo $arbeit->exportModule()->export(array("module" => "PDFExportModule", "user" => $_GET["mitarbeiter"], "month" => $_GET["monat"], "year" => $_GET["jahr"]));
$arbeit->exportModule()->getExportModule("PDFExportModule")->saveAsPdf(array("user" => $_GET["mitarbeiter"], "month" => $_GET["monat"], "year" => $_GET["jahr"]));
?>

0 comments on commit 9cf23ee

Please sign in to comment.