| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\Api\Resource; |
| 4 |
|
| 5 |
use FluentCart\App\Models\Label; |
| 6 |
use FluentCart\App\Models\LabelRelationship; |
| 7 |
use FluentCart\Framework\Database\Orm\Builder; |
| 8 |
use FluentCart\Framework\Support\Arr; |
| 9 |
use FluentCart\Framework\Support\Collection; |
| 10 |
|
| 11 |
class LabelResource extends BaseResourceApi |
| 12 |
{ |
| 13 |
public static function getQuery(): Builder |
| 14 |
{ |
| 15 |
return Label::query(); |
| 16 |
} |
| 17 |
|
| 18 |
/** |
| 19 |
* Retrieve labels with additional data based on specified parameters. |
| 20 |
* |
| 21 |
* @param array $params Optional. Additional parameters for label retrieval. |
| 22 |
* $params = [ |
| 23 |
* |
| 24 |
* ] |
| 25 |
* |
| 26 |
*/ |
| 27 |
public static function get(array $params = []) |
| 28 |
{ |
| 29 |
$getLabels = static::getQuery()->get(); |
| 30 |
|
| 31 |
return [ |
| 32 |
'labels' => $getLabels |
| 33 |
]; |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Find an label by id |
| 38 |
* |
| 39 |
* @param string $id Required. The id of the label to find. |
| 40 |
* @param array $params Optional. Additional parameters for label retrieval. |
| 41 |
* [ |
| 42 |
* // Include optional parameters, if any. |
| 43 |
* ] |
| 44 |
* |
| 45 |
*/ |
| 46 |
public static function find($id, $params = []) |
| 47 |
{ |
| 48 |
// |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Create a label with the provided data. |
| 53 |
* |
| 54 |
* @param array $data Required. Array containing the necessary parameters for label creation. |
| 55 |
* $data = [ |
| 56 |
* 'value' => (string) Required. The name of the label, |
| 57 |
* // Include additional parameters, if any. |
| 58 |
* ] |
| 59 |
* @param array $params Optional. Additional parameters for label creation. |
| 60 |
* [ |
| 61 |
* // Include optional parameters, if any. |
| 62 |
* ] |
| 63 |
* |
| 64 |
*/ |
| 65 |
public static function create($data, $params = []) |
| 66 |
{ |
| 67 |
$isCreated = static::getQuery()->create($data); |
| 68 |
|
| 69 |
if ($isCreated) { |
| 70 |
return static::makeSuccessResponse( |
| 71 |
$isCreated, |
| 72 |
__('Label created successfully!', 'fluent-cart') |
| 73 |
); |
| 74 |
} |
| 75 |
|
| 76 |
return static::makeErrorResponse([ |
| 77 |
['code' => 400, 'message' => __('Label creation failed.', 'fluent-cart')] |
| 78 |
]); |
| 79 |
} |
| 80 |
|
| 81 |
public static function createAndAttach($data, $params = []) |
| 82 |
{ |
| 83 |
$isCreated = static::getQuery()->create( |
| 84 |
Arr::only($data, 'value') |
| 85 |
); |
| 86 |
|
| 87 |
|
| 88 |
if ($isCreated) { |
| 89 |
|
| 90 |
if (!empty($data['bind_to_type']) && !empty($data['bind_to_id'])) { |
| 91 |
if (class_exists('FluentCart\App\Models\\' . $data['bind_to_type'])) { |
| 92 |
LabelRelationship::query()->create([ |
| 93 |
'labelable_id' => $data['bind_to_id'], |
| 94 |
'labelable_type' => 'FluentCart\App\Models\\' . $data['bind_to_type'], |
| 95 |
'label_id' => $isCreated->id |
| 96 |
]); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
return static::makeSuccessResponse( |
| 101 |
$isCreated, |
| 102 |
__('Label created successfully!', 'fluent-cart') |
| 103 |
); |
| 104 |
} |
| 105 |
|
| 106 |
return static::makeErrorResponse([ |
| 107 |
['code' => 400, 'message' => __('Label creation failed.', 'fluent-cart')] |
| 108 |
]); |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Update a label with the provided data. |
| 113 |
* |
| 114 |
* @param object $data Required. |
| 115 |
* @param int $id Required. The ID of the label to update. |
| 116 |
* @param array $params Optional. Additional parameters for label update. |
| 117 |
* [ |
| 118 |
* // Include optional parameters, if any. |
| 119 |
* ] |
| 120 |
* |
| 121 |
*/ |
| 122 |
public static function update($data, $id, $params = []) |
| 123 |
{ |
| 124 |
// |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Delete a label and associated data by id. |
| 129 |
* |
| 130 |
* @param int $id Required. The id of the label to delete. |
| 131 |
* @param array $params Optional. Additional parameters for label deletion. |
| 132 |
* [ |
| 133 |
* // Include optional parameters, if any. |
| 134 |
* ] |
| 135 |
* |
| 136 |
*/ |
| 137 |
public static function delete($id, $params = []) |
| 138 |
{ |
| 139 |
// |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Add label to the label relationships for a specific labelable type with the provided data. |
| 144 |
* |
| 145 |
* @param object $model Required. |
| 146 |
* @param array $params Optional. Additional parameters to add label. |
| 147 |
* [ |
| 148 |
* // Include optional parameters, if any. |
| 149 |
* ] |
| 150 |
* |
| 151 |
*/ |
| 152 |
public static function addLabelToLabelRelationships($model, $params = []) |
| 153 |
{ |
| 154 |
$labelableId = Arr::get($params, 'labelable_id', null); |
| 155 |
$labelableType = Arr::get($params, 'labelable_type'); |
| 156 |
$newLabelIds = Arr::get($params, 'new_label_ids', []); |
| 157 |
$existingLabelIds = Arr::get($params, 'existing_label_ids', []); |
| 158 |
|
| 159 |
if (!empty($existingLabelIds)) { |
| 160 |
if (!empty($newLabelIds)) { |
| 161 |
$newLabelIds = Collection::make($newLabelIds); |
| 162 |
$deletedLabelIds = $existingLabelIds->diff($newLabelIds); |
| 163 |
} else { |
| 164 |
$deletedLabelIds = $existingLabelIds; |
| 165 |
} |
| 166 |
foreach ($deletedLabelIds as $labelId) { |
| 167 |
LabelRelationship::where('label_id', $labelId) |
| 168 |
->where('labelable_id', $labelableId) |
| 169 |
->where('labelable_type', $labelableType) |
| 170 |
->delete(); |
| 171 |
} |
| 172 |
} |
| 173 |
if (!empty($newLabelIds)) { |
| 174 |
foreach ($newLabelIds as $labelId) { |
| 175 |
// Check if the label relationship already exists |
| 176 |
$isExist = LabelRelationship::where('label_id', $labelId) |
| 177 |
->where('labelable_id', $labelableId) |
| 178 |
->where('labelable_type', $labelableType) |
| 179 |
->exists(); |
| 180 |
|
| 181 |
if (!$isExist) { |
| 182 |
// Create the new label relationship |
| 183 |
$data['label_id'] = $labelId; |
| 184 |
$label = new LabelRelationship($data); |
| 185 |
$model->labels()->save($label); |
| 186 |
} |
| 187 |
} |
| 188 |
} |
| 189 |
return true; |
| 190 |
} |
| 191 |
} |