| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Models; |
| 4 |
use FluentCart\App\Models\Concerns\CanSearch; |
| 5 |
use FluentCart\App\Models\Concerns\CanUpdateBatch; |
| 6 |
use FluentCart\Framework\Support\Arr; |
| 7 |
|
| 8 |
/** |
| 9 |
* Attributes Terms Model - DB Model for Attributes Terms eg for Size: Small, Medium, Large |
| 10 |
* |
| 11 |
* Database Model |
| 12 |
* |
| 13 |
* @package FluentCart\App\Models |
| 14 |
* |
| 15 |
* @version 1.0.0 |
| 16 |
*/ |
| 17 |
class AttributeTerm extends Model |
| 18 |
{ |
| 19 |
use CanSearch, CanUpdateBatch; |
| 20 |
|
| 21 |
protected $table = 'fct_atts_terms'; |
| 22 |
|
| 23 |
protected $fillable = [ |
| 24 |
'group_id', |
| 25 |
'serial', |
| 26 |
'title', |
| 27 |
'slug', |
| 28 |
'description', |
| 29 |
'settings', |
| 30 |
]; |
| 31 |
|
| 32 |
public function setSettingsAttribute($value) |
| 33 |
{ |
| 34 |
if (is_array($value) || is_object($value)) { |
| 35 |
$value = json_encode($value, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); |
| 36 |
} |
| 37 |
$this->attributes['settings'] = $value; |
| 38 |
} |
| 39 |
|
| 40 |
public function getSettingsAttribute($value) |
| 41 |
{ |
| 42 |
if (is_string($value)) { |
| 43 |
$decoded = json_decode($value, true); |
| 44 |
return $decoded ?: $value; |
| 45 |
} |
| 46 |
|
| 47 |
return $value; |
| 48 |
} |
| 49 |
|
| 50 |
public function group() |
| 51 |
{ |
| 52 |
return $this->belongsTo(AttributeGroup::class, 'group_id', 'id'); |
| 53 |
} |
| 54 |
|
| 55 |
public function scopeApplyCustomFilters( $query, $filters ) { |
| 56 |
if ( ! $filters ) { |
| 57 |
return $query; |
| 58 |
} |
| 59 |
|
| 60 |
$acceptedKeys = $this->fillable; |
| 61 |
|
| 62 |
foreach ( $filters as $filterKey => $filter ) { |
| 63 |
|
| 64 |
if ( ! in_array( $filterKey, $acceptedKeys ) ) { |
| 65 |
continue; |
| 66 |
} |
| 67 |
|
| 68 |
$value = Arr::get( $filter, 'value', '' ); |
| 69 |
$operator = Arr::get( $filter, 'operator', '' ); |
| 70 |
|
| 71 |
if ( ! $value || ! $operator || is_array( $value ) ) { |
| 72 |
continue; |
| 73 |
} |
| 74 |
|
| 75 |
switch (strtolower($operator)) { |
| 76 |
case 'includes': |
| 77 |
$operator = "like_all"; |
| 78 |
break; |
| 79 |
case 'not_includes': |
| 80 |
$operator = "not_like"; |
| 81 |
break; |
| 82 |
case 'gt': |
| 83 |
$operator = ">"; |
| 84 |
break; |
| 85 |
case 'lt': |
| 86 |
$operator = "<"; |
| 87 |
break; |
| 88 |
|
| 89 |
default: |
| 90 |
|
| 91 |
} |
| 92 |
|
| 93 |
$param = [ $filterKey => [ "column" => $filterKey, "operator" => $operator, "value" => trim( $value ) ] ]; |
| 94 |
$query->when($param, function ($query) use ($param) { |
| 95 |
return $query->search($param); |
| 96 |
}); |
| 97 |
} |
| 98 |
|
| 99 |
return $query; |
| 100 |
} |
| 101 |
} |
| 102 |
|