| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- <?php
-
- namespace App\Http\Controllers;
-
- use App\Http\Controllers\Concerns\EditsDesignSections;
- use App\Models\Store;
- use App\StorePlan\HeroImages;
- use App\StorePlan\PlanSchema;
- use App\StorePlan\PlanValidator;
- use App\StorePlan\SectionEditor;
- use Illuminate\Http\RedirectResponse;
- use Illuminate\Http\Request;
- use Illuminate\Validation\Rule;
- use Illuminate\Validation\ValidationException;
-
- /**
- * The Design tab's Bento box: add bento tiles (filled with a sensible start), edit every tile, or take them out.
- */
- class BentoSectionController extends Controller
- {
- use EditsDesignSections;
-
- public function store(Request $request, PlanValidator $validator, string $slug): RedirectResponse
- {
- $store = $this->storeWithDesign($request, $slug);
- $data = $request->validate([
- 'after' => ['nullable', 'string', Rule::in(array_column(SectionEditor::homeSections($store->plan), 'id'))],
- ]);
-
- [$plan] = SectionEditor::addToHome($store->plan, $data['after'] ?? null, 'highlights', [
- 'type' => 'bento',
- 'variant' => 'mosaic',
- 'content' => ['heading' => null, 'tiles' => $this->starterTiles($store)],
- ]);
-
- return $this->saveDesign($store, $validator, $plan, __('Bento tiles added. Change any tile below.'));
- }
-
- public function update(Request $request, PlanValidator $validator, string $slug, string $sectionId): RedirectResponse
- {
- $store = $this->storeWithDesign($request, $slug);
- $this->assertSection($store, $sectionId, 'bento');
-
- $data = $request->validate([
- 'heading' => ['nullable', 'string', 'max:80'],
- 'variant' => ['required', Rule::in(PlanSchema::SECTIONS['bento'])],
- 'tiles' => ['required', 'array', 'min:3', 'max:6'],
- 'tiles.*.title' => ['required', 'string', 'max:60'],
- 'tiles.*.text' => ['nullable', 'string', 'max:140'],
- 'tiles.*.href' => ['required', 'string', 'max:300', 'regex:'.PlanSchema::LINK_PATTERN],
- 'tiles.*.image' => ['nullable', 'string', 'regex:'.HeroImages::PATTERN],
- 'tiles.*.tone' => ['nullable', Rule::in(['primary', 'accent', 'dark', 'surface'])],
- ], [
- 'tiles.min' => __('Keep at least 3 tiles.'),
- 'tiles.max' => __('Use at most 6 tiles.'),
- 'tiles.*.href.regex' => __('Use a link such as /shop, /collections/gift-sets, #about or https://...'),
- ]);
-
- $images = array_values(array_filter(array_column($data['tiles'], 'image')));
-
- if (HeroImages::unknown($store, $images) !== []) {
- throw ValidationException::withMessages(['section' => __('Choose photos from your products or uploads.')]);
- }
-
- $tiles = array_map(fn (array $tile) => [
- 'title' => trim($tile['title']),
- 'text' => filled($tile['text'] ?? null) ? trim($tile['text']) : null,
- 'href' => trim($tile['href']),
- 'image' => $tile['image'] ?? null,
- 'tone' => $tile['tone'] ?? 'surface',
- ], array_values($data['tiles']));
-
- $content = ['heading' => filled($data['heading'] ?? null) ? trim($data['heading']) : null, 'tiles' => $tiles];
-
- return $this->saveDesign($store, $validator, SectionEditor::update($store->plan, $sectionId, $content, $data['variant']), __('Bento tiles saved.'));
- }
-
- public function destroy(Request $request, PlanValidator $validator, string $slug, string $sectionId): RedirectResponse
- {
- $store = $this->storeWithDesign($request, $slug);
- $this->assertSection($store, $sectionId, 'bento');
-
- return $this->saveDesign($store, $validator, SectionEditor::remove($store->plan, $sectionId), __('Bento tiles removed.'));
- }
-
- /**
- * Three tiles that work for any store: the whole range (with a product photo when there is one),
- * the first collection or the products section, and the brand story.
- *
- * @return list<array<string, mixed>>
- */
- private function starterTiles(Store $store): array
- {
- $photo = HeroImages::choices($store);
- $productPhoto = collect($photo)->first(fn (array $choice) => str_starts_with($choice['ref'], 'product:'));
- $collection = $store->collections()->first();
- $sections = array_column(SectionEditor::homeSections($store->plan ?? []), 'id');
- $about = collect(SectionEditor::ofType($store->plan ?? [], 'about'))->firstWhere('page_id', 'home');
- $grid = collect(SectionEditor::ofType($store->plan ?? [], 'product_grid'))->firstWhere('page_id', 'home');
-
- return [
- ['title' => 'Shop the range', 'text' => null, 'href' => '/shop', 'image' => $productPhoto['ref'] ?? null, 'tone' => 'primary'],
- $collection !== null
- ? ['title' => $collection->name, 'text' => null, 'href' => '/collections/'.$collection->slug, 'image' => null, 'tone' => 'accent']
- : ['title' => 'Bestsellers', 'text' => null, 'href' => $grid !== null && in_array($grid['section']['id'], $sections, true) ? '#'.$grid['section']['id'] : '/shop', 'image' => null, 'tone' => 'accent'],
- ['title' => 'Our story', 'text' => null, 'href' => $about !== null ? '#'.$about['section']['id'] : '/shop', 'image' => null, 'tone' => 'dark'],
- ];
- }
- }
|