RecruitArticleController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <?php
  2. namespace App\Admin\Controllers\Recruit;
  3. use App\Http\Controllers\Controller;
  4. use Encore\Admin\Grid;
  5. use Encore\Admin\Form;
  6. use Encore\Admin\Layout\Content;
  7. use App\Models\RecruitArticle as RecruitArticleModel;
  8. use Encore\Admin\Facades\Admin;
  9. use App\Admin\Extensions\Form\ValidateForm;
  10. use App\Models\Recruit;
  11. use Encore\Admin\Controllers\HasResourceActions;
  12. class RecruitArticleController extends Controller
  13. {
  14. use HasResourceActions;
  15. public function __construct()
  16. {
  17. }
  18. /**
  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(view('admin.recruit.article')->with(['grid'=>$this->grid()]));
  29. }
  30. /**
  31. * Make a grid builder.
  32. *
  33. * @return Grid
  34. */
  35. protected function grid()
  36. {
  37. $grid = new Grid(new RecruitArticleModel);
  38. $grid->column('新闻标题')->display(function () {
  39. $style="color:".$this->tit_color.';';
  40. if ($this->tit_b=='1') {
  41. $style .='font-weight:bold;';
  42. }
  43. $title_url = url(admin_base_path().'/recruit/article/index');
  44. return '<span style="'.$style.'">'.$this->title.'</span>';
  45. })->width(200);
  46. $grid->small_img('缩略图')->display(function () {
  47. if ($this->small_img) {
  48. return '<span class="vtip" title="<img src=\''.upload_asset($this->small_img).'\' height=120>">
  49. <img class="avatar small" src="'.upload_asset($this->small_img).'" align="absmiddle" style="width: 22px;height: 22px;">
  50. </span>';
  51. } else {
  52. return '';
  53. }
  54. });
  55. $grid->robot('添加方式');
  56. $grid->show_property()->category_name('新闻属性');
  57. $grid->list_order('排序');
  58. $grid->click('点击量');
  59. $grid->created_at('添加时间');
  60. //新增按钮
  61. if (Admin::user()->can('content_article_list_create')) {
  62. $grid->disableCreateButton(false);
  63. }
  64. //批量删除
  65. if (Admin::user()->can('content_article_list_delete')) {
  66. $grid->tools(function ($tools) {
  67. $tools->batch(function ($batch) {
  68. $batch->disableDelete(false);
  69. });
  70. });
  71. } else {
  72. $grid->disableRowSelector();
  73. }
  74. $grid->actions(function ($actions) {
  75. if (Admin::user()->can('content_article_list_delete')) {
  76. if ($actions->row['subsite_id']== get_subsite_id() || get_subsite_id()==0) {
  77. $actions->disableEdit(false);
  78. }
  79. }
  80. if (Admin::user()->can('content_article_list_edit')) {
  81. if ($actions->row['subsite_id']== get_subsite_id() || get_subsite_id()==0) {
  82. $actions->disableDelete(false);
  83. }
  84. }
  85. });
  86. $grid->filter(function ($filter) {
  87. $filter->disableIdFilter();
  88. $filter->equal('ID', '新闻ID');
  89. $filter->like('title', '新闻标题');
  90. $date3 = date('Y-m-d', strtotime("-3 day"));
  91. $date7 = date('Y-m-d', strtotime("-7 day"));
  92. $date30 = date("Y-m-d", strtotime("-1 month"));
  93. $date180 = date("Y-m-d", strtotime("-6 month"));
  94. $date360 = date("Y-m-d", strtotime("-1 year"));
  95. $date_option = array(
  96. '' => '不限',
  97. $date3 => '三天内',
  98. $date7 => '一周内',
  99. $date30 => '一月内',
  100. $date180 => '半年内',
  101. $date360 => '一年内',
  102. );
  103. $filter->where(function ($query) {
  104. $query->where('created_at', '>=', "{$this->input}");
  105. }, '添加时间', 'created_at')->radio($date_option);
  106. });
  107. return $grid;
  108. }
  109. /**
  110. * Create interface.
  111. *
  112. * @param Content $content
  113. * @return Content
  114. */
  115. public function create(Content $content)
  116. {
  117. return $content
  118. ->header('补充公告')
  119. ->description(' ')
  120. ->body($this->form());
  121. }
  122. /**
  123. * Edit interface.
  124. *
  125. * @param mixed $id
  126. * @param Content $content
  127. * @return Content
  128. */
  129. public function edit($id, Content $content)
  130. {
  131. return $content
  132. ->header('补充公告')
  133. ->description(' ')
  134. ->body($this->editForm($id)->edit($id));
  135. }
  136. /**
  137. * Make a form builder.
  138. *
  139. * @return Form
  140. */
  141. protected function form()
  142. {
  143. $form = new ValidateForm(new RecruitArticleModel);
  144. $form->text('title', '新闻标题')->rules('required|max:100', array('required'=>'新闻标题不能为空。','max'=>'新闻标题长度不能大于100。'))->setWidth(4)->setMustMark();
  145. $form->color('tit_color', '标题颜色')->rules('required|regex:/^#[a-fA-F0-9]{6}$/', array('required'=>'标题颜色不能为空。','regex'=>'标题颜色格式不正确。'))->default('#000000');
  146. $option = Recruit::select('id', 'name')->pluck('name','id');
  147. $form->select('recruit_id', '所属招考场次')->options($option)->default(key($option))->rules('required', array('required'=>'请选择所属招考场次。'))->setWidth(4)->setMustMark();
  148. $form->image('small_img', '缩略图')->uniqueName()->rules('image|mimes:jpeg,bmp,png', array('image'=>'缩略图请选择图片文件。','mimes'=>'请选择jpeg,bmp,png格式的缩略图上传。'))->setWidth(3);
  149. $form->editor('content', '内容')->rules('required', array('required'=>'内容不能为空。'))->setMustMark();
  150. $display_option = [
  151. 'on' => ['value' => 1, 'text' => '是', 'color' => 'success'],
  152. 'off' => ['value' => 0, 'text' => '否', 'color' => 'danger'],
  153. ];
  154. $form->switch('is_display', '是否显示')->states($display_option)->default('1')->setMustMark();
  155. $form->switch('tit_b', '标题加粗')->states($display_option)->default(0)->setMustMark();
  156. $form->date('released_at', '发布日期')->format('YYYY-MM-DD')->rules('required|date', array('required'=>'发布日期不能为空。', 'date'=>'发布日期格式不正确。'))->default(date('Y-m-d', time()));
  157. $form->number('list_order', '新闻排序')->min(0)->default(255)->rules('required', array('required'=>'新闻排序不能为空。'))->help('(数字越大越靠前)');
  158. $form->text('author', '作者')->rules('max:20', array('max'=>'作者长度不能大于20。'))->setWidth(4);
  159. $form->text('source', '来源')->rules('max:20', array('max'=>'来源长度不能大于20。'))->setWidth(4);
  160. $form->url('is_url', '外部链接')->rules('max:250', array('max'=>'外部链接长度不能超过250。'))->help('(请输入包含http://或https://的完整链接)');
  161. $form->text('seo_keywords', 'Keywords')->placeholder('合理设置Keywords有利于搜索引擎排名')->rules('max:80', array('max:Keywords不能大于80个字符。'));
  162. $form->textarea('seo_description', 'Description')->placeholder('合理设置Description有利于搜索引擎排名')->rules('max:80', array('max'=>'Description不能大于80个字符。'));
  163. $form->saving(function (Form $form) {
  164. $form->released_at = strtotime($form->released_at);
  165. $author = $form->author?$form->author:'';
  166. $source = $form->source?$form->source:'';
  167. $is_url = $form->is_url?$form->is_url:'';
  168. $seo_keywords = $form->seo_keywords?$form->seo_keywords:'';
  169. $seo_description = $form->seo_description?$form->seo_description:'';
  170. $form->author = $author;
  171. $form->source = $source;
  172. $form->is_url = $is_url;
  173. $form->seo_keywords = $seo_keywords;
  174. $form->seo_description = $seo_description;
  175. });
  176. $form->saved(function (Form $form) {
  177. });
  178. $form->footer(function ($footer) {
  179. $footer->disableViewCheck();
  180. $footer->disableEditingCheck();
  181. $footer->disableCreatingCheck();
  182. $footer->disableReset();
  183. });
  184. return $form;
  185. }
  186. protected function editForm($id)
  187. {
  188. $info = RecruitArticleModel::find($id);
  189. $form = new ValidateForm(new RecruitArticleModel);
  190. $form->text('title', '新闻标题')->rules('required|max:100', array('required'=>'新闻标题不能为空。','max'=>'新闻标题长度不能大于100。'))->setWidth(4)->setMustMark();
  191. $form->color('tit_color', '标题颜色')->default('#000000')->rules('required|regex:/^#[a-fA-F0-9]{6}$/', array('required'=>'标题颜色不能为空。','regex'=>'标题颜色格式不正确。'));
  192. $option = Recruit::select('id', 'name')->pluck('name','id');
  193. $form->select('recruit_id', '所属招考场次')->options($option)->default(key($option))->rules('required', array('required'=>'请选择所属招考场次。'))->setWidth(4)->setMustMark();
  194. $form->editor('content', '内容')->rules('required', array('required'=>'内容不能为空。'))->setMustMark();
  195. $display_option = [
  196. 'on' => ['value' => 1, 'text' => '是', 'color' => 'success'],
  197. 'off' => ['value' => 0, 'text' => '否', 'color' => 'danger'],
  198. ];
  199. $form->switch('is_display', '是否显示')->states($display_option)->default('1')->setMustMark();
  200. $form->switch('tit_b', '标题加粗')->states($display_option)->default(0)->setMustMark();
  201. $form->date('released_at', '发布日期')->format('YYYY-MM-DD')->rules('required|date', array('required'=>'发布日期不能为空。', 'date'=>'发布日期格式不正确。'))->default(date('Y-m-d', time()));
  202. $form->number('list_order', '新闻排序')->min(0)->default(255)->rules('required', array('required'=>'新闻排序不能为空。'))->help('(数字越大越靠前)');
  203. $form->text('author', '作者')->rules('max:20', array('max'=>'作者长度不能大于20。'))->setWidth(4);
  204. $form->text('source', '来源')->rules('max:20', array('max'=>'来源长度不能大于20。'))->setWidth(4);
  205. $form->url('is_url', '外部链接')->rules('max:250', array('max'=>'外部链接长度不能超过250。'))->help('(请输入包含http://或https://的完整链接)');
  206. $form->text('seo_keywords', 'Keywords')->placeholder('合理设置Keywords有利于搜索引擎排名')->rules('max:80', array('max:Keywords不能大于80个字符。'));
  207. $form->textarea('seo_description', 'Description')->placeholder('合理设置Description有利于搜索引擎排名')->rules('max:80', array('max'=>'Description不能大于80个字符。'));
  208. $form->saving(function (Form $form) {
  209. if (!request()->has(Form\Field::FILE_DELETE_FLAG)) {
  210. $form->released_at = strtotime($form->released_at);
  211. $author = $form->author?$form->author:'';
  212. $source = $form->source?$form->source:'';
  213. $is_url = $form->is_url?$form->is_url:'';
  214. $seo_keywords = $form->seo_keywords?$form->seo_keywords:'';
  215. $seo_description = $form->seo_description?$form->seo_description:'';
  216. $form->author = $author;
  217. $form->source = $source;
  218. $form->is_url = $is_url;
  219. $form->seo_keywords = $seo_keywords;
  220. $form->seo_description = $seo_description;
  221. }
  222. });
  223. $form->saved(function (Form $form) {
  224. if (!request()->has(Form\Field::FILE_DELETE_FLAG)) {
  225. }
  226. });
  227. $form->footer(function ($footer) {
  228. $footer->disableViewCheck();
  229. $footer->disableEditingCheck();
  230. $footer->disableCreatingCheck();
  231. $footer->disableReset();
  232. });
  233. $form->tools(function (Form\Tools $tools) {
  234. $tools->disableDelete();
  235. $tools->disableView();
  236. });
  237. return $form;
  238. }
  239. }