|
2 | 2 |
|
3 | 3 | namespace Statamic\Query\Scopes\Filters\Fields; |
4 | 4 |
|
5 | | -class Bard extends Textarea |
| 5 | +use Statamic\Support\Arr; |
| 6 | +use Statamic\Support\Str; |
| 7 | + |
| 8 | +class Bard extends FieldtypeFilter |
6 | 9 | { |
7 | | - // |
| 10 | + public function fieldItems() |
| 11 | + { |
| 12 | + return [ |
| 13 | + 'operator' => [ |
| 14 | + 'type' => 'select', |
| 15 | + 'placeholder' => __('Select Operator'), |
| 16 | + 'options' => [ |
| 17 | + 'like' => __('Contains'), |
| 18 | + 'null' => __('Empty'), |
| 19 | + 'not-null' => __('Not empty'), |
| 20 | + ], |
| 21 | + 'default' => 'like', |
| 22 | + ], |
| 23 | + 'value' => [ |
| 24 | + 'type' => 'text', |
| 25 | + 'if' => [ |
| 26 | + 'operator' => 'like', |
| 27 | + ], |
| 28 | + 'required' => false, |
| 29 | + ], |
| 30 | + ]; |
| 31 | + } |
| 32 | + |
| 33 | + public function apply($query, $handle, $values) |
| 34 | + { |
| 35 | + $operator = $values['operator']; |
| 36 | + $value = $values['value']; |
| 37 | + |
| 38 | + if ($operator === 'like') { |
| 39 | + $value = Str::ensureLeft($value, '%'); |
| 40 | + $value = Str::ensureRight($value, '%'); |
| 41 | + } |
| 42 | + |
| 43 | + match ($operator) { |
| 44 | + 'null' => $query->whereNull($handle), |
| 45 | + 'not-null' => $query->whereNotNull($handle), |
| 46 | + default => $query->where($handle, $operator, $value), |
| 47 | + }; |
| 48 | + } |
| 49 | + |
| 50 | + public function badge($values) |
| 51 | + { |
| 52 | + $field = $this->fieldtype->field()->display(); |
| 53 | + $operator = $values['operator']; |
| 54 | + $translatedOperator = Arr::get($this->fieldItems(), "operator.options.{$operator}"); |
| 55 | + $value = $values['value']; |
| 56 | + |
| 57 | + return $field.' '.strtolower($translatedOperator).' '.$value; |
| 58 | + } |
8 | 59 | } |
0 commit comments