bericht/api/pages.php
Eduard Wisch 44a86fa63d
All checks were successful
Deploy bericht / deploy (push) Successful in 1s
feat: API-Endpoints für Phase 4 Block 1
- api/pages.php:
  - DELETE (oder POST ?delete=1) — Seite aus Bericht entfernen
    (source_path wird nur gelöscht wenn unter bericht/work/, damit
     Anhänge von Auftrag/Rechnung nicht mit rausfliegen)
  - POST {note, rotation} — Meta einer Seite updaten
  - POST ?action=signature&bericht_id=X + multipart file= — PNG
    Unterschrift als neue Seite am Bericht anhängen mit
    note='Unterschrift Kunde'
- api/pdf.php — liefert finales PDF oder on-the-fly Preview mit
  JWT-Auth (damit PWA das PDF direkt im <iframe> anzeigen kann)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
[deploy]
2026-04-09 00:54:56 +02:00

94 lines
3.9 KiB
PHP

<?php
/* Page-Operationen für Bericht-Seiten (PWA).
*
* DELETE /api/pages.php?id=<page_id> — Seite löschen
* POST /api/pages.php?id=<page_id> — Body { note: "..." } — Notiz setzen
* Body { rotation: 90 } — rotieren
* POST /api/pages.php?action=signature&bericht_id=<id>
* multipart: file=<PNG> — Unterschrift als neue Seite am Bericht anhängen
*/
require_once __DIR__.'/_inc.php';
api_authenticate();
global $db, $user;
if (!$user->hasRight('bericht', 'write')) api_fail('Permission denied', 403);
$method = $_SERVER['REQUEST_METHOD'];
$action = $_GET['action'] ?? '';
/* ---------- Unterschrift als neue Seite anhängen ---------- */
if ($method === 'POST' && $action === 'signature') {
$bericht_id = (int) ($_GET['bericht_id'] ?? 0);
if (!$bericht_id) api_fail('bericht_id fehlt');
if (empty($_FILES['file']['tmp_name'])) api_fail('file fehlt');
$bericht = new Bericht($db);
if ($bericht->fetch($bericht_id) <= 0) api_fail('Bericht nicht gefunden', 404);
require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
$workdir = DOL_DATA_ROOT.'/bericht/work/'.$bericht_id;
if (!is_dir($workdir)) dol_mkdir($workdir);
$filename = 'signature_'.dol_print_date(dol_now(), '%Y%m%d_%H%M%S').'.png';
$target = $workdir.'/'.$filename;
if (!move_uploaded_file($_FILES['file']['tmp_name'], $target)) api_fail('Upload fehlgeschlagen', 500);
$relpath = str_replace(DOL_DATA_ROOT.'/', '', $target);
$resm = $db->query("SELECT COALESCE(MAX(page_order),0) AS m FROM ".$db->prefix()."bericht_page WHERE fk_bericht = ".$bericht_id);
$next = ($resm && ($o = $db->fetch_object($resm))) ? ((int) $o->m) + 1 : 1;
$page = new BerichtPage($db);
$page->fk_bericht = $bericht_id;
$page->page_order = $next;
$page->source_type = 'upload';
$page->source_path = $relpath;
$page->note = 'Unterschrift Kunde';
if ($page->create() <= 0) api_fail('Page-Insert fehlgeschlagen', 500);
api_ok(array('page_id' => $page->id, 'filename' => $filename));
}
$page_id = (int) ($_GET['id'] ?? 0);
if (!$page_id) api_fail('id fehlt');
// Page laden + prüfen dass sie zum User-scope gehört (einfach: Admin oder Bericht-schreiben)
$pres = $db->query("SELECT rowid, fk_bericht, source_path, source_type FROM ".$db->prefix()."bericht_page WHERE rowid = ".$page_id);
if (!$pres || !($prow = $db->fetch_object($pres))) api_fail('Seite nicht gefunden', 404);
/* ---------- DELETE page ---------- */
if ($method === 'DELETE' || ($method === 'POST' && ($_GET['delete'] ?? '') === '1')) {
if (!$user->hasRight('bericht', 'delete')) api_fail('Permission denied', 403);
// Quell-Datei löschen nur wenn sie im bericht/work/ liegt (nicht bei Anhängen von Auftrag/Rechnung)
if ($prow->source_path && strpos($prow->source_path, 'bericht/work/') === 0) {
$full = bericht_resolve_data_path($prow->source_path);
if ($full && file_exists($full)) @unlink($full);
}
// Multi-Image-Slot-Einträge mit löschen
$db->query("DELETE FROM ".$db->prefix()."bericht_page_image WHERE fk_page = ".$page_id);
$db->query("DELETE FROM ".$db->prefix()."bericht_page WHERE rowid = ".$page_id);
api_ok();
}
/* ---------- UPDATE page (note, rotation) ---------- */
if ($method === 'POST') {
$in = api_input();
$sets = array();
if (isset($in['note'])) {
$note = (string) $in['note'];
$sets[] = "note = ".($note !== '' ? "'".$db->escape($note)."'" : "NULL");
}
if (isset($in['rotation'])) {
$rot = (int) $in['rotation'];
$rot = (($rot % 360) + 360) % 360;
$sets[] = "rotation = ".$rot;
}
if (empty($sets)) api_fail('Nichts zu aktualisieren');
$sql = "UPDATE ".$db->prefix()."bericht_page SET ".implode(',', $sets)." WHERE rowid = ".$page_id;
if (!$db->query($sql)) api_fail($db->lasterror(), 500);
api_ok();
}
api_fail('Methode nicht unterstützt', 405);