6
0
Fork 0
jiqu-library-server/app/Admin/Forms/SettingSpuSpecs.php

95 lines
2.8 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace App\Admin\Forms;
use App\Models\ProductSku;
use App\Models\ProductSpu;
use App\Models\ProductSpuSpec;
use Dcat\Admin\Contracts\LazyRenderable;
use Dcat\Admin\Traits\LazyWidget;
use Dcat\Admin\Widgets\Form;
class SettingSpuSpecs extends Form implements LazyRenderable
{
use LazyWidget;
protected function authorize($user): bool
{
return $user->can('dcat.admin.product_spus.setting_specs');
}
/**
* Handle the form request.
*
* @param array $input
*
* @return mixed
*/
public function handle(array $input)
{
if ($input) {
$spuId = $this->payload['spu_id'] ?? 0;
foreach ($input as $id => $items) {
$spuSpec = ProductSpuSpec::findOrFail($id);
$spuSpecsItems = array_column($spuSpec->items, 'name');
//除去空值
foreach ($items as $key => $item) {
if ($item['name'] === '' || $item['value'] === '') {
unset($items[$item]);
}
}
//如果删除某个属性需要判断该属性下是否有sku
$nowSpecsItems = array_column($items, 'name');
$diffSpecsItems = array_diff($spuSpecsItems, $nowSpecsItems);
foreach ($diffSpecsItems as $name) {
if (ProductSku::where('spu_id', $spuId)->where('specs->'.$id, $name)->exists()) {
return $this->response()->error(__('admin_message.forms.setting_spu_specs.errors.has_specs', ['name'=>$name]));
}
}
ProductSpuSpec::where('id', $id)->update([
'items' => $items,
]);
}
}
// return $this->response()->error('Your error message.');
return $this
->response()
->success(__('admin.update_succeeded'))
->refresh();
}
/**
* Build a form here.
*/
public function form()
{
$spuId = $this->payload['spu_id'] ?? null;
$spu = ProductSpu::findOrFail($spuId);
foreach ($spu->specs as $spec) {
$this->table($spec->id, $spec->name, function ($table) {
$table->text('name', __('admin_message.forms.setting_spu_specs.table.name'));
$table->currency('value', __('admin_message.forms.setting_spu_specs.table.value'))->symbol('¥')->default(0);
})->customFormat(function () use ($spec) {
return $spec->items;
});
}
$this->disableResetButton();
}
/**
* The data of the form.
*
* @return array
*/
public function default()
{
return [
// 'name' => 'John Doe',
// 'email' => 'John.Doe@gmail.com',
];
}
}