| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- <?php
-
- namespace App\Http\Controllers;
-
- use App\Http\Controllers\Concerns\EditsDesignSections;
- use App\StorePlan\PlanSchema;
- use App\StorePlan\PlanValidator;
- use App\StorePlan\SectionEditor;
- use Illuminate\Http\RedirectResponse;
- use Illuminate\Http\Request;
- use Illuminate\Validation\Rule;
-
- /**
- * The Design tab's Video box: add a YouTube or Vimeo video section, change it, or take it out.
- */
- class VideoSectionController extends Controller
- {
- use EditsDesignSections;
-
- public function store(Request $request, PlanValidator $validator, string $slug): RedirectResponse
- {
- $store = $this->storeWithDesign($request, $slug);
- $data = $this->validated($request, [
- 'after' => ['nullable', 'string', Rule::in(array_column(SectionEditor::homeSections($store->plan), 'id'))],
- ]);
-
- [$plan] = SectionEditor::addToHome($store->plan, $data['after'] ?? null, 'video', [
- 'type' => 'video',
- 'variant' => $data['variant'],
- 'content' => $this->content($data),
- ]);
-
- return $this->saveDesign($store, $validator, $plan, __('Video added.'));
- }
-
- public function update(Request $request, PlanValidator $validator, string $slug, string $sectionId): RedirectResponse
- {
- $store = $this->storeWithDesign($request, $slug);
- $this->assertSection($store, $sectionId, 'video');
- $data = $this->validated($request);
-
- return $this->saveDesign($store, $validator, SectionEditor::update($store->plan, $sectionId, $this->content($data), $data['variant']), __('Video saved.'));
- }
-
- public function destroy(Request $request, PlanValidator $validator, string $slug, string $sectionId): RedirectResponse
- {
- $store = $this->storeWithDesign($request, $slug);
- $this->assertSection($store, $sectionId, 'video');
-
- return $this->saveDesign($store, $validator, SectionEditor::remove($store->plan, $sectionId), __('Video removed.'));
- }
-
- /**
- * @param array<string, list<mixed>> $extra
- * @return array<string, mixed>
- */
- private function validated(Request $request, array $extra = []): array
- {
- return $request->validate([
- 'video_url' => ['required', 'string', 'max:300', 'regex:'.PlanSchema::VIDEO_PATTERN],
- 'heading' => ['required', 'string', 'max:80'],
- 'text' => ['nullable', 'string', 'max:300'],
- 'variant' => ['required', Rule::in(PlanSchema::SECTIONS['video'])],
- ...$extra,
- ], [
- 'video_url.regex' => __('Paste a YouTube or Vimeo link, such as https://youtu.be/abc123.'),
- ]);
- }
-
- /**
- * @param array<string, mixed> $data
- * @return array<string, mixed>
- */
- private function content(array $data): array
- {
- return [
- 'heading' => trim($data['heading']),
- 'text' => filled($data['text'] ?? null) ? trim($data['text']) : null,
- 'videoUrl' => trim($data['video_url']),
- 'cta' => null,
- ];
- }
- }
|