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