|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace {namespace}; |
| 6 | + |
| 7 | +use Illuminate\Validation\Rule; |
| 8 | +use Leeto\Seo\Models\Seo; |
| 9 | +use Leeto\Seo\Rules\UrlRule; |
| 10 | +use MoonShine\ImportExport\Contracts\HasImportExportContract; |
| 11 | +use MoonShine\ImportExport\ExportHandler; |
| 12 | +use MoonShine\ImportExport\ImportHandler; |
| 13 | +use MoonShine\ImportExport\Traits\ImportExportConcern; |
| 14 | +use MoonShine\Laravel\Handlers\Handler; |
| 15 | +use MoonShine\Laravel\Resources\ModelResource; |
| 16 | +use MoonShine\Support\Enums\PageType; |
| 17 | +use MoonShine\Support\ListOf; |
| 18 | +use MoonShine\TinyMce\Fields\TinyMce; |
| 19 | +use MoonShine\UI\Components\ActionButton; |
| 20 | +use MoonShine\UI\Components\Layout\Box; |
| 21 | +use MoonShine\UI\Fields\ID; |
| 22 | +use MoonShine\UI\Fields\Text; |
| 23 | + |
| 24 | +class SeoResource extends ModelResource implements HasImportExportContract |
| 25 | +{ |
| 26 | + use ImportExportConcern; |
| 27 | + |
| 28 | + protected string $model = Seo::class; |
| 29 | + |
| 30 | + protected string $title = 'Seo'; |
| 31 | + |
| 32 | + protected string $column = 'title'; |
| 33 | + |
| 34 | + protected bool $detailInModal = true; |
| 35 | + |
| 36 | + protected ?PageType $redirectAfterSave = PageType::INDEX; |
| 37 | + |
| 38 | + protected function formFields(): iterable |
| 39 | + { |
| 40 | + return [ |
| 41 | + Box::make([ |
| 42 | + ID::make(), |
| 43 | + Text::make('Url') |
| 44 | + ->required(), |
| 45 | + Text::make('Title') |
| 46 | + ->required(), |
| 47 | + Text::make('Description'), |
| 48 | + Text::make('Keywords'), |
| 49 | + TinyMce::make('Text'), |
| 50 | + ]) |
| 51 | + ]; |
| 52 | + } |
| 53 | + |
| 54 | + protected function indexFields(): iterable |
| 55 | + { |
| 56 | + return [ |
| 57 | + ID::make() |
| 58 | + ->sortable(), |
| 59 | + Text::make('Url'), |
| 60 | + Text::make('Title'), |
| 61 | + Text::make('Description'), |
| 62 | + Text::make('Keywords'), |
| 63 | + ]; |
| 64 | + } |
| 65 | + |
| 66 | + protected function detailFields(): iterable |
| 67 | + { |
| 68 | + return [ |
| 69 | + ID::make(), |
| 70 | + Text::make('Url'), |
| 71 | + Text::make('Title'), |
| 72 | + Text::make('Description'), |
| 73 | + Text::make('Keywords'), |
| 74 | + TinyMce::make('Text') |
| 75 | + ]; |
| 76 | + } |
| 77 | + |
| 78 | + protected function exportFields(): iterable |
| 79 | + { |
| 80 | + return [ |
| 81 | + ID::make(), |
| 82 | + Text::make('Url'), |
| 83 | + Text::make('Title'), |
| 84 | + Text::make('Description'), |
| 85 | + Text::make('Keywords'), |
| 86 | + ]; |
| 87 | + } |
| 88 | + |
| 89 | + protected function importFields(): iterable |
| 90 | + { |
| 91 | + return $this->exportFields(); |
| 92 | + } |
| 93 | + |
| 94 | + protected function rules($item): array |
| 95 | + { |
| 96 | + return [ |
| 97 | + 'title' => [ |
| 98 | + 'required', |
| 99 | + 'string', |
| 100 | + 'min:3' |
| 101 | + ], |
| 102 | + 'url' => [ |
| 103 | + 'required', |
| 104 | + 'string', |
| 105 | + new UrlRule, |
| 106 | + Rule::unique('seo')->ignoreModel($item) |
| 107 | + ] |
| 108 | + ]; |
| 109 | + } |
| 110 | + |
| 111 | + public function search(): array |
| 112 | + { |
| 113 | + return ['id', 'url', 'title']; |
| 114 | + } |
| 115 | + |
| 116 | + public function filters(): array |
| 117 | + { |
| 118 | + return [ |
| 119 | + Text::make('Url'), |
| 120 | + Text::make('Title'), |
| 121 | + ]; |
| 122 | + } |
| 123 | + |
| 124 | + public function import(): ?Handler |
| 125 | + { |
| 126 | + return ImportHandler::make('Импорт') |
| 127 | + ->notifyUsers(fn() => [auth()->id()]); |
| 128 | + } |
| 129 | + |
| 130 | + public function export(): ?Handler |
| 131 | + { |
| 132 | + return ExportHandler::make('Экспорт') |
| 133 | + ->filename('seo_resource_export__' . date('Ymd-His')) |
| 134 | + ->dir('export_files') |
| 135 | + ->notifyUsers(fn() => [auth()->id()]); |
| 136 | + } |
| 137 | + |
| 138 | + public function indexButtons(): ListOf |
| 139 | + { |
| 140 | + return parent::indexButtons()->add( |
| 141 | + ActionButton::make('На страницу', static fn (Seo $item) => $item->url) |
| 142 | + ->icon('arrow-top-right-on-square') |
| 143 | + ->blank() |
| 144 | + ); |
| 145 | + } |
| 146 | +} |
0 commit comments