first commit
This commit is contained in:
134
app/Admin/Controllers/Category/IndexController.php
Normal file
134
app/Admin/Controllers/Category/IndexController.php
Normal file
@@ -0,0 +1,134 @@
|
||||
<?php
|
||||
|
||||
namespace App\Admin\Controllers\Category;
|
||||
|
||||
use App\Models\Article;
|
||||
use App\Models\Category;
|
||||
use Encore\Admin\Controllers\AdminController;
|
||||
use Encore\Admin\Form;
|
||||
use Encore\Admin\Layout\Column;
|
||||
use Encore\Admin\Layout\Row;
|
||||
use Encore\Admin\Tree;
|
||||
use Encore\Admin\Widgets\Box;
|
||||
use Encore\Admin\Widgets\Form as WidgetsForm;
|
||||
|
||||
class IndexController extends AdminController
|
||||
{
|
||||
|
||||
protected $title = '分类管理';
|
||||
|
||||
/**
|
||||
* Index interface.
|
||||
* @return \Closure
|
||||
*/
|
||||
public function grid()
|
||||
{
|
||||
return 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->text('title', '分类名称')->rules('required');
|
||||
$form->select('type', '分类类型')
|
||||
->options(Category::TYPES)
|
||||
->required();
|
||||
$form->textarea('description', '分类简介')
|
||||
->rules('nullable');
|
||||
$form->image('cover', 'Logo')
|
||||
->move('images/' . date('Y/m/d'))
|
||||
->removable()
|
||||
->uniqueName();
|
||||
$form->number('order', '排序')->default(0);
|
||||
$form->switch('status', '显示')->states()->default(1);
|
||||
$form->action(admin_url('categories'));
|
||||
|
||||
$column->append((new Box('新增分类', $form))->style('success'));
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @return 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>{$branch['type']}</small> ";
|
||||
$payload .= " <small style='color:#999'>{$branch['description']}</small>";
|
||||
|
||||
return $payload;
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 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->text('title', '分类名称')->rules('required');
|
||||
$form->select('type', '分类类型')
|
||||
->options(Category::TYPES)
|
||||
->required()
|
||||
->rules('required');
|
||||
$form->textarea('description', '分类简介')->rows(4)->rules('nullable');
|
||||
$form->image('cover', 'Logo')
|
||||
->move('images/' . date('Y/m/d'))
|
||||
->removable()
|
||||
->uniqueName();
|
||||
$form->number('order', '排序')->default(0)->help('正序优先');
|
||||
$form->select('article_id', '关联文章')
|
||||
->options(function ($option, $info) {
|
||||
$category = $this;
|
||||
if ($category) {
|
||||
return Article::where('category_id', $category->id)->pluck('title', 'id');
|
||||
} else {
|
||||
return [0 => '没有数据'];
|
||||
}
|
||||
})->help('当分类类型是文章详情的时候需要选择关联文章');
|
||||
|
||||
$form->switch('status', '显示')->states()->default(1);
|
||||
$form->saving(function (Form $form) {
|
||||
|
||||
if (request()->has('title')) {
|
||||
if (request()->type == Category::TYPE_SHOW && empty(request()->article_id)) {
|
||||
$error = new MessageBag([
|
||||
'title' => '错误',
|
||||
'message' => '文章类型是文章详情的时候需要选择关联文章',
|
||||
]);
|
||||
|
||||
return back()->withInput()->with(compact('error'));
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
public function destroy($id)
|
||||
{
|
||||
return $this->form()->destroy($id);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user