|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Eclipse\Catalogue\Filament\Filters\Operators; |
| 4 | + |
| 5 | +use Filament\Forms\Components\Component; |
| 6 | +use Filament\Forms\Components\TextInput; |
| 7 | +use Filament\Tables\Filters\QueryBuilder\Constraints\Operators\Operator; |
| 8 | +use Illuminate\Database\Eloquent\Builder; |
| 9 | + |
| 10 | +class CustomPropertyEndsWithOperator extends Operator |
| 11 | +{ |
| 12 | + /** |
| 13 | + * Get the name of the operator. |
| 14 | + */ |
| 15 | + public function getName(): string |
| 16 | + { |
| 17 | + return 'ends_with'; |
| 18 | + } |
| 19 | + |
| 20 | + /** |
| 21 | + * Get the label of the operator. |
| 22 | + */ |
| 23 | + public function getLabel(): string |
| 24 | + { |
| 25 | + return __( |
| 26 | + $this->isInverse() ? |
| 27 | + 'filament-tables::filters/query-builder.operators.text.ends_with.label.inverse' : |
| 28 | + 'filament-tables::filters/query-builder.operators.text.ends_with.label.direct', |
| 29 | + ); |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * Get the summary of the operator. |
| 34 | + */ |
| 35 | + public function getSummary(): string |
| 36 | + { |
| 37 | + return __( |
| 38 | + $this->isInverse() ? |
| 39 | + 'filament-tables::filters/query-builder.operators.text.ends_with.summary.inverse' : |
| 40 | + 'filament-tables::filters/query-builder.operators.text.ends_with.summary.direct', |
| 41 | + [ |
| 42 | + 'attribute' => $this->getConstraint()->getAttributeLabel(), |
| 43 | + 'text' => $this->getSettings()['text'], |
| 44 | + ], |
| 45 | + ); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Get the form schema of the operator. |
| 50 | + * |
| 51 | + * @return array<Component> |
| 52 | + */ |
| 53 | + public function getFormSchema(): array |
| 54 | + { |
| 55 | + return [ |
| 56 | + TextInput::make('text') |
| 57 | + ->label(__('filament-tables::filters/query-builder.operators.text.form.text.label')) |
| 58 | + ->required() |
| 59 | + ->columnSpanFull(), |
| 60 | + ]; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Apply the operator to the query. |
| 65 | + */ |
| 66 | + public function apply(Builder $query, string $qualifiedColumn): Builder |
| 67 | + { |
| 68 | + $text = trim($this->getSettings()['text']); |
| 69 | + |
| 70 | + if (empty($text)) { |
| 71 | + return $query; |
| 72 | + } |
| 73 | + |
| 74 | + $constraintName = $this->getConstraint()->getName(); |
| 75 | + $propertyId = (int) str_replace('custom_property_', '', $constraintName); |
| 76 | + |
| 77 | + return $query->whereHas('customPropertyValues', function ($q) use ($propertyId, $text) { |
| 78 | + $q->where('property_id', $propertyId) |
| 79 | + ->whereRaw('REGEXP_REPLACE(value, "<[^>]*>", "") LIKE ?', ["%{$text}"]); |
| 80 | + }); |
| 81 | + } |
| 82 | +} |
0 commit comments