123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346 |
- <?php
- namespace Dcat\Admin\Controllers;
- use Dcat\Admin\Admin;
- use Dcat\Admin\Form;
- use Dcat\Admin\Grid;
- use Dcat\Admin\Layout\Content;
- use Dcat\Admin\Layout\Row;
- use Dcat\Admin\Models\Repositories\Permission;
- use Dcat\Admin\Show;
- use Dcat\Admin\SimpleGrid;
- use Dcat\Admin\Tree;
- use Illuminate\Routing\Controller;
- use Illuminate\Support\Str;
- class PermissionController extends Controller
- {
- use HasResourceActions;
- /**
- * Index interface.
- *
- * @param Content $content
- *
- * @return Content
- */
- public function index(Content $content)
- {
- if (request(SimpleGrid::QUERY_NAME)) {
- return $content->body($this->simpleGrid());
- }
- return $content
- ->header(trans('admin.permissions'))
- ->description(trans('admin.list'))
- ->body(function (Row $row) {
- if (request('_layout')) {
- $row->column(12, $this->grid());
- } else {
- $row->column(12, $this->treeView());
- }
- });
- }
- /**
- * Show interface.
- *
- * @param mixed $id
- * @param Content $content
- *
- * @return Content
- */
- public function show($id, Content $content)
- {
- return $content
- ->header(trans('admin.permissions'))
- ->description(trans('admin.detail'))
- ->body($this->detail($id));
- }
- /**
- * Edit interface.
- *
- * @param $id
- * @param Content $content
- *
- * @return Content
- */
- public function edit($id, Content $content)
- {
- return $content
- ->header(trans('admin.permissions'))
- ->description(trans('admin.edit'))
- ->body($this->form()->edit($id));
- }
- /**
- * Create interface.
- *
- * @param Content $content
- *
- * @return Content
- */
- public function create(Content $content)
- {
- return $content
- ->header(trans('admin.permissions'))
- ->description(trans('admin.create'))
- ->body($this->form());
- }
- protected function simpleGrid()
- {
- $grid = new SimpleGrid(new Permission());
- $grid->id->bold()->sortable();
- $grid->slug;
- $grid->name;
- $grid->filter(function (Grid\Filter $filter) {
- $filter->like('slug');
- $filter->like('name');
- });
- return $grid;
- }
- /**
- * @return \Dcat\Admin\Tree
- */
- protected function treeView()
- {
- $model = config('admin.database.permissions_model');
- $tree = new Tree(new $model());
- $tree->disableCreateButton();
- $tree->tools(function (Tree\Tools $tools) {
- $label = trans('admin.table');
- $url = url(request()->getPathInfo()).'?_layout=1';
- $tools->add("<a class='btn btn-sm btn-default' href='{$url}'>$label</a>");
- });
- $tree->branch(function ($branch) {
- $payload = "<div class='pull-left' style='min-width:310px'><b>{$branch['name']}</b> [<span class='text-blue'>{$branch['slug']}</span>]";
- $path = array_filter($branch['http_path']);
- if (! $path) {
- return $payload.'</div> ';
- }
- $max = 3;
- if (count($path) > $max) {
- $path = array_slice($path, 0, $max);
- array_push($path, '...');
- }
- $method = $branch['http_method'] ?: [];
- $path = collect($path)->map(function ($path) use ($branch, &$method) {
- if (Str::contains($path, ':')) {
- [$me, $path] = explode(':', $path);
- $method = array_merge($method, explode(',', $me));
- }
- if ($path !== '...' && ! empty(config('admin.route.prefix'))) {
- $path = admin_base_path($path);
- }
- return "<code>$path</code>";
- })->implode(' ');
- $method = collect($method ?: ['ANY'])->unique()->map(function ($name) {
- return strtoupper($name);
- })->map(function ($name) {
- return "<span class='label label-primary'>{$name}</span>";
- })->implode(' ').' ';
- $payload .= "</div> $method<a class=\"dd-nodrag\">$path</a>";
- return $payload;
- });
- return $tree;
- }
- /**
- * Make a grid builder.
- *
- * @return Grid
- */
- protected function grid()
- {
- $grid = new Grid(new Permission());
- $grid->disableCreateButton();
- $grid->id('ID')->bold()->sortable();
- $grid->slug->label('primary');
- $grid->name;
- $grid->http_path->display(function ($path) {
- if (! $path) {
- return;
- }
- $method = $this->http_method ?: ['ANY'];
- $method = collect($method)->map(function ($name) {
- return strtoupper($name);
- })->map(function ($name) {
- return "<span class='label label-primary'>{$name}</span>";
- })->implode(' ').' ';
- return collect($path)->filter()->map(function ($path) use ($method) {
- if (Str::contains($path, ':')) {
- [$method, $path] = explode(':', $path);
- $method = collect(explode(',', $method))->map(function ($name) {
- return strtoupper($name);
- })->map(function ($name) {
- return "<span class='label label-primary'>{$name}</span>";
- })->implode(' ').' ';
- }
- if (! empty(config('admin.route.prefix'))) {
- $path = admin_base_path($path);
- }
- return "<div style='margin-bottom: 5px;'>$method<code>$path</code></div>";
- })->implode('');
- });
- $grid->created_at;
- $grid->updated_at->sortable();
- $grid->tools(function (Grid\Tools $tools) {
- $tools->batch(function (Grid\Tools\BatchActions $actions) {
- $actions->disableDelete();
- });
- $label = trans('admin.default');
- $url = url(request()->getPathInfo());
- $tools->append("<a class='btn btn-sm btn-default' href='{$url}'>$label</a>");
- });
- $grid->filter(function (Grid\Filter $filter) {
- $filter->like('slug');
- $filter->like('name');
- $filter->like('http_path');
- });
- return $grid;
- }
- /**
- * Make a show builder.
- *
- * @param mixed $id
- *
- * @return Show
- */
- protected function detail($id)
- {
- $show = new Show(new Permission());
- $show->setKey($id);
- $show->id;
- $show->slug;
- $show->name;
- $show->http_path->unescape()->as(function ($path) {
- return collect($path)->filter()->map(function ($path) {
- $method = $this->http_method ?: ['ANY'];
- if (Str::contains($path, ':')) {
- [$method, $path] = explode(':', $path);
- $method = explode(',', $method);
- }
- $method = collect($method)->map(function ($name) {
- return strtoupper($name);
- })->map(function ($name) {
- return "<span class='label label-primary'>{$name}</span>";
- })->implode(' ');
- if (! empty(config('admin.route.prefix'))) {
- $path = '/'.trim(config('admin.route.prefix'), '/').$path;
- }
- return "<div style='margin-bottom: 5px;'>$method<code>$path</code></div>";
- })->implode('');
- });
- $show->created_at;
- $show->updated_at;
- return $show;
- }
- /**
- * Make a form builder.
- *
- * @return Form
- */
- public function form()
- {
- return Admin::form(new Permission(), function (Form $form) {
- $permissionTable = config('admin.database.permissions_table');
- $connection = config('admin.database.connection');
- $id = $form->key();
- $form->display('id', 'ID');
- $form->text('slug', trans('admin.slug'))
- ->required()
- ->creationRules(['required', "unique:{$connection}.{$permissionTable}"])
- ->updateRules(['required', "unique:{$connection}.{$permissionTable},slug,$id"]);
- $form->text('name', trans('admin.name'))->required();
- $form->multipleSelect('http_method', trans('admin.http.method'))
- ->options($this->getHttpMethodsOptions())
- ->help(trans('admin.all_methods_if_empty'));
- $form->tags('http_path', trans('admin.http.path'))
- ->options($this->getRoutes());
- $form->display('created_at', trans('admin.created_at'));
- $form->display('updated_at', trans('admin.updated_at'));
- $form->disableViewButton();
- $form->disableViewCheck();
- });
- }
- /**
- * @return array
- */
- public function getRoutes()
- {
- $prefix = config('admin.route.prefix');
- return collect(app('router')->getRoutes())->map(function ($route) use ($prefix) {
- if (! Str::startsWith($uri = $route->uri(), $prefix)) {
- return;
- }
- return Str::replaceFirst($prefix, '', preg_replace('/{.*}+/', '*', $uri));
- })->filter()->all();
- }
- /**
- * Get options of HTTP methods select field.
- *
- * @return array
- */
- protected function getHttpMethodsOptions()
- {
- $permissionModel = config('admin.database.permissions_model');
- return array_combine($permissionModel::$httpMethods, $permissionModel::$httpMethods);
- }
- }
|