| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Services\Filter; |
| 4 |
|
| 5 |
use FluentCart\App\Helpers\Status; |
| 6 |
use FluentCart\App\Services\DateTime\DateTime; |
| 7 |
use FluentCartPro\App\Modules\Licensing\Models\License; |
| 8 |
|
| 9 |
|
| 10 |
class LicenseFilter extends BaseFilter |
| 11 |
{ |
| 12 |
|
| 13 |
public function applySimpleFilter(?string $search = null): void |
| 14 |
{ |
| 15 |
$isApplied = $this->applySimpleOperatorFilter($search); |
| 16 |
if ($isApplied) { |
| 17 |
return; |
| 18 |
} |
| 19 |
|
| 20 |
$this->query->when($search ?? $this->search, function ($query, $search) { |
| 21 |
// If search is an array, implode it |
| 22 |
$search = is_array($search) ? implode(' ', $search) : $search; |
| 23 |
|
| 24 |
// If search is empty or null, return the query |
| 25 |
if (empty($search)) { |
| 26 |
return $query; |
| 27 |
} |
| 28 |
|
| 29 |
$query->where(function ($query) use ($search) { |
| 30 |
$query->where('license_key', 'like', '%' . $search . '%') |
| 31 |
->orWhere('order_id', 'like', '%' . $search . '%') |
| 32 |
->orWhereHas('customer', function ($query) use ($search) { |
| 33 |
$query |
| 34 |
->whereRaw("CONCAT(first_name, ' ', last_name) LIKE ?", ["%{$search}%"]) |
| 35 |
->orWhere('email', 'like', '%' . $search . '%'); |
| 36 |
}) |
| 37 |
->orWhereHas('activations', function ($query) use ($search) { |
| 38 |
$query->whereHas('site', function ($query) use ($search) { |
| 39 |
$query->where('site_url', 'like', '%' . $search . '%'); |
| 40 |
}); |
| 41 |
}) |
| 42 |
; |
| 43 |
}); |
| 44 |
|
| 45 |
return $query; |
| 46 |
}); |
| 47 |
} |
| 48 |
|
| 49 |
public function tabsMap(): array |
| 50 |
{ |
| 51 |
return [ |
| 52 |
'inactive' => __('Inactive', 'fluent-cart'), |
| 53 |
'active' => __('Active', 'fluent-cart'), |
| 54 |
'expired' => __('Expired', 'fluent-cart'), |
| 55 |
'disabled' => __('disabled', 'fluent-cart') |
| 56 |
]; |
| 57 |
} |
| 58 |
|
| 59 |
public function getModel(): string |
| 60 |
{ |
| 61 |
return License::class; |
| 62 |
} |
| 63 |
|
| 64 |
public static function getFilterName(): string |
| 65 |
{ |
| 66 |
return 'licenses'; |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
public function applyActiveViewFilter(?string $activeView = null): void |
| 71 |
{ |
| 72 |
$activeView = $activeView ?? $this->activeView; |
| 73 |
$tabsMap = $this->tabsMap(); |
| 74 |
|
| 75 |
$this->query->when($activeView, function ($query, $activeView) use ($tabsMap) { |
| 76 |
|
| 77 |
$validStatuses = [ |
| 78 |
'active', |
| 79 |
'expired', |
| 80 |
'disabled', |
| 81 |
'inactive' |
| 82 |
]; |
| 83 |
|
| 84 |
if (in_array($activeView, $validStatuses)) { |
| 85 |
if ($activeView == 'expired') { |
| 86 |
$query->where('expiration_date', '<', DateTime::gmtNow()); |
| 87 |
} else if ($activeView == 'active') { |
| 88 |
$query->where(function ($query) { |
| 89 |
$query->where('expiration_date', '>', DateTime::gmtNow()) |
| 90 |
->orWhereNull('expiration_date'); |
| 91 |
}) |
| 92 |
->where('status', 'active'); |
| 93 |
} else { |
| 94 |
$query->where('status', $activeView); |
| 95 |
} |
| 96 |
} else if ($activeView == 'inactive') { |
| 97 |
$query->where('status', 'active') |
| 98 |
->whereDoesntHave('activations'); |
| 99 |
} |
| 100 |
|
| 101 |
return $query; |
| 102 |
}); |
| 103 |
} |
| 104 |
|
| 105 |
public static function getSearchableFields(): array |
| 106 |
{ |
| 107 |
return [ |
| 108 |
'key' => [ |
| 109 |
'column' => 'license_key', |
| 110 |
'description' => 'license_key', |
| 111 |
'type' => 'string' |
| 112 |
] |
| 113 |
]; |
| 114 |
} |
| 115 |
|
| 116 |
public static function advanceFilterOptions(): array |
| 117 |
{ |
| 118 |
$filters = [ |
| 119 |
'product' => [ |
| 120 |
'label' => __('Products', 'fluent-cart'), |
| 121 |
'value' => 'product', |
| 122 |
'children' => [ |
| 123 |
[ |
| 124 |
'label' => __('By Products', 'fluent-cart'), |
| 125 |
'value' => 'product', |
| 126 |
'column' => 'variation_id', |
| 127 |
'filter_type' => 'relation', |
| 128 |
'relation' => 'productVariant', |
| 129 |
'remote_data_key' => 'product_variations', |
| 130 |
'type' => 'remote_tree_select', |
| 131 |
'limit' => 10, |
| 132 |
], |
| 133 |
], |
| 134 |
], |
| 135 |
'customer' => [ |
| 136 |
'label' => __('Customer Property', 'fluent-cart'), |
| 137 |
'value' => 'customer', |
| 138 |
'children' => [ |
| 139 |
[ |
| 140 |
'label' => __('Customer first name', 'fluent-cart'), |
| 141 |
'value' => 'customer_first_name', |
| 142 |
'type' => 'text', |
| 143 |
'filter_type' => 'relation', |
| 144 |
'column' => 'first_name', |
| 145 |
'relation' => 'customer', |
| 146 |
], |
| 147 |
[ |
| 148 |
'label' => __('Customer last name', 'fluent-cart'), |
| 149 |
'value' => 'customer_last_name', |
| 150 |
'type' => 'text', |
| 151 |
'filter_type' => 'relation', |
| 152 |
'column' => 'last_name', |
| 153 |
'relation' => 'customer', |
| 154 |
] |
| 155 |
], |
| 156 |
], |
| 157 |
'license' => [ |
| 158 |
'label' => __('License Property', 'fluent-cart'), |
| 159 |
'value' => 'license', |
| 160 |
'children' => [ |
| 161 |
[ |
| 162 |
'label' => __('License key', 'fluent-cart'), |
| 163 |
'value' => 'license_key', |
| 164 |
'type' => 'text', |
| 165 |
'filter_type' => 'column', |
| 166 |
'column' => 'license_key', |
| 167 |
], |
| 168 |
[ |
| 169 |
'label' => __('Status', 'fluent-cart'), |
| 170 |
'value' => 'status', |
| 171 |
'type' => 'selections', |
| 172 |
'filter_type' => 'column', |
| 173 |
'column' => 'status', |
| 174 |
'options' => [ |
| 175 |
Status::LICENSE_ACTIVE => __('Active', 'fluent-cart'), |
| 176 |
Status::LICENSE_DISABLED => __('Disabled', 'fluent-cart'), |
| 177 |
Status::LICENSE_EXPIRED => __('Expired', 'fluent-cart'), |
| 178 |
], |
| 179 |
'is_multiple' => true, |
| 180 |
'is_only_in' => true |
| 181 |
], |
| 182 |
[ |
| 183 |
'label' => __('Activation Count', 'fluent-cart'), |
| 184 |
'value' => 'activation_count', |
| 185 |
'type' => 'numeric', |
| 186 |
'filter_type' => 'column', |
| 187 |
'column' => 'activation_count', |
| 188 |
], |
| 189 |
[ |
| 190 |
'label' => __('Expiration Date', 'fluent-cart'), |
| 191 |
'value' => 'expiration_date', |
| 192 |
'type' => 'dates', |
| 193 |
'filter_type' => 'date', |
| 194 |
'column' => 'expiration_date', |
| 195 |
] |
| 196 |
], |
| 197 |
], |
| 198 |
]; |
| 199 |
return LabelFilter::advanceFilterOptionsForOther($filters); |
| 200 |
} |
| 201 |
|
| 202 |
} |
| 203 |
|