|
@@ -0,0 +1,208 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace App\Distributor\Controllers;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+use App\Distributor\Repositories\DistProduct;
|
|
|
+use App\Distributor\Repositories\DistProductCategory;
|
|
|
+use App\Distributor\Repositories\SiteMenu;
|
|
|
+use App\Distributor\Repositories\SitePages;
|
|
|
+use App\Distributor\Repositories\SitePagesTag;
|
|
|
+use Dcat\Admin\Form;
|
|
|
+use Dcat\Admin\Grid;
|
|
|
+use Dcat\Admin\Show;
|
|
|
+use Dcat\Admin\Http\Controllers\AdminController;
|
|
|
+use Dcat\Admin\Layout\Content;
|
|
|
+use Dcat\Admin\Admin;
|
|
|
+use Dcat\Admin\Tree;
|
|
|
+use Dcat\Admin\Layout\Row;
|
|
|
+use function Symfony\Component\String\u;
|
|
|
+
|
|
|
+class SiteMenuController extends AdminController
|
|
|
+{
|
|
|
+ /**
|
|
|
+ * page index
|
|
|
+ */
|
|
|
+ public function index(Content $content)
|
|
|
+ {
|
|
|
+ return $content
|
|
|
+ ->header(admin_trans( 'admin.top_menu') )
|
|
|
+ ->description('')
|
|
|
+ ->body($this->tree());
|
|
|
+ }
|
|
|
+
|
|
|
+ private function tree()
|
|
|
+ {
|
|
|
+ return function (Row $row) {
|
|
|
+ $tree = new Tree(new SiteMenu);
|
|
|
+
|
|
|
+ $tree->branch(function ($branch) {
|
|
|
+ $arr[] = $branch['title'];
|
|
|
+ if ($branch['uri']) {
|
|
|
+ $arr[] = $branch['uri'];
|
|
|
+ }
|
|
|
+ return implode(' - ', $arr);
|
|
|
+ });
|
|
|
+
|
|
|
+ //按钮
|
|
|
+ $tree->disableEditButton();
|
|
|
+ $tree->disableCreateButton();
|
|
|
+ $tree->showQuickCreateButton();
|
|
|
+ $tree->showQuickEditButton();
|
|
|
+ $row->column(6, $tree);
|
|
|
+ };
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Make a grid builder.
|
|
|
+ *
|
|
|
+ * @return Grid
|
|
|
+ */
|
|
|
+// protected function grid()
|
|
|
+// {
|
|
|
+// return Grid::make(new SiteMenu(), function (Grid $grid) {
|
|
|
+// $grid->column('id')->sortable();
|
|
|
+// $grid->column('parent_id');
|
|
|
+// $grid->column('order');
|
|
|
+// $grid->column('title');
|
|
|
+// $grid->column('uri');
|
|
|
+// $grid->column('show');
|
|
|
+// $grid->column('menu_type');
|
|
|
+// $grid->column('menu_config');
|
|
|
+// $grid->column('created_at');
|
|
|
+// $grid->column('updated_at')->sortable();
|
|
|
+//
|
|
|
+// $grid->filter(function (Grid\Filter $filter) {
|
|
|
+// $filter->equal('id');
|
|
|
+//
|
|
|
+// });
|
|
|
+// });
|
|
|
+// }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Make a show builder.
|
|
|
+ *
|
|
|
+ * @param mixed $id
|
|
|
+ *
|
|
|
+ * @return Show
|
|
|
+ */
|
|
|
+// protected function detail($id)
|
|
|
+// {
|
|
|
+// return Show::make($id, new SiteMenu(), function (Show $show) {
|
|
|
+// $show->field('id');
|
|
|
+// $show->field('parent_id');
|
|
|
+// $show->field('order');
|
|
|
+// $show->field('title');
|
|
|
+// $show->field('uri');
|
|
|
+// $show->field('show');
|
|
|
+// $show->field('menu_type');
|
|
|
+// $show->field('menu_config');
|
|
|
+// $show->field('created_at');
|
|
|
+// $show->field('updated_at');
|
|
|
+// });
|
|
|
+// }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Make a form builder.
|
|
|
+ *
|
|
|
+ * @return Form
|
|
|
+ */
|
|
|
+ protected function form()
|
|
|
+ {
|
|
|
+ $thisObject = $this;
|
|
|
+ return Form::make(new SiteMenu(), function (Form $form) use ($thisObject) {
|
|
|
+ $menuConfig = $form->model()->menu_config;
|
|
|
+ $form->select('parent_id', admin_trans_label('parent_id'))
|
|
|
+ ->options(SiteMenu::selectOptions())
|
|
|
+ ->required();
|
|
|
+ $form->text('title')->required();
|
|
|
+ $form->select('menu_type')->options(config('dictionary.menu_type'))->required()
|
|
|
+ ->when(1, function (Form $form) use ($menuConfig,$thisObject) {
|
|
|
+ //选择产品
|
|
|
+ $form->select('collections_type')->options(config('dictionary.collections_type'))
|
|
|
+ ->value($thisObject->getArrayValue('collections_type',$menuConfig))
|
|
|
+ ->when(0, function (Form $form) use ($menuConfig,$thisObject) {
|
|
|
+ $form->select('product_category')->options(DistProductCategory::selectOptions())->value($thisObject->getArrayValue('product_category',$menuConfig));
|
|
|
+ })->when(1, function (Form $form) use ($menuConfig,$thisObject) {
|
|
|
+ $form->select('pages_tag')->options('api/tag')->value($thisObject->getArrayValue('posts_tag',$menuConfig));
|
|
|
+ });
|
|
|
+ })
|
|
|
+ ->when(2, function (Form $form) use ($menuConfig,$thisObject) {
|
|
|
+ //选择产品
|
|
|
+ $form->select('product',admin_trans_label('select_product'))->options('api/products')
|
|
|
+ ->value($thisObject->getArrayValue('product',$menuConfig))
|
|
|
+ ->help('The latest 30 products are displayed by default, and you can also use the search function to find all products.');
|
|
|
+ })
|
|
|
+ ->when(3, function (Form $form) use ($menuConfig,$thisObject) {
|
|
|
+ //选择页面
|
|
|
+ $form->select('posts',admin_trans_label('select_pages'))->options('api/pages')
|
|
|
+ ->value($thisObject->getArrayValue('posts',$menuConfig))
|
|
|
+ ->help('The latest 30 published posts are displayed by default, and you can also use the search function to find all posts.');
|
|
|
+ })
|
|
|
+ ->when(4, function (Form $form) use ($menuConfig,$thisObject) {
|
|
|
+ //url
|
|
|
+ $form->url('url')->value($thisObject->getArrayValue('url',$menuConfig));
|
|
|
+ });
|
|
|
+
|
|
|
+ $form->switch('show')->default(1);
|
|
|
+ $form->hidden('uri');
|
|
|
+ $form->textarea('menu_config')->hideInDialog();
|
|
|
+ $form->ignore(['collections_type','product_category','pages_tag','product','posts','url']);
|
|
|
+ //保存事件
|
|
|
+ $form->submitted(function (Form $form) use ($thisObject) {
|
|
|
+ $result = $thisObject->convertMenuConfig($form);
|
|
|
+ $form->uri = $result['uri'];
|
|
|
+ $form->menu_config = $result['menuConfig'];
|
|
|
+ });
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function convertMenuConfig(Form $form) {
|
|
|
+ $uri = "";
|
|
|
+ $menuConfig = [
|
|
|
+ 'collections_type' => $form->input('collections_type'),
|
|
|
+ 'product_category' => $form->input('product_category'),
|
|
|
+ 'posts_tag' => $form->input('posts_tag'),
|
|
|
+ 'product' => $form->input('product'),
|
|
|
+ 'posts' => $form->input('posts'),
|
|
|
+ 'url' => $form->input('url'),
|
|
|
+ ];
|
|
|
+ $menuType = $form->input('menu_type');
|
|
|
+ switch ($menuType) {
|
|
|
+ case 0: //选择产品
|
|
|
+ $uri ="/";
|
|
|
+ break;
|
|
|
+ case 1: //集合
|
|
|
+ if ($form->input('collections_type') == 0) {
|
|
|
+ //产品分类
|
|
|
+ $row = DistProductCategory::getOneById($form->input('product_category'));
|
|
|
+ $uri = $row ? "/product-category/".$row->slug : "/";
|
|
|
+ } else {
|
|
|
+ //文章标签
|
|
|
+ $row = SitePagesTag::getOneById($form->input('posts_tag'));
|
|
|
+ $uri = $row ? "/collections/".$row->slug : "/";
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case 2: //选择产品
|
|
|
+ $row = DistProduct::getOneById($form->input('product'));
|
|
|
+ $uri = $row ? "/products/".$row->slug : "/";
|
|
|
+ break;
|
|
|
+ case 3: //选择页面
|
|
|
+ $row = SitePages::getOneById($form->input('posts'));
|
|
|
+ $uri = $row ? "/pages/".$row->slug : "/";
|
|
|
+ break;
|
|
|
+ case 4: //url
|
|
|
+ $uri = $form->input('url');
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ return ['uri'=>$uri,'menuConfig'=>$menuConfig];
|
|
|
+ }
|
|
|
+ protected function getArrayValue($key,$arr) {
|
|
|
+ if (isset($arr[$key])) {
|
|
|
+ return $arr[$key];
|
|
|
+ }
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+}
|