<?php
/* Speichert Layout / image_scale / image_align einer Seite.
 * POST: pageid, layout, image_scale, image_align, token
 */
require_once __DIR__.'/_inc.php';

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

$pageid = (int) ($_POST['pageid'] ?? 0);
if (!$pageid) bericht_ajax_fail('pageid fehlt');

$layout = $_POST['layout']      ?? 'single';
$scale  = (float) ($_POST['image_scale'] ?? 1.0);
$align  = $_POST['image_align'] ?? 'fit';

$valid_layouts = array('single', 'grid_2', 'grid_2v', 'grid_4', 'grid_6');
if (!in_array($layout, $valid_layouts, true)) $layout = 'single';

$valid_align = array('fit', 'center', 'topleft', 'topright', 'bottomleft', 'bottomright');
if (!in_array($align, $valid_align, true)) $align = 'fit';

$scale = max(0.2, min(1.0, $scale));

$sql = "UPDATE ".$db->prefix()."bericht_page SET "
    ."layout = '".$db->escape($layout)."',"
    ."image_scale = ".((float) $scale).","
    ."image_align = '".$db->escape($align)."'"
    ." WHERE rowid = ".((int) $pageid);
if (!$db->query($sql)) bericht_ajax_fail($db->lasterror());

bericht_ajax_ok(array('layout' => $layout, 'image_scale' => $scale, 'image_align' => $align));
