| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Services\Filter; |
| 4 |
|
| 5 |
use FluentCart\App\Models\AttributeGroup; |
| 6 |
use FluentCart\App\Services\Filter\BaseFilter; |
| 7 |
use FluentCart\App\Services\Filter\Concerns\HasIdTitleSlugSearch; |
| 8 |
|
| 9 |
class AttrGroupFilter extends BaseFilter |
| 10 |
{ |
| 11 |
use HasIdTitleSlugSearch; |
| 12 |
|
| 13 |
// Groups render in the merchant's manual drag-order (fct_atts_groups.serial), |
| 14 |
// the same contract terms use. Forced here so the list the sidebar drags |
| 15 |
// matches the order the reorder endpoint persists. |
| 16 |
public string $defaultSortBy = 'serial'; |
| 17 |
public string $defaultSortType = 'asc'; |
| 18 |
|
| 19 |
protected function parseSortBy(): string |
| 20 |
{ |
| 21 |
return 'serial'; |
| 22 |
} |
| 23 |
|
| 24 |
protected function parseSortType(): string |
| 25 |
{ |
| 26 |
return 'asc'; |
| 27 |
} |
| 28 |
|
| 29 |
public function applySelect(): void |
| 30 |
{ |
| 31 |
$this->query->select(['id', 'title', 'settings', 'serial', 'is_system', 'created_at', 'updated_at']); |
| 32 |
} |
| 33 |
|
| 34 |
protected function applyWith(): void |
| 35 |
{ |
| 36 |
parent::applyWith(); |
| 37 |
$this->query = $this->query->withCount(['terms', 'usedTerms']); |
| 38 |
} |
| 39 |
|
| 40 |
protected function buildCommonQuery() |
| 41 |
{ |
| 42 |
parent::buildCommonQuery(); |
| 43 |
|
| 44 |
// Deterministic tie-breaker so two groups with the same sort key |
| 45 |
// (created_at, title, etc.) paginate in a stable order across requests. |
| 46 |
// Appended AFTER parent::buildCommonQuery() so applySort runs first |
| 47 |
// and id ASC trails as the secondary key. If this ran inside |
| 48 |
// applyWith() instead, ORDER BY clauses register in the wrong order |
| 49 |
// and the user's sort silently becomes the tie-breaker. |
| 50 |
$this->query->orderBy('id', 'ASC'); |
| 51 |
} |
| 52 |
|
| 53 |
public function tabsMap(): array |
| 54 |
{ |
| 55 |
return [ |
| 56 |
'dropdown' => 'styling', |
| 57 |
'button' => 'styling', |
| 58 |
]; |
| 59 |
} |
| 60 |
|
| 61 |
public function getModel(): string |
| 62 |
{ |
| 63 |
return AttributeGroup::class; |
| 64 |
} |
| 65 |
|
| 66 |
public static function getFilterName(): string |
| 67 |
{ |
| 68 |
return 'attr_groups'; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* @return array<string, array<string, mixed>> |
| 73 |
*/ |
| 74 |
protected static function sortableColumns(): array |
| 75 |
{ |
| 76 |
return [ |
| 77 |
'id' => ['label' => __('ID', 'fluent-cart'), 'column' => 'id'], |
| 78 |
'title' => ['label' => __('Title', 'fluent-cart'), 'column' => 'title'], |
| 79 |
'created_at' => ['label' => __('Created at', 'fluent-cart'), 'column' => 'created_at'], |
| 80 |
]; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Intentionally empty, and verified so: AttrGroupsTable.js sends no `with` |
| 85 |
* at all, so there is nothing to allow. Add an entry only when a caller |
| 86 |
* actually needs it — do not pre-open relations. |
| 87 |
* |
| 88 |
* @return array<string, callable> |
| 89 |
*/ |
| 90 |
protected function allowedWiths(): array |
| 91 |
{ |
| 92 |
return []; |
| 93 |
} |
| 94 |
|
| 95 |
public function applyActiveViewFilter(?string $activeView = null): void |
| 96 |
{ |
| 97 |
$activeView = $activeView ?? $this->activeView; |
| 98 |
$this->query->when($activeView, function ($query, $activeView) { |
| 99 |
$this->whereStyling($query, sanitize_text_field($activeView)); |
| 100 |
}); |
| 101 |
} |
| 102 |
|
| 103 |
private function whereStyling($query, string $styling): void |
| 104 |
{ |
| 105 |
$query->whereRaw( |
| 106 |
"JSON_UNQUOTE(JSON_EXTRACT(settings, '$.styling')) = ?", |
| 107 |
[$styling] |
| 108 |
); |
| 109 |
} |
| 110 |
} |
| 111 |
|