diff --git a/database/migrations/create_admin_kit_vacancy_gallery_table.php.stub b/database/migrations/create_admin_kit_vacancy_gallery_table.php.stub new file mode 100644 index 0000000..47d1da5 --- /dev/null +++ b/database/migrations/create_admin_kit_vacancy_gallery_table.php.stub @@ -0,0 +1,29 @@ +id(); + + // add fields + $table->foreignId('vacancy_id') + ->constrained('admin_kit_vacancy') + ->cascadeOnDelete(); + $table->jsonb('title'); + + $table->timestamps(); + }); + } + + public function down() + { + Schema::dropIfExists('admin_kit_vacancy_gallerys'); + } +}; diff --git a/database/migrations/create_admin_kit_vacancy_table.php.stub b/database/migrations/create_admin_kit_vacancy_table.php.stub index 8f13751..48633f5 100644 --- a/database/migrations/create_admin_kit_vacancy_table.php.stub +++ b/database/migrations/create_admin_kit_vacancy_table.php.stub @@ -12,7 +12,10 @@ return new class extends Migration $table->id(); // add fields - $table->jsonb('title')->default('{}'); + $table->jsonb('title'); + $table->jsonb('subtitle')->nullable(); + $table->jsonb('action_title')->nullable(); + $table->jsonb('action_link')->nullable(); $table->timestamps(); }); diff --git a/resources/lang/en/vacancy.php b/resources/lang/en/vacancy.php index 641894a..61177fd 100644 --- a/resources/lang/en/vacancy.php +++ b/resources/lang/en/vacancy.php @@ -7,6 +7,10 @@ 'id' => 'ID', 'title' => 'Title', + 'subtitle' => 'Subtitle', + 'image' => 'Image', + 'action_button' => 'Action button', + 'link' => 'Link', 'created_at' => 'Created At', 'updated_at' => 'Updated At', diff --git a/resources/lang/ru/vacancy.php b/resources/lang/ru/vacancy.php index 44b0040..39e1baf 100644 --- a/resources/lang/ru/vacancy.php +++ b/resources/lang/ru/vacancy.php @@ -2,11 +2,15 @@ return [ 'resource' => [ - 'label' => 'Vacancy', - 'plural_label' => 'Vacancy', + 'label' => 'Вакансия', + 'plural_label' => 'Вакансии', 'id' => 'ID', - 'title' => 'Title', + 'title' => 'Заголовок', + 'subtitle' => 'Подзаголовок', + 'image' => 'Изображение', + 'action_button' => 'Кнопка действия', + 'link' => 'Ссылка', 'created_at' => 'Создан', 'updated_at' => 'Обновлен', diff --git a/src/Models/Vacancy.php b/src/Models/Vacancy.php index fee1909..69e8fc3 100644 --- a/src/Models/Vacancy.php +++ b/src/Models/Vacancy.php @@ -2,20 +2,27 @@ namespace AdminKit\Vacancy\Models; +use Spatie\MediaLibrary\HasMedia; +use Spatie\MediaLibrary\InteractsWithMedia; use AdminKit\Core\Abstracts\Models\AbstractModel; +use Illuminate\Database\Eloquent\Relations\HasMany; use AdminKit\Vacancy\Database\Factories\VacancyFactory; use Illuminate\Database\Eloquent\Factories\HasFactory; use Spatie\Translatable\HasTranslations; -class Vacancy extends AbstractModel +class Vacancy extends AbstractModel implements HasMedia { use HasFactory; use HasTranslations; + use InteractsWithMedia; protected $table = 'admin_kit_vacancy'; protected $fillable = [ 'title', + 'subtitle', + 'action_title', + 'action_link', ]; protected $casts = [ @@ -24,10 +31,18 @@ class Vacancy extends AbstractModel protected $translatable = [ 'title', + 'subtitle', + 'action_title', + 'action_link', ]; protected static function newFactory(): VacancyFactory { return new VacancyFactory(); } + + public function gallery(): HasMany + { + return $this->hasMany(VacancyGallery::class); + } } diff --git a/src/Models/VacancyGallery.php b/src/Models/VacancyGallery.php new file mode 100644 index 0000000..6c44c52 --- /dev/null +++ b/src/Models/VacancyGallery.php @@ -0,0 +1,36 @@ +first(); - public function show(int $id) - { - return Vacancy::findOrFail($id); + return new VacancyResource($vacancies); } } diff --git a/src/UI/API/Resources/VacancyGalleryResource.php b/src/UI/API/Resources/VacancyGalleryResource.php new file mode 100644 index 0000000..3d2f279 --- /dev/null +++ b/src/UI/API/Resources/VacancyGalleryResource.php @@ -0,0 +1,22 @@ + + */ + public function toArray(Request $request): array + { + return [ + 'title' => $this->title, + 'background' => $this->getFirstMediaUrl('gallery'), + ]; + } +} diff --git a/src/UI/API/Resources/VacancyResource.php b/src/UI/API/Resources/VacancyResource.php new file mode 100644 index 0000000..aecb020 --- /dev/null +++ b/src/UI/API/Resources/VacancyResource.php @@ -0,0 +1,28 @@ + + */ + public function toArray(Request $request): array + { + return [ + 'title' => $this->title, + 'subtitle' => $this->subtitle, + 'background' => $this->getFirstMediaUrl('main'), + 'action' => [ + 'title' => $this->action_title, + 'link' => $this->action_link, + ], + 'gallery' => VacancyGalleryResource::collection($this->gallery), + ]; + } +} diff --git a/src/UI/API/Routes/api.php b/src/UI/API/Routes/api.php index 6efbf8d..6fc7fd3 100644 --- a/src/UI/API/Routes/api.php +++ b/src/UI/API/Routes/api.php @@ -1,7 +1,6 @@ schema([ + Forms\Components\SpatieMediaLibraryFileUpload::make('background') + ->label(__('admin-kit-vacancy::vacancy.resource.image')) + ->collection('main') + ->image() + ->columnSpan(2) + ->required(), TranslatableTabs::make(fn ($locale) => Forms\Components\Tabs\Tab::make($locale)->schema([ - Forms\Components\TextInput::make('title') + Forms\Components\TextInput::make('title.'.$locale) ->label(__('admin-kit-vacancy::vacancy.resource.title')) - ->required($locale === app()->getLocale()), + ->required(), + Forms\Components\TextInput::make('subtitle.'.$locale) + ->label(__('admin-kit-vacancy::vacancy.resource.subtitle')) + ->required(), + Forms\Components\Section::make(__('admin-kit-vacancy::vacancy.resource.action_button'))->schema([ + Forms\Components\TextInput::make('action_title.'.$locale) + ->label(__('admin-kit-vacancy::vacancy.resource.title')), + Forms\Components\TextInput::make('action_link.'.$locale) + ->label(__('admin-kit-vacancy::vacancy.resource.link')), + ])->columns(2), ])), ]) ->columns(1); @@ -57,7 +73,7 @@ public static function table(Tables\Table $table): Tables\Table public static function getRelations(): array { return [ - // + VacancyGalleryRelationManager::class, ]; } @@ -80,8 +96,13 @@ public static function getPluralLabel(): ?string return __('admin-kit-vacancy::vacancy.resource.plural_label'); } - public static function getNavigationGroup(): ?string + public static function getNavigationUrl(): string { - return __('admin-kit-vacancy::vacancy.resource.plural_label'); + $vacancy = Vacancy::query() + ->first(); + + return $vacancy + ? route('filament.admin-kit.resources.vacancies.edit', $vacancy) + : route('filament.admin-kit.resources.vacancies.create'); } } diff --git a/src/UI/Filament/Resources/VacancyResource/RelationManagers/VacancyGalleryRelationManager.php b/src/UI/Filament/Resources/VacancyResource/RelationManagers/VacancyGalleryRelationManager.php new file mode 100644 index 0000000..a7ec8c7 --- /dev/null +++ b/src/UI/Filament/Resources/VacancyResource/RelationManagers/VacancyGalleryRelationManager.php @@ -0,0 +1,62 @@ +schema([ + Forms\Components\SpatieMediaLibraryFileUpload::make('background') + ->label(__('admin-kit-vacancy::vacancy.resource.image')) + ->collection('gallery') + ->image() + ->columnSpan(2) + ->required(), + TranslatableTabs::make(fn ($locale) => Tab::make($locale)->schema([ + Forms\Components\TextInput::make('title.'.$locale) + ->label(__('admin-kit-vacancy::vacancy.resource.title')) + ->required(), + ]))->columnSpan(2), + ]); + } + + public function table(Table $table): Table + { + return $table + ->recordTitleAttribute('title') + ->columns([ + Tables\Columns\SpatieMediaLibraryImageColumn::make('background') + ->collection('gallery') + ->label(__('admin-kit-vacancy::vacancy.resource.image')), + Tables\Columns\TextColumn::make('title') + ->label(__('admin-kit-vacancy::vacancy.resource.title')), + ]) + ->filters([ + // + ]) + ->headerActions([ + Tables\Actions\CreateAction::make(), + ]) + ->actions([ + Tables\Actions\EditAction::make(), + Tables\Actions\DeleteAction::make(), + ]) + ->bulkActions([ + Tables\Actions\BulkActionGroup::make([ + Tables\Actions\DeleteBulkAction::make(), + ]), + ]); + } +} diff --git a/src/VacancyServiceProvider.php b/src/VacancyServiceProvider.php index 690d817..2eedf29 100644 --- a/src/VacancyServiceProvider.php +++ b/src/VacancyServiceProvider.php @@ -21,7 +21,10 @@ public function configurePackage(Package $package): void ->hasConfigFile() ->hasViews() ->hasTranslations() - ->hasMigration('create_admin_kit_vacancy_table') + ->hasMigrations([ + 'create_admin_kit_vacancy_table', + 'create_admin_kit_vacancy_gallery_table', + ]) ->hasCommand(VacancyCommand::class); }