|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Nova; |
| 4 | + |
| 5 | +use Illuminate\Http\Request; |
| 6 | +use Laravel\Nova\Fields\Boolean; |
| 7 | +use Laravel\Nova\Fields\ID; |
| 8 | +use Laravel\Nova\Fields\Text; |
| 9 | +use Laravel\Nova\Fields\Trix; |
| 10 | +use Laravel\Nova\Http\Requests\NovaRequest; |
| 11 | +use Laravel\Nova\Panel; |
| 12 | + |
| 13 | +class HackathonsPage extends Resource |
| 14 | +{ |
| 15 | + public static $group = 'Content'; |
| 16 | + |
| 17 | + public static $model = \App\HackathonsPage::class; |
| 18 | + |
| 19 | + public static $title = 'id'; |
| 20 | + |
| 21 | + public static function label() |
| 22 | + { |
| 23 | + return 'Hackathons Page'; |
| 24 | + } |
| 25 | + |
| 26 | + public static function singularLabel() |
| 27 | + { |
| 28 | + return 'Hackathons Page'; |
| 29 | + } |
| 30 | + |
| 31 | + public static function uriKey(): string |
| 32 | + { |
| 33 | + return 'hackathons-page'; |
| 34 | + } |
| 35 | + |
| 36 | + public static function indexQuery(NovaRequest $request, $query) |
| 37 | + { |
| 38 | + return $query->where('id', 1); |
| 39 | + } |
| 40 | + |
| 41 | + private static function localesSorted(): array |
| 42 | + { |
| 43 | + $locales = config('app.locales', ['en']); |
| 44 | + if (is_string($locales)) { |
| 45 | + $locales = array_map('trim', explode(',', $locales)); |
| 46 | + } |
| 47 | + $locales = array_values(array_filter($locales)); |
| 48 | + if (empty($locales)) { |
| 49 | + $locales = ['en']; |
| 50 | + } |
| 51 | + sort($locales); |
| 52 | + |
| 53 | + return $locales; |
| 54 | + } |
| 55 | + |
| 56 | + public function fields(Request $request): array |
| 57 | + { |
| 58 | + $translationKeys = [ |
| 59 | + 'hero_title' => 'Hero title', |
| 60 | + 'hero_subtitle' => 'Hero subtitle', |
| 61 | + 'intro_title' => 'Intro title', |
| 62 | + 'intro_paragraph_1' => 'Intro paragraph 1', |
| 63 | + 'intro_paragraph_2' => 'Intro paragraph 2', |
| 64 | + 'details_title' => 'Details title', |
| 65 | + 'details_paragraph_1' => 'Details paragraph 1', |
| 66 | + 'details_paragraph_2' => 'Details paragraph 2', |
| 67 | + 'details_paragraph_3' => 'Details paragraph 3', |
| 68 | + 'details_paragraph_4' => 'Details paragraph 4', |
| 69 | + 'recap_button_text' => 'Recap button text', |
| 70 | + 'toolkit_button_text' => 'Toolkit button text', |
| 71 | + ]; |
| 72 | + |
| 73 | + $longTextKeys = [ |
| 74 | + 'hero_subtitle', |
| 75 | + 'intro_paragraph_1', |
| 76 | + 'intro_paragraph_2', |
| 77 | + 'details_paragraph_1', |
| 78 | + 'details_paragraph_2', |
| 79 | + 'details_paragraph_3', |
| 80 | + 'details_paragraph_4', |
| 81 | + ]; |
| 82 | + |
| 83 | + $translationFields = []; |
| 84 | + foreach (self::localesSorted() as $locale) { |
| 85 | + if ($locale === 'en') { |
| 86 | + continue; |
| 87 | + } |
| 88 | + |
| 89 | + foreach ($translationKeys as $key => $label) { |
| 90 | + $fieldName = 'locale_' . $locale . '_' . $key; |
| 91 | + if (in_array($key, $longTextKeys, true)) { |
| 92 | + $translationFields[] = Trix::make($label . ' (' . strtoupper($locale) . ')', $fieldName) |
| 93 | + ->nullable() |
| 94 | + ->resolveUsing(function () use ($locale, $key) { |
| 95 | + $overrides = $this->resource->locale_overrides ?? []; |
| 96 | + |
| 97 | + return $overrides[$locale][$key] ?? ''; |
| 98 | + }) |
| 99 | + ->fillUsing(function ($request, $model, $attribute, $requestAttribute) use ($locale, $key) { |
| 100 | + $overrides = $model->locale_overrides ?? []; |
| 101 | + $overrides[$locale][$key] = $request->get($requestAttribute) ?: null; |
| 102 | + $model->locale_overrides = $overrides; |
| 103 | + }); |
| 104 | + } else { |
| 105 | + $translationFields[] = Text::make($label . ' (' . strtoupper($locale) . ')', $fieldName) |
| 106 | + ->nullable() |
| 107 | + ->resolveUsing(function () use ($locale, $key) { |
| 108 | + $overrides = $this->resource->locale_overrides ?? []; |
| 109 | + |
| 110 | + return $overrides[$locale][$key] ?? ''; |
| 111 | + }) |
| 112 | + ->fillUsing(function ($request, $model, $attribute, $requestAttribute) use ($locale, $key) { |
| 113 | + $overrides = $model->locale_overrides ?? []; |
| 114 | + $overrides[$locale][$key] = $request->get($requestAttribute) ?: null; |
| 115 | + $model->locale_overrides = $overrides; |
| 116 | + }); |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + $fields = [ |
| 122 | + ID::make()->onlyOnForms(), |
| 123 | + Boolean::make('Use dynamic content', 'dynamic_content') |
| 124 | + ->help('Keep OFF to use current static template content. Turn ON to use the values below.'), |
| 125 | + |
| 126 | + Panel::make('Hero', [ |
| 127 | + Text::make('Hero title', 'hero_title')->nullable(), |
| 128 | + Trix::make('Hero subtitle', 'hero_subtitle')->nullable(), |
| 129 | + ])->collapsable()->collapsedByDefault(), |
| 130 | + |
| 131 | + Panel::make('Intro section', [ |
| 132 | + Text::make('Intro title', 'intro_title')->nullable(), |
| 133 | + Trix::make('Intro paragraph 1', 'intro_paragraph_1')->nullable(), |
| 134 | + Trix::make('Intro paragraph 2', 'intro_paragraph_2')->nullable(), |
| 135 | + ])->collapsable()->collapsedByDefault(), |
| 136 | + |
| 137 | + Panel::make('Details section', [ |
| 138 | + Text::make('Details title', 'details_title')->nullable(), |
| 139 | + Trix::make('Details paragraph 1', 'details_paragraph_1')->nullable(), |
| 140 | + Trix::make('Details paragraph 2', 'details_paragraph_2')->nullable(), |
| 141 | + Trix::make('Details paragraph 3', 'details_paragraph_3')->nullable(), |
| 142 | + Trix::make('Details paragraph 4', 'details_paragraph_4')->nullable(), |
| 143 | + Text::make('Video URL (embed)', 'video_url')->nullable(), |
| 144 | + Text::make('Recap button text', 'recap_button_text')->nullable(), |
| 145 | + Text::make('Recap button link', 'recap_button_link')->nullable(), |
| 146 | + Text::make('Toolkit button text', 'toolkit_button_text')->nullable(), |
| 147 | + Text::make('Toolkit button link', 'toolkit_button_link')->nullable(), |
| 148 | + ])->collapsable()->collapsedByDefault(), |
| 149 | + ]; |
| 150 | + |
| 151 | + if (!empty($translationFields)) { |
| 152 | + $fields[] = new Panel('Translations', $translationFields); |
| 153 | + } |
| 154 | + |
| 155 | + return $fields; |
| 156 | + } |
| 157 | +} |
0 commit comments