| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- <?php
-
- namespace App\Http\Controllers;
-
- use App\Jobs\PrepareBannerPhoto;
- use App\Models\Store;
- use App\StorePlan\BannerPhoto;
- 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='.BannerPhoto::MIN_WIDTH.',min_height='.BannerPhoto::MIN_HEIGHT.',max_width=8000,max_height=8000',
- ],
- ], [
- '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 :width pixels wide and :height high, and no bigger than 8000 pixels.', [
- 'width' => BannerPhoto::MIN_WIDTH,
- 'height' => BannerPhoto::MIN_HEIGHT,
- ]),
- ]);
-
- $image = $store->images()->create(['path' => $request->file('photo')->store("stores/{$store->id}", 'public')]);
- // Enlarged, shrunk or turned upright in the background. The photo shows as uploaded until then.
- PrepareBannerPhoto::dispatch($image);
- $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;
- }
- }
|