| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Models; |
| 4 |
use FluentCart\Framework\Support\Arr; |
| 5 |
use FluentCart\App\Models\Concerns\CanSearch; |
| 6 |
|
| 7 |
/** |
| 8 |
* Attributes Group Model - DB Model for Attributes Group eg: color, size etc |
| 9 |
* |
| 10 |
* Database Model |
| 11 |
* |
| 12 |
* @package FluentCart\App\Models |
| 13 |
* |
| 14 |
* @version 1.0.0 |
| 15 |
*/ |
| 16 |
class AttributeGroup extends Model |
| 17 |
{ |
| 18 |
use CanSearch; |
| 19 |
|
| 20 |
public static function boot() |
| 21 |
{ |
| 22 |
parent::boot(); |
| 23 |
static::deleting(function ($model) { |
| 24 |
$model->terms()->delete(); |
| 25 |
}); |
| 26 |
} |
| 27 |
|
| 28 |
protected $table = 'fct_atts_groups'; |
| 29 |
|
| 30 |
protected $fillable = [ |
| 31 |
'title', |
| 32 |
'slug', |
| 33 |
'description', |
| 34 |
'settings', |
| 35 |
]; |
| 36 |
|
| 37 |
public function setSettingsAttribute($value) |
| 38 |
{ |
| 39 |
if (is_array($value)) { |
| 40 |
$value = json_encode($value, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); |
| 41 |
} |
| 42 |
|
| 43 |
$this->attributes['settings'] = $value; |
| 44 |
|
| 45 |
} |
| 46 |
|
| 47 |
public function getSettingsAttribute($value) |
| 48 |
{ |
| 49 |
if (is_string($value)) { |
| 50 |
$decoded = json_decode($value, true); |
| 51 |
return $decoded ?: $value; |
| 52 |
} |
| 53 |
|
| 54 |
return $value; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* hasMany: Group has many Terms |
| 59 |
* |
| 60 |
* @return \FluentCart\Framework\Database\Orm\Relations\hasMany |
| 61 |
*/ |
| 62 |
public function terms() |
| 63 |
{ |
| 64 |
return $this->hasMany(AttributeTerm::class, 'group_id', 'id'); |
| 65 |
} |
| 66 |
|
| 67 |
public function usedTerms() |
| 68 |
{ |
| 69 |
return $this->hasMany(AttributeRelation::class, 'group_id', 'id'); |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
public function scopeApplyCustomFilters( $query, $filters ) { |
| 75 |
if ( ! $filters ) { |
| 76 |
return $query; |
| 77 |
} |
| 78 |
|
| 79 |
$acceptedKeys = $this->fillable; |
| 80 |
|
| 81 |
foreach ( $filters as $filterKey => $filter ) { |
| 82 |
|
| 83 |
if ( ! in_array( $filterKey, $acceptedKeys ) && $filterKey !== 'terms_count') { |
| 84 |
continue; |
| 85 |
} |
| 86 |
|
| 87 |
$value = Arr::get( $filter, 'value', '' ); |
| 88 |
$operator = Arr::get( $filter, 'operator', '' ); |
| 89 |
|
| 90 |
if ( ! $value || ! $operator || is_array( $value ) ) { |
| 91 |
continue; |
| 92 |
} |
| 93 |
|
| 94 |
switch (strtolower($operator)) { |
| 95 |
case 'includes': |
| 96 |
$operator = "like_all"; |
| 97 |
break; |
| 98 |
case 'not_includes': |
| 99 |
$operator = "not_like"; |
| 100 |
break; |
| 101 |
case 'gt': |
| 102 |
$operator = ">"; |
| 103 |
break; |
| 104 |
case 'lt': |
| 105 |
$operator = "<"; |
| 106 |
break; |
| 107 |
|
| 108 |
default: |
| 109 |
|
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
if ($filterKey === 'terms_count') { |
| 115 |
// Validate operator is in whitelist |
| 116 |
$allowedOperators = ['=', '>', '<', '>=', '<=', '!=']; |
| 117 |
if (in_array($operator, $allowedOperators)) { |
| 118 |
return $query->havingRaw($filterKey.' '.$operator.' ?', [(int)$value]); |
| 119 |
}else{ |
| 120 |
return $query; |
| 121 |
} |
| 122 |
|
| 123 |
// Use parameterized query |
| 124 |
|
| 125 |
} |
| 126 |
|
| 127 |
$param = [ $filterKey => [ "column" => $filterKey, "operator" => $operator, "value" => trim( $value ) ] ]; |
| 128 |
$query->when($param, function ($query) use ($param) { |
| 129 |
return $query->search($param); |
| 130 |
}); |
| 131 |
} |
| 132 |
|
| 133 |
return $query; |
| 134 |
} |
| 135 |
|
| 136 |
} |
| 137 |
|