diff --git a/application/AssetManagement/AssetManagementController.php b/application/AssetManagement/AssetManagementController.php index 0411e996c..1353aa134 100644 --- a/application/AssetManagement/AssetManagementController.php +++ b/application/AssetManagement/AssetManagementController.php @@ -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.']); } -} +} \ No newline at end of file diff --git a/application/AssetManagement/AssetManagementModel.php b/application/AssetManagement/AssetManagementModel.php index ac7fa3ca6..9cef32f94 100644 --- a/application/AssetManagement/AssetManagementModel.php +++ b/application/AssetManagement/AssetManagementModel.php @@ -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; diff --git a/db/migrations/20250709125000_asset_management_add_image_ids.php b/db/migrations/20250709125000_asset_management_add_image_ids.php new file mode 100644 index 000000000..fe12e8eee --- /dev/null +++ b/db/migrations/20250709125000_asset_management_add_image_ids.php @@ -0,0 +1,52 @@ +getEnvironment() == "thetool") { + $table = $this->table("AssetManagement"); + $table->renameColumn('imageId', 'mainImageId'); + $table->changeColumn('mainImageId', 'integer', [ + 'null' => true, + 'default' => null, + 'comment' => 'Foreign key to the File table for the asset\'s main image', + ]); + $table->addColumn('imageIds', 'json', [ + 'null' => true, + 'default' => null, + 'after' => 'mainImageId', + 'comment' => 'An array of file IDs for all asset images', + ]); + $table->update(); + $this->execute(" + UPDATE `AssetManagement` + SET `imageIds` = JSON_ARRAY(`mainImageId`) + WHERE `mainImageId` IS NOT NULL; + "); + } + } + + public function down(): void + { + if ($this->getEnvironment() == "thetool") { + $table = $this->table("AssetManagement"); + $this->execute(" + UPDATE `AssetManagement` + SET `mainImageId` = JSON_UNQUOTE(JSON_EXTRACT(`imageIds`, '$[0]')) + WHERE JSON_TYPE(`imageIds`) = 'ARRAY' AND JSON_LENGTH(`imageIds`) > 0; + "); + $table->removeColumn('imageIds'); + $table->renameColumn('mainImageId', 'imageId'); + $table->changeColumn('imageId', 'integer', [ + 'null' => true, + 'default' => null, + 'comment' => 'Foreign key to the File table for the asset\'s image', + ]); + $table->update(); + } + } +} diff --git a/public/js/pages/AssetManagement/AssetManagement.js b/public/js/pages/AssetManagement/AssetManagement.js index 911fcb2cc..b3ccbceeb 100644 --- a/public/js/pages/AssetManagement/AssetManagement.js +++ b/public/js/pages/AssetManagement/AssetManagement.js @@ -12,7 +12,6 @@ window.TT_CONFIG["CRUD_CONFIG"]["additionalActions"] = [ Vue.component('asset-management', { template: ` - - - - -