127 lines
4.1 KiB
PHP
127 lines
4.1 KiB
PHP
<?php
|
|
|
|
namespace App\Admin\Controllers;
|
|
|
|
use App\Admin\Repositories\Message;
|
|
use App\Models\Message as MessageModel;
|
|
use Dcat\Admin\Admin;
|
|
use Dcat\Admin\Form;
|
|
use Dcat\Admin\Grid;
|
|
use Dcat\Admin\Http\Controllers\AdminController;
|
|
use Dcat\Admin\Show;
|
|
use Dcat\Admin\Widgets\Card;
|
|
|
|
class MessageController extends AdminController
|
|
{
|
|
/**
|
|
* Make a grid builder.
|
|
*
|
|
* @return Grid
|
|
*/
|
|
protected function grid()
|
|
{
|
|
return Grid::make(new Message(), function (Grid $grid) {
|
|
$grid->model()->where('user_id', '=', 0);
|
|
$grid->column('id')->sortable();
|
|
$grid->column('title');
|
|
$grid->column('content')->display('详情') // 设置按钮名称
|
|
->expand(function () {
|
|
// 这里返回 content 字段内容,并用 Card 包裹起来
|
|
$card = new Card(null, $this->content);
|
|
|
|
return "<div style='padding:10px 10px 0;text-align: center'>$card</div>";
|
|
});
|
|
$grid->column('jump_type')->using([
|
|
'0'=>__('admin_message.ad.jump_type.radio.0'),
|
|
'1'=>__('admin_message.ad.jump_type.radio.1'),
|
|
'2'=>__('admin_message.ad.jump_type.radio.2'),
|
|
])->label();
|
|
$grid->column('jump_link');
|
|
$grid->column('is_push')->bool();
|
|
$grid->column('created_at')->sortable();
|
|
|
|
$grid->model()->orderBy('created_at', 'desc');
|
|
// $grid->column('updated_at');
|
|
/** 操作 **/
|
|
//新增
|
|
if (Admin::user()->can('dcat.admin.messages.create')) {
|
|
$grid->disableCreateButton(false);
|
|
$grid->enableDialogCreate();
|
|
}
|
|
//修改
|
|
$grid->showQuickEditButton(Admin::user()->can('dcat.admin.messages.edit'));
|
|
//删除以及自定义操作
|
|
$grid->actions(function (Grid\Displayers\Actions $actions) {
|
|
$actions->disableDelete(Admin::user()->cannot('dcat.admin.messages.destroy'));
|
|
});
|
|
$grid->filter(function (Grid\Filter $filter) {
|
|
// $filter->equal('id');
|
|
$filter->panel();
|
|
$filter->like('title')->width(3);
|
|
});
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Make a show builder.
|
|
*
|
|
* @param mixed $id
|
|
*
|
|
* @return Show
|
|
*/
|
|
protected function detail($id)
|
|
{
|
|
return Show::make($id, new Message(), function (Show $show) {
|
|
$show->field('id');
|
|
$show->field('title');
|
|
$show->field('content');
|
|
$show->field('user_id');
|
|
$show->field('ext');
|
|
$show->field('jump_type');
|
|
$show->field('jump_link');
|
|
$show->field('created_at');
|
|
$show->field('updated_at');
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Make a form builder.
|
|
*
|
|
* @return Form
|
|
*/
|
|
protected function form()
|
|
{
|
|
return Form::make(new Message(), function (Form $form) {
|
|
$form->display('id');
|
|
$form->text('title');
|
|
$form->textarea('content');
|
|
// $form->text('user_id');
|
|
// $form->text('ext');
|
|
$form->radio('jump_type')->options([
|
|
'0'=>__('admin_message.ad.jump_type.radio.0'),
|
|
'1'=>__('admin_message.ad.jump_type.radio.1'),
|
|
'2'=>__('admin_message.ad.jump_type.radio.2'),
|
|
])->default(0);
|
|
$form->text('jump_link');
|
|
if ($form->isEditing()) {
|
|
$form->switch('is_push')->disable();
|
|
} else {
|
|
$form->switch('is_push')->default(0);
|
|
}
|
|
$form->saved(function (Form $form, $result) {
|
|
// 判断是否是新增操作
|
|
if ($form->isCreating()) {
|
|
$message = MessageModel::findOrFail($result);
|
|
if ($message->needPush()) {
|
|
$message->pushMessage();
|
|
}
|
|
return;
|
|
}
|
|
});
|
|
|
|
$form->display('created_at');
|
|
$form->display('updated_at');
|
|
});
|
|
}
|
|
}
|