|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Daun\StatamicMux\Query\Scopes\Filters\Fields; |
| 4 | + |
| 5 | +use Illuminate\Support\Arr; |
| 6 | +use Statamic\Query\Scopes\Filters\Fields\FieldtypeFilter; |
| 7 | + |
| 8 | +class MuxMirrorFieldtypeFilter extends FieldtypeFilter |
| 9 | +{ |
| 10 | + public function fieldItems() |
| 11 | + { |
| 12 | + return [ |
| 13 | + 'field' => [ |
| 14 | + 'type' => 'select', |
| 15 | + 'options' => [ |
| 16 | + 'status' => __('Status'), |
| 17 | + 'policy' => __('Policy'), |
| 18 | + 'id' => __('ID'), |
| 19 | + ], |
| 20 | + 'default' => 'status', |
| 21 | + ], |
| 22 | + 'id' => [ |
| 23 | + 'type' => 'text', |
| 24 | + 'placeholder' => __('ID'), |
| 25 | + 'required' => false, |
| 26 | + 'if' => [ |
| 27 | + 'field' => 'id', |
| 28 | + ], |
| 29 | + ], |
| 30 | + 'policy' => [ |
| 31 | + 'type' => 'select', |
| 32 | + 'options' => [ |
| 33 | + 'public' => __('Public'), |
| 34 | + 'signed' => __('Signed'), |
| 35 | + ], |
| 36 | + 'required' => false, |
| 37 | + 'if' => [ |
| 38 | + 'field' => 'policy', |
| 39 | + ], |
| 40 | + ], |
| 41 | + 'status' => [ |
| 42 | + 'type' => 'select', |
| 43 | + 'options' => [ |
| 44 | + 'uploaded' => __('Uploaded'), |
| 45 | + 'not_uploaded' => __('Not Uploaded'), |
| 46 | + 'ignored' => __('Ignored'), |
| 47 | + ], |
| 48 | + 'required' => false, |
| 49 | + 'if' => [ |
| 50 | + 'field' => 'status', |
| 51 | + ], |
| 52 | + ], |
| 53 | + ]; |
| 54 | + } |
| 55 | + |
| 56 | + public function apply($query, $handle, $values) |
| 57 | + { |
| 58 | + $field = $values['field']; |
| 59 | + |
| 60 | + if ($field === 'status') { |
| 61 | + match ($values['status'] ?? null) { |
| 62 | + 'uploaded' => $query->where('is_video', true)->whereNotNull("{$handle}->id"), |
| 63 | + 'not_uploaded' => $query->where('is_video', true)->whereNull("{$handle}->id"), |
| 64 | + 'ignored' => $query->where('is_video', false), |
| 65 | + }; |
| 66 | + } |
| 67 | + |
| 68 | + if ($field === 'policy') { |
| 69 | + match ($values['policy'] ?? null) { |
| 70 | + 'public' => $query->whereNotNull("{$handle}->playback_ids->public"), |
| 71 | + 'signed' => $query->whereNotNull("{$handle}->playback_ids->signed"), |
| 72 | + }; |
| 73 | + } |
| 74 | + |
| 75 | + if ($field === 'id' && ($values['id'] ?? null)) { |
| 76 | + $query->where(fn ($q) => $q |
| 77 | + ->where("{$handle}->id", 'like', "%{$values['id']}%") |
| 78 | + ->orWhere("{$handle}->playback_ids", 'like', "%{$values['id']}%") |
| 79 | + ); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + public function badge($values) |
| 84 | + { |
| 85 | + $base = 'Mux'; |
| 86 | + $field = match ($values['field'] ?? null) { |
| 87 | + 'status' => __('Status'), |
| 88 | + 'policy' => __('Policy'), |
| 89 | + 'id' => __('ID'), |
| 90 | + }; |
| 91 | + $value = match ($values['field'] ?? null) { |
| 92 | + 'status' => Arr::get($this->fieldItems(), "status.options.{$values['status']}"), |
| 93 | + 'policy' => Arr::get($this->fieldItems(), "policy.options.{$values['policy']}"), |
| 94 | + 'id' => $values['id'] ?? null, |
| 95 | + }; |
| 96 | + |
| 97 | + return $base.' '.$field.' '.__('Is').' '.$value; |
| 98 | + } |
| 99 | + |
| 100 | + public function isComplete($values): bool |
| 101 | + { |
| 102 | + $values = array_filter($values); |
| 103 | + |
| 104 | + if (! $field = Arr::get($values, 'field')) { |
| 105 | + return false; |
| 106 | + } |
| 107 | + |
| 108 | + if ($field === 'status' && Arr::has($values, 'status')) { |
| 109 | + return true; |
| 110 | + } |
| 111 | + |
| 112 | + if ($field === 'policy' && Arr::has($values, 'policy')) { |
| 113 | + return true; |
| 114 | + } |
| 115 | + |
| 116 | + if ($field === 'id' && Arr::has($values, 'id')) { |
| 117 | + return true; |
| 118 | + } |
| 119 | + |
| 120 | + return false; |
| 121 | + } |
| 122 | +} |
0 commit comments