| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- <?php
- namespace Dcat\Admin\Controllers;
- use Dcat\Admin\Models\Repositories\Administrator;
- use Dcat\Admin\Form;
- use Dcat\Admin\Grid;
- use Dcat\Admin\Layout\Content;
- use Dcat\Admin\MiniGrid;
- use Dcat\Admin\Show;
- use Dcat\Admin\Widgets\Tree;
- use Illuminate\Routing\Controller;
- class UserController extends Controller
- {
- use HasResourceActions;
- /**
- * Index interface.
- *
- * @return Content
- */
- public function index(Content $content)
- {
- if (request('_mini')) {
- return $content->body($this->miniGrid());
- }
- return $content
- ->header(trans('admin.administrator'))
- ->description(trans('admin.list'))
- ->body($this->grid());
- }
- /**
- * Show interface.
- *
- * @param mixed $id
- * @param Content $content
- *
- * @return Content
- */
- public function show($id, Content $content)
- {
- return $content
- ->header(trans('admin.administrator'))
- ->description(trans('admin.detail'))
- ->body($this->detail($id));
- }
- /**
- * Edit interface.
- *
- * @param $id
- *
- * @return Content
- */
- public function edit($id, Content $content)
- {
- return $content
- ->header(trans('admin.administrator'))
- ->description(trans('admin.edit'))
- ->body($this->form($id)->edit($id));
- }
- /**
- * Create interface.
- *
- * @return Content
- */
- public function create(Content $content)
- {
- return $content
- ->header(trans('admin.administrator'))
- ->description(trans('admin.create'))
- ->body($this->form());
- }
- /**
- * Make a grid builder.
- *
- * @return Grid
- */
- protected function grid()
- {
- $grid = new Grid(new Administrator());
- $grid->disableBatchDelete();
- $grid->model()->with('roles');
- $grid->id->bold()->sortable();
- $grid->username;
- $grid->name;
- $grid->roles->pluck('name')->label('primary');
- $permissionModel = config('admin.database.permissions_model');
- $roleModel = config('admin.database.roles_model');
- $nodes = (new $permissionModel)->allNodes();
- $grid->permissions->display(function ($v, $column) use (&$nodes, $roleModel) {
- if (empty($this->roles)) {
- return;
- }
- return $column->tree(function (Grid\Displayers\Tree $tree) use (&$nodes, $roleModel) {
- $tree->nodes($nodes);
- foreach (array_column($this->roles, 'slug') as $slug) {
- if ($roleModel::isAdministrator($slug)) {
- $tree->checkedAll();
- }
- }
- });
- });
- $grid->created_at;
- $grid->updated_at->sortable();
- $grid->actions(function (Grid\Displayers\Actions $actions) {
- if ($actions->getKey() == 1) {
- $actions->disableDelete();
- }
- });
- $grid->filter(function (Grid\Filter $filter) {
- $filter->equal('id');
- $filter->like('username');
- $filter->like('name');
- });
- return $grid;
- }
- protected function miniGrid()
- {
- $grid = new MiniGrid(new Administrator());
- $grid->id->bold()->sortable();
- $grid->username;
- $grid->name;
- $grid->filter(function (Grid\Filter $filter) {
- $filter->equal('id')->width('270px');
- $filter->like('username')->width('270px');
- $filter->like('name')->width('270px');
- });
- return $grid;
- }
- /**
- * Make a show builder.
- *
- * @param mixed $id
- *
- * @return Show
- */
- protected function detail($id)
- {
- $show = new Show(new Administrator());
- $show->setId($id);
- $show->id;
- $show->username;
- $show->name;
- $show->newline();
- $show->created_at;
- $show->updated_at;
- $show->divider();
- $show->roles->width(6)->as(function ($roles) {
- return collect($roles)->pluck('name');
- })->label('primary');
- $show->permissions->width(6)->unescape()->as(function () {
- $permissionModel = config('admin.database.permissions_model');
- $roleModel = config('admin.database.roles_model');
- $permissionModel = new $permissionModel;
- $nodes = $permissionModel->allNodes();
- $tree = Tree::make($nodes);
- $isAdministrator = false;
- foreach (array_column($this->roles, 'slug') as $slug) {
- if ($roleModel::isAdministrator($slug)) {
- $tree->checkedAll();
- $isAdministrator = true;
- }
- }
- if (!$isAdministrator) {
- $keyName = $permissionModel->getKeyName();
- $tree->checked(
- $roleModel::getPermissionId(array_column($this->roles, $keyName))->flatten()
- );
- }
- return $tree->render();
- });
- return $show;
- }
- /**
- * Make a form builder.
- *
- * @return Form
- */
- public function form($id = null)
- {
- $roleModel = config('admin.database.roles_model');
- $form = new Form(new Administrator());
- $form->display('id', 'ID');
- if (request()->isMethod('POST')) {
- $userTable = config('admin.database.users_table');
- $userNameRules = "required|unique:{$userTable}";
- } else {
- $userNameRules = 'required';
- }
- $form->text('username', trans('admin.username'))->rules($userNameRules);
- $form->text('name', trans('admin.name'))->rules('required');
- $form->image('avatar', trans('admin.avatar'));
- $form->password('password', trans('admin.password'))->rules('required|confirmed');
- $form->password('password_confirmation', trans('admin.password_confirmation'))
- ->rules('required')
- ->default(function ($form) {
- return $form->model()->get('password');
- });
- $form->ignore(['password_confirmation']);
- $form->multipleSelect('roles', trans('admin.roles'))
- ->options($roleModel::all()->pluck('name', 'id'))
- ->customFormat(function ($v) {
- return array_column($v, 'id');
- });
- if ($id) {
- $form->display('created_at', trans('admin.created_at'));
- $form->display('updated_at', trans('admin.updated_at'));
- }
- $form->saving(function (Form $form) {
- if ($form->password && $form->model()->get('password') != $form->password) {
- $form->password = bcrypt($form->password);
- }
- });
- return $form;
- }
- }
|