PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.3.26
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.3.26
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 / AttrGroupResource.php

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

193 lines 7.3 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\AttributeGroup;
7 use FluentCart\App\Models\AttributeRelation;
8 use FluentCart\Framework\Database\Orm\Builder;
9 use FluentCart\Framework\Support\Arr;
10
11 class AttrGroupResource extends BaseResourceApi
12 {
13 use HelperTrait;
14
15 private static array $orderByCols = ['title', 'id', 'slug', 'created_at'];
16
17 public static function getQuery(): Builder
18 {
19 return AttributeGroup::query();
20 }
21
22 /**
23 * Retrieve attribute groups based on the provided parameters.
24 *
25 * @param array $params Optional. Params containing the necessary parameters to retrieve.
26 * [
27 * 'params' => [
28 * 'with' => (array) Optional. Relationships name to be eager loaded,
29 * "search" => (array) Optional.
30 * [ "column name(e.g., title|slug)" => [
31 * "column" => "column name(e.g., title|slug)",
32 * "operator" => "(string)(e.g., like_all|rlike|or_rlike)",
33 * "value" => (string|array) ]
34 * ],
35 * "filters" => (array) Optional.
36 * [ "column name(e.g., title|slug)" => [
37 * "column" => "column name(e.g., title|slug)",
38 * "operator" => "(string)(e.g., in)",
39 * "value" => (string|array) ]
40 * ],
41 * 'order_by' => (string) Optional. Column to order by,
42 * 'order_type' => (string) Optional. Order type for sorting (ASC or DESC),
43 * 'per_page' => (int) Optional. Number of items for per page,
44 * 'page' => (int) Optional. Page number for pagination
45 * ]
46 * ]
47 *
48 */
49 public static function get(array $params = [])
50 {
51 $with = Arr::get($params["params"], 'with', []);
52
53 return static::getQuery()->with($with)->withCount($with)
54 ->when(Arr::get($params["params"], 'search'), function ($query) use ($params) {
55 return $query->search(Arr::get($params["params"], 'search', ''));
56 })
57 ->when(!empty($with), function ($query) use ($params) {
58 return $query->applyCustomFilters(Arr::get($params["params"], 'filters', []));
59 })
60 ->orderBy(
61 sanitize_sql_orderby(static::getValWithinEnum(Arr::get($params["params"], 'order_by'), static::$orderByCols, 'title')),
62 sanitize_sql_orderby(static::getValWithinEnum(Arr::get($params["params"], 'order_type'), static::$orderByEnum, 'ASC'))
63 )
64 ->paginate(Arr::get($params["params"], 'per_page', 10), ['*'], 'page', Arr::get($params["params"], 'page'));
65
66 }
67
68 /**
69 * Find and retrieve attribute group based on the ID and given params.
70 *
71 * @param int $id Required. The ID of the attribute group to find and retrieve.
72 * @param array $params Optional. Additional parameters for finding attribute groups.
73 * [
74 * 'params' => [
75 * 'with' => (array) Optional. Relationships name to be eager loaded
76 * ]
77 * ]
78 *
79 */
80 public static function find($id, $params = [])
81 {
82 $with = Arr::get($params, 'with', []);
83
84 $query = static::getQuery();
85
86 if (empty($with)) {
87 return $query->find($id);
88 }
89
90 $with = static::getArrValWithinEnum($with, ['terms'], 'terms');
91 return $query->with($with)->find($id);
92 }
93
94 /**
95 * Create attribute group with the provided data.
96 *
97 * @param array $data Required. Array containing the necessary parameters
98 * $data => (array) Required. Array of attribute group data.
99 * [
100 * 'title' => (string) Required. The title of the attr group.
101 * 'slug' => (string) Required. The slug of the attr group.
102 * 'description' => (string) Optional. The description of the attr group.
103 * ]
104 * @param array $params Optional. Additional parameters for attribute group creation.
105 * [
106 * // Include optional parameters, if any.
107 * ]
108 *
109 */
110 public static function create($data, $params = [])
111 {
112 $groupCreated = static::getQuery()->create($data);
113
114 if ($groupCreated) {
115 return static::makeSuccessResponse(
116 $groupCreated,
117 __('Successfully created!', 'fluent-cart')
118 );
119 }
120
121 return static::makeErrorResponse([
122 ['code' => 400, 'message' => __('Group creation failed.', 'fluent-cart')]
123 ]);
124 }
125
126 /**
127 * Update attribute group with the provided data.
128 *
129 * @param array $data Required. Array containing the necessary parameters
130 * $data => (array) Required. Array of attribute group data.
131 * [
132 * 'title' => (string) Required. The title of the attr group.
133 * 'description' => (string) Optional. The description of the attr group.
134 * ]
135 * @param int $groupId Required. The id of the attribute group to update.
136 * @param array $params Optional. Additional parameters for attribute group creation.
137 * [
138 * // Include optional parameters, if any.
139 * ]
140 *
141 */
142 public static function update($data, $groupId, $params = [])
143 {
144 $isUpdated = static::getQuery()->findOrFail($groupId)->update($data);
145
146 if ($isUpdated) {
147 return static::makeSuccessResponse(
148 $isUpdated,
149 __('Group updated successfully!', 'fluent-cart')
150 );
151 }
152
153 return static::makeErrorResponse([
154 ['code' => 400, 'message' => __('Group info update failed.', 'fluent-cart')]
155 ]);
156 }
157
158
159 /**
160 * Delete attribute group by ID.
161 *
162 * @param int $groupId Required. The ID of the attribute group to delete.
163 * @param array $params Optional. Additional parameters for attribute group deletion.
164 * [
165 * // Include optional parameters, if any.
166 * ]
167 *
168 */
169 public static function delete($groupId, $params = [])
170 {
171 $isUsed = AttributeRelation::query()->where('group_id', $groupId)->first();
172
173 if (!$isUsed) {
174 $group = static::getQuery()->find($groupId);
175 if ($group) {
176 $group->delete();
177 return static::makeSuccessResponse(
178 '',
179 __('Attribute group successfully deleted!', 'fluent-cart')
180 );
181 }
182
183 return static::makeErrorResponse([
184 ['code' => 404, 'message' => __('Attribute group not found in database, failed to remove.', 'fluent-cart')]
185 ]);
186 }
187
188 return static::makeErrorResponse([
189 ['code' => 403, 'message' => __('This group is already in use, can not be deleted.', 'fluent-cart')]
190 ]);
191 }
192 }
193