New Initial Commit

This commit is contained in:
twotalesanimation
2025-12-09 12:04:54 +02:00
commit 6de01c13c5
350 changed files with 42032 additions and 0 deletions
@@ -0,0 +1,38 @@
<?php
namespace App\Filament\Resources\ProductResource\Pages;
use App\Filament\Resources\ProductResource;
use App\Models\ProductImage;
use Filament\Resources\Pages\CreateRecord;
class CreateProduct extends CreateRecord
{
protected static string $resource = ProductResource::class;
protected function mutateFormDataBeforeSave(array $data): array
{
// Store images in a temporary property
if (isset($data['images']) && is_array($data['images'])) {
$this->pendingImages = $data['images'];
}
unset($data['images']);
return $data;
}
protected function afterSave(): void
{
if (!isset($this->pendingImages)) {
return;
}
foreach ($this->pendingImages as $index => $imagePath) {
ProductImage::create([
'product_id' => $this->record->id,
'image_path' => $imagePath,
'sort_order' => $index,
]);
}
}
}
@@ -0,0 +1,65 @@
<?php
namespace App\Filament\Resources\ProductResource\Pages;
use App\Filament\Resources\ProductResource;
use App\Models\ProductImage;
use Filament\Actions;
use Filament\Resources\Pages\EditRecord;
class EditProduct extends EditRecord
{
protected static string $resource = ProductResource::class;
protected function getHeaderActions(): array
{
return [
Actions\DeleteAction::make(),
];
}
protected function mutateFormDataBeforeFill(array $data): array
{
// Load existing images for the form
$data['images'] = $this->record->images()->orderBy('sort_order')->pluck('image_path')->toArray();
return $data;
}
protected function mutateFormDataBeforeSave(array $data): array
{
// Store images for processing after save
$this->pendingImages = $data['images'] ?? [];
unset($data['images']);
return $data;
}
protected function afterSave(): void
{
if (!isset($this->pendingImages)) {
return;
}
$newImages = $this->pendingImages;
$existingImages = $this->record->images()->pluck('image_path')->toArray();
// Find images to delete (in existing but not in new)
$imagesToDelete = array_diff($existingImages, $newImages);
foreach ($imagesToDelete as $imagePath) {
$this->record->images()->where('image_path', $imagePath)->delete();
}
// Add new images and update sort order
foreach ($newImages as $index => $imagePath) {
ProductImage::firstOrCreate(
[
'product_id' => $this->record->id,
'image_path' => $imagePath,
],
[
'sort_order' => $index,
]
)->update(['sort_order' => $index]);
}
}
}
@@ -0,0 +1,19 @@
<?php
namespace App\Filament\Resources\ProductResource\Pages;
use App\Filament\Resources\ProductResource;
use Filament\Actions;
use Filament\Resources\Pages\ListRecords;
class ListProducts extends ListRecords
{
protected static string $resource = ProductResource::class;
protected function getHeaderActions(): array
{
return [
Actions\CreateAction::make(),
];
}
}