<?php
/* Speichert Meta-Felder eines Berichts (Titel, Template, Format, Orientation).
 * POST: berichtid, titel, template_odt, page_format, page_orientation, token
 */
require_once __DIR__.'/_inc.php';

global $db, $user;
if (!$user->hasRight('bericht', 'write')) bericht_ajax_fail('Permission denied', 403);

$berichtid = (int) ($_POST['berichtid'] ?? 0);
$bericht = new Bericht($db);
if ($bericht->fetch($berichtid) <= 0) bericht_ajax_fail('Bericht nicht gefunden', 404);

if (isset($_POST['titel']))            $bericht->titel = (string) $_POST['titel'];
if (isset($_POST['template_odt']))     $bericht->template_odt = (string) $_POST['template_odt'];
if (isset($_POST['page_format']))      $bericht->page_format = in_array($_POST['page_format'], array('A4','A3','A5','Letter'), true) ? $_POST['page_format'] : 'A4';
if (isset($_POST['page_orientation'])) $bericht->page_orientation = in_array($_POST['page_orientation'], array('P','L'), true) ? $_POST['page_orientation'] : 'P';

if ($bericht->update($user) > 0) bericht_ajax_ok();
bericht_ajax_fail($bericht->error ?: 'Update fehlgeschlagen');
