HelpController.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <?php
  2. namespace App\Admin\Controllers\Content;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\Help;
  5. use App\Models\HelpCategory;
  6. use Encore\Admin\Admin;
  7. use Encore\Admin\Controllers\HasResourceActions;
  8. use Encore\Admin\Facades\Admin as userAdmin;
  9. use Encore\Admin\Form;
  10. use Encore\Admin\Grid;
  11. use Encore\Admin\Layout\Content;
  12. use Encore\Admin\Show;
  13. use Illuminate\Http\Request;
  14. class HelpController extends Controller
  15. {
  16. use HasResourceActions;
  17. /**
  18. * Index interface.
  19. *
  20. * @param Content $content
  21. * @return Content
  22. */
  23. public function index(Content $content)
  24. {
  25. return $content
  26. ->header('帮助中心')
  27. ->description(' ')
  28. ->body($this->grid());
  29. }
  30. /**
  31. * Show interface.
  32. *
  33. * @param mixed $id
  34. * @param Content $content
  35. * @return Content
  36. */
  37. public function show($id, Content $content)
  38. {
  39. return $content
  40. ->header('帮助中心')
  41. ->description(' ')
  42. ->body($this->detail($id));
  43. }
  44. /**
  45. * Edit interface.
  46. *
  47. * @param mixed $id
  48. * @param Content $content
  49. * @return Content
  50. */
  51. public function edit($id, Content $content)
  52. {
  53. return $content
  54. ->header('帮助中心')
  55. ->description(' ')
  56. ->body($this->form()->edit($id));
  57. }
  58. /**
  59. * Create interface.
  60. *
  61. * @param Content $content
  62. * @return Content
  63. */
  64. public function create(Content $content)
  65. {
  66. return $content
  67. ->header('帮助中心')
  68. ->description(' ')
  69. ->body($this->form());
  70. }
  71. /**
  72. * Make a grid builder.
  73. *
  74. * @return Grid
  75. */
  76. protected function grid()
  77. {
  78. $grid = new Grid(new Help);
  79. $grid->model()->orderBy('list_order', 'desc')->orderBy('created_at', 'desc');
  80. $grid->title('标题')->display(function () {
  81. $cate = HelpCategory::where(array('id'=>$this->type_id))->first();
  82. if ($this->parent_id=='0') {
  83. $params = '?type_id=&parent_id='.$this->type_id;
  84. } else {
  85. $params = '?type_id='.$this->type_id.'&parent_id='.$this->parent_id;
  86. }
  87. $title_url = url(admin_base_path().'/content/help/index'.$params);
  88. return '<a href='.$title_url.' style="color: #006699;">['.$cate->category_name.']</a>'.' '.$this->title;
  89. })->width(450);
  90. $grid->list_order('排序');
  91. $grid->click('点击量');
  92. $grid->created_at('添加时间');
  93. $grid->tools(function (Grid\Tools $tools) {
  94. $tools->disableRefreshButton();
  95. });
  96. //新增按钮
  97. if (userAdmin::user()->can('content_help_list_create')) {
  98. $grid->disableCreateButton(false);
  99. }
  100. //批量删除
  101. if (userAdmin::user()->can('content_help_list_delete')) {
  102. $grid->tools(function ($tools) {
  103. $tools->batch(function ($batch) {
  104. $batch->disableDelete(false);
  105. });
  106. });
  107. } else {
  108. $grid->disableRowSelector();
  109. }
  110. $grid->actions(function ($actions) {
  111. if (userAdmin::user()->can('content_help_list_edit')) {
  112. $actions->disableEdit(false);
  113. }
  114. if (userAdmin::user()->can('content_help_list_delete')) {
  115. $actions->disableDelete(false);
  116. }
  117. });
  118. $this->script = <<<EOT
  119. $('select[name="type_id"]').parent().parent().attr('id','type_div');
  120. $('select[name="parent_id"]').change(function(){
  121. if($('select[name="parent_id"]').val()){
  122. $('#type_div').css('display','block');
  123. }else{
  124. $('#type_div').css('display','none');
  125. $('#type_div select[name="type_id"]').val('');
  126. }
  127. });
  128. $(document).ready(function(){
  129. //判断是否显示子分类
  130. var \$children = $('select[name="parent_id"]').val();
  131. if(\$children){
  132. $('#type_div').css('display','block');
  133. }else{
  134. $('#type_div').css('display','none');
  135. }
  136. });
  137. EOT;
  138. Admin::script($this->script);
  139. $grid->filter(function ($filter) {
  140. $filter->disableIdFilter();
  141. $filter->equal('ID', 'ID');
  142. $filter->like('title', '标题');
  143. $cate_option = HelpCategory::where(array('parent_id'=>0))->OrderBy('list_order', 'desc')->OrderBy('created_at', 'desc')->get()->pluck('category_name', 'id');
  144. $cate_option = $cate_option->toArray();
  145. $cate_option = array(''=>'不限')+$cate_option;
  146. $filter->where(function ($query) {
  147. $query->where('parent_id', '=', "{$this->input}")->orWhere('type_id', '=', "{$this->input}");
  148. }, '帮助分类', 'parent_id')->select($cate_option)->load('type_id', 'childCategory', 'id', 'category_name');
  149. $pid = \Illuminate\Support\Facades\Request::input('parent_id');
  150. if ($pid) {
  151. $child_option = HelpCategory::where(array('parent_id'=>$pid))->OrderBy('list_order', 'desc')->OrderBy('created_at', 'desc')->get()->pluck('category_name', 'id')->toArray();
  152. $child_option = array(''=>'不限')+$child_option ;
  153. } else {
  154. $child_option = array(''=>'不限');
  155. }
  156. $filter ->equal('type_id', '子分类')->select($child_option);
  157. });
  158. return $grid;
  159. }
  160. public function childCategorys(Request $request)
  161. {
  162. $parent_id = $request->get('q');
  163. $rst = HelpCategory::where('parent_id', $parent_id)->get(['id', 'category_name'])->toArray();
  164. $pre_arr = array('id'=>'','category_name'=>'不限');
  165. array_unshift($rst, $pre_arr);
  166. return collect($rst);
  167. }
  168. /**
  169. * Make a show builder.
  170. *
  171. * @param mixed $id
  172. * @return Show
  173. */
  174. protected function detail($id)
  175. {
  176. $show = new Show(Help::findOrFail($id));
  177. $show->id('ID');
  178. $show->title('标题');
  179. /*$show->show_category()->category_name('所属分类');*/
  180. $show->show_category("所属分类")->as(function ($show_category) {
  181. return $show_category->category_name;
  182. });
  183. $show->content('内容')->as(function ($content) {
  184. return $content?$content:'&nbsp;';
  185. })->setEscape(false);
  186. $show->list_order('排序');
  187. $show->click('点击量');
  188. $show->created_at('添加时间');
  189. $show->updated_at('更新时间');
  190. /*$show->panel()
  191. ->tools(function ($tools) {
  192. $tools->disableEdit();
  193. $tools->disableDelete();
  194. });*/
  195. return $show;
  196. }
  197. /**
  198. * Make a form builder.
  199. *
  200. * @return Form
  201. */
  202. protected function form()
  203. {
  204. $form = new Form(new Help);
  205. $form->text('title', '标题')->rules('required|max:50', array('required'=>'标题不能为空。','max'=>'标题长度不能超过50。'))->setWidth(4)->setMustMark();
  206. $form->number('list_order', '排序')->min(0)->default(0)->rules('required', array('required'=>'排序不能为空。'))
  207. ->attribute('maxlength', '10')->help('数字越大越靠前')->setMustMark();
  208. $cate_option = HelpCategory::selectOptions(function ($query) {
  209. return $query->orderBy('list_order', 'desc')->orderBy('created_at', 'desc');
  210. });
  211. unset($cate_option[0]);
  212. $form->select('type_id', '帮助分类')->options($cate_option)
  213. ->default(key($cate_option))->rules('required', array('required'=>'请选择帮助分类。'))
  214. ->setWidth(4)->setMustMark();
  215. $form->editor('content', '内容')->rules('required', array('required'=>'内容不能为空。'))->setMustMark();
  216. $form->hidden('parent_id');
  217. $form->hidden('click');
  218. $form->saving(function (Form $form) {
  219. if (!$id = $form->model()->id) {
  220. $form->click = 0;
  221. }
  222. $type_info = HelpCategory::find($form->type_id);
  223. $form->parent_id = $type_info->parent_id;
  224. });
  225. $form->footer(function ($footer) {
  226. $footer->disableViewCheck();
  227. $footer->disableEditingCheck();
  228. $footer->disableCreatingCheck();
  229. $footer->disableReset();
  230. });
  231. $form->tools(function (Form\Tools $tools) {
  232. $tools->disableDelete();
  233. $tools->disableView();
  234. });
  235. return $form;
  236. }
  237. }