HotwordController.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. namespace App\Admin\Controllers\System;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\Hotword;
  5. use Encore\Admin\Controllers\HasResourceActions;
  6. use Encore\Admin\Facades\Admin;
  7. use Encore\Admin\Form;
  8. use Encore\Admin\Grid;
  9. use Encore\Admin\Layout\Content;
  10. use Encore\Admin\Show;
  11. class HotwordController extends Controller
  12. {
  13. use HasResourceActions;
  14. /**
  15. * Index interface.
  16. *
  17. * @param Content $content
  18. * @return Content
  19. */
  20. public function index(Content $content)
  21. {
  22. return $content
  23. ->header('热门关键字列表')
  24. ->description('')
  25. ->body($this->grid());
  26. }
  27. /**
  28. * Show interface.
  29. *
  30. * @param mixed $id
  31. * @param Content $content
  32. * @return Content
  33. */
  34. public function show($id, Content $content)
  35. {
  36. return $content
  37. ->header('热门关键字详情')
  38. ->description('')
  39. ->body($this->detail($id));
  40. }
  41. /**
  42. * Edit interface.
  43. *
  44. * @param mixed $id
  45. * @param Content $content
  46. * @return Content
  47. */
  48. public function edit($id, Content $content)
  49. {
  50. return $content
  51. ->header('热门关键字编辑')
  52. ->description('')
  53. ->body($this->editForm()->edit($id));
  54. }
  55. /**
  56. * Create interface.
  57. *
  58. * @param Content $content
  59. * @return Content
  60. */
  61. public function create(Content $content)
  62. {
  63. return $content
  64. ->header('热门关键字创建')
  65. ->description('')
  66. ->body($this->createForm());
  67. }
  68. /**
  69. * Make a grid builder.
  70. *
  71. * @return Grid
  72. */
  73. protected function grid()
  74. {
  75. $grid = new Grid(new Hotword);
  76. $grid->model()->orderBy('list_order', 'DESC')->orderBy('w_hot', 'DESC');
  77. $grid->id('ID');
  78. $grid->w_word('关键词')->width(150);
  79. $grid->type('搜索类型')->width(150)->display(function () {
  80. if ($this->type == '2') {
  81. return '企业搜索';
  82. } elseif ($this->type == '3') {
  83. return '个人简历搜索';
  84. } else {
  85. return '职位搜索';
  86. }
  87. });
  88. $grid->w_hot('搜索次数')->width(200);
  89. $grid->list_order('显示顺序')->editable()->width(150);
  90. $grid->created_at('添加时间');
  91. $grid->updated_at('更新时间');
  92. $grid->actions(function ($actions) use ($grid) {
  93. if (Admin::user()->can('system_hotword_edit')) {
  94. $actions->disableEdit(false);
  95. }
  96. if (Admin::user()->can('system_hotword_delete')) {
  97. $actions->disableDelete(false);
  98. }
  99. $actions->disableView();
  100. });
  101. if (Admin::user()->can('system_hotword_delete')) {
  102. $grid->tools(function ($tools) {
  103. $tools->batch(function ($batch) {
  104. $batch->disableDelete(false);
  105. });
  106. });
  107. $grid->disableRowSelector(false);
  108. }
  109. if (Admin::user()->can('system_hotword_create')) {
  110. $grid->disableCreateButton(false);
  111. }
  112. return $grid;
  113. }
  114. /**
  115. * Make a show builder.
  116. *
  117. * @param mixed $id
  118. * @return Show
  119. */
  120. protected function detail($id)
  121. {
  122. $show = new Show(Hotword::findOrFail($id));
  123. $show->id('ID');
  124. $show->w_word('关键词');
  125. $show->type('搜索类型')->as(function ($type) {
  126. if ($type == 2) {
  127. return '企业搜索';
  128. } elseif ($type == 3) {
  129. return '个人简历搜索';
  130. } else {
  131. return '职位搜索';
  132. }
  133. });
  134. $show->w_hot('搜索次数');
  135. $show->list_order('显示顺序');
  136. return $show;
  137. }
  138. /**
  139. * Make a form builder.
  140. *
  141. * @return Form
  142. */
  143. protected function form()
  144. {
  145. $form = new Form(new Hotword);
  146. $form->display('ID');
  147. $form->display('Created at');
  148. $form->display('Updated at');
  149. return $form;
  150. }
  151. /**
  152. * Make a form builder.
  153. *
  154. * @return Form
  155. */
  156. protected function editForm()
  157. {
  158. $form = new Form(new Hotword);
  159. $form->text('w_word', '关键词')->rules([
  160. 'required',
  161. ])->setWidth(3)->setMustMark();
  162. //搜索类型
  163. $type_arr = ['1'=>'职位搜索','2'=>'企业搜索','3'=>'个人简历搜索'];
  164. $form->select('type', '搜索类型')
  165. ->options($type_arr)
  166. ->rules('required', array('required'=>'请选择搜索类型。'))
  167. ->setWidth(3)
  168. ->setMustMark();
  169. $form->number('w_hot', '搜索次数')->min(0)->default(0);
  170. $form->number('list_order', '显示排序')->min(0)->default(0)->help('(越大越靠前)');
  171. return $form;
  172. }
  173. protected function createForm()
  174. {
  175. $form = new Form(new Hotword);
  176. $form->text('w_word', '关键词')->rules([
  177. 'required',
  178. ])->setWidth(3)->setMustMark();
  179. //搜索类型
  180. $type_arr = ['1'=>'职位搜索','2'=>'企业搜索','3'=>'个人简历搜索'];
  181. $form->select('type', '搜索类型')
  182. ->options($type_arr)
  183. ->default(key($type_arr))
  184. ->rules('required', array('required'=>'请选择搜索类型。'))
  185. ->setWidth(3)
  186. ->setMustMark();
  187. $form->number('w_hot', '搜索次数')->min(0)->default(0);
  188. $form->number('list_order', '显示排序')->min(0)->default(0)->help('(越大越靠前)');
  189. return $form;
  190. }
  191. /**
  192. * Store a newly created resource in storage.
  193. *
  194. * @return mixed
  195. */
  196. public function store()
  197. {
  198. return $this->createForm()->store();
  199. }
  200. /**
  201. * Update the specified resource in storage.
  202. *
  203. * @param int $id
  204. *
  205. * @return \Illuminate\Http\Response|\Symfony\Component\HttpFoundation\Response
  206. */
  207. public function update($id)
  208. {
  209. return $this->editForm()->update($id);
  210. }
  211. }