AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

HeroImageController.php 3.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Jobs\PrepareBannerPhoto;
  4. use App\Models\Store;
  5. use App\StorePlan\BannerPhoto;
  6. use App\StorePlan\HeroImages;
  7. use App\StorePlan\PlanSchema;
  8. use App\StorePlan\PlanValidator;
  9. use Illuminate\Http\RedirectResponse;
  10. use Illuminate\Http\Request;
  11. use Illuminate\Validation\Rule;
  12. use Inertia\Inertia;
  13. /**
  14. * The photo and layout of a hero section, from the Design tab.
  15. * This changes the working design, so on a live store it shows after "Publish changes".
  16. */
  17. class HeroImageController extends Controller
  18. {
  19. /**
  20. * Choose a photo (an upload, a product photo, or automatic) and the layout.
  21. */
  22. public function update(Request $request, PlanValidator $validator, string $slug, string $sectionId): RedirectResponse
  23. {
  24. $store = $this->ownedStoreWithHero($request, $slug, $sectionId);
  25. $validated = $request->validate([
  26. 'image' => ['nullable', 'string', 'regex:'.HeroImages::PATTERN],
  27. 'variant' => ['required', Rule::in(PlanSchema::SECTIONS['hero'])],
  28. ]);
  29. $image = $validated['image'] ?? null;
  30. if ($image !== null && HeroImages::unknown($store, [$image]) !== []) {
  31. return back()->withErrors(['image' => __('Choose one of your photos.')]);
  32. }
  33. return $this->save($store, $validator, $sectionId, $image, $validated['variant']);
  34. }
  35. /**
  36. * Upload a new photo and use it in this hero. A text-only hero switches to "photo beside text" so it shows.
  37. */
  38. public function upload(Request $request, PlanValidator $validator, string $slug, string $sectionId): RedirectResponse
  39. {
  40. $store = $this->ownedStoreWithHero($request, $slug, $sectionId);
  41. $request->validate([
  42. 'photo' => [
  43. 'required', 'image', 'mimes:jpg,jpeg,png,webp', 'max:6144',
  44. 'dimensions:min_width='.BannerPhoto::MIN_WIDTH.',min_height='.BannerPhoto::MIN_HEIGHT.',max_width=8000,max_height=8000',
  45. ],
  46. ], [
  47. 'photo.max' => __('The photo must be 6 MB or smaller.'),
  48. 'photo.mimes' => __('Use a JPG, PNG or WebP photo.'),
  49. 'photo.dimensions' => __('Use a photo at least :width pixels wide and :height high, and no bigger than 8000 pixels.', [
  50. 'width' => BannerPhoto::MIN_WIDTH,
  51. 'height' => BannerPhoto::MIN_HEIGHT,
  52. ]),
  53. ]);
  54. $image = $store->images()->create(['path' => $request->file('photo')->store("stores/{$store->id}", 'public')]);
  55. // Enlarged, shrunk or turned upright in the background. The photo shows as uploaded until then.
  56. PrepareBannerPhoto::dispatch($image);
  57. $current = collect(HeroImages::sections($store->plan ?? []))->firstWhere('section_id', $sectionId);
  58. $variant = ($current['variant'] ?? 'centered') === 'centered' ? 'split' : $current['variant'];
  59. return $this->save($store, $validator, $sectionId, $image->reference(), $variant);
  60. }
  61. private function save(Store $store, PlanValidator $validator, string $sectionId, ?string $image, string $variant): RedirectResponse
  62. {
  63. $store->plan = $validator->validate(HeroImages::update($store->plan ?? [], $sectionId, $image, $variant));
  64. $store->save();
  65. Inertia::flash('toast', ['type' => 'success', 'message' => $store->isPublished()
  66. ? __('Saved. Click "Publish changes" to show it on your live store.')
  67. : __('Hero saved.')]);
  68. return to_route('stores.show', $store->slug);
  69. }
  70. private function ownedStoreWithHero(Request $request, string $slug, string $sectionId): Store
  71. {
  72. $store = $request->user()->stores()->where('slug', $slug)->firstOrFail();
  73. abort_if($store->plan === null, 404);
  74. abort_unless(in_array($sectionId, array_column(HeroImages::sections($store->plan), 'section_id'), true), 404);
  75. return $store;
  76. }
  77. }