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