| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- <?php
-
- namespace App\Http\Controllers;
-
- use App\Models\Collection;
- use App\Models\Product;
- use App\Models\Store;
- use Illuminate\Http\RedirectResponse;
- use Illuminate\Http\Request;
- use Illuminate\Validation\Rule;
- use Inertia\Inertia;
- use Inertia\Response;
-
- /**
- * The merchant's collections: groups of products such as "Sun care". Listed on the Products tab.
- */
- class CollectionController extends Controller
- {
- public const int MAX_PRODUCTS = 200;
-
- public function create(Request $request, string $slug): Response
- {
- $store = $this->ownedStore($request, $slug);
-
- return Inertia::render('stores/collections/form', [
- 'store' => ['name' => $store->name, 'slug' => $store->slug],
- 'collection' => null,
- 'products' => $this->productChoices($store),
- ]);
- }
-
- public function store(Request $request, string $slug): RedirectResponse
- {
- $store = $this->ownedStore($request, $slug);
- $data = $this->validated($request, $store);
-
- $collection = $store->collections()->create([
- 'name' => $data['name'],
- 'description' => $data['description'] ?? null,
- 'position' => (int) $store->collections()->max('position') + 1,
- ]);
- $this->syncProducts($collection, $data['product_ids'] ?? []);
-
- Inertia::flash('toast', ['type' => 'success', 'message' => __('Collection ":name" created.', ['name' => $collection->name])]);
-
- return to_route('stores.products.index', $store->slug);
- }
-
- public function edit(Request $request, string $slug, int $collection): Response
- {
- $store = $this->ownedStore($request, $slug);
- $model = $this->ownedCollection($store, $collection);
-
- return Inertia::render('stores/collections/form', [
- 'store' => ['name' => $store->name, 'slug' => $store->slug],
- 'collection' => [
- 'id' => $model->id,
- 'name' => $model->name,
- 'slug' => $model->slug,
- 'description' => $model->description,
- 'product_ids' => $model->products->modelKeys(),
- ],
- 'products' => $this->productChoices($store),
- ]);
- }
-
- public function update(Request $request, string $slug, int $collection): RedirectResponse
- {
- $store = $this->ownedStore($request, $slug);
- $model = $this->ownedCollection($store, $collection);
- $data = $this->validated($request, $store);
-
- // The address (slug) stays the same, so menu links and shared links keep working after a rename.
- $model->fill(['name' => $data['name'], 'description' => $data['description'] ?? null])->save();
- $this->syncProducts($model, $data['product_ids'] ?? []);
-
- Inertia::flash('toast', ['type' => 'success', 'message' => __('Collection saved.')]);
-
- return to_route('stores.products.index', $store->slug);
- }
-
- public function destroy(Request $request, string $slug, int $collection): RedirectResponse
- {
- $store = $this->ownedStore($request, $slug);
-
- // Products stay; only the group goes. A "shop by" section simply stops showing it.
- $this->ownedCollection($store, $collection)->delete();
-
- Inertia::flash('toast', ['type' => 'success', 'message' => __('Collection deleted.')]);
-
- return to_route('stores.products.index', $store->slug);
- }
-
- /**
- * @return array<string, mixed>
- */
- private function validated(Request $request, Store $store): array
- {
- return $request->validate([
- 'name' => ['required', 'string', 'min:2', 'max:60'],
- 'description' => ['nullable', 'string', 'max:500'],
- 'product_ids' => ['nullable', 'array', 'max:'.self::MAX_PRODUCTS],
- 'product_ids.*' => ['integer', Rule::exists('products', 'id')->where('store_id', $store->id)],
- ], [
- 'product_ids.*.exists' => __('Choose products from this store.'),
- ]);
- }
-
- /**
- * @param array<int, mixed> $ids
- */
- private function syncProducts(Collection $collection, array $ids): void
- {
- $positions = [];
-
- foreach (array_values(array_unique(array_map('intval', $ids))) as $position => $id) {
- $positions[$id] = ['position' => $position];
- }
-
- $collection->products()->sync($positions);
- }
-
- /**
- * @return list<array{id: int, name: string, image: string|null}>
- */
- private function productChoices(Store $store): array
- {
- return array_values($store->products()->with('images')->orderBy('name')->get()
- ->map(fn (Product $product) => [
- 'id' => $product->id,
- 'name' => $product->name,
- 'image' => $product->images->first()?->url(),
- ])
- ->all());
- }
-
- private function ownedStore(Request $request, string $slug): Store
- {
- return $request->user()->stores()->where('slug', $slug)->firstOrFail();
- }
-
- private function ownedCollection(Store $store, int $id): Collection
- {
- return $store->collections()->with('products')->whereKey($id)->firstOrFail();
- }
- }
|