added multiple images feature for asset mgmt

This commit is contained in:
Luca Haid
2025-07-09 12:54:01 +02:00
parent 18bc10fc46
commit a0a7d92aa5
5 changed files with 204 additions and 35 deletions
@@ -25,6 +25,19 @@ class AssetManagementController extends TTCrud
}
}
/**
* This method is automatically called by the parent TTCrud->getByIdAction.
* It decodes the JSON string from the database into a PHP array.
*/
protected function getByIdParse($data) {
if (!empty($data['imageIds'])) {
$data['imageIds'] = json_decode($data['imageIds'], true);
} else {
$data['imageIds'] = [];
}
return $data;
}
protected function getAction()
{
$json = json_decode(file_get_contents('php://input'), true);
@@ -61,6 +74,13 @@ class AssetManagementController extends TTCrud
$rows = [];
foreach ($assets as $asset) {
$row = (array)$asset;
// Decode imageIds for table view if needed, though not directly displayed, useful for logic
if (!empty($row['imageIds'])) {
$row['imageIds'] = json_decode($row['imageIds'], true);
} else {
$row['imageIds'] = [];
}
$latestJournal = $journalMap[$asset->id] ?? null;
// Determine current status based on the latest journal entry.
@@ -190,6 +210,35 @@ class AssetManagementController extends TTCrud
}
}
protected function deleteAssetImageAction() {
$post = json_decode(file_get_contents('php://input'), true);
if (empty($post['assetId']) || empty($post['imageId'])) {
self::sendError("Asset ID or Image ID is missing.");
}
$asset = AssetManagementModel::get($post['assetId']);
if (!$asset) self::sendError("Asset not found.");
$imageIds = !empty($asset->imageIds) ? json_decode($asset->imageIds, true) : [];
// Remove the imageId
$imageIds = array_filter($imageIds, fn($id) => $id != $post['imageId']);
$asset->imageIds = json_encode(array_values($imageIds)); // Re-index array
// If the deleted image was the main image, set main image to the first available image or null
if ($asset->mainImageId == $post['imageId']) {
$asset->mainImageId = !empty($imageIds) ? $imageIds[0] : null;
}
AssetManagementModel::update((array)$asset);
// Optional: Delete the actual file from storage
// mfUpload::delete($post['imageId']);
self::returnJson(['success' => true, 'message' => 'Image deleted.', 'asset' => $this->getByIdParse((array)$asset)]);
}
protected function getReservationsAction() {
if (empty($this->request->assetId)) self::sendError("Asset ID fehlt.");
$reservations = AssetManagementReservationModel::getAll(['assetId' => $this->request->assetId], null, 0, ['key' => 'startDate', 'order' => 'ASC']);
@@ -225,4 +274,4 @@ class AssetManagementController extends TTCrud
AssetManagementReservationModel::delete($post['id']);
self::returnJson(['success' => true, 'message' => 'Reservierung gelöscht.']);
}
}
}
@@ -4,7 +4,8 @@ class AssetManagementModel extends TTCrudBaseModel {
public int $id;
public string $name;
public ?string $description;
public ?int $imageId;
public ?int $mainImageId; // Renamed from imageId
public ?string $imageIds; // Changed to JSON (will be a string in PHP)
public string $assetNumber;
public string $location;
public ?int $serviceDueDate;