HrtoolsController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. <?php
  2. namespace App\Admin\Controllers\Content;
  3. use App\Admin\Extensions\Form\ValidateForm;
  4. use App\Http\Controllers\Controller;
  5. use App\Models\Hrtools;
  6. use App\Models\HrtoolsCategory;
  7. use Encore\Admin\Controllers\HasResourceActions;
  8. use Encore\Admin\Facades\Admin;
  9. use Encore\Admin\Form;
  10. use Encore\Admin\Grid;
  11. use Encore\Admin\Layout\Content;
  12. use Encore\Admin\Show;
  13. class HrtoolsController extends Controller
  14. {
  15. use HasResourceActions;
  16. /**
  17. * Index interface.
  18. *
  19. * @param Content $content
  20. * @return Content
  21. */
  22. public function index(Content $content)
  23. {
  24. return $content
  25. ->header('HR工具箱')
  26. ->description(' ')
  27. ->body($this->grid());
  28. }
  29. /**
  30. * Show interface.
  31. *
  32. * @param mixed $id
  33. * @param Content $content
  34. * @return Content
  35. */
  36. public function show($id, Content $content)
  37. {
  38. return $content
  39. ->header('HR工具箱')
  40. ->description(' ')
  41. ->body($this->detail($id));
  42. }
  43. /**
  44. * Edit interface.
  45. *
  46. * @param mixed $id
  47. * @param Content $content
  48. * @return Content
  49. */
  50. public function edit($id, Content $content)
  51. {
  52. return $content
  53. ->header('HR工具箱')
  54. ->description(' ')
  55. ->body($this->editForm()->edit($id));
  56. }
  57. /**
  58. * Create interface.
  59. *
  60. * @param Content $content
  61. * @return Content
  62. */
  63. public function create(Content $content)
  64. {
  65. return $content
  66. ->header('HR工具箱')
  67. ->description(' ')
  68. ->body($this->form());
  69. }
  70. /**
  71. * Make a grid builder.
  72. *
  73. * @return Grid
  74. */
  75. protected function grid()
  76. {
  77. $grid = new Grid(new Hrtools);
  78. $grid->model()->orderBy('list_order', 'desc')->orderBy('created_at', 'DESC');
  79. $grid->id('ID');
  80. $grid->title('名称')->display(function () {
  81. $style="color:".$this->tit_color.';';
  82. if ($this->tit_b=='1') {
  83. $style .='font-weight:bold;';
  84. }
  85. return '<span style="'.$style.'">'.$this->title.'</span>';
  86. })->width(250);
  87. $grid->column('文件路径')->display(function () {
  88. if (preg_match('/^files\//', $this->file_url)) {
  89. $filepath = public_path('uploads').'/'.$this->file_url;
  90. if (file_exists($filepath)) {
  91. return '<a href="download/'.$this->file_url.'">'.$this->file_url.'</a>';
  92. } else {
  93. return '';
  94. }
  95. } else {
  96. return '<a href="'.$this->file_url.'">'.$this->file_url.'</a>';
  97. }
  98. })->width(250);
  99. $grid->show_category()->category_name('所属分类');
  100. $grid->list_order('排序');
  101. $grid->created_at('添加时间');
  102. //新增按钮
  103. if (Admin::user()->can('content_hrtools_list_create')) {
  104. $grid->disableCreateButton(false);
  105. }
  106. //批量删除
  107. if (Admin::user()->can('content_hrtools_list_delete')) {
  108. $grid->tools(function ($tools) {
  109. $tools->batch(function ($batch) {
  110. $batch->disableDelete(false);
  111. });
  112. });
  113. } else {
  114. $grid->disableRowSelector();
  115. }
  116. $grid->actions(function ($actions) {
  117. if (Admin::user()->can('content_hrtools_list_edit')) {
  118. $actions->disableEdit(false);
  119. }
  120. if (Admin::user()->can('content_hrtools_list_delete')) {
  121. $actions->disableDelete(false);
  122. }
  123. });
  124. $grid->filter(function ($filter) {
  125. $filter->disableIdFilter();
  126. $filter->like('title', '名称');
  127. $cate_option = HrtoolsCategory::orderBy('list_order', 'desc')->orderBy('created_at', 'desc')->pluck('category_name', 'id');
  128. if ($cate_option) {
  129. $cate_option = $cate_option->toArray();
  130. $cate_option = array(''=>'不限') + $cate_option;
  131. } else {
  132. $cate_option = array(''=>'不限');
  133. }
  134. $filter->equal('type_id', '所属分类')->select($cate_option);
  135. });
  136. return $grid;
  137. }
  138. /**
  139. * Make a show builder.
  140. *
  141. * @param mixed $id
  142. * @return Show
  143. */
  144. protected function detail($id)
  145. {
  146. $show = new Show(Hrtools::findOrFail($id));
  147. $show->id('ID');
  148. $show->title('名称');
  149. $show->tit_color('标题颜色')->as(function ($tit_color) {
  150. $html = '<i style="background-color: '.$tit_color.';padding: 5px 15px;"></i>';
  151. return $html;
  152. })->setEscape(false);
  153. $show->tit_b('标题加粗')->as(function ($tit_b) {
  154. return $tit_b?'是':'否';
  155. });
  156. $show->file_url('文件路径');
  157. /*$show->show_category()->category_name('所属分类');*/
  158. $show->show_category("所属分类")->as(function ($show_category) {
  159. return $show_category->category_name;
  160. });
  161. $show->list_order('排序');
  162. $show->created_at('添加时间');
  163. $show->updated_at('更新时间');
  164. /* $show->panel()
  165. ->tools(function ($tools) {
  166. $tools->disableEdit();
  167. $tools->disableDelete();
  168. });*/
  169. return $show;
  170. }
  171. /**
  172. * Make a form builder.
  173. *
  174. * @return Form
  175. */
  176. protected function form()
  177. {
  178. $form = new ValidateForm(new Hrtools);
  179. $form->text('title', '名称')->rules('required|max:25', array('required'=>'名称不能为空。', 'max'=>'名称长度不能大于25个字符。'))->setWidth(4)->setMustMark();
  180. $form->color('tit_color', '名称颜色')->default('#000000');
  181. $display_option = [
  182. 'on' => ['value' => 1, 'text' => '是', 'color' => 'success'],
  183. 'off' => ['value' => 0, 'text' => '否', 'color' => 'danger'],
  184. ];
  185. $form->switch('tit_b', '名称加粗')->states($display_option)->default(0)->setMustMark();
  186. $cate_option = HrtoolsCategory::orderBy('list_order', 'desc')->orderBy('created_at', 'desc')->pluck('category_name', 'id');
  187. $form->select('type_id', '所属分类')->options($cate_option)->rules('required', array('required'=>'请选择所属分类。'))->setWidth(4)->setMustMark();
  188. $form->file('up_file', '上传文件')->uniqueName()
  189. ->rules('required_without:url|mimes:doc,docx,ppt,xls,rtf', array('required_without'=>'上传文件或者文件路径填写一项。','mimes'=>'上传文件格式不正确。'))
  190. ->help('(只能上传doc/ppt/xls/rtf文件)')
  191. ->setMustMark()
  192. ->setWidth(4);
  193. $form->url('url', '或填写文件路径')
  194. ->rules('required_without:up_file', array('required_without'=>'上传文件或者文件路径填写一项。'))
  195. ->help('(请输入完整文件路径,如:http://www.91caiqing.com/123.doc)')
  196. ->setWidth(4);
  197. $form->number('list_order', '排序')->min(0)->default(0)
  198. ->rules('required', array('required'=>'排序不能为空。'))
  199. ->attribute('maxlength', '10')->help('(数字越大越靠前)')->setMustMark();
  200. $form->footer(function ($footer) {
  201. $footer->disableViewCheck();
  202. $footer->disableEditingCheck();
  203. $footer->disableCreatingCheck();
  204. $footer->disableReset();
  205. });
  206. return $form;
  207. }
  208. protected function editForm()
  209. {
  210. $form = new ValidateForm(new Hrtools);
  211. $form->text('title', '名称')->rules('required|max:25', array('required'=>'名称不能为空。', 'max'=>'名称长度不能大于25个字符。'))->setWidth(4)->setMustMark();
  212. $form->color('tit_color', '名称颜色')->default('#000000');
  213. $display_option = [
  214. 'on' => ['value' => 1, 'text' => '是', 'color' => 'success'],
  215. 'off' => ['value' => 0, 'text' => '否', 'color' => 'danger'],
  216. ];
  217. $form->switch('tit_b', '名称加粗')->states($display_option)->default(0)->setMustMark();
  218. $cate_option = HrtoolsCategory::orderBy('list_order', 'desc')->orderBy('created_at', 'desc')->pluck('category_name', 'id');
  219. $form->select('type_id', '所属分类')->options($cate_option)->rules('required', array('required'=>'请选择所属分类。'))->setWidth(4)->setMustMark();
  220. $form->file('up_file', '上传文件')->uniqueName()->rules('mimes:doc,docx,ppt,xls,rtf', array('mimes'=>'上传文件格式不正确。'))->help('(只能上传doc/ppt/xls/rtf文件)')->setWidth(4)->setMustMark()->options(['layoutTemplates' => ['actionDelete' => '']]);
  221. $form->url('url', '或填写文件路径')->help('(请输入完整文件路径,如:http://www.baidu.com/123.doc)')->setWidth(4);
  222. $form->number('list_order', '排序')->min(0)->default(0)->rules('required', array('required'=>'排序不能为空。'))->attribute('maxlength', '10')->help('(数字越大越靠前)')->setMustMark();
  223. $form->footer(function ($footer) {
  224. $footer->disableViewCheck();
  225. $footer->disableEditingCheck();
  226. $footer->disableCreatingCheck();
  227. $footer->disableReset();
  228. });
  229. $form->tools(function (Form\Tools $tools) {
  230. $tools->disableDelete();
  231. $tools->disableView();
  232. });
  233. return $form;
  234. }
  235. public function update($id)
  236. {
  237. $data=$this->editForm()->getValidateInput();
  238. $time = date('Y-m-d H:i:s', time());
  239. $up_file = null;
  240. $url = $data['url'];
  241. if (array_key_exists('up_file', $data)) {
  242. $up_file = $data['up_file'];
  243. }
  244. $file_url = $up_file?$up_file:$url;
  245. $form_data = array(
  246. 'title' => $data['title'],
  247. 'type_id' => $data['type_id'],
  248. 'tit_color' => $data['tit_color']?$data['tit_color']:'#000000',
  249. 'tit_b' => $data['tit_b'],
  250. 'list_order' => $data['list_order'],
  251. 'updated_at' => $time
  252. );
  253. if ($file_url) {
  254. $form_data['file_url'] = $file_url;
  255. }
  256. if (Hrtools::where(array('id'=>$id))->update($form_data)) {
  257. admin_toastr(trans('admin.save_succeeded'));
  258. return redirect(admin_base_path('/content/hrtools/index'));
  259. } else {
  260. throw new \Exception(trans('admin.save_failed'));
  261. }
  262. }
  263. public function store()
  264. {
  265. $data=$this->form()->getValidateInput();
  266. $time = date('Y-m-d H:i:s', time());
  267. $up_file = null;
  268. if (array_key_exists('up_file', $data)) {
  269. $up_file = $data['up_file'];
  270. }
  271. $url = $data['url'];
  272. $file_url = $up_file?$up_file:$url;
  273. $form_data = array(
  274. 'title' => $data['title'],
  275. 'type_id' => $data['type_id'],
  276. 'tit_color' => $data['tit_color']?$data['tit_color']:'#000000',
  277. 'tit_b' => $data['tit_b'],
  278. 'file_url' => $file_url,
  279. 'list_order' => $data['list_order'],
  280. 'created_at' => $time,
  281. 'updated_at' => $time
  282. );
  283. if (Hrtools::insert($form_data)) {
  284. admin_toastr(trans('admin.save_succeeded'));
  285. return redirect(admin_base_path('/content/hrtools/index'));
  286. } else {
  287. throw new \Exception(trans('admin.save_failed'));
  288. }
  289. }
  290. public function download($type, $filename)
  291. {
  292. $filepath = public_path('uploads').'/'.$type . '/' . $filename;
  293. $file = fopen($filepath, "r");
  294. Header("Content-type: application/octet-stream");
  295. Header("Accept-Ranges: bytes");
  296. Header("Accept-Length: " . filesize($filepath));
  297. Header("Content-Disposition: attachment; filename=" . $filename);
  298. fclose($file);
  299. }
  300. }