SiteMenuController.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. namespace App\Distributor\Controllers;
  3. use App\Distributor\Actions\MenuShow;
  4. use App\Distributor\Repositories\DistProduct;
  5. use App\Distributor\Repositories\DistProductCategory;
  6. use App\Distributor\Repositories\DistVideoCategory;
  7. use App\Distributor\Repositories\SiteMenu;
  8. use App\Distributor\Repositories\SitePages;
  9. use App\Distributor\Repositories\SitePagesTag;
  10. use App\Libraries\CommonHelper;
  11. use Dcat\Admin\Form;
  12. use Dcat\Admin\Grid;
  13. use Dcat\Admin\Layout\Content;
  14. use Dcat\Admin\Admin;
  15. use Dcat\Admin\Tree;
  16. use Dcat\Admin\Layout\Row;
  17. use Dcat\Admin\Widgets\Box;
  18. use function Symfony\Component\String\u;
  19. use Dcat\Admin\Layout\Column;
  20. class SiteMenuController extends AdminDistController
  21. {
  22. /**
  23. * page index
  24. */
  25. public function index(Content $content)
  26. {
  27. $location = isset($_GET['location']) ? intval($_GET['location']) : 0;
  28. //保存临时变量
  29. setTempValue('location', $location);
  30. //翻译
  31. $header = admin_trans( 'admin.top_menu');
  32. if ($location == 1) {
  33. $header = admin_trans( 'admin.footer_menu');
  34. }
  35. return $content
  36. ->header($header)
  37. ->description('')
  38. ->body($this->tree($location));
  39. }
  40. private function tree($location)
  41. {
  42. $thisObject = $this;
  43. return function (Row $row) use ($location, $thisObject) {
  44. $tree = new Tree(new SiteMenu);
  45. //两层
  46. $tree->maxDepth(2);
  47. //标题显示
  48. $tree->branch(function ($branch) {
  49. return $branch['title'];
  50. });
  51. //是否显示操作
  52. $tree->actions(function (Tree\Actions $actions) {
  53. $actions->prepend(new MenuShow());
  54. });
  55. //按钮
  56. $tree->disableEditButton();
  57. $tree->disableCreateButton();
  58. $tree->showQuickCreateButton();
  59. $tree->showQuickEditButton();
  60. $row->column(6, $tree);
  61. //增加JS,使新增与编辑带location
  62. $paramsUrl = 'location='.$location;
  63. CommonHelper::replaceAddEditerUrl('.tree-quick-create', '.tree-quick-edit', $paramsUrl);
  64. //主页权限
  65. $tree->query(function ($model) use ($location) {
  66. return $model->where('dist_id', getDistributorId())->where('menu_location', $location);
  67. });
  68. };
  69. }
  70. /**
  71. * Make a form builder.
  72. *
  73. * @return Form
  74. */
  75. protected function form()
  76. {
  77. $location = isset($_GET['location']) ? intval($_GET['location']) : 0;
  78. $thisObject = $this;
  79. return Form::make(new SiteMenu(), function (Form $form) use ($thisObject,$location) {
  80. $menuConfig = $form->model()->menu_config;
  81. //$menuConfig = json_decode($menuConfig,true);
  82. //父ID
  83. $form->select('parent_id', admin_trans_label('parent_id'))
  84. ->options(SiteMenu::selectOptions(function ($query) use ($location) {
  85. $query = $query->where('menu_location',$location)->where('dist_id', getDistributorId())->orderBy('order', 'asc');
  86. return $query;
  87. }))->required();
  88. //标题
  89. $form->text('title')->required();
  90. //类型
  91. $form->select('menu_type')->options(admin_trans_array(config('dictionary.menu_type')))->required()
  92. ->when(1, function (Form $form) use ($menuConfig,$thisObject) {
  93. //选择集合类型
  94. $form->select('collections_type')->options(admin_trans_array(config('dictionary.collections_type')))
  95. ->value($thisObject->getArrayValue('collections_type',$menuConfig))
  96. ->when(0, function (Form $form) use ($menuConfig,$thisObject) {
  97. //产品分类
  98. $form->select('product_category')->options(DistProductCategory::selectOptions())->value($thisObject->getArrayValue('product_category',$menuConfig));
  99. })->when(1, function (Form $form) use ($menuConfig,$thisObject) {
  100. //文章标签
  101. $form->select('pages_tag')->options('api/tag')->value($thisObject->getArrayValue('pages_tag',$menuConfig));
  102. })->when(2, function (Form $form) use ($menuConfig,$thisObject) {
  103. //视频
  104. $form->select('video_category')->options(DistVideoCategory::selectOptions())->value($thisObject->getArrayValue('video_category',$menuConfig));
  105. });
  106. })
  107. ->when(2, function (Form $form) use ($menuConfig,$thisObject) {
  108. //选择产品
  109. $form->select('product',admin_trans_label('select_product'))->options('api/products')
  110. ->value($thisObject->getArrayValue('product',$menuConfig))
  111. ->help('The latest 30 products are displayed by default, and you can also use the search function to find all products.');
  112. })
  113. ->when(3, function (Form $form) use ($menuConfig,$thisObject) {
  114. //选择页面
  115. $form->select('pages',admin_trans_label('select_pages'))->options('api/pages')
  116. ->value($thisObject->getArrayValue('pages',$menuConfig))
  117. ->help('The latest 30 published posts are displayed by default, and you can also use the search function to find all posts.');
  118. })
  119. ->when(10, function (Form $form) use ($menuConfig,$thisObject) {
  120. //url
  121. $form->text('url')->value($thisObject->getArrayValue('url',$menuConfig));
  122. })
  123. ->when(4, function (Form $form) use ($menuConfig,$thisObject) {
  124. $form->select('landing_page',admin_trans_label('landing_page'))->options('api/landing-pages')
  125. ->value($thisObject->getArrayValue('landing_page',$menuConfig));
  126. })->when(5, function (Form $form) use ($menuConfig,$thisObject) {
  127. $form->select('videos',admin_trans_label('videos'))->options('api/videos')
  128. ->value($thisObject->getArrayValue('videos',$menuConfig));
  129. });
  130. //显示
  131. $form->switch('show')->default(1);
  132. //隐藏字段
  133. $form->textarea('menu_config')->hideInDialog();
  134. $form->hidden('menu_location')->value($location);
  135. $form->hidden('uri');
  136. $form->hidden('dist_id'); // 隐藏dist_id字段,用于保存
  137. //以下字段不保存
  138. $form->ignore(['collections_type','product_category','pages_tag','product','pages','url','landing_page','videos','video_category']);
  139. //保存事件
  140. $form->submitted(function (Form $form) use ($thisObject) {
  141. $result = $thisObject->convertMenuConfig($form);
  142. $form->uri = $result['uri'];
  143. $form->menu_config = $result['menuConfig'];
  144. });
  145. //保存前,强制写死dist_id
  146. $form->saving(function (Form $form) {
  147. $form->dist_id =getDistributorId();
  148. });
  149. });
  150. }
  151. protected function convertMenuConfig(Form $form) {
  152. $menuConfig = [
  153. 'collections_type' => $form->input('collections_type'),
  154. 'product_category' => $form->input('product_category'),
  155. 'video_category' => $form->input('video_category'),
  156. 'pages_tag' => $form->input('pages_tag'),
  157. 'product' => $form->input('product'),
  158. 'pages' => $form->input('pages'),
  159. 'url' => $form->input('url'),
  160. 'landing_page' => $form->input('landing_page'),
  161. 'videos' => $form->input('videos'),
  162. ];
  163. $menuType = $form->input('menu_type');
  164. $uri = SiteMenu::generateUri($menuType,$menuConfig);
  165. return ['uri'=>$uri,'menuConfig'=>$menuConfig];
  166. }
  167. protected function getArrayValue($key,$arr) {
  168. if (isset($arr[$key])) {
  169. return $arr[$key];
  170. }
  171. return "";
  172. }
  173. /*
  174. * 重写store
  175. */
  176. public function store()
  177. {
  178. $location = getTempValue('location');
  179. return $this->form()->store(null,'/site-menu?location='.$location);
  180. }
  181. }