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

VideoSectionController.php 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Controllers\Concerns\EditsDesignSections;
  4. use App\StorePlan\PlanSchema;
  5. use App\StorePlan\PlanValidator;
  6. use App\StorePlan\SectionEditor;
  7. use Illuminate\Http\RedirectResponse;
  8. use Illuminate\Http\Request;
  9. use Illuminate\Validation\Rule;
  10. /**
  11. * The Design tab's Video box: add a YouTube or Vimeo video section, change it, or take it out.
  12. */
  13. class VideoSectionController extends Controller
  14. {
  15. use EditsDesignSections;
  16. public function store(Request $request, PlanValidator $validator, string $slug): RedirectResponse
  17. {
  18. $store = $this->storeWithDesign($request, $slug);
  19. $data = $this->validated($request, [
  20. 'after' => ['nullable', 'string', Rule::in(array_column(SectionEditor::homeSections($store->plan), 'id'))],
  21. ]);
  22. [$plan] = SectionEditor::addToHome($store->plan, $data['after'] ?? null, 'video', [
  23. 'type' => 'video',
  24. 'variant' => $data['variant'],
  25. 'content' => $this->content($data),
  26. ]);
  27. return $this->saveDesign($store, $validator, $plan, __('Video added.'));
  28. }
  29. public function update(Request $request, PlanValidator $validator, string $slug, string $sectionId): RedirectResponse
  30. {
  31. $store = $this->storeWithDesign($request, $slug);
  32. $this->assertSection($store, $sectionId, 'video');
  33. $data = $this->validated($request);
  34. return $this->saveDesign($store, $validator, SectionEditor::update($store->plan, $sectionId, $this->content($data), $data['variant']), __('Video saved.'));
  35. }
  36. public function destroy(Request $request, PlanValidator $validator, string $slug, string $sectionId): RedirectResponse
  37. {
  38. $store = $this->storeWithDesign($request, $slug);
  39. $this->assertSection($store, $sectionId, 'video');
  40. return $this->saveDesign($store, $validator, SectionEditor::remove($store->plan, $sectionId), __('Video removed.'));
  41. }
  42. /**
  43. * @param array<string, list<mixed>> $extra
  44. * @return array<string, mixed>
  45. */
  46. private function validated(Request $request, array $extra = []): array
  47. {
  48. return $request->validate([
  49. 'video_url' => ['required', 'string', 'max:300', 'regex:'.PlanSchema::VIDEO_PATTERN],
  50. 'heading' => ['required', 'string', 'max:80'],
  51. 'text' => ['nullable', 'string', 'max:300'],
  52. 'variant' => ['required', Rule::in(PlanSchema::SECTIONS['video'])],
  53. ...$extra,
  54. ], [
  55. 'video_url.regex' => __('Paste a YouTube or Vimeo link, such as https://youtu.be/abc123.'),
  56. ]);
  57. }
  58. /**
  59. * @param array<string, mixed> $data
  60. * @return array<string, mixed>
  61. */
  62. private function content(array $data): array
  63. {
  64. return [
  65. 'heading' => trim($data['heading']),
  66. 'text' => filled($data['text'] ?? null) ? trim($data['text']) : null,
  67. 'videoUrl' => trim($data['video_url']),
  68. 'cta' => null,
  69. ];
  70. }
  71. }