| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Services\Filter; |
| 4 |
|
| 5 |
use FluentCart\App\Models\Product; |
| 6 |
use FluentCart\Framework\Database\Orm\Builder; |
| 7 |
|
| 8 |
class VariationFilter extends BaseFilter |
| 9 |
{ |
| 10 |
|
| 11 |
public function applySimpleFilter(?string $search = null): void |
| 12 |
{ |
| 13 |
|
| 14 |
$this->query = $this->query->when($search ?? $this->search, function ($query, $search) { |
| 15 |
return $query->where('post_title', 'LIKE', "%{$search}%") |
| 16 |
->orWhereHas('variants', function (Builder $query) use ($search) { |
| 17 |
$query->where('variation_title', 'LIKE', "%{$search}%"); |
| 18 |
}); |
| 19 |
}); |
| 20 |
} |
| 21 |
|
| 22 |
protected function applyMustLoadIds() |
| 23 |
{ |
| 24 |
$this->query = $this->query->orWhereIn('ID', $this->includeIds) |
| 25 |
->orWhereHas('variants', function (Builder $query) { |
| 26 |
$query->orWhereIn('id', $this->includeIds); |
| 27 |
}); |
| 28 |
} |
| 29 |
|
| 30 |
public function tabsMap(): array |
| 31 |
{ |
| 32 |
return [ |
| 33 |
'publish' => 'post_status', |
| 34 |
'simple' => 'variation_type', |
| 35 |
'simple_variations' => 'variation_type', |
| 36 |
'physical' => 'fulfillment_type', |
| 37 |
'digital' => 'fulfillment_type', |
| 38 |
]; |
| 39 |
} |
| 40 |
|
| 41 |
public function getModel(): string |
| 42 |
{ |
| 43 |
return Product::class; |
| 44 |
} |
| 45 |
|
| 46 |
public static function getFilterName(): string |
| 47 |
{ |
| 48 |
return 'product_variation'; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Intentionally empty. This filter cannot receive a request `with` today: |
| 53 |
* its only caller is AdvanceFilterController::getFilterOption(), which |
| 54 |
* hand-builds a five-key argument array (remote_data_key, search, |
| 55 |
* include_ids, limit, parent_id) and never forwards the raw request. |
| 56 |
* |
| 57 |
* That is the reason the map is empty, NOT an oversight — if this filter is |
| 58 |
* ever refactored to take `$request->all()`, an empty map is what keeps it |
| 59 |
* closed, and any relation it then needs must be added here deliberately as |
| 60 |
* a context key mapped to a callable. |
| 61 |
* |
| 62 |
* @return array<string, callable> |
| 63 |
*/ |
| 64 |
protected function allowedWiths(): array |
| 65 |
{ |
| 66 |
return []; |
| 67 |
} |
| 68 |
|
| 69 |
public function applyActiveViewFilter(?string $activeView = null): void |
| 70 |
{ |
| 71 |
|
| 72 |
} |
| 73 |
|
| 74 |
public static function getTreeFilterOptions(array $args): array |
| 75 |
{ |
| 76 |
|
| 77 |
return static::make($args)->get()->map(function ($product) { |
| 78 |
return [ |
| 79 |
'value' => $product->ID, |
| 80 |
'label' => $product->post_title, |
| 81 |
'children' => $product->variants->map(function ($variation) { |
| 82 |
return [ |
| 83 |
'value' => $variation->id, |
| 84 |
'label' => $variation->variation_title, |
| 85 |
]; |
| 86 |
})->toArray() |
| 87 |
]; |
| 88 |
})->toArray(); |
| 89 |
} |
| 90 |
} |