136 lines
4.1 KiB
PHP
136 lines
4.1 KiB
PHP
<?php
|
|
|
|
namespace App\Admin\Controllers;
|
|
|
|
use App\Admin\Renderable\ProductAttrTable;
|
|
use App\Admin\Repositories\ProductGroup;
|
|
use App\Models\ProductGroup as ProductGroupModel;
|
|
use Dcat\Admin\Admin;
|
|
use Dcat\Admin\Form;
|
|
use Dcat\Admin\Grid;
|
|
use Dcat\Admin\Http\Controllers\AdminController;
|
|
use Dcat\Admin\Show;
|
|
use Illuminate\Http\Request;
|
|
|
|
class ProductGroupController extends AdminController
|
|
{
|
|
/**
|
|
* Make a grid builder.
|
|
*
|
|
* @return Grid
|
|
*/
|
|
protected function grid()
|
|
{
|
|
return Grid::make(new ProductGroup(), function (Grid $grid) {
|
|
$grid->column('id')->sortable();
|
|
$grid->column('name');
|
|
$grid->column('attrs')->display(function ($value) {
|
|
return collect($value)->pluck('title');
|
|
})->label();
|
|
$grid->column('specs')->display(function ($value) {
|
|
return collect($value)->pluck('title');
|
|
})->label();
|
|
$grid->column('created_at')->sortable();
|
|
|
|
//排序
|
|
$grid->model()->orderBy('created_at', 'desc');
|
|
|
|
/** 操作 **/
|
|
//新增
|
|
if (Admin::user()->can('dcat.admin.product_groups.create')) {
|
|
$grid->disableCreateButton(false);
|
|
$grid->enableDialogCreate();
|
|
}
|
|
|
|
//修改
|
|
$grid->showQuickEditButton(Admin::user()->can('dcat.admin.product_groups.edit'));
|
|
//删除以及自定义操作
|
|
$grid->actions(function (Grid\Displayers\Actions $actions) {
|
|
$actions->disableDelete(Admin::user()->cannot('dcat.admin.product_groups.destroy'));
|
|
});
|
|
|
|
/** 查询 **/
|
|
$grid->filter(function (Grid\Filter $filter) {
|
|
$filter->panel();
|
|
$filter->like('name')->width(3);
|
|
});
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Make a show builder.
|
|
*
|
|
* @param mixed $id
|
|
*
|
|
* @return Show
|
|
*/
|
|
protected function detail($id)
|
|
{
|
|
return Show::make($id, new ProductGroup(), function (Show $show) {
|
|
$show->field('id');
|
|
$show->field('name');
|
|
$show->field('created_at');
|
|
$show->field('updated_at');
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Make a form builder.
|
|
*
|
|
* @return Form
|
|
*/
|
|
protected function form()
|
|
{
|
|
return Form::make(new ProductGroup(), function (Form $form) {
|
|
$form->display('id');
|
|
$form->text('name')->required();
|
|
$form->table('attrs', function ($table) {
|
|
$table->text('title');
|
|
$table->textarea('value');
|
|
});
|
|
$form->table('specs', function ($table) {
|
|
$table->text('title');
|
|
$table->textarea('value');
|
|
});
|
|
// $form->multipleSelectTable('attrs')
|
|
// ->from(ProductAttrTable::make(['id'=>$form->getKey()]));
|
|
|
|
$form->display('created_at');
|
|
$form->display('updated_at');
|
|
});
|
|
}
|
|
|
|
public function details(Request $request)
|
|
{
|
|
$group_id = (int) $request->input('group_id');
|
|
$group = ProductGroupModel::where('id', $group_id)->first();
|
|
$data = [];
|
|
if ($group) {
|
|
foreach ($group->attrs as $attr) {
|
|
$data['attrs'][] = [
|
|
'name' => $attr['title'],
|
|
'attrs'=> array_map(function ($value) {
|
|
return [
|
|
'name'=>$value,
|
|
'value'=>'',
|
|
];
|
|
}, explode(',', str_replace("\r\n", ',', trim($attr['value'])))),
|
|
];
|
|
}
|
|
foreach ($group->specs as $spec) {
|
|
$data['specs'][] = [
|
|
'name' => $spec['title'],
|
|
'specs'=> array_map(function ($value) {
|
|
return [
|
|
'name'=>$value,
|
|
'value'=>'',
|
|
];
|
|
}, explode(',', str_replace("\r\n", ',', trim($spec['value'])))),
|
|
];
|
|
}
|
|
}
|
|
|
|
return response()->json($data);
|
|
}
|
|
}
|