PolicyController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. <?php
  2. namespace App\Admin\Controllers\Content;
  3. use App\Models\Category;
  4. use App\Models\Policy;
  5. use App\Http\Controllers\Controller;
  6. use App\Models\PolicyCategory;
  7. use App\Models\PolicyProperty;
  8. use Encore\Admin\Controllers\HasResourceActions;
  9. use Encore\Admin\Form;
  10. use Encore\Admin\Grid;
  11. use Encore\Admin\Layout\Content;
  12. use Encore\Admin\Show;
  13. use Illuminate\Support\Facades\Input;
  14. class PolicyController 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->editForm($id)->edit($id));
  57. }
  58. public function update($id)
  59. {
  60. return $this->editForm($id)->update($id);
  61. }
  62. /**
  63. * Create interface.
  64. *
  65. * @param Content $content
  66. * @return Content
  67. */
  68. public function create(Content $content)
  69. {
  70. return $content
  71. ->header('政策管理')
  72. ->description(' ')
  73. ->body($this->form());
  74. }
  75. /**
  76. * Make a grid builder.
  77. *
  78. * @return Grid
  79. */
  80. protected function grid()
  81. {
  82. $grid = new Grid(new Policy);
  83. $grid->id('ID');
  84. $grid->model()->orderBy('article_order', 'DESC')->orderBy('created_at', 'DESC');
  85. $grid->column('文章标题')->display(function () {
  86. $style="color:".$this->tit_color.';';
  87. if ($this->tit_b=='1') {
  88. $style .='font-weight:bold;';
  89. }
  90. $cate = PolicyCategory::where(array('id'=>$this->parentid))->first();
  91. if ($this->parentid=='0') {
  92. $params = '?parentid='.$this->type_id;
  93. } else {
  94. $params = '?parentid='.$this->parentid;
  95. }
  96. $title_url = url(admin_base_path().'/content/policy'.$params);
  97. $pbstr='';
  98. if($this->is_display==0){
  99. $pbstr= '<span style="color:#999999">&nbsp;&nbsp;&nbsp;&nbsp;[已屏蔽]</span>';
  100. }
  101. $title_url1 = url('content/policys/show/'.$this->id);
  102. if ($cate) {
  103. return $pbstr.'<a href='.$title_url.' style="color: #006699;">['.$cate->categoryname.']</a> <a target="_blank" href='.$title_url1.'><span style="'.$style.'">'.$this->title.'</span></a>';
  104. } else {
  105. return $pbstr.'<span style="'.$style.'">'.$this->title.'</span>';
  106. }
  107. })->width(200);
  108. $grid->policyproperty()->categoryname('政策层次');
  109. $grid->article_order('排序');
  110. $grid->click('点击');
  111. $grid->created_at('添加时间');
  112. $grid->disableCreateButton(false);
  113. $grid->actions(function ($actions) {
  114. $actions->disableEdit(false);
  115. $actions->disableDelete(false);
  116. });
  117. $grid->tools(function ($tools) {
  118. $tools->batch(function ($batch) {
  119. $batch->disableDelete(false);
  120. });
  121. });
  122. $grid->filter(function ($filter) {
  123. $filter->equal('id', 'ID');
  124. $filter->like('title', '标题');
  125. $policy_cates = PolicyCategory::where(['parent_id'=>0])->orderBy("id", 'asc')->get()->pluck('categoryname', 'id');
  126. $filter->where(function ($query) {
  127. $query->where('parentid', '=', "{$this->input}")->orWhere('type_id', '=', "{$this->input}");
  128. }, '政策分类', 'parentid')->select($policy_cates);
  129. $policy_propertys = PolicyProperty::select()->orderBy("id", 'asc')->get()->pluck('categoryname', 'id');
  130. $filter->where(function ($query) {
  131. $query->where('focos', '=', "{$this->input}");
  132. }, '政策层次', 'focos')->select($policy_propertys);
  133. $date3 = date('Y-m-d', strtotime("-3 day"));
  134. $date7 = date('Y-m-d', strtotime("-7 day"));
  135. $date30 = date("Y-m-d", strtotime("-1 month"));
  136. $date180 = date("Y-m-d", strtotime("-6 month"));
  137. $date360 = date("Y-m-d", strtotime("-1 year"));
  138. $date_option = array(
  139. '' => '不限',
  140. $date3 => '三天内',
  141. $date7 => '一周内',
  142. $date30 => '一月内',
  143. $date180 => '半年内',
  144. $date360 => '一年内',
  145. );
  146. $filter->where(function ($query) {
  147. $query->where('created_at', '>=', "{$this->input}");
  148. }, '添加时间', 'created_at')->radio($date_option);
  149. });
  150. return $grid;
  151. }
  152. /**
  153. * Make a show builder.
  154. *
  155. * @param mixed $id
  156. * @return Show
  157. */
  158. protected function detail($id)
  159. {
  160. $show = new Show(Policy::findOrFail($id));
  161. $show->id('ID');
  162. $show->created_at('Created at');
  163. $show->updated_at('Updated at');
  164. return $show;
  165. }
  166. /**
  167. * Make a form builder.
  168. *
  169. * @return Form
  170. */
  171. protected function form()
  172. {
  173. $form = new Form(new Policy);
  174. $form->text('title', '标题')->rules('required|max:100', array('required'=>'标题不能为空。','max'=>'标题长度不能大于100。'))->setWidth(4)->setMustMark();
  175. $form->color('tit_color', '标题颜色')->rules('required|regex:/^#[a-fA-F0-9]{6}$/', array('required'=>'标题颜色不能为空。','regex'=>'标题颜色格式不正确。'))->default('#000000');
  176. $cate_option = PolicyCategory::selectOptions(function ($query) {
  177. return $query->orderBy('category_order', 'desc')->orderBy('created_at', 'desc');
  178. });
  179. unset($cate_option[0]);
  180. $form->select('type_id', '政策分类')->options($cate_option)->default(key($cate_option))->rules('required', array('required'=>'请选择政策分类。'))->setWidth(4)->setMustMark();
  181. $policyProperty = PolicyProperty::select()->pluck('categoryname', 'id');
  182. // $policyProperty[0] ='不限';
  183. $arr = array(
  184. '1' => 'RC_category_fj',
  185. '2' => 'RC_category_qz',
  186. '7' => 'RC_category_jj',
  187. '8' => 'RC_category'
  188. );
  189. $form->select('focos', '政策层次')->options($policyProperty)->load('level_ids', admin_base_path('/content/policys/treats/category'))->rules("required")->setMustMark();
  190. $form->multipleSelect('level_ids', '面向人才')->rules(['required'], ['required'=>'请填写面向人才'])->setMustMark();
  191. $form->image('small_img', '缩略图')->uniqueName()->rules('image|mimes:jpeg,bmp,png', array('image'=>'缩略图请选择图片文件。','mimes'=>'请选择jpeg,bmp,png格式的缩略图上传。'))->setWidth(3);
  192. $form->editor('content', '内容')->rules('required', array('required'=>'内容不能为空。'))->setMustMark();
  193. $display_option = [
  194. 'on' => ['value' => 1, 'text' => '是', 'color' => 'success'],
  195. 'off' => ['value' => 0, 'text' => '否', 'color' => 'danger'],
  196. ];
  197. $form->switch('is_display', '是否显示')->default(1)->states($display_option)->setMustMark();
  198. $form->switch('tit_b', '标题加粗')->states($display_option)->setMustMark();
  199. // $form->date('addtime', '发布日期')->format('YYYY-MM-DD')->rules('required|date', array('required'=>'发布日期不能为空。', 'date'=>'发布日期格式不正确。'));
  200. $form->number('article_order', '新闻排序')->min(0)->default(255)->rules('required', array('required'=>'新闻排序不能为空。'))->help('(数字越大越靠前)');
  201. $form->text('author', '作者')->rules('max:20', array('max'=>'作者长度不能大于20。'))->setWidth(3);
  202. $form->text('source', '来源')->rules('max:20', array('max'=>'来源长度不能大于20。'))->setWidth(3);
  203. $form->url('is_url', '外部链接')->rules('max:250', array('max'=>'外部链接长度不能超过250。'))->help('(请输入包含http://或https://的完整链接)');
  204. $form->text('seo_keywords', 'Keywords')->placeholder('合理设置Keywords有利于搜索引擎排名')->rules('max:80', array('max:Keywords不能大于80个字符。'));
  205. $form->textarea('seo_description', 'Description')->placeholder('合理设置Description有利于搜索引擎排名')->rules('max:80', array('max'=>'Description不能大于80个字符。'));
  206. $form->hidden('addtime');//添加时间
  207. $form->hidden('parentid');//政策大类
  208. $form->hidden('level_name');//面向人才
  209. $form->hidden('level_id');//面向人才
  210. $form->ignore(['level_ids']);
  211. $form->saving(function (Form $form){
  212. //设置添加时间
  213. $form->addtime=time();
  214. // 设置政策大类
  215. $policyCategory= PolicyCategory::where(['id'=>$form->type_id])->first();
  216. if($policyCategory->parent_id==0){
  217. $form->parentid=$form->type_id;
  218. }else{
  219. $form->parentid=$policyCategory->parent_id;
  220. }
  221. $level_id= Input::get('level_ids');
  222. $level_id = array_filter($level_id);
  223. $form->level_id=implode(',',$level_id);
  224. $levels=Category::whereIn('id',$level_id)->select('demand')->get()->toArray();
  225. $form->level_name=implode(",", array_column($levels, 'demand'));
  226. });
  227. return $form;
  228. }
  229. protected function editForm($id)
  230. {
  231. $form = new Form(new Policy);
  232. $policyData = Policy::where('id', $id)->first()->toArray();
  233. $form->text('title', '标题')->rules('required|max:100', array('required'=>'标题不能为空。','max'=>'标题长度不能大于100。'))->setWidth(4)->setMustMark();
  234. $form->color('tit_color', '标题颜色')->rules('required|regex:/^#[a-fA-F0-9]{6}$/', array('required'=>'标题颜色不能为空。','regex'=>'标题颜色格式不正确。'))->default('#000000');
  235. $cate_option = PolicyCategory::selectOptions(function ($query) {
  236. return $query->orderBy('category_order', 'desc')->orderBy('created_at', 'desc');
  237. });
  238. unset($cate_option[0]);
  239. $form->select('type_id', '政策分类')->options($cate_option)->default(key($cate_option))->rules('required', array('required'=>'请选择政策分类。'))->setWidth(4)->setMustMark();
  240. $policyProperty = PolicyProperty::select()->pluck('categoryname', 'id');
  241. // $policyProperty[0] ='不限';
  242. $arr = array(
  243. '1' => 'RC_category_fj',
  244. '2' => 'RC_category_qz',
  245. '7' => 'RC_category_jj',
  246. '8' => 'RC_category'
  247. );
  248. $form->select('focos', '政策层次')->options($policyProperty)->load('level_ids', admin_base_path('/content/policys/treats/category'))->rules("required")->setMustMark();
  249. $form->multipleSelect('level_ids', '面向人才')->options(Category::categoryType($arr[$policyData['focos']]))->default(explode(",", $policyData['level_id']))->rules(['required'], ['required'=>'请填写面向人才'])->setMustMark();
  250. $form->image('small_img', '缩略图')->uniqueName()->rules('image|mimes:jpeg,bmp,png', array('image'=>'缩略图请选择图片文件。','mimes'=>'请选择jpeg,bmp,png格式的缩略图上传。'))->setWidth(3);
  251. $form->editor('content', '内容')->rules('required', array('required'=>'内容不能为空。'))->setMustMark();
  252. $display_option = [
  253. 'on' => ['value' => 1, 'text' => '是', 'color' => 'success'],
  254. 'off' => ['value' => 0, 'text' => '否', 'color' => 'danger'],
  255. ];
  256. $form->switch('is_display', '是否显示')->states($display_option)->setMustMark();
  257. $form->switch('tit_b', '标题加粗')->states($display_option)->setMustMark();
  258. // $form->date('created_at', '发布日期')->format('YYYY-MM-DD')->rules('required|date', array('required'=>'发布日期不能为空。', 'date'=>'发布日期格式不正确。'));
  259. $form->number('article_order', '新闻排序')->min(0)->default(0)->rules('required', array('required'=>'新闻排序不能为空。'))->help('(数字越大越靠前)');
  260. $form->text('author', '作者')->rules('max:20', array('max'=>'作者长度不能大于20。'))->setWidth(3);
  261. $form->text('source', '来源')->rules('max:20', array('max'=>'来源长度不能大于20。'))->setWidth(3);
  262. $form->url('is_url', '外部链接')->rules('max:250', array('max'=>'外部链接长度不能超过250。'))->help('(请输入包含http://或https://的完整链接)');
  263. $form->text('seo_keywords', 'Keywords')->placeholder('合理设置Keywords有利于搜索引擎排名')->rules('max:80', array('max:Keywords不能大于80个字符。'));
  264. $form->textarea('seo_description', 'Description')->placeholder('合理设置Description有利于搜索引擎排名')->rules('max:80', array('max'=>'Description不能大于80个字符。'));
  265. $form->hidden('addtime');//添加时间
  266. $form->hidden('parentid');//政策大类
  267. $form->hidden('level_name');//面向人才
  268. $form->hidden('level_id');//面向人才
  269. $form->ignore(['level_ids']);
  270. $form->saving(function (Form $form){
  271. //设置政策大类
  272. $policyCategory= PolicyCategory::where(['id'=>$form->type_id])->first();
  273. if($policyCategory->parent_id==0){
  274. $form->parentid=$form->type_id;
  275. }else{
  276. $form->parentid=$policyCategory->parent_id;
  277. }
  278. $level_id= Input::get('level_ids');
  279. $level_id = array_filter($level_id);
  280. $form->level_id=implode(',',$level_id);
  281. $levels=Category::whereIn('id',$level_id)->select('demand')->get()->toArray();
  282. $form->level_name=implode(",", array_column($levels, 'demand'));
  283. });
  284. return $form;
  285. }
  286. public function destroy($id)
  287. {
  288. $ids = array();
  289. if ($id) {
  290. $ids = explode(',', $id);
  291. }
  292. if (!$ids) {
  293. return admin_toastr('请勾选需要删除的政策', 'error');
  294. }
  295. \DB::beginTransaction();
  296. try {
  297. Policy::whereIn('id', $ids)->delete();
  298. $data = [
  299. 'status' => true,
  300. 'message' => '删除成功!',
  301. ];
  302. \DB::commit();
  303. return response()->json($data);
  304. } catch (\Exception $e) {
  305. \DB::rollback();
  306. $data = [
  307. 'status' => false,
  308. 'message' => '删除失败!',
  309. ];
  310. return response()->json($data);
  311. }
  312. }
  313. }