AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. namespace App\StorePlan;
  3. use App\Models\Product;
  4. use App\Models\ProductImage;
  5. use App\Models\Store;
  6. use App\Models\StoreImage;
  7. use Illuminate\Support\Collection;
  8. /**
  9. * Photos for hero sections.
  10. *
  11. * A hero's "image" points to a photo by a short name: "upload:12" for a photo the merchant uploaded
  12. * in the Design tab, or "product:sambal-hijau" for the main photo of one of their products.
  13. * With no image chosen (or when the chosen photo is gone), the hero uses the store's product photos,
  14. * so a store looks finished as soon as it has photos.
  15. */
  16. final class HeroImages
  17. {
  18. public const string PATTERN = '/^(upload:[1-9][0-9]*|product:[a-z0-9]+(?:-[a-z0-9]+)*)$/';
  19. /** How many product photos the storefront gets for heroes with no photo of their own. */
  20. private const int FALLBACK_COUNT = 4;
  21. /**
  22. * Every hero section, with the page it is on.
  23. *
  24. * @param array<string, mixed> $plan
  25. * @return list<array{page_id: string, page_title: string, section_id: string, headline: string, variant: string, image: string|null}>
  26. */
  27. public static function sections(array $plan): array
  28. {
  29. $found = [];
  30. foreach ($plan['pages'] ?? [] as $page) {
  31. foreach ($page['sections'] ?? [] as $section) {
  32. if (($section['type'] ?? null) === 'hero') {
  33. $found[] = [
  34. 'page_id' => (string) ($page['id'] ?? ''),
  35. 'page_title' => (string) ($page['title'] ?? ''),
  36. 'section_id' => (string) ($section['id'] ?? ''),
  37. 'headline' => (string) ($section['content']['headline'] ?? ''),
  38. 'variant' => (string) ($section['variant'] ?? ''),
  39. 'image' => is_string($section['content']['image'] ?? null) ? $section['content']['image'] : null,
  40. ];
  41. }
  42. }
  43. }
  44. return $found;
  45. }
  46. /**
  47. * The plan with one hero changed: its photo, its layout, or both. Null for $image means "automatic".
  48. *
  49. * @param array<string, mixed> $plan
  50. * @return array<string, mixed>
  51. */
  52. public static function update(array $plan, string $sectionId, ?string $image, ?string $variant = null): array
  53. {
  54. foreach ($plan['pages'] ?? [] as $pageIndex => $page) {
  55. foreach ($page['sections'] ?? [] as $sectionIndex => $section) {
  56. if (($section['id'] ?? null) === $sectionId && ($section['type'] ?? null) === 'hero') {
  57. $plan['pages'][$pageIndex]['sections'][$sectionIndex]['content']['image'] = $image;
  58. if ($variant !== null) {
  59. $plan['pages'][$pageIndex]['sections'][$sectionIndex]['variant'] = $variant;
  60. }
  61. }
  62. }
  63. }
  64. return $plan;
  65. }
  66. /**
  67. * Photos the merchant (or the AI) can choose from: uploads first, then one per product.
  68. *
  69. * @return list<array{ref: string, label: string, url: string}>
  70. */
  71. public static function choices(Store $store): array
  72. {
  73. $choices = $store->images()->latest('id')->limit(30)->get()
  74. ->map(fn (StoreImage $image) => ['ref' => $image->reference(), 'label' => 'Uploaded photo', 'url' => $image->url()])
  75. ->all();
  76. foreach (self::productsWithPhotos($store, 60) as $product) {
  77. $choices[] = ['ref' => 'product:'.$product->slug, 'label' => $product->name, 'url' => $product->images->first()->url()];
  78. }
  79. return array_values($choices);
  80. }
  81. /**
  82. * References that do not point to a photo of this store.
  83. *
  84. * @param list<string> $refs
  85. * @return list<string>
  86. */
  87. public static function unknown(Store $store, array $refs): array
  88. {
  89. $known = array_column(self::resolve($store, $refs), 'ref');
  90. return array_values(array_diff($refs, $known));
  91. }
  92. /**
  93. * What the storefront needs: the address of every photo the plan points to, and a few product photos
  94. * for heroes without a photo of their own.
  95. *
  96. * @param array<string, mixed> $plan
  97. * @return array{refs: object, fallback: list<string>}
  98. */
  99. public static function forStorefront(Store $store, array $plan): array
  100. {
  101. $refs = array_values(array_unique(array_filter(array_column(self::sections($plan), 'image'))));
  102. return [
  103. // An object even when empty, so the frontend always gets {} and never [].
  104. 'refs' => (object) collect(self::resolve($store, $refs))->mapWithKeys(fn (array $photo) => [$photo['ref'] => $photo['url']])->all(),
  105. 'fallback' => array_values(self::productsWithPhotos($store, self::FALLBACK_COUNT)
  106. ->map(fn (Product $product) => $product->images->first()->url())
  107. ->all()),
  108. ];
  109. }
  110. /**
  111. * @param list<string> $refs
  112. * @return list<array{ref: string, url: string}>
  113. */
  114. private static function resolve(Store $store, array $refs): array
  115. {
  116. $uploadIds = [];
  117. $slugs = [];
  118. foreach ($refs as $ref) {
  119. [$kind, $value] = explode(':', $ref, 2) + [1 => ''];
  120. if ($kind === 'upload') {
  121. $uploadIds[] = (int) $value;
  122. } elseif ($kind === 'product') {
  123. $slugs[] = $value;
  124. }
  125. }
  126. $found = [];
  127. if ($uploadIds !== []) {
  128. foreach ($store->images()->whereKey($uploadIds)->get() as $image) {
  129. $found[] = ['ref' => $image->reference(), 'url' => $image->url()];
  130. }
  131. }
  132. if ($slugs !== []) {
  133. $products = $store->products()->whereIn('slug', $slugs)->whereHas('images')->with('images')->get();
  134. foreach ($products as $product) {
  135. $found[] = ['ref' => 'product:'.$product->slug, 'url' => $product->images->first()->url()];
  136. }
  137. }
  138. return $found;
  139. }
  140. /**
  141. * Active products that have at least one photo, newest first.
  142. *
  143. * @return Collection<int, Product>
  144. */
  145. private static function productsWithPhotos(Store $store, int $limit): Collection
  146. {
  147. return $store->products()
  148. ->active()
  149. ->whereHas('images')
  150. ->with('images')
  151. ->latest('id')
  152. ->limit($limit)
  153. ->get()
  154. ->filter(fn (Product $product) => $product->images->first() instanceof ProductImage)
  155. ->values()
  156. ->toBase();
  157. }
  158. }