-
Notifications
You must be signed in to change notification settings - Fork 1
ADO-3145 Predefined list of available fields from each data source fo… #388
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sozhanggov1
wants to merge
1
commit into
dev
Choose a base branch
from
ADO-3145
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
182 changes: 182 additions & 0 deletions
182
app/Filament/Forms/Resources/DataBindingMappingResource.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,182 @@ | ||
| <?php | ||
|
|
||
| namespace App\Filament\Forms\Resources; | ||
|
|
||
| use App\Helpers\DataBindingsHelper; | ||
| use App\Models\DataBindingMapping; | ||
| use App\Models\FormMetadata\FormDataSource; | ||
| use Filament\Forms; | ||
| use Filament\Forms\Form; | ||
| use Filament\Forms\Get; | ||
| use Filament\Forms\Set; | ||
| use Filament\Forms\Components\Grid; | ||
| use Filament\Forms\Components\Section; | ||
| use Filament\Forms\Components\TextInput; | ||
| use Filament\Forms\Components\Textarea; | ||
| use Filament\Forms\Components\Select; | ||
| use Filament\Resources\Resource; | ||
| use Filament\Tables; | ||
| use Filament\Tables\Table; | ||
| use Filament\Tables\Columns\TextColumn; | ||
| use Filament\Tables\Filters\SelectFilter; | ||
| use Illuminate\Database\Eloquent\Model; | ||
| use Illuminate\Support\Str; | ||
| use Illuminate\Validation\Rule; | ||
|
|
||
| use App\Filament\Forms\Resources\DataBindingMappingResource\Pages; | ||
|
|
||
| class DataBindingMappingResource extends Resource | ||
| { | ||
| protected static ?string $model = DataBindingMapping::class; | ||
|
|
||
| protected static ?string $navigationGroup = 'Form Building'; | ||
| protected static ?string $navigationLabel = 'Databinding Mappings'; | ||
| protected static ?string $slug = 'data-bindings'; | ||
| protected static ?string $navigationIcon = 'heroicon-o-link'; | ||
|
|
||
| // Admin-only gate | ||
| public static function canViewAny(): bool { return auth()->user()?->hasRole('admin') ?? false; } | ||
| public static function canCreate(): bool { return self::canViewAny(); } | ||
| public static function canEdit(Model $record): bool { return self::canViewAny(); } | ||
| public static function canDelete(Model $record): bool { return self::canViewAny(); } | ||
| public static function canDeleteAny(): bool { return self::canViewAny(); } | ||
|
|
||
| public static function form(Form $form): Form | ||
| { | ||
| return $form->schema([ | ||
| Section::make('Details')->columns(2)->schema([ | ||
| TextInput::make('label') | ||
| ->label('Label') | ||
| ->required() | ||
| ->maxLength(255), | ||
|
|
||
| TextInput::make('endpoint') | ||
| ->label('Endpoint') | ||
| ->helperText('ICM Endpoint'), | ||
|
|
||
| Textarea::make('description') | ||
| ->label('Description') | ||
| ->rows(3) | ||
| ->columnSpanFull(), | ||
| ]), | ||
|
|
||
| Section::make('Binding')->columns(2)->schema([ | ||
|
|
||
| // Data source dropdown which comes from Databinding Sources | ||
| Select::make('data_source') | ||
| ->label('Data source') | ||
| ->required() | ||
| ->searchable() | ||
| ->preload() | ||
| // options from form_data_sources.name | ||
| ->options(fn () => | ||
| FormDataSource::query() | ||
| ->orderBy('name') | ||
| ->pluck('name', 'name') | ||
| ->all() | ||
| ) | ||
| // keep JSONPath preview synced | ||
| ->live(debounce: 400) | ||
| ->afterStateUpdated(function (Set $set, $state, Get $get) { | ||
| $set('data_path', self::composeJsonPath( | ||
| (string) $state, | ||
| (string) $get('path_label') | ||
| )); | ||
| }) | ||
| // ensure chosen value exists in form_data_sources.name | ||
| ->rule(function () { | ||
| $table = (new FormDataSource)->getTable(); | ||
| return Rule::exists($table, 'name'); | ||
| }), | ||
|
|
||
| // Path label: free text + dynamic datalist scoped by current data_source; preview updates on blur | ||
| DataBindingsHelper::pathLabelField( | ||
| sourceField: 'data_source', | ||
| sourceIsId: false, | ||
| targetPathField: 'data_path', | ||
| ), | ||
|
|
||
| Grid::make(2)->schema([ | ||
| TextInput::make('data_path') | ||
| ->label('Data path') | ||
| ->disabled() | ||
| ->dehydrated(false) | ||
| ->helperText("Composed as: \$['{Data source}']['{Path label}']") | ||
| ->afterStateHydrated(function (Set $set, Get $get) { | ||
| $set('data_path', self::composeJsonPath( | ||
| (string) $get('data_source'), | ||
| (string) $get('path_label') | ||
| )); | ||
| }), | ||
|
|
||
| TextInput::make('repeating_path') | ||
| ->label('Repeating path') | ||
| ->helperText("Repeating path for container element type e.g. $.['{Data source}'].[*]"), | ||
| ])->columnSpanFull(), | ||
| ]), | ||
| ]); | ||
| } | ||
|
|
||
| public static function table(Table $table): Table | ||
| { | ||
| return $table | ||
| ->columns([ | ||
| TextColumn::make('label')->label('Label')->searchable()->sortable(), | ||
| TextColumn::make('description')->label('Description')->limit(40)->toggleable(), | ||
| TextColumn::make('data_source')->label('Data source')->sortable()->searchable(), | ||
| TextColumn::make('endpoint')->label('End point')->limit(40)->toggleable(), | ||
| TextColumn::make('path_label')->label('Path label')->searchable(), | ||
| TextColumn::make('data_path')->label('Data path')->wrap(), | ||
| TextColumn::make('repeating_path')->label('Repeating path')->wrap(), | ||
| TextColumn::make('updated_at')->label('Updated')->dateTime()->sortable()->toggleable(isToggledHiddenByDefault: true), | ||
| ]) | ||
| ->defaultSort('updated_at', 'desc') | ||
| ->filters([ | ||
| SelectFilter::make('data_source') | ||
| ->label('Data source') | ||
| ->multiple() | ||
| ->options( | ||
| DataBindingMapping::query() | ||
| ->whereNotNull('data_source') | ||
| ->distinct() | ||
| ->orderBy('data_source') | ||
| ->pluck('data_source', 'data_source') | ||
| ->all() | ||
| ), | ||
| ]) | ||
| ->actions([ | ||
| Tables\Actions\ViewAction::make(), | ||
| Tables\Actions\EditAction::make(), | ||
| ]) | ||
| ->bulkActions([ | ||
| Tables\Actions\DeleteBulkAction::make(), | ||
| ]); | ||
| } | ||
|
|
||
| public static function getPages(): array | ||
| { | ||
| return [ | ||
| 'index' => Pages\ListDataBindingMappings::route('/'), | ||
| 'create' => Pages\CreateDataBindingMapping::route('/create'), | ||
| 'view' => Pages\ViewDataBindingMapping::route('/{record}'), | ||
| 'edit' => Pages\EditDataBindingMapping::route('/{record}/edit'), | ||
| ]; | ||
| } | ||
|
|
||
| // build JSONPath from source + label without mutating inputs | ||
| public static function composeJsonPath(?string $source, ?string $label): string | ||
| { | ||
| $s = trim((string) $source); | ||
| $l = trim((string) $label); | ||
|
|
||
| if ($s === '' || $l === '') { | ||
| return ''; | ||
| } | ||
|
|
||
| // normalise minimally for preview safety | ||
| $s = str_replace(['"', "\r", "\n"], ["'", ' ', ' '], $s); | ||
| $l = str_replace(['"', "\r", "\n"], ["'", ' ', ' '], $l); | ||
|
|
||
| return "$.['{$s}'].['{$l}']"; | ||
| } | ||
| } | ||
22 changes: 22 additions & 0 deletions
22
app/Filament/Forms/Resources/DataBindingMappingResource/Pages/CreateDataBindingMapping.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| <?php | ||
|
|
||
| namespace App\Filament\Forms\Resources\DataBindingMappingResource\Pages; | ||
|
|
||
| use App\Filament\Forms\Resources\DataBindingMappingResource; | ||
| use Filament\Resources\Pages\CreateRecord; | ||
|
|
||
| class CreateDataBindingMapping extends CreateRecord | ||
| { | ||
| protected static string $resource = DataBindingMappingResource::class; | ||
|
|
||
| protected function mutateFormDataBeforeCreate(array $data): array | ||
| { | ||
| $data['data_path'] = DataBindingMappingResource::composeJsonPath( | ||
| $data['data_source'] ?? '', | ||
| $data['path_label'] ?? '', | ||
| ); | ||
|
|
||
| return $data; | ||
| } | ||
|
|
||
| } |
22 changes: 22 additions & 0 deletions
22
app/Filament/Forms/Resources/DataBindingMappingResource/Pages/EditDataBindingMapping.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| <?php | ||
|
|
||
| namespace App\Filament\Forms\Resources\DataBindingMappingResource\Pages; | ||
|
|
||
| use App\Filament\Forms\Resources\DataBindingMappingResource; | ||
| use Filament\Resources\Pages\EditRecord; | ||
|
|
||
| class EditDataBindingMapping extends EditRecord | ||
| { | ||
| protected static string $resource = DataBindingMappingResource::class; | ||
|
|
||
| protected function mutateFormDataBeforeSave(array $data): array | ||
| { | ||
| $data['data_path'] = DataBindingMappingResource::composeJsonPath( | ||
| $data['data_source'] ?? '', | ||
| $data['path_label'] ?? '', | ||
| ); | ||
|
|
||
| return $data; | ||
| } | ||
|
|
||
| } |
22 changes: 22 additions & 0 deletions
22
app/Filament/Forms/Resources/DataBindingMappingResource/Pages/ListDataBindingMappings.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| <?php | ||
|
|
||
| namespace App\Filament\Forms\Resources\DataBindingMappingResource\Pages; | ||
|
|
||
| use App\Filament\Forms\Resources\DataBindingMappingResource; | ||
| use Filament\Actions; | ||
| use Filament\Resources\Pages\ListRecords; | ||
|
|
||
| class ListDataBindingMappings extends ListRecords | ||
| { | ||
| protected static string $resource = DataBindingMappingResource::class; | ||
|
|
||
| protected function getHeaderActions(): array | ||
| { | ||
| return [ | ||
| Actions\CreateAction::make() | ||
| ->label('New Databinding Mapping') | ||
| ->icon('heroicon-m-plus') | ||
| ->authorize(fn () => DataBindingMappingResource::canCreate()), | ||
| ]; | ||
| } | ||
| } |
19 changes: 19 additions & 0 deletions
19
app/Filament/Forms/Resources/DataBindingMappingResource/Pages/ViewDataBindingMapping.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| <?php | ||
|
|
||
| namespace App\Filament\Forms\Resources\DataBindingMappingResource\Pages; | ||
|
|
||
| use App\Filament\Forms\Resources\DataBindingMappingResource; | ||
| use Filament\Actions; | ||
| use Filament\Resources\Pages\ViewRecord; | ||
|
|
||
| class ViewDataBindingMapping extends ViewRecord | ||
| { | ||
| protected static string $resource = DataBindingMappingResource::class; | ||
|
|
||
| protected function getHeaderActions(): array | ||
| { | ||
| return [ | ||
| Actions\EditAction::make(), | ||
| ]; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The admin view access has been properly implemented which is great, but would prefer to follow our practice of policies for management of access on the Model level. ~./app/Policies demonstrates permissions management for this.
The Readme reviews the workflow process for adding new models: https://github.com/bcgov/klamm?tab=readme-ov-file#general-developer-workflows-for-adding-new-content