| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Http\Controllers; |
| 4 |
|
| 5 |
use FluentCart\Api\Resource\LabelResource; |
| 6 |
use FluentCart\App\Http\Requests\LabelRequest; |
| 7 |
use FluentCart\App\Models\Label; |
| 8 |
use FluentCart\App\Models\Model; |
| 9 |
use FluentCart\App\Models\Order; |
| 10 |
use FluentCart\App\Models\Product; |
| 11 |
use FluentCart\Framework\Http\Request\Request; |
| 12 |
use FluentCart\Framework\Support\Arr; |
| 13 |
use FluentCart\Framework\Support\Collection; |
| 14 |
use FluentCart\Framework\Support\Str; |
| 15 |
|
| 16 |
class LabelController extends Controller |
| 17 |
{ |
| 18 |
public function index(Request $request) |
| 19 |
{ |
| 20 |
return LabelResource::get(); |
| 21 |
} |
| 22 |
|
| 23 |
public function create(LabelRequest $request) |
| 24 |
{ |
| 25 |
$data = $request->getSafe($request->sanitize()); |
| 26 |
|
| 27 |
// $data = [ |
| 28 |
// 'value' => sanitize_text_field($request->get('label')), |
| 29 |
// ]; |
| 30 |
|
| 31 |
$isCreated = LabelResource::createAndAttach($data); |
| 32 |
|
| 33 |
|
| 34 |
// LabelResource::addLabelToLabelRelationships($order, [ |
| 35 |
// 'labelable_id' => $orderId, |
| 36 |
// 'labelable_type' => Order::class, |
| 37 |
// 'new_label_ids' => $newLabelIds, |
| 38 |
// 'existing_label_ids' => $existingLabelIds |
| 39 |
// ]); |
| 40 |
|
| 41 |
if (is_wp_error($isCreated)) { |
| 42 |
return $isCreated; |
| 43 |
} |
| 44 |
return $this->response->sendSuccess($isCreated); |
| 45 |
} |
| 46 |
|
| 47 |
public function updateSelections(Request $request) |
| 48 |
{ |
| 49 |
$data = $request->getSafe( |
| 50 |
[ |
| 51 |
'bind_to_type' => 'sanitize_text_field', |
| 52 |
'bind_to_id' => 'sanitize_text_field', |
| 53 |
'selectedLabels.*' => 'sanitize_text_field' |
| 54 |
] |
| 55 |
); |
| 56 |
|
| 57 |
|
| 58 |
if (!empty($data['bind_to_type']) && !empty($data['bind_to_id'])) { |
| 59 |
|
| 60 |
if (!Str::contains($data['bind_to_type'], '\\')) { |
| 61 |
$modelClass = 'FluentCart\App\Models\\' . $data['bind_to_type']; |
| 62 |
} else { |
| 63 |
$modelClass = $data['bind_to_type']; |
| 64 |
} |
| 65 |
|
| 66 |
//Check If the model class exist |
| 67 |
if (class_exists($modelClass)) { |
| 68 |
/** |
| 69 |
* @var Model $modelInstance |
| 70 |
*/ |
| 71 |
$modelInstance = new $modelClass(); |
| 72 |
$newLabelIds = Arr::get($data, 'selectedLabels', []); |
| 73 |
|
| 74 |
|
| 75 |
$model = $modelInstance->newQuery()->with('labels')->find($data['bind_to_id']); |
| 76 |
// Pluck and convert $existingLabelIds to a collection of strings |
| 77 |
$existingLabelIds = Collection::make($model['labels'])->pluck('label_id')->map(function ($value) { |
| 78 |
return (string)$value; |
| 79 |
}); |
| 80 |
|
| 81 |
$modelId = $model->{$modelInstance->getKeyName()}; |
| 82 |
$isAttached = LabelResource::addLabelToLabelRelationships($model, [ |
| 83 |
'labelable_id' => $modelId, |
| 84 |
'labelable_type' => $modelClass, |
| 85 |
'new_label_ids' => $newLabelIds, |
| 86 |
'existing_label_ids' => $existingLabelIds |
| 87 |
]); |
| 88 |
if ($isAttached) { |
| 89 |
return $this->sendSuccess([ |
| 90 |
'message' => __('Labels Updated Successfully', 'fluent-cart') |
| 91 |
]); |
| 92 |
} |
| 93 |
|
| 94 |
return $this->sendError([ |
| 95 |
'message' => __('Failed To Update Labels', 'fluent-cart') |
| 96 |
]); |
| 97 |
|
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
|