PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.3.21
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.3.21
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 / api / Resource / AttrTermResource.php

AttrTermResource.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.3.21, at api/Resource/AttrTermResource.php

260 lines 9.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCart\Api\Resource;
4
5 use FluentCart\App\Helpers\HelperTrait;
6 use FluentCart\App\Models\AttributeRelation;
7 use FluentCart\App\Models\AttributeTerm;
8 use FluentCart\Framework\Database\Orm\Builder;
9 use FluentCart\Framework\Support\Arr;
10
11 class AttrTermResource extends BaseResourceApi
12 {
13 use HelperTrait;
14
15 private static array $orderByCols = ['id', 'title', 'slug', 'serial', 'created_at'];
16
17 private static array $moveTo = ['up', 'down'];
18
19 public static function getQuery(): Builder
20 {
21 return AttributeTerm::query();
22 }
23
24 /**
25 * Retrieve attribute terms based on the provided parameters.
26 *
27 * @param array $params Optional. Params containing the necessary parameters to retrieve.
28 * [
29 * 'params' => [
30 * "search" => (array) Optional.
31 * [ "column name(e.g., title|slug)" => [
32 * "column" => "column name(e.g., title|slug)",
33 * "operator" => "(string)(e.g., like_all|rlike|or_rlike)",
34 * "value" => (string|array) ]
35 * ],
36 * "filters" => (array) Optional.
37 * [ "column name(e.g., title|slug)" => [
38 * "column" => "column name(e.g., title|slug)",
39 * "operator" => "(string)(e.g., in)",
40 * "value" => (string|array) ]
41 * ],
42 * 'order_by' => (string) Optional. Column to order by,
43 * 'order_type' => (string) Optional. Order type for sorting (ASC or DESC),
44 * 'per_page' => (int) Optional. Number of items for per page,
45 * 'page' => (int) Optional. Page number for pagination
46 * ]
47 * ]
48 *
49 */
50 public static function get(array $params = [])
51 {
52 $orderBy = static::getValWithinEnum(Arr::get($params["params"], 'order_by', 'serial'), static::$orderByCols, 'serial');
53 $orderDir = static::getValWithinEnum(Arr::get($params["params"], 'order_type', 'ASC'), static::$orderByEnum, 'ASC');
54
55 return static::getQuery()->where('group_id', Arr::get($params, 'group_id'))
56 ->when(Arr::get($params["params"], 'search'), function ($query) use ($params) {
57 return $query->search(Arr::get($params["params"], 'search', ''));
58 })
59 ->applyCustomFilters(Arr::get($params["params"], 'filters', []))
60 ->orderBy(
61 sanitize_sql_orderby($orderBy),
62 sanitize_sql_orderby($orderDir)
63 )
64 ->paginate(Arr::get($params["params"], 'per_page', 15), ['*'], 'page', Arr::get($params["params"], 'page'));
65 }
66
67 /**
68 * Find and retrieve attribute term based on the ID and given params.
69 *
70 * @param int $id Required. The ID of the attribute term to find and retrieve.
71 * @param array $params Optional. Additional parameters for finding attribute terms.
72 * [
73 * // Include optional parameters, if any.
74 * ]
75 *
76 */
77 public static function find($id, $params = [])
78 {
79 return static::getQuery()->find($id);
80 }
81
82 /**
83 * Create attribute term with the provided data.
84 *
85 * @param array $data Required. Array containing the necessary parameters
86 * $data => (array) Required. Array of attribute term data.
87 * [
88 * 'title' => (string) Required. The title of the attr term.
89 * 'slug' => (string) Required. The slug of the attr term.
90 * 'description' => (string) Optional. The description of the attr term.
91 * 'serial' => (int) Optional. The serial of the attr term.
92 * ]
93 * @param array $params Required. Additional parameters for attribute term creation.
94 * [
95 * 'params' => [
96 * 'group_id'=> (int) Required. The id of the attr group.
97 * ]
98 * ]
99 *
100 */
101 public static function create($data, $params = [])
102 {
103 $groupId = Arr::get($params, 'group_id');
104
105 $group = static::getQuery()->find($groupId);
106
107 if (!$group) {
108 return static::makeErrorResponse([
109 [ 'code' => 404, 'message' => __('Information mismatch.', 'fluent-cart') ]
110 ]);
111 }
112
113 $data['serial'] = empty($data['serial']) ? 10 : $data['serial'];
114 $data['group_id'] = $group->id;
115
116 $term = AttributeTerm::create($data);
117
118 if ($term) {
119 return static::makeSuccessResponse(
120 $term,
121 __('Successfully created!', 'fluent-cart')
122 );
123 }
124
125 return static::makeErrorResponse([
126 [ 'code' => 400, 'message' => __('Term creation failed.', 'fluent-cart') ]
127 ]);
128 }
129
130 /**
131 * Update attribute term with the provided data.
132 *
133 * @param array $data Required. Array containing the necessary parameters
134 * $data => (array) Required. Array of attribute term data.
135 * [
136 * 'title' => (string) Required. The title of the attr term.
137 * 'slug' => (string) Required. The slug of the attr term.
138 * 'description' => (string) Optional. The description of the attr term.
139 * 'serial' => (int) Optional. The serial of the attr term.
140 * ]
141 * @param int $termId Required. The id of the attribute term to update.
142 * @param array $params Required. Additional parameters for attribute term creation.
143 * [
144 * 'params' => [
145 * 'group_id'=> (int) Required. The id of the attr group.
146 * ]
147 * ]
148 *
149 */
150 public static function update($data, $termId, $params = [])
151 {
152 $groupId = Arr::get($params, 'group_id');
153
154 $term = static::getQuery()->where('id', $termId)->where('group_id', $groupId)->first();
155
156 if (!$term) {
157 return static::makeErrorResponse([
158 [ 'code' => 404, 'message' => __('Information mismatch.', 'fluent-cart') ]
159 ]);
160 }
161
162 $isUpdated = $term->update($data);
163
164 if ($isUpdated) {
165 return static::makeSuccessResponse(
166 $isUpdated,
167 __('Successfully updated!', 'fluent-cart')
168 );
169 }
170
171 return static::makeErrorResponse([
172 [ 'code' => 400, 'message' => __('Term info update failed.', 'fluent-cart') ]
173 ]);
174 }
175
176 /**
177 * Delete attribute term by term ID and given params.
178 *
179 * @param int $termId Required. The ID of the attribute term to delete.
180 * @param array $params Required. Additional parameters for attribute term deletion.
181 * [
182 * 'params' => [
183 * 'group_id'=> (int) Required. The id of the attr group.
184 * ]
185 * ]
186 *
187 */
188 public static function delete($termId, $params = [])
189 {
190 $groupId = Arr::get($params, 'group_id');
191
192 $isUsed = AttributeRelation::query()->where('group_id', $groupId)->where('term_id', $termId)->first();
193
194 if (!$isUsed) {
195
196 $term = static::getQuery()->find($termId);
197
198 if ($term->group_id == $groupId) {
199
200 $term->delete();
201
202 return static::makeSuccessResponse(
203 '',
204 __('Attribute term successfully deleted!', 'fluent-cart')
205 );
206 }
207
208 return static::makeErrorResponse([
209 [ 'code' => 404, 'message' => __('Term not found in database, failed to remove.', 'fluent-cart') ]
210 ]);
211 }
212
213 return static::makeErrorResponse([
214 [ 'code' => 403, 'message' => __('This term is already in use, can not be deleted.', 'fluent-cart') ]
215 ]);
216 }
217
218 /**
219 * Update attribute term serial index with the provided params.
220 *
221 * @param array $params Required. Array containing the necessary parameters
222 * $params => (array) Required. Array of attribute term data.
223 * [
224 * 'term_id' => (int) Required. The term id of the attr term.
225 * 'group_id' => (int) Required. The group id of the attr term.
226 * 'move' => (int) Required. The move of the attr term.
227 * ]
228 *
229 */
230 public static function updateSerial($params = [])
231 {
232 $termId = Arr::get($params, 'term_id');
233 $groupId = Arr::get($params, 'group_id');
234 $move = Arr::get($params, 'move');
235
236 $term = static::getQuery()->find($termId);
237
238 if ($term->group_id == $groupId) {
239
240 $move = static::getValWithinEnum($move, static::$moveTo, 'down');
241
242 if ($move == 'up') {
243 $term->serial = $term->serial > 0 ? ($term->serial - 1) : 0;
244 } else {
245 $term->serial++;
246 }
247
248 $term->save();
249
250 return static::makeSuccessResponse(
251 $term->serial,
252 __('Serial updated.', 'fluent-cart')
253 );
254 }
255
256 return static::makeErrorResponse([
257 [ 'code' => 404, 'message' => __('Info mismatch.', 'fluent-cart') ]
258 ]);
259 }
260 }