162 lines
6.0 KiB
PHP
162 lines
6.0 KiB
PHP
<?php
|
|
|
|
namespace App\Admin\Controllers\Category;
|
|
|
|
use App\Models\Article;
|
|
use App\Models\CategoryOld;
|
|
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 OldController 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(CategoryOld::selectOptions(function ($model) {
|
|
return $model->where('status', 1);
|
|
}, '一级分类'));
|
|
$form->text('title', '分类名称')->rules('required');
|
|
$form->text('alias', '别名');
|
|
$form->select('type', '分类类型')
|
|
->options(CategoryOld::TYPES)
|
|
->when('show', function (WidgetsForm $form) {
|
|
$form->select('article_id', '关联文章')
|
|
->options(function ($option, $info) {
|
|
return Article::whereHas('categories', function ($q) {
|
|
$q->where('type', 'show');
|
|
})->pluck('title', 'id');
|
|
})->help('当分类类型是文章详情的时候需要选择关联文章');
|
|
})
|
|
->required();
|
|
$form->textarea('description', '分类简介')
|
|
->rules('nullable');
|
|
$form->image('logo', 'Logo')
|
|
->rules('image|mimes:jpeg,jpg,png')
|
|
->move('logos/'.date('Y/m/d'))
|
|
->removable()
|
|
->uniqueName();
|
|
$form->image('cover', '封面')
|
|
->rules('image|mimes:jpeg,jpg,png')
|
|
->move('images/'.date('Y/m/d'))
|
|
->removable()
|
|
->uniqueName();
|
|
$form->text('template', '模板');
|
|
$form->number('order', '排序')->default(0);
|
|
$form->switch('top_show', '顶部导航显示')->states()->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 CategoryOld::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 .= " <strong>{$branch['alias']}</strong> ";
|
|
$payload .= " <small>{$branch['type']}</small> ";
|
|
$payload .= " <small style='color:#999'>{$branch['template']}</small>";
|
|
|
|
return $payload;
|
|
});
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Make a form builder.
|
|
*
|
|
* @return Form
|
|
*/
|
|
protected function form(): Form
|
|
{
|
|
$form = new Form(new CategoryOld);
|
|
|
|
$form->select('parent_id', '上级分类')->options(CategoryOld::selectOptions(function ($model) {
|
|
return $model->where('status', 1);
|
|
}, '一级分类'));
|
|
$form->text('title', '分类名称')->rules('required');
|
|
$form->text('alias', '别名');
|
|
$form->select('type', '分类类型')
|
|
->options(CategoryOld::TYPES)
|
|
->when('show', function (Form $form) {
|
|
$form->select('article_id', '关联文章')
|
|
->options(function ($option, $info) {
|
|
return Article::whereHas('categories', function ($q) {
|
|
$q->where('type', 'show');
|
|
})->pluck('title', 'id');
|
|
})->help('当分类类型是文章详情的时候需要选择关联文章');
|
|
})
|
|
->required()
|
|
->rules('required');
|
|
$form->textarea('description', '分类简介')->rows(4)->rules('nullable');
|
|
$form->image('logo', 'Logo')
|
|
->rules('image|mimes:jpeg,jpg,png')
|
|
->move('logos/'.date('Y/m/d'))
|
|
->removable()
|
|
->uniqueName();
|
|
$form->image('cover', '封面')
|
|
->rules('image|mimes:jpeg,jpg,png')
|
|
->move('images/'.date('Y/m/d'))
|
|
->removable()
|
|
->uniqueName();
|
|
$form->text('template', '模板');
|
|
$form->number('order', '排序')->default(0)->help('正序优先');
|
|
$form->switch('top_show', '顶部导航显示')->states()->default(0);
|
|
$form->switch('status', '显示')->states()->default(1);
|
|
$form->saving(function (Form $form) {
|
|
|
|
if (request()->has('title')) {
|
|
// if (request()->type == CategoryOld::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);
|
|
}
|
|
|
|
}
|