|
14 | 14 | use Throwable; |
15 | 15 | use App\Models\PredefinedFilter; |
16 | 16 | use App\Services\FilterService\FilterService; |
| 17 | +use App\Services\PredefinedFilterPermissionService; |
17 | 18 | use Illuminate\Http\Request; |
18 | 19 | use Illuminate\Pagination\LengthAwarePaginator; |
19 | 20 | use Illuminate\Support\Collection; |
@@ -60,94 +61,82 @@ public function getFilterWithOptionalPermissionsById(int $id, bool $include_pred |
60 | 61 | $permissions = $this->predefinedFilterPermissionService->getPermissionsByPredefinedFilterId($id); |
61 | 62 | $predefinedFilter['permissions'] = $permissions; |
62 | 63 | } |
63 | | - |
| 64 | + |
| 65 | + return $predefinedFilter; |
| 66 | + } |
| 67 | + |
| 68 | + public function getFilterWithIdAndNameValues(int $id) |
| 69 | + { |
| 70 | + $predefinedFilter = $this->getFilterWithOptionalPermissionsById($id); |
| 71 | + |
64 | 72 | if (!$predefinedFilter){ |
65 | 73 | return null; |
66 | 74 | } |
67 | 75 |
|
| 76 | + $fieldsToLookup = [ |
| 77 | + 'company', |
| 78 | + 'model', |
| 79 | + 'category', |
| 80 | + 'status_label', |
| 81 | + 'location', |
| 82 | + 'rtd_location', |
| 83 | + 'manufacturer', |
| 84 | + 'supplier' |
| 85 | + ]; |
| 86 | + |
68 | 87 | $filters = $predefinedFilter->filter_data; |
69 | | - |
| 88 | + |
70 | 89 | foreach ($filters as &$filter) { |
| 90 | + |
| 91 | + $model = null; |
71 | 92 |
|
72 | | - |
| 93 | + if (isset($filter['field']) && !in_array($filter['field'], $fieldsToLookup)){ |
| 94 | + continue; |
| 95 | + } |
| 96 | + |
73 | 97 | if (!empty($filter['value']) && is_array($filter['value']) && is_int($filter['value'][0])) { |
| 98 | + |
74 | 99 | $values =[]; |
| 100 | + |
75 | 101 | foreach ($filter['value'] as $valueId){ |
76 | 102 | switch ($filter['field']) { |
77 | 103 | case 'company': |
78 | | - $company = Company::find($valueId); |
79 | | - if ($company) { |
80 | | - $values[] = [ |
81 | | - 'id' => $company->id, |
82 | | - 'name' => $company->name, |
83 | | - ]; |
84 | | - } |
| 104 | + $model = Company::find($valueId); |
85 | 105 | break; |
86 | 106 | case 'model': |
87 | 107 | $model = AssetModel::find($valueId); |
88 | | - if ($model) { |
89 | | - $values[] = [ |
90 | | - 'id' => $model->id, |
91 | | - 'name' => $model->name, |
92 | | - ]; |
93 | | - } |
94 | 108 | break; |
95 | 109 | case 'category': |
96 | | - $category = Category::find($valueId); |
97 | | - if ($category) { |
98 | | - $values[] = [ |
99 | | - 'id' => $category->id, |
100 | | - 'name' => $category->name, |
101 | | - ]; |
102 | | - } |
| 110 | + $model = Category::find($valueId); |
103 | 111 | break; |
104 | | - case 'statuslabel': |
105 | | - $status = Statuslabel::find($valueId); |
106 | | - if ($status) { |
107 | | - $values[] = [ |
108 | | - 'id' => $status->id, |
109 | | - 'name' => $status->name, |
110 | | - ]; |
111 | | - } |
| 112 | + case 'status_label': |
| 113 | + $model = Statuslabel::find($valueId); |
112 | 114 | break; |
113 | 115 | case 'location': |
114 | | - case 'default_location': |
115 | | - $location = Location::find($valueId); |
116 | | - if ($location) { |
117 | | - $values[] = [ |
118 | | - 'id' => $location->id, |
119 | | - 'name' => $location->name, |
120 | | - ]; |
121 | | - } |
| 116 | + case 'rtd_location': |
| 117 | + $model = Location::find($valueId); |
122 | 118 | break; |
123 | 119 | case 'manufacturer': |
124 | | - $manufacturer = Manufacturer::find($valueId); |
125 | | - if ($manufacturer) { |
126 | | - $values[] = [ |
127 | | - 'id' => $manufacturer->id, |
128 | | - 'name' => $manufacturer->name, |
129 | | - ]; |
130 | | - } |
| 120 | + $model = Manufacturer::find($valueId); |
131 | 121 | break; |
132 | 122 | case 'supplier': |
133 | | - $supplier = Supplier::find($valueId); |
134 | | - if ($supplier) { |
135 | | - $values[] = [ |
136 | | - 'id' => $supplier->id, |
137 | | - 'name' => $supplier->name, |
138 | | - ]; |
139 | | - } |
| 123 | + $model = Supplier::find($valueId); |
140 | 124 | break; |
141 | 125 | default: |
142 | 126 | break; |
143 | 127 | } |
| 128 | + if ($model){ |
| 129 | + $values[] = [ |
| 130 | + 'id' => $model->id, |
| 131 | + 'name' => $model->name |
| 132 | + ]; |
| 133 | + } |
| 134 | + $filter['value'] = $values; |
144 | 135 | } |
145 | | - |
146 | | - $filter['value'] = $values; |
147 | | - $predefinedFilter->filter_data = $filters; |
148 | | - |
| 136 | + |
149 | 137 | } |
150 | 138 | } |
| 139 | + $predefinedFilter->filter_data = $filters; |
151 | 140 | return $predefinedFilter; |
152 | 141 | } |
153 | 142 |
|
|
0 commit comments