AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

BentoSectionController.php 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Controllers\Concerns\EditsDesignSections;
  4. use App\Models\Store;
  5. use App\StorePlan\HeroImages;
  6. use App\StorePlan\PlanSchema;
  7. use App\StorePlan\PlanValidator;
  8. use App\StorePlan\SectionEditor;
  9. use Illuminate\Http\RedirectResponse;
  10. use Illuminate\Http\Request;
  11. use Illuminate\Validation\Rule;
  12. use Illuminate\Validation\ValidationException;
  13. /**
  14. * The Design tab's Bento box: add bento tiles (filled with a sensible start), edit every tile, or take them out.
  15. */
  16. class BentoSectionController extends Controller
  17. {
  18. use EditsDesignSections;
  19. public function store(Request $request, PlanValidator $validator, string $slug): RedirectResponse
  20. {
  21. $store = $this->storeWithDesign($request, $slug);
  22. $data = $request->validate([
  23. 'after' => ['nullable', 'string', Rule::in(array_column(SectionEditor::homeSections($store->plan), 'id'))],
  24. ]);
  25. [$plan] = SectionEditor::addToHome($store->plan, $data['after'] ?? null, 'highlights', [
  26. 'type' => 'bento',
  27. 'variant' => 'mosaic',
  28. 'content' => ['heading' => null, 'tiles' => $this->starterTiles($store)],
  29. ]);
  30. return $this->saveDesign($store, $validator, $plan, __('Bento tiles added. Change any tile below.'));
  31. }
  32. public function update(Request $request, PlanValidator $validator, string $slug, string $sectionId): RedirectResponse
  33. {
  34. $store = $this->storeWithDesign($request, $slug);
  35. $this->assertSection($store, $sectionId, 'bento');
  36. $data = $request->validate([
  37. 'heading' => ['nullable', 'string', 'max:80'],
  38. 'variant' => ['required', Rule::in(PlanSchema::SECTIONS['bento'])],
  39. 'tiles' => ['required', 'array', 'min:3', 'max:6'],
  40. 'tiles.*.title' => ['required', 'string', 'max:60'],
  41. 'tiles.*.text' => ['nullable', 'string', 'max:140'],
  42. 'tiles.*.href' => ['required', 'string', 'max:300', 'regex:'.PlanSchema::LINK_PATTERN],
  43. 'tiles.*.image' => ['nullable', 'string', 'regex:'.HeroImages::PATTERN],
  44. 'tiles.*.tone' => ['nullable', Rule::in(['primary', 'accent', 'dark', 'surface'])],
  45. ], [
  46. 'tiles.min' => __('Keep at least 3 tiles.'),
  47. 'tiles.max' => __('Use at most 6 tiles.'),
  48. 'tiles.*.href.regex' => __('Use a link such as /shop, /collections/gift-sets, #about or https://...'),
  49. ]);
  50. $images = array_values(array_filter(array_column($data['tiles'], 'image')));
  51. if (HeroImages::unknown($store, $images) !== []) {
  52. throw ValidationException::withMessages(['section' => __('Choose photos from your products or uploads.')]);
  53. }
  54. $tiles = array_map(fn (array $tile) => [
  55. 'title' => trim($tile['title']),
  56. 'text' => filled($tile['text'] ?? null) ? trim($tile['text']) : null,
  57. 'href' => trim($tile['href']),
  58. 'image' => $tile['image'] ?? null,
  59. 'tone' => $tile['tone'] ?? 'surface',
  60. ], array_values($data['tiles']));
  61. $content = ['heading' => filled($data['heading'] ?? null) ? trim($data['heading']) : null, 'tiles' => $tiles];
  62. return $this->saveDesign($store, $validator, SectionEditor::update($store->plan, $sectionId, $content, $data['variant']), __('Bento tiles saved.'));
  63. }
  64. public function destroy(Request $request, PlanValidator $validator, string $slug, string $sectionId): RedirectResponse
  65. {
  66. $store = $this->storeWithDesign($request, $slug);
  67. $this->assertSection($store, $sectionId, 'bento');
  68. return $this->saveDesign($store, $validator, SectionEditor::remove($store->plan, $sectionId), __('Bento tiles removed.'));
  69. }
  70. /**
  71. * Three tiles that work for any store: the whole range (with a product photo when there is one),
  72. * the first collection or the products section, and the brand story.
  73. *
  74. * @return list<array<string, mixed>>
  75. */
  76. private function starterTiles(Store $store): array
  77. {
  78. $photo = HeroImages::choices($store);
  79. $productPhoto = collect($photo)->first(fn (array $choice) => str_starts_with($choice['ref'], 'product:'));
  80. $collection = $store->collections()->first();
  81. $sections = array_column(SectionEditor::homeSections($store->plan ?? []), 'id');
  82. $about = collect(SectionEditor::ofType($store->plan ?? [], 'about'))->firstWhere('page_id', 'home');
  83. $grid = collect(SectionEditor::ofType($store->plan ?? [], 'product_grid'))->firstWhere('page_id', 'home');
  84. return [
  85. ['title' => 'Shop the range', 'text' => null, 'href' => '/shop', 'image' => $productPhoto['ref'] ?? null, 'tone' => 'primary'],
  86. $collection !== null
  87. ? ['title' => $collection->name, 'text' => null, 'href' => '/collections/'.$collection->slug, 'image' => null, 'tone' => 'accent']
  88. : ['title' => 'Bestsellers', 'text' => null, 'href' => $grid !== null && in_array($grid['section']['id'], $sections, true) ? '#'.$grid['section']['id'] : '/shop', 'image' => null, 'tone' => 'accent'],
  89. ['title' => 'Our story', 'text' => null, 'href' => $about !== null ? '#'.$about['section']['id'] : '/shop', 'image' => null, 'tone' => 'dark'],
  90. ];
  91. }
  92. }