ArticleController.php 25 KB

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