181 lines
4.9 KiB
PHP
181 lines
4.9 KiB
PHP
<?php
|
|
|
|
namespace App\Admin\Controllers;
|
|
|
|
use App\Models\Category;
|
|
use Encore\Admin\Controllers\AdminController;
|
|
use Encore\Admin\Controllers\HasResourceActions;
|
|
use Encore\Admin\Form;
|
|
use Encore\Admin\Grid;
|
|
use Encore\Admin\Layout\Column;
|
|
use Encore\Admin\Layout\Content;
|
|
use Encore\Admin\Layout\Row;
|
|
use Encore\Admin\Show;
|
|
use Encore\Admin\Tree;
|
|
use Encore\Admin\Widgets\Box;
|
|
use Encore\Admin\Widgets\Form as WidgetsForm;
|
|
|
|
class CategoryController extends AdminController
|
|
{
|
|
use HasResourceActions;
|
|
|
|
/**
|
|
* Index interface.
|
|
*
|
|
* @param Content $content
|
|
* @return Content
|
|
*/
|
|
public function index(Content $content)
|
|
{
|
|
return $content
|
|
->header('分类')
|
|
->row(function (Row $row) {
|
|
$row->column(6, $this->treeView());
|
|
|
|
$row->column(6, function (Column $column) {
|
|
$form = new WidgetsForm();
|
|
|
|
$form->select('parent_id', '上级分类')->options(Category::selectOptions(function ($model) {
|
|
return $model->where('status', 1);
|
|
}, '一级分类'));
|
|
$form->select('type', '分类类型')->options(['goods' => '商品', 'article' => '文章']);
|
|
$form->text('title', '分类名称')->rules('required');
|
|
$form->textarea('description', '分类简介')->rows(4)->rules('nullable');
|
|
$form->image('cover', 'Logo');
|
|
$form->number('order', '排序')->default(0);
|
|
$form->switch('status', '显示')->states()->default(1);
|
|
|
|
$form->action('/admin/categories');
|
|
|
|
$column->append((new Box('新增分类', $form))->style('success'));
|
|
});
|
|
});
|
|
}
|
|
|
|
/**
|
|
* @return \Encore\Admin\Tree
|
|
*/
|
|
protected function treeView()
|
|
{
|
|
return Category::tree(function (Tree $tree) {
|
|
$tree->disableCreate();
|
|
|
|
$tree->branch(function ($branch) {
|
|
if ($branch['status'] == 1) {
|
|
$payload = "<i class='fa fa-eye text-primary'></i> ";
|
|
} else {
|
|
$payload = "<i class='fa fa-eye text-gray'></i> ";
|
|
}
|
|
$payload .= " [ID:{$branch['id']}] - ";
|
|
$payload .= " <strong>{$branch['title']}</strong> ";
|
|
$payload .= " <small style='color:#999'>{$branch['description']}</small>";
|
|
|
|
return $payload;
|
|
});
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Show interface.
|
|
*
|
|
* @param mixed $id
|
|
* @param Content $content
|
|
* @return Content
|
|
*/
|
|
public function show($id, Content $content)
|
|
{
|
|
return $content
|
|
->header('Detail')
|
|
->description('description')
|
|
->body($this->detail($id));
|
|
}
|
|
|
|
/**
|
|
* Edit interface.
|
|
*
|
|
* @param mixed $id
|
|
* @param Content $content
|
|
* @return Content
|
|
*/
|
|
public function edit($id, Content $content)
|
|
{
|
|
return $content
|
|
->header('Edit')
|
|
->description('description')
|
|
->body($this->form()->edit($id));
|
|
}
|
|
|
|
/**
|
|
* Create interface.
|
|
*
|
|
* @param Content $content
|
|
* @return Content
|
|
*/
|
|
public function create(Content $content)
|
|
{
|
|
return $content
|
|
->header('Create')
|
|
->description('description')
|
|
->body($this->form());
|
|
}
|
|
|
|
/**
|
|
* Make a grid builder.
|
|
*
|
|
* @return Grid
|
|
*/
|
|
protected function grid()
|
|
{
|
|
$grid = new Grid(new Category);
|
|
|
|
$grid->id('Id');
|
|
$grid->title('分类名称');
|
|
$grid->description('分类简介');
|
|
$grid->order('排序');
|
|
$grid->status('显示')->switch();
|
|
$grid->created_at('创建时间');
|
|
|
|
return $grid;
|
|
}
|
|
|
|
/**
|
|
* Make a show builder.
|
|
*
|
|
* @param mixed $id
|
|
* @return Show
|
|
*/
|
|
protected function detail($id)
|
|
{
|
|
$show = new Show(Category::findOrFail($id));
|
|
|
|
$show->id('Id');
|
|
$show->created_at('Created at');
|
|
$show->updated_at('Updated at');
|
|
|
|
return $show;
|
|
}
|
|
|
|
/**
|
|
* Make a form builder.
|
|
*
|
|
* @return Form
|
|
*/
|
|
protected function form()
|
|
{
|
|
$form = new Form(new Category);
|
|
|
|
$form->select('parent_id', '上级分类')->options(Category::selectOptions(function ($model) {
|
|
return $model->where('status', 1);
|
|
}, '一级分类'));
|
|
$form->select('type', '分类类型')->options(['goods' => '商品', 'article' => '文章'])->rules('required');
|
|
$form->text('title', '分类名称')->rules('required');
|
|
$form->textarea('description', '分类简介')->rows(4)->rules('nullable');
|
|
$form->image('cover', 'Logo');
|
|
$form->number('order', '排序')->default(0);
|
|
$form->switch('status', '显示')->states()->default(1);
|
|
|
|
return $form;
|
|
}
|
|
|
|
}
|