PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.3.25
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.3.25
1.6.5 1.6.4 1.6.3 1.6.2 1.6.1 1.6.0 1.5.4 1.5.5 1.5.3 1.5.2 1.5.1 1.5.0 1.4.2 1.4.1 1.4.0 1.3.28 1.3.27 1.3.26 1.3.25 1.3.23 1.3.22 1.3.21 1.3.20 1.3.19 trunk All 48 releases
fluent-cart / app / Models / AttributeTerm.php

AttributeTerm.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.3.25, at app/Models/AttributeTerm.php

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