AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

HeroImageController.php 3.4KB

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