| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- <?php
-
- namespace App\Http\Controllers;
-
- use App\Models\Store;
- use App\StorePlan\HeroImages;
- use App\StorePlan\PlanSchema;
- use App\StorePlan\PlanValidator;
- use Illuminate\Http\RedirectResponse;
- use Illuminate\Http\Request;
- use Illuminate\Validation\Rule;
- use Inertia\Inertia;
-
- /**
- * The photo and layout of a hero section, from the Design tab.
- * This changes the working design, so on a live store it shows after "Publish changes".
- */
- class HeroImageController extends Controller
- {
- /**
- * Choose a photo (an upload, a product photo, or automatic) and the layout.
- */
- public function update(Request $request, PlanValidator $validator, string $slug, string $sectionId): RedirectResponse
- {
- $store = $this->ownedStoreWithHero($request, $slug, $sectionId);
-
- $validated = $request->validate([
- 'image' => ['nullable', 'string', 'regex:'.HeroImages::PATTERN],
- 'variant' => ['required', Rule::in(PlanSchema::SECTIONS['hero'])],
- ]);
-
- $image = $validated['image'] ?? null;
-
- if ($image !== null && HeroImages::unknown($store, [$image]) !== []) {
- return back()->withErrors(['image' => __('Choose one of your photos.')]);
- }
-
- return $this->save($store, $validator, $sectionId, $image, $validated['variant']);
- }
-
- /**
- * Upload a new photo and use it in this hero. A text-only hero switches to "photo beside text" so it shows.
- */
- public function upload(Request $request, PlanValidator $validator, string $slug, string $sectionId): RedirectResponse
- {
- $store = $this->ownedStoreWithHero($request, $slug, $sectionId);
-
- $request->validate([
- 'photo' => ['required', 'image', 'mimes:jpg,jpeg,png,webp', 'max:6144', 'dimensions:min_width=600,min_height=400'],
- ], [
- 'photo.max' => __('The photo must be 6 MB or smaller.'),
- 'photo.mimes' => __('Use a JPG, PNG or WebP photo.'),
- 'photo.dimensions' => __('Use a photo at least 600 pixels wide and 400 high, so it stays sharp.'),
- ]);
-
- $image = $store->images()->create(['path' => $request->file('photo')->store("stores/{$store->id}", 'public')]);
- $current = collect(HeroImages::sections($store->plan ?? []))->firstWhere('section_id', $sectionId);
- $variant = ($current['variant'] ?? 'centered') === 'centered' ? 'split' : $current['variant'];
-
- return $this->save($store, $validator, $sectionId, $image->reference(), $variant);
- }
-
- private function save(Store $store, PlanValidator $validator, string $sectionId, ?string $image, string $variant): RedirectResponse
- {
- $store->plan = $validator->validate(HeroImages::update($store->plan ?? [], $sectionId, $image, $variant));
- $store->save();
-
- Inertia::flash('toast', ['type' => 'success', 'message' => $store->isPublished()
- ? __('Saved. Click "Publish changes" to show it on your live store.')
- : __('Hero saved.')]);
-
- return to_route('stores.show', $store->slug);
- }
-
- private function ownedStoreWithHero(Request $request, string $slug, string $sectionId): Store
- {
- $store = $request->user()->stores()->where('slug', $slug)->firstOrFail();
-
- abort_if($store->plan === null, 404);
- abort_unless(in_array($sectionId, array_column(HeroImages::sections($store->plan), 'section_id'), true), 404);
-
- return $store;
- }
- }
|