NoticeController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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\Notice;
  6. use App\Models\NoticeCategory;
  7. use App\Models\Subsite;
  8. use App\Models\SubsiteNotice;
  9. use Encore\Admin\Controllers\HasResourceActions;
  10. use Encore\Admin\Facades\Admin;
  11. use Encore\Admin\Form;
  12. use Encore\Admin\Grid;
  13. use Encore\Admin\Layout\Content;
  14. use Encore\Admin\Show;
  15. use Illuminate\Support\Facades\Cache;
  16. class NoticeController extends Controller
  17. {
  18. use HasResourceActions;
  19. /**
  20. * Index interface.
  21. *
  22. * @param Content $content
  23. * @return Content
  24. */
  25. public function index(Content $content)
  26. {
  27. return $content
  28. ->header('公告列表')
  29. ->description(' ')
  30. ->body($this->grid());
  31. }
  32. /**
  33. * Show interface.
  34. *
  35. * @param mixed $id
  36. * @param Content $content
  37. * @return Content
  38. */
  39. public function show($id, Content $content)
  40. {
  41. return $content
  42. ->header('公告')
  43. ->description(' ')
  44. ->body($this->detail($id));
  45. }
  46. /**
  47. * Edit interface.
  48. *
  49. * @param mixed $id
  50. * @param Content $content
  51. * @return Content
  52. */
  53. public function edit($id, Content $content)
  54. {
  55. return $content
  56. ->header('公告')
  57. ->description(' ')
  58. ->body($this->editForm($id)->edit($id));
  59. }
  60. /**
  61. * Create interface.
  62. *
  63. * @param Content $content
  64. * @return Content
  65. */
  66. public function create(Content $content)
  67. {
  68. return $content
  69. ->header('公告')
  70. ->description(' ')
  71. ->body($this->form());
  72. }
  73. /**
  74. * Make a grid builder.
  75. *
  76. * @return Grid
  77. */
  78. protected function grid()
  79. {
  80. $grid = new Grid(new Notice);
  81. $grid->model()->when(get_subsite_id()>0, function ($query) {
  82. $query->where('subsite_id', get_subsite_id());
  83. })->orderBy('sort', 'ASC')->orderBy('id', 'DESC');
  84. $grid->id('ID');
  85. $grid->column('标题')->display(function () {
  86. $style="color:".$this->tit_color.';';
  87. if ($this->tit_b=='1') {
  88. $style .='font-weight:bold;';
  89. }
  90. return '<a href="'.route(url_rewrite('AIX_noticeshow'),['id'=>$this->id]).'" target="_blank"><span style="'.$style.'">'.$this->title.'</span></a>';
  91. })->width(400);
  92. $grid->show_category()->category_name('所属分类');
  93. if(get_subsite_open()){
  94. $grid->subsite_id('所属分站')->display(function () {
  95. $subsites = Cache::get('subsites_list');
  96. if (array_has($subsites, $this->subsite_id)) {
  97. return $subsites[$this->subsite_id]['sitename'];
  98. }
  99. return '';
  100. });
  101. }
  102. $grid->sort('排序');
  103. $grid->click('点击量');
  104. $grid->created_at('添加时间');
  105. if (Admin::user()->can('content_noticemanager_noticle_create')) {
  106. $grid->disableCreateButton(false);
  107. }
  108. if (Admin::user()->can('content_noticemanager_noticle_delete')) {
  109. $grid->tools(function ($tools) {
  110. $tools->batch(function ($batch) {
  111. $batch->disableDelete(false);
  112. });
  113. });
  114. } else {
  115. $grid->disableRowSelector();
  116. }
  117. $grid->actions(function ($actions) {
  118. if (Admin::user()->can('content_noticemanager_noticle_edit')) {
  119. $actions->disableEdit(false);
  120. }
  121. if (Admin::user()->can('content_noticemanager_noticle_delete')) {
  122. $actions->disableDelete(false);
  123. }
  124. });
  125. $grid->filter(function ($filter) {
  126. $filter->disableIdFilter();
  127. $filter->column(1/3, function ($filter) {
  128. $cate_option = NoticeCategory::all()->pluck('category_name', 'id');
  129. $cate_option = $cate_option->toArray();
  130. $cate_option = array('' => '不限') + $cate_option;
  131. $filter->equal('type_id', '所属分类')->select($cate_option)->default('');
  132. $filter->like('title', '标题');
  133. });
  134. $filter->column(2/3, function ($filter) {
  135. $subsites = Subsite::where(array('effective'=>1))->orderBy('order', 'asc')->get()->pluck('sitename', 'id')->toArray();
  136. if ($subsites) {
  137. $subsites = array('' => '不限', '0' => '总站') + $subsites;
  138. $filter->equal('subsite_id', '所属分站')->select($subsites);
  139. }
  140. $date3 = date('Y-m-d', strtotime("-3 day"));
  141. $date7 = date('Y-m-d', strtotime("-7 day"));
  142. $date30 = date("Y-m-d", strtotime("-1 month"));
  143. $date180 = date("Y-m-d", strtotime("-6 month"));
  144. $date360 = date("Y-m-d", strtotime("-1 year"));
  145. $date_option = array(
  146. '' => '不限',
  147. $date3 => '三天内',
  148. $date7 => '一周内',
  149. $date30 => '一月内',
  150. $date180 => '半年内',
  151. $date360 => '一年内',
  152. );
  153. $filter->where(function ($query) {
  154. $query->where('created_at', '>=', "{$this->input}");}, '添加时间', 'created_at')->radio($date_option);
  155. });
  156. });
  157. return $grid;
  158. }
  159. /**
  160. * Make a show builder.
  161. *
  162. * @param mixed $id
  163. * @return Show
  164. */
  165. protected function detail($id)
  166. {
  167. $show = new Show(Notice::findOrFail($id));
  168. $show->id('ID');
  169. $show->title('标题')->as(function ($title) {
  170. return $title;
  171. });
  172. $show->tit_color('标题颜色')->as(function ($tit_color) {
  173. $html = '<i style="background-color: '.$tit_color.';padding: 5px 15px;"></i>';
  174. return $html;
  175. })->setEscape(false);
  176. $show->show_category("所属分类")->as(function ($show_category) {
  177. return $show_category->category_name;
  178. });
  179. if(get_subsite_open()){
  180. $show->subsite_id('所属分站')->as(function ($subsite_id) {
  181. if ($subsite_id) {
  182. $Subsite = Subsite::findOrFail($subsite_id);
  183. return $Subsite->sitename;
  184. }
  185. return '总站';
  186. });
  187. }
  188. $show->content('内容')->setEscape(false);
  189. $show->is_display('是否显示')->as(function ($is_display) {
  190. return $is_display?'是':'否';
  191. });
  192. $show->tit_b('是否加粗')->as(function ($tit_b) {
  193. return $tit_b?'是':'否';
  194. });
  195. $show->sort('排序');
  196. $show->is_url('外部链接')->as(function ($is_url) {
  197. return $is_url?$is_url:'&nbsp;';
  198. });
  199. $show->seo_keywords('Keywords')->as(function ($seo_keywords) {
  200. return $seo_keywords?$seo_keywords:'&nbsp;';
  201. });
  202. $show->seo_description('Description')->as(function ($seo_description) {
  203. return $seo_description?$seo_description:'&nbsp;';
  204. });
  205. $show->created_at('添加时间');
  206. $show->updated_at('更新时间');
  207. return $show;
  208. }
  209. /**
  210. * Make a form builder.
  211. *
  212. * @return Form
  213. */
  214. protected function form()
  215. {
  216. $form = new ValidateForm(new Notice);
  217. $form->text('title', '标题')->rules('required|max:100', array('required'=>'标题不能为空。', 'max'=>'页面名称长度不能大于100个字符。'))->setWidth(4)->setMustMark();
  218. $form->color('tit_color', '标题颜色')->default('#000000');
  219. $cate_option = NoticeCategory::all()->pluck('category_name', 'id');
  220. $form->select('type_id', '所属分类')->options($cate_option)->default(key($cate_option->toArray()))->setWidth(4)->setMustMark();
  221. if (get_subsite_open()) {
  222. if (get_subsite_id()==0) {
  223. $subsites = Subsite::where(array('effective'=>1))->orderBy('order', 'asc')->get()->pluck('sitename', 'id');
  224. } else {
  225. $subsites = Subsite::where(array(array('effective','=',1),array('id','<>', get_subsite_id())))->orderBy('order', 'asc')->get()->pluck('sitename', 'id')->toArray();
  226. $subsites['0']= '总站';
  227. $subsites = collect($subsites);
  228. }
  229. if ($subsites->isNotEmpty() && get_subsite_id()==0) {
  230. $form->multipleSelect('relate_subsites', '同步分站')->options($subsites);
  231. } else {
  232. $form->hidden('relate_subsites');
  233. }
  234. }
  235. $form->hidden('subsite_id')->value(get_subsite_id());
  236. $form->editor('content', '内容')->rules('required', array('required'=>'内容不能为空。'))->setMustMark();
  237. $display_option = [
  238. 'on' => ['value' => 1, 'text' => '是', 'color' => 'success'],
  239. 'off' => ['value' => 0, 'text' => '否', 'color' => 'danger'],
  240. ];
  241. $form->switch('is_display', '是否显示')->states($display_option)->default('1')->setMustMark();
  242. $form->switch('tit_b', '标题加粗')->states($display_option)->default(0)->setMustMark();
  243. $form->number('sort', '排序')->min(1)->max(255)->default(255)->help('(数字越小越靠前)')->setMustMark();
  244. $form->url('is_url', '外部链接')->help('(请输入包含http://或https://的完整链接)');
  245. $form->text('seo_keywords', 'Keywords')
  246. ->placeholder('合理设置Keywords有利于搜索引擎排名')
  247. ->rules('max:80', array('max:Keywords不能大于80个字符。'));
  248. $form->textarea('seo_description', 'Description')
  249. ->placeholder('合理设置Description有利于搜索引擎排名')
  250. ->rules('max:80', array('max'=>'Description不能大于80个字符。'));
  251. $form->ignore(['relate_subsites']);
  252. $form->saved(function (Form $form) {
  253. $subsites = \Illuminate\Support\Facades\Request::input('relate_subsites');
  254. if (empty($subsites)) {
  255. $subsites = [];
  256. }
  257. $subsites = array_merge(array(get_subsite_id()), $subsites);
  258. $set_data = array();
  259. foreach ($subsites as $k => $v) {
  260. if ($v !== null) {
  261. $set_data[] = array(
  262. 'notice_id' => $form->model()->id,
  263. 'subsite_id'=> $v
  264. );
  265. }
  266. }
  267. SubsiteNotice::insert($set_data);
  268. });
  269. return $form;
  270. }
  271. protected function editForm($id)
  272. {
  273. $form = new Form(new Notice);
  274. $info = Notice::find($id);
  275. $form->text('title', '标题')
  276. ->rules('required|max:100', array('required'=>'标题不能为空。', 'max'=>'页面名称长度不能大于100个字符。'))->setWidth(4)->setMustMark();
  277. $form->color('tit_color', '标题颜色');
  278. $form->radio('type_id', '所属分类')->options(NoticeCategory::all()->pluck('category_name', 'id'))->setMustMark();
  279. if ($info->subsite_id==0) {
  280. $subsites = Subsite::where(array('effective'=>1))->orderBy('order', 'asc')->get()->pluck('sitename', 'id');
  281. } else {
  282. $subsites = Subsite::where(array(array('effective','=',1),array('id','<>', $info->subsite_id)))->orderBy('order', 'asc')->get()->pluck('sitename', 'id')->toArray();
  283. $subsites['0']= '总站';
  284. $subsites = collect($subsites);
  285. }
  286. if ($subsites->isNotEmpty() && get_subsite_id()==0) {
  287. $relations = SubsiteNotice::where(array('notice_id'=>$id))->get()->pluck('subsite_id')->toArray();
  288. $form->multipleSelect('relate_subsites', '同步分站')->options($subsites)->default($relations);
  289. } else {
  290. $form->hidden('relate_subsites');
  291. }
  292. $form->hidden('subsite_id')->value(get_subsite_id());
  293. $form->editor('content', '内容')->rules('required', array('required'=>'内容不能为空。'))->setMustMark();
  294. $display_option = [
  295. 'on' => ['value' => 1, 'text' => '是', 'color' => 'success'],
  296. 'off' => ['value' => 0, 'text' => '否', 'color' => 'danger'],
  297. ];
  298. $form->switch('is_display', '是否显示')->states($display_option)->setMustMark();
  299. $form->switch('tit_b', '标题加粗')->states($display_option)->setMustMark();
  300. $form->number('sort', '排序')->min(1)->max(255)->help('(数字越小越靠前)')->setMustMark();
  301. $form->url('is_url', '外部链接')->help('(请输入包含http://或https://的完整链接)');
  302. $form->text('seo_keywords', 'Keywords')
  303. ->placeholder('合理设置Keywords有利于搜索引擎排名')
  304. ->rules('max:80', array('max:Keywords不能大于80个字符。'));
  305. $form->textarea('seo_description', 'Description')
  306. ->placeholder('合理设置Description有利于搜索引擎排名')
  307. ->rules('max:80', array('max'=>'Description不能大于80个字符。'));
  308. $form->ignore(['relate_subsites']);
  309. $form->saved(function (Form $form) {
  310. $subsites = \Illuminate\Support\Facades\Request::input('relate_subsites');
  311. if (empty($subsites)) {
  312. $subsites = [];
  313. }
  314. $subsites = array_merge(array($form->model()->subsite_id), $subsites);
  315. SubsiteNotice::where('notice_id', $form->model()->id)->delete();
  316. $set_data = array();
  317. foreach ($subsites as $k => $v) {
  318. if ($v !== null) {
  319. $set_data[] = array(
  320. 'notice_id' => $form->model()->id,
  321. 'subsite_id'=> $v
  322. );
  323. }
  324. }
  325. SubsiteNotice::insert($set_data);
  326. });
  327. $form->footer(function ($footer) {
  328. $footer->disableViewCheck();
  329. $footer->disableEditingCheck();
  330. $footer->disableCreatingCheck();
  331. $footer->disableReset();
  332. });
  333. $form->tools(function (Form\Tools $tools) {
  334. $tools->disableDelete();
  335. $tools->disableView();
  336. });
  337. return $form;
  338. }
  339. public function update($id)
  340. {
  341. return $this->editForm($id)->update($id);
  342. }
  343. public function destroy($id)
  344. {
  345. $ids = array();
  346. if ($id) {
  347. $ids = explode(',', $id);
  348. }
  349. if (get_subsite_id() == 0) {
  350. $filter_id = $id;
  351. } else {
  352. $seletctors = Notice::where(array('subsite_id'=>get_subsite_id()))->whereIn('id', $ids)->get()->pluck('id')->toarray();
  353. if ($seletctors) {
  354. $filter_id = implode(',', $seletctors);
  355. } else {
  356. $filter_id = '';
  357. }
  358. }
  359. if ($filter_id) {
  360. if ($this->form()->destroy($filter_id)) {
  361. $data = [
  362. 'status' => true,
  363. 'message' => trans('admin.delete_succeeded'),
  364. ];
  365. } else {
  366. $data = [
  367. 'status' => false,
  368. 'message' => trans('admin.delete_failed'),
  369. ];
  370. }
  371. } else {
  372. $data = [
  373. 'status' => false,
  374. 'message' => '不能删除其它分站数据!',
  375. ];
  376. }
  377. return response()->json($data);
  378. }
  379. }