ArticleController.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  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\Article;
  6. use App\Models\ArticleCategory;
  7. use App\Models\ArticleProperty;
  8. use App\Models\Subsite;
  9. use App\Models\SubsiteArticle;
  10. use Encore\Admin\Controllers\HasResourceActions;
  11. use Encore\Admin\Facades\Admin;
  12. use Encore\Admin\Form;
  13. use Encore\Admin\Grid;
  14. use Encore\Admin\Layout\Content;
  15. use Encore\Admin\Show;
  16. use Illuminate\Http\Request;
  17. use Illuminate\Support\Facades\Cache;
  18. class ArticleController extends Controller
  19. {
  20. use HasResourceActions;
  21. /**
  22. * Index interface.
  23. *
  24. * @param Content $content
  25. * @return Content
  26. */
  27. public function index(Content $content)
  28. {
  29. return $content
  30. ->header('新闻列表')
  31. ->description(' ')
  32. ->body(view('admin.content.article')->with(['grid'=>$this->grid()]));
  33. }
  34. /**
  35. * Show interface.
  36. *
  37. * @param mixed $id
  38. * @param Content $content
  39. * @return Content
  40. */
  41. public function show($id, Content $content)
  42. {
  43. return $content
  44. ->header('新闻')
  45. ->description(' ')
  46. ->body($this->detail($id));
  47. }
  48. /**
  49. * Edit interface.
  50. *
  51. * @param mixed $id
  52. * @param Content $content
  53. * @return Content
  54. */
  55. public function edit($id, Content $content)
  56. {
  57. return $content
  58. ->header('新闻')
  59. ->description(' ')
  60. ->body($this->editForm($id)->edit($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 Article);
  83. $grid->model()->when(get_subsite_id()>0, function ($query) {
  84. $query->where('subsite_id', get_subsite_id());
  85. })->when(Admin::user()->isRole('health'), function ($query) {
  86. $query->whereIn('type_id',[65,66]);
  87. })->when(Admin::user()->isRole('ic_group'), function ($query) {
  88. $query->where('type_id',67);
  89. })->orderBy('list_order', 'DESC')->orderBy('created_at', 'DESC');
  90. $grid->column('新闻标题')->display(function () {
  91. $style="color:".$this->tit_color.';';
  92. if ($this->tit_b=='1') {
  93. $style .='font-weight:bold;';
  94. }
  95. $cate = ArticleCategory::where(array('id'=>$this->type_id))->first();
  96. if ($this->parent_id=='0') {
  97. $params = '?type_id=&parent_id='.$this->type_id;
  98. } else {
  99. $params = '?type_id='.$this->type_id.'&parent_id='.$this->parent_id;
  100. }
  101. $title_url = url(admin_base_path().'/content/article/index'.$params);
  102. if ($cate) {
  103. return '<a href='.$title_url.' style="color: #006699;">['.$cate->category_name.']</a> <span style="'.$style.'">'.$this->title.'</span>';
  104. } else {
  105. return '<span style="'.$style.'">'.$this->title.'</span>';
  106. }
  107. })->width(200);
  108. $grid->small_img('缩略图')->display(function () {
  109. if ($this->small_img) {
  110. return '<span class="vtip" title="<img src=\''.upload_asset($this->small_img).'\' height=120>">
  111. <img class="avatar small" src="'.upload_asset($this->small_img).'" align="absmiddle" style="width: 22px;height: 22px;">
  112. </span>';
  113. } else {
  114. return '';
  115. }
  116. });
  117. $grid->robot('添加方式');
  118. $grid->show_property()->category_name('新闻属性');
  119. if (get_subsite_open()) {
  120. $grid->subsite_id('所属分站')->display(function () {
  121. $subsites = Cache::get('subsites_list');
  122. if (array_has($subsites, $this->subsite_id)) {
  123. return $subsites[$this->subsite_id]['sitename'];
  124. }
  125. return '';
  126. });
  127. }
  128. $grid->list_order('排序');
  129. $grid->click('点击量');
  130. $grid->created_at('添加时间');
  131. //新增按钮
  132. if (userAdmin::user()->can('content_article_list_create')) {
  133. $grid->disableCreateButton(false);
  134. }
  135. //批量删除
  136. if (userAdmin::user()->can('content_article_list_delete')) {
  137. $grid->tools(function ($tools) {
  138. $tools->batch(function ($batch) {
  139. $batch->disableDelete(false);
  140. });
  141. });
  142. } else {
  143. $grid->disableRowSelector();
  144. }
  145. $grid->actions(function ($actions) {
  146. if (userAdmin::user()->can('content_article_list_delete')) {
  147. if ($actions->row['subsite_id']== get_subsite_id() || get_subsite_id()==0) {
  148. $actions->disableEdit(false);
  149. }
  150. }
  151. if (userAdmin::user()->can('content_article_list_edit')) {
  152. if ($actions->row['subsite_id']== get_subsite_id() || get_subsite_id()==0) {
  153. $actions->disableDelete(false);
  154. }
  155. }
  156. });
  157. $grid->filter(function ($filter) {
  158. $filter->disableIdFilter();
  159. $filter->equal('ID', '新闻ID');
  160. $filter->like('title', '新闻标题');
  161. $not_ids = ArticleCategory::categoryIds();
  162. if ($not_ids) {
  163. $cate_option = ArticleCategory::where('parent_id', '0')->WhereNotIn('id', $not_ids)->get()->pluck('category_name', 'id');
  164. } else {
  165. $cate_option = ArticleCategory::where('parent_id', '0')->get()->pluck('category_name', 'id');
  166. }
  167. /*$filter->where(function ($query) {
  168. $query->where('parent_id', '=', "{$this->input}")->orWhere('type_id', '=', "{$this->input}");
  169. }, '新闻分类', 'parent_id')->select($cate_option)->load('type_id', 'get_category', 'id', 'category_name');
  170. $pid = \Illuminate\Support\Facades\Request::input('parent_id');
  171. if ($pid) {
  172. $child_cates = ArticleCategory::where(array('parent_id'=>$pid))->get(['id', 'category_name'])->toArray();
  173. } else {
  174. $child_cates = array();
  175. }
  176. $pre_arr = array('id'=>'','category_name'=>'不限');
  177. array_unshift($child_cates, $pre_arr);
  178. $filter->equal('type_id', '新闻子分类')->select(collect($child_cates)->pluck('category_name', 'id'));*/
  179. /*$menuModel = config('admin.database.menu_model');
  180. $form->select('parent_id', trans('admin.parent_id'))->options($menuModel::selectOptions());*/
  181. $article_cates = ArticleCategory::selectOptions(function ($query) {
  182. return $query->orderBy('list_order', 'desc')->orderBy('created_at', 'desc');
  183. });
  184. if (array_has($article_cates, '0')) {
  185. unset($article_cates[0]);
  186. }
  187. $filter->where(function ($query) {
  188. $query->where('parent_id', '=', "{$this->input}")->orWhere('type_id', '=', "{$this->input}");
  189. }, '新闻分类', 'parent_id')->select($article_cates);
  190. $property_option = ArticleProperty::orderBy('list_order', 'DESC')->orderBy('created_at', 'DESC')->pluck('category_name', 'id');
  191. $filter->equal('property_id', '新闻属性')->select($property_option)->default('');
  192. $subsites = Subsite::where(array('effective'=>1))->orderBy('order', 'asc')->get()->pluck('sitename', 'id')->toArray();
  193. if ($subsites) {
  194. $subsites = array('' => '不限', '0' => '总站') + $subsites;
  195. $filter->equal('subsite_id', '所属分站')->select($subsites);
  196. }
  197. $date3 = date('Y-m-d', strtotime("-3 day"));
  198. $date7 = date('Y-m-d', strtotime("-7 day"));
  199. $date30 = date("Y-m-d", strtotime("-1 month"));
  200. $date180 = date("Y-m-d", strtotime("-6 month"));
  201. $date360 = date("Y-m-d", strtotime("-1 year"));
  202. $date_option = array(
  203. '' => '不限',
  204. $date3 => '三天内',
  205. $date7 => '一周内',
  206. $date30 => '一月内',
  207. $date180 => '半年内',
  208. $date360 => '一年内',
  209. );
  210. $filter->where(function ($query) {
  211. $query->where('created_at', '>=', "{$this->input}");
  212. }, '添加时间', 'created_at')->radio($date_option);
  213. });
  214. return $grid;
  215. }
  216. public function getCategory(Request $request)
  217. {
  218. $parent_id = $request->input('q');
  219. $where = array('parent_id'=>$parent_id);
  220. $not_ids = ArticleCategory::categoryIds();
  221. if ($not_ids) {
  222. $cates = ArticleCategory::where($where)->WhereNotIn('id', $not_ids)->get(['id', 'category_name'])->toArray();
  223. } else {
  224. $cates = ArticleCategory::where($where)->get(['id', 'category_name'])->toArray();
  225. }
  226. $pre_arr = array('id'=>'','category_name'=>'不限');
  227. array_unshift($cates, $pre_arr);
  228. return collect($cates);
  229. }
  230. /**
  231. * Make a show builder.
  232. *
  233. * @param mixed $id
  234. * @return Show
  235. */
  236. protected function detail($id)
  237. {
  238. $show = new Show(Article::findOrFail($id));
  239. $show->id('ID');
  240. $show->title('标题');
  241. $show->tit_color('标题颜色')->as(function ($tit_color) {
  242. $html = '<i style="background-color: '.$tit_color.';padding: 5px 15px;"></i>';
  243. return $html;
  244. })->setEscape(false);
  245. $show->show_category("新闻分类")->as(function ($show_category) {
  246. return $show_category->category_name;
  247. });
  248. $show->small_img('缩略图')->image();
  249. if (get_subsite_open()) {
  250. $show->subsite_id('所属分站')->as(function ($subsite_id) {
  251. if ($subsite_id) {
  252. $Subsite = Subsite::findOrFail($subsite_id);
  253. return $Subsite->sitename;
  254. }
  255. return '总站';
  256. });
  257. }
  258. $show->content('内容')->setEscape(false);
  259. $show->is_display('是否显示')->as(function ($is_display) {
  260. return $is_display?'是':'否';
  261. });
  262. $show->tit_b('标题加粗')->as(function ($tit_b) {
  263. return $tit_b?'是':'否';
  264. });
  265. $show->released_at('发布日期');
  266. $show->list_order('新闻排序');
  267. $show->author('作者')->as(function ($author) {
  268. return $author?$author:'&nbsp';
  269. })->setEscape(false);
  270. $show->source('来源')->as(function ($source) {
  271. return $source?$source:'&nbsp';
  272. })->setEscape(false);
  273. $show->show_property("新闻属性")->as(function ($show_property) {
  274. return $show_property->category_name;
  275. });
  276. $show->is_url('外部链接')->as(function ($is_url) {
  277. return $is_url?$is_url:'&nbsp';
  278. })->setEscape(false);
  279. $show->seo_keywords('Keywords')->as(function ($seo_keywords) {
  280. return $seo_keywords?$seo_keywords:'&nbsp';
  281. })->setEscape(false);
  282. $show->seo_description('Description')->as(function ($seo_description) {
  283. return $seo_description?$seo_description:'&nbsp';
  284. })->setEscape(false);
  285. $show->click('点击量');
  286. $show->created_at('添加时间');
  287. $show->updated_at('更新时间');
  288. return $show;
  289. }
  290. /**
  291. * Make a form builder.
  292. *
  293. * @return Form
  294. */
  295. protected function form()
  296. {
  297. $form = new ValidateForm(new Article);
  298. $form->text('title', '新闻标题')->rules('required|max:100', array('required'=>'新闻标题不能为空。','max'=>'新闻标题长度不能大于100。'))->setWidth(4)->setMustMark();
  299. $form->color('tit_color', '标题颜色')->rules('required|regex:/^#[a-fA-F0-9]{6}$/', array('required'=>'标题颜色不能为空。','regex'=>'标题颜色格式不正确。'))->default('#000000');
  300. //$cate_option = ArticleCategory::selectOptions();
  301. $cate_option = ArticleCategory::selectOptions(function ($query) {
  302. if(Admin::user()->isRole('health')){
  303. return $query->whereIn('id',[65,66])->orderBy('list_order', 'desc')->orderBy('created_at', 'desc');
  304. }
  305. if(Admin::user()->isRole('ic_group')){
  306. return $query->whereIn('id',[67])->orderBy('list_order', 'desc')->orderBy('created_at', 'desc');
  307. }
  308. return $query->orderBy('list_order', 'desc')->orderBy('created_at', 'desc');
  309. });
  310. unset($cate_option[0]);
  311. $form->select('type_id', '新闻分类')->options($cate_option)->default(key($cate_option))->rules('required', array('required'=>'请选择新闻分类。'))->setWidth(4)->setMustMark();
  312. $form->image('small_img', '缩略图')->uniqueName()->rules('image|mimes:jpeg,bmp,png', array('image'=>'缩略图请选择图片文件。','mimes'=>'请选择jpeg,bmp,png格式的缩略图上传。'))->setWidth(3);
  313. if (get_subsite_id()==0) {
  314. $subsites = Subsite::where(array('effective'=>1))->orderBy('order', 'asc')->get()->pluck('sitename', 'id');
  315. } else {
  316. $subsites = Subsite::where(array(array('effective','=',1),array('id','<>', get_subsite_id())))->orderBy('order', 'asc')->get()->pluck('sitename', 'id')->toArray();
  317. $subsites['0']= '总站';
  318. $subsites = collect($subsites);
  319. }
  320. if ($subsites->isNotEmpty() && get_subsite_id()==0) {
  321. $form->multipleSelect('relate_subsites', '同步分站')->options($subsites);
  322. } else {
  323. $form->hidden('relate_subsites');
  324. }
  325. $form->ignore(['relate_subsites']);
  326. $form->hidden('subsite_id')->value(get_subsite_id());
  327. $form->hidden('parent_id')->value(0);
  328. $form->editor('content', '内容')->rules('required', array('required'=>'内容不能为空。'))->setMustMark();
  329. $display_option = [
  330. 'on' => ['value' => 1, 'text' => '是', 'color' => 'success'],
  331. 'off' => ['value' => 0, 'text' => '否', 'color' => 'danger'],
  332. ];
  333. $form->switch('is_display', '是否显示')->states($display_option)->default('1')->setMustMark();
  334. $form->switch('tit_b', '标题加粗')->states($display_option)->default(0)->setMustMark();
  335. $form->date('released_at', '发布日期')->format('YYYY-MM-DD')->rules('required|date', array('required'=>'发布日期不能为空。', 'date'=>'发布日期格式不正确。'))->default(date('Y-m-d', time()));
  336. $form->number('list_order', '新闻排序')->min(0)->default(255)->rules('required', array('required'=>'新闻排序不能为空。'))->help('(数字越大越靠前)');
  337. $form->text('author', '作者')->rules('max:20', array('max'=>'作者长度不能大于20。'))->setWidth(4);
  338. $form->text('source', '来源')->rules('max:20', array('max'=>'来源长度不能大于20。'))->setWidth(4);
  339. $cate_option = ArticleProperty::orderBy('list_order', 'DESC')->orderBy('created_at', 'DESC')->pluck('category_name', 'id');
  340. $form->select('property_id', '新闻属性')
  341. ->options($cate_option)
  342. ->default(key($cate_option->toArray()))
  343. ->rules('required', array('required'=>'请选择新闻属性。'))
  344. ->setWidth(4)
  345. ->setMustMark();
  346. $form->url('is_url', '外部链接')->rules('max:250', array('max'=>'外部链接长度不能超过250。'))->help('(请输入包含http://或https://的完整链接)');
  347. $form->text('seo_keywords', 'Keywords')->placeholder('合理设置Keywords有利于搜索引擎排名')->rules('max:80', array('max:Keywords不能大于80个字符。'));
  348. $form->textarea('seo_description', 'Description')->placeholder('合理设置Description有利于搜索引擎排名')->rules('max:80', array('max'=>'Description不能大于80个字符。'));
  349. $form->saving(function (Form $form) {
  350. $form->released_at = strtotime($form->released_at);
  351. $author = $form->author?$form->author:'';
  352. $source = $form->source?$form->source:'';
  353. $is_url = $form->is_url?$form->is_url:'';
  354. $seo_keywords = $form->seo_keywords?$form->seo_keywords:'';
  355. $seo_description = $form->seo_description?$form->seo_description:'';
  356. $form->author = $author;
  357. $form->source = $source;
  358. $form->is_url = $is_url;
  359. $form->seo_keywords = $seo_keywords;
  360. $form->seo_description = $seo_description;
  361. $type_info = ArticleCategory::find($form->type_id);
  362. $parent_id = $type_info->parent_id;
  363. $form->parent_id = $parent_id;
  364. });
  365. $form->saved(function (Form $form) {
  366. $subsites = \Illuminate\Support\Facades\Request::input('relate_subsites');
  367. if (empty($subsites)) {
  368. $subsites = [];
  369. }
  370. $subsites = array_merge(array(get_subsite_id()), $subsites);
  371. $set_data = array();
  372. foreach ($subsites as $k => $v) {
  373. if ($v !== null) {
  374. $set_data[] = array(
  375. 'article_id' => $form->model()->id,
  376. 'subsite_id'=> $v
  377. );
  378. }
  379. Cache::forget('article_index_list_home_'.$v); //清除缓存
  380. }
  381. SubsiteArticle::insert($set_data);
  382. });
  383. $form->footer(function ($footer) {
  384. $footer->disableViewCheck();
  385. $footer->disableEditingCheck();
  386. $footer->disableCreatingCheck();
  387. $footer->disableReset();
  388. });
  389. return $form;
  390. }
  391. protected function editForm($id)
  392. {
  393. $info = Article::find($id);
  394. $form = new ValidateForm(new Article);
  395. $form->text('title', '新闻标题')->rules('required|max:100', array('required'=>'新闻标题不能为空。','max'=>'新闻标题长度不能大于100。'))->setWidth(4)->setMustMark();
  396. $form->color('tit_color', '标题颜色')->default('#000000')->rules('required|regex:/^#[a-fA-F0-9]{6}$/', array('required'=>'标题颜色不能为空。','regex'=>'标题颜色格式不正确。'));
  397. //$cate_option = ArticleCategory::all()->pluck('category_name', 'id');
  398. //$cate_option = ArticleCategory::selectOptions();
  399. $cate_option = ArticleCategory::selectOptions(function ($query) {
  400. return $query->orderBy('list_order', 'desc')->orderBy('created_at', 'desc');
  401. });
  402. unset($cate_option[0]);
  403. $form->select('type_id', '新闻分类')->options($cate_option)->default(key($cate_option))->rules('required', array('required'=>'请选择新闻分类。'))->setWidth(4)->setMustMark();
  404. $form->image('small_img', '缩略图')->uniqueName()->rules('image|mimes:jpeg,bmp,png', array('image'=>'缩略图请选择图片文件。','mimes'=>'请选择jpeg,bmp,png格式的缩略图上传。'))->setWidth(3);
  405. if ($info->subsite_id==0) {
  406. $subsites = Subsite::where(array('effective'=>1))->orderBy('order', 'asc')->get()->pluck('sitename', 'id');
  407. } else {
  408. $subsites = Subsite::where(array(array('effective','=',1),array('id','<>', $info->subsite_id)))->orderBy('order', 'asc')->get()->pluck('sitename', 'id')->toArray();
  409. $subsites['0']= '总站';
  410. $subsites = collect($subsites);
  411. }
  412. if ($subsites->isNotEmpty() && $info->subsite_id==0) {
  413. $relations = SubsiteArticle::where(array('article_id'=>$id))->get()->pluck('subsite_id')->toArray();
  414. $form->multipleSelect('relate_subsites', '同步分站')->options($subsites)->default($relations);
  415. } else {
  416. $form->hidden('relate_subsites');
  417. }
  418. $form->ignore(['relate_subsites']);
  419. $form->hidden('subsite_id')->value(get_subsite_id());
  420. $form->hidden('parent_id');
  421. $form->editor('content', '内容')->rules('required', array('required'=>'内容不能为空。'))->setMustMark();
  422. $display_option = [
  423. 'on' => ['value' => 1, 'text' => '是', 'color' => 'success'],
  424. 'off' => ['value' => 0, 'text' => '否', 'color' => 'danger'],
  425. ];
  426. $form->switch('is_display', '是否显示')->states($display_option)->setMustMark();
  427. $form->switch('tit_b', '标题加粗')->states($display_option)->setMustMark();
  428. $form->date('released_at', '发布日期')->format('YYYY-MM-DD')->rules('required|date', array('required'=>'发布日期不能为空。', 'date'=>'发布日期格式不正确。'));
  429. $form->number('list_order', '新闻排序')->min(0)->default(0)->rules('required', array('required'=>'新闻排序不能为空。'))->help('(数字越大越靠前)');
  430. $form->text('author', '作者')->rules('max:20', array('max'=>'作者长度不能大于20。'))->setWidth(3);
  431. $form->text('source', '来源')->rules('max:20', array('max'=>'来源长度不能大于20。'))->setWidth(3);
  432. $cate_option = ArticleProperty::orderBy('list_order', 'DESC')->orderBy('created_at', 'DESC')->pluck('category_name', 'id');
  433. $form->select('property_id', '新闻属性')->options($cate_option)->default(key($cate_option->toArray()))->rules('required', array('required'=>'请选择新闻属性。'))->setWidth(3)->setMustMark();
  434. $form->url('is_url', '外部链接')->rules('max:250', array('max'=>'外部链接长度不能超过250。'))->help('(请输入包含http://或https://的完整链接)');
  435. $form->text('seo_keywords', 'Keywords')->placeholder('合理设置Keywords有利于搜索引擎排名')->rules('max:80', array('max:Keywords不能大于80个字符。'));
  436. $form->textarea('seo_description', 'Description')->placeholder('合理设置Description有利于搜索引擎排名')->rules('max:80', array('max'=>'Description不能大于80个字符。'));
  437. $form->saving(function (Form $form) {
  438. if (!request()->has(Form\Field::FILE_DELETE_FLAG)) {
  439. $form->released_at = strtotime($form->released_at);
  440. $author = $form->author?$form->author:'';
  441. $source = $form->source?$form->source:'';
  442. $is_url = $form->is_url?$form->is_url:'';
  443. $seo_keywords = $form->seo_keywords?$form->seo_keywords:'';
  444. $seo_description = $form->seo_description?$form->seo_description:'';
  445. $form->author = $author;
  446. $form->source = $source;
  447. $form->is_url = $is_url;
  448. $form->seo_keywords = $seo_keywords;
  449. $form->seo_description = $seo_description;
  450. $type_info = ArticleCategory::find($form->type_id);
  451. $parent_id = $type_info->parent_id;
  452. $form->parent_id = $parent_id;
  453. }
  454. });
  455. $form->saved(function (Form $form) {
  456. if (!request()->has(Form\Field::FILE_DELETE_FLAG)) {
  457. $subsites = \Illuminate\Support\Facades\Request::input('relate_subsites');
  458. if (empty($subsites)) {
  459. $subsites = [];
  460. }
  461. $subsites = array_merge(array($form->model()->subsite_id), $subsites);
  462. SubsiteArticle::where('article_id', $form->model()->id)->delete();
  463. $set_data = array();
  464. foreach ($subsites as $k => $v) {
  465. if ($v !== null) {
  466. $set_data[] = array(
  467. 'article_id' => $form->model()->id,
  468. 'subsite_id'=> $v
  469. );
  470. }
  471. Cache::forget('article_index_list_home_'.$v); //清除缓存
  472. }
  473. SubsiteArticle::insert($set_data);
  474. }
  475. });
  476. $form->footer(function ($footer) {
  477. $footer->disableViewCheck();
  478. $footer->disableEditingCheck();
  479. $footer->disableCreatingCheck();
  480. $footer->disableReset();
  481. });
  482. $form->tools(function (Form\Tools $tools) {
  483. $tools->disableDelete();
  484. $tools->disableView();
  485. });
  486. return $form;
  487. }
  488. public function update($id)
  489. {
  490. return $this->editForm($id)->update($id);
  491. }
  492. public function destroy($id)
  493. {
  494. $ids = array();
  495. if ($id) {
  496. $ids = explode(',', $id);
  497. }
  498. if (get_subsite_id() == 0) {
  499. $filter_id = $id;
  500. } else {
  501. $seletctors = Article::where(array('subsite_id'=>get_subsite_id()))->whereIn('id', $ids)->get()->pluck('id')->toarray();
  502. if ($seletctors) {
  503. $filter_id = implode(',', $seletctors);
  504. } else {
  505. $filter_id = '';
  506. }
  507. }
  508. //获取所有分站信息
  509. $subsites = Subsite::where(array('effective'=>1))->orderBy('order', 'asc')->get()->pluck('sitename', 'id')->toArray();
  510. if ($subsites) {
  511. foreach ($subsites as $key => $val) {
  512. Cache::forget('article_index_list_home_'.$key);
  513. }
  514. }
  515. if ($filter_id) {
  516. if ($this->form()->destroy($filter_id)) {
  517. $data = [
  518. 'status' => true,
  519. 'message' => trans('admin.delete_succeeded'),
  520. ];
  521. } else {
  522. $data = [
  523. 'status' => false,
  524. 'message' => trans('admin.delete_failed'),
  525. ];
  526. }
  527. } else {
  528. $data = [
  529. 'status' => false,
  530. 'message' => '不能删除其它分站数据!',
  531. ];
  532. }
  533. return response()->json($data);
  534. }
  535. }