Files
casadoc/public/userportal/api/home-save.php
T
2026-07-27 21:04:07 +03:00

88 lines
3.5 KiB
PHP

<?php
require __DIR__ . '/_bootstrap.php';
/**
* @OA\Post(
* path="/home-save.php",
* tags={"Homes"},
* summary="Create or update property",
* description="idhome missing/0 creates; otherwise updates own property.",
* security={{"bearerAuth":{}}},
* @OA\RequestBody(required=true, @OA\JsonContent(
* @OA\Property(property="idhome", type="integer"),
* @OA\Property(property="name", type="string"),
* @OA\Property(property="comment", type="string"),
* @OA\Property(property="fulladdress", type="string"),
* @OA\Property(property="address", type="string"),
* @OA\Property(property="zip", type="string"),
* @OA\Property(property="city", type="string"),
* @OA\Property(property="country", type="string"),
* @OA\Property(property="latitude", type="string"),
* @OA\Property(property="longitude", type="string"),
* @OA\Property(property="cadastral_municipality", type="string"),
* @OA\Property(property="cadastral_section", type="string"),
* @OA\Property(property="cadastral_sheet", type="string"),
* @OA\Property(property="cadastral_particle", type="string"),
* @OA\Property(property="cadastral_sub", type="string"),
* @OA\Property(property="cadastral_category", type="string"),
* @OA\Property(property="cadastral_class", type="string"),
* @OA\Property(property="cadastral_surface", type="string"),
* @OA\Property(property="cadastral_rendita", type="string"),
* @OA\Property(property="cadastral_notes", type="string")
* )),
* @OA\Response(response=200, description="OK", @OA\JsonContent(
* @OA\Property(property="data", ref="#/components/schemas/Home"))),
* @OA\Response(response=403, ref="#/components/responses/Forbidden"),
* @OA\Response(response=422, ref="#/components/responses/ValidationError")
* )
*/
require_method('POST');
$user = require_auth($pdo);
$in = body();
$allowed = [
'name', 'comment', 'fulladdress', 'address', 'zip', 'city', 'country',
'latitude', 'longitude', 'cadastral_municipality', 'cadastral_section',
'cadastral_sheet', 'cadastral_particle', 'cadastral_sub', 'cadastral_category',
'cadastral_class', 'cadastral_surface', 'cadastral_rendita', 'cadastral_notes',
];
$data = [];
foreach ($allowed as $col) {
if (array_key_exists($col, $in)) {
$data[$col] = $in[$col];
}
}
$idhome = (int) ($in['idhome'] ?? 0);
if ($idhome > 0) {
// Update: own property only.
if (!user_owns_home($pdo, $user, $idhome)) {
json_error(403, 'No access to this home');
}
if ($data) {
$set = implode(', ', array_map(fn ($c) => "$c = ?", array_keys($data)));
$stmt = $pdo->prepare("UPDATE home SET $set WHERE idhome = ? AND iduser = ?");
$stmt->execute([...array_values($data), $idhome, $user['id']]);
}
} else {
// Create.
// These columns are NOT NULL without a default in the legacy schema.
foreach (['name', 'address', 'zip', 'city', 'country', 'cadastral_municipality',
'latitude', 'longitude', 'fulladdress'] as $col) {
$data[$col] ??= '';
}
$data['iduser'] = $user['id'];
$cols = implode(', ', array_keys($data));
$ph = implode(', ', array_fill(0, count($data), '?'));
$stmt = $pdo->prepare("INSERT INTO home ($cols) VALUES ($ph)");
$stmt->execute(array_values($data));
$idhome = (int) $pdo->lastInsertId();
}
$stmt = $pdo->prepare('SELECT * FROM home WHERE idhome = ? LIMIT 1');
$stmt->execute([$idhome]);
json_data(present_home($stmt->fetch(), true, home_counts($pdo, [$idhome])[$idhome] ?? null));