BuyHouseNewsController.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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 as userAdmin;
  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 BuyHouseNewsController 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.buy_house_news')->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()->where('type_id',56)->orderBy('list_order', 'DESC')->orderBy('created_at', '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 '<span style="'.$style.'">'.$this->title.'</span>';
  91. })->style('max-width:200px');
  92. $grid->small_img('缩略图')->display(function () {
  93. if ($this->small_img) {
  94. return '<span class="vtip" title="<img src=\''.upload_asset($this->small_img).'\' height=120>">
  95. <img class="avatar small" src="'.upload_asset($this->small_img).'" align="absmiddle" style="width: 22px;height: 22px;">
  96. </span>';
  97. } else {
  98. return '';
  99. }
  100. });
  101. $grid->robot('添加方式');
  102. $grid->show_property()->category_name('公告属性');
  103. $grid->list_order('排序');
  104. $grid->click('点击量');
  105. $grid->created_at('添加时间');
  106. //新增按钮
  107. $grid->disableCreateButton(false);
  108. //批量删除
  109. $grid->tools(function ($tools) {
  110. $tools->batch(function ($batch) {
  111. $batch->disableDelete(false);
  112. });
  113. });
  114. $grid->actions(function ($actions) {
  115. $actions->disableEdit(false);
  116. $actions->disableDelete(false);
  117. });
  118. $grid->filter(function ($filter) {
  119. $filter->disableIdFilter();
  120. $filter->equal('ID', '公告ID');
  121. $filter->like('title', '公告标题');
  122. $date3 = date('Y-m-d', strtotime("-3 day"));
  123. $date7 = date('Y-m-d', strtotime("-7 day"));
  124. $date30 = date("Y-m-d", strtotime("-1 month"));
  125. $date180 = date("Y-m-d", strtotime("-6 month"));
  126. $date360 = date("Y-m-d", strtotime("-1 year"));
  127. $date_option = array(
  128. '' => '不限',
  129. $date3 => '三天内',
  130. $date7 => '一周内',
  131. $date30 => '一月内',
  132. $date180 => '半年内',
  133. $date360 => '一年内',
  134. );
  135. $filter->where(function ($query) {
  136. $query->where('created_at', '>=', "{$this->input}");
  137. }, '添加时间', 'created_at')->radio($date_option);
  138. });
  139. return $grid;
  140. }
  141. public function getCategory(Request $request)
  142. {
  143. $parent_id = $request->input('q');
  144. $where = array('parent_id'=>$parent_id);
  145. $not_ids = ArticleCategory::categoryIds();
  146. if ($not_ids) {
  147. $cates = ArticleCategory::where($where)->WhereNotIn('id', $not_ids)->get(['id', 'category_name'])->toArray();
  148. } else {
  149. $cates = ArticleCategory::where($where)->get(['id', 'category_name'])->toArray();
  150. }
  151. $pre_arr = array('id'=>'','category_name'=>'不限');
  152. array_unshift($cates, $pre_arr);
  153. return collect($cates);
  154. }
  155. /**
  156. * Make a show builder.
  157. *
  158. * @param mixed $id
  159. * @return Show
  160. */
  161. protected function detail($id)
  162. {
  163. $show = new Show(Article::findOrFail($id));
  164. $show->id('ID');
  165. $show->title('标题');
  166. $show->tit_color('标题颜色')->as(function ($tit_color) {
  167. $html = '<i style="background-color: '.$tit_color.';padding: 5px 15px;"></i>';
  168. return $html;
  169. })->setEscape(false);
  170. $show->small_img('缩略图')->image();
  171. $show->content('内容')->setEscape(false);
  172. $show->is_display('是否显示')->as(function ($is_display) {
  173. return $is_display?'是':'否';
  174. });
  175. $show->tit_b('标题加粗')->as(function ($tit_b) {
  176. return $tit_b?'是':'否';
  177. });
  178. $show->released_at('发布日期');
  179. $show->list_order('公告排序');
  180. $show->author('作者')->as(function ($author) {
  181. return $author?$author:'&nbsp';
  182. })->setEscape(false);
  183. $show->source('来源')->as(function ($source) {
  184. return $source?$source:'&nbsp';
  185. })->setEscape(false);
  186. $show->is_url('外部链接')->as(function ($is_url) {
  187. return $is_url?$is_url:'&nbsp';
  188. })->setEscape(false);
  189. $show->seo_keywords('Keywords')->as(function ($seo_keywords) {
  190. return $seo_keywords?$seo_keywords:'&nbsp';
  191. })->setEscape(false);
  192. $show->seo_description('Description')->as(function ($seo_description) {
  193. return $seo_description?$seo_description:'&nbsp';
  194. })->setEscape(false);
  195. $show->click('点击量');
  196. $show->created_at('添加时间');
  197. $show->updated_at('更新时间');
  198. return $show;
  199. }
  200. /**
  201. * Make a form builder.
  202. *
  203. * @return Form
  204. */
  205. protected function form()
  206. {
  207. $form = new ValidateForm(new Article);
  208. $form->text('title', '公告标题')->rules('required|max:100', array('required'=>'公告标题不能为空。','max'=>'公告标题长度不能大于100。'))->setWidth(4)->setMustMark();
  209. $form->color('tit_color', '标题颜色')->rules('required|regex:/^#[a-fA-F0-9]{6}$/', array('required'=>'标题颜色不能为空。','regex'=>'标题颜色格式不正确。'))->default('#000000');
  210. $form->image('small_img', '缩略图')->uniqueName()->rules('image|mimes:jpeg,bmp,png', array('image'=>'缩略图请选择图片文件。','mimes'=>'请选择jpeg,bmp,png格式的缩略图上传。'))->setWidth(3);
  211. $form->ignore(['relate_subsites']);
  212. $form->hidden('type_id')->value(56);
  213. $form->hidden('parent_id')->value(0);
  214. $form->hidden('property_id')->value(1);
  215. $form->editor('content', '内容')->rules('required', array('required'=>'内容不能为空。'))->setMustMark();
  216. $display_option = [
  217. 'on' => ['value' => 1, 'text' => '是', 'color' => 'success'],
  218. 'off' => ['value' => 0, 'text' => '否', 'color' => 'danger'],
  219. ];
  220. $form->switch('is_display', '是否显示')->states($display_option)->default('1')->setMustMark();
  221. $form->switch('tit_b', '标题加粗')->states($display_option)->default(0)->setMustMark();
  222. $form->date('released_at', '发布日期')->format('YYYY-MM-DD')->rules('required|date', array('required'=>'发布日期不能为空。', 'date'=>'发布日期格式不正确。'))->default(date('Y-m-d', time()));
  223. $form->number('list_order', '公告排序')->min(0)->default(255)->rules('required', array('required'=>'公告排序不能为空。'))->help('(数字越大越靠前)');
  224. $form->text('author', '作者')->rules('max:20', array('max'=>'作者长度不能大于20。'))->setWidth(4);
  225. $form->text('source', '来源')->rules('max:20', array('max'=>'来源长度不能大于20。'))->setWidth(4);
  226. $form->url('is_url', '外部链接')->rules('max:250', array('max'=>'外部链接长度不能超过250。'))->help('(请输入包含http://或https://的完整链接)');
  227. $form->text('seo_keywords', 'Keywords')->placeholder('合理设置Keywords有利于搜索引擎排名')->rules('max:80', array('max:Keywords不能大于80个字符。'));
  228. $form->textarea('seo_description', 'Description')->placeholder('合理设置Description有利于搜索引擎排名')->rules('max:80', array('max'=>'Description不能大于80个字符。'));
  229. $form->saving(function (Form $form) {
  230. $form->released_at = strtotime($form->released_at);
  231. $author = $form->author?$form->author:'';
  232. $source = $form->source?$form->source:'';
  233. $is_url = $form->is_url?$form->is_url:'';
  234. $seo_keywords = $form->seo_keywords?$form->seo_keywords:'';
  235. $seo_description = $form->seo_description?$form->seo_description:'';
  236. $form->author = $author;
  237. $form->source = $source;
  238. $form->is_url = $is_url;
  239. $form->seo_keywords = $seo_keywords;
  240. $form->seo_description = $seo_description;
  241. });
  242. $form->saved(function (Form $form) {
  243. $subsites = \Illuminate\Support\Facades\Request::input('relate_subsites');
  244. if (empty($subsites)) {
  245. $subsites = [];
  246. }
  247. $subsites = array_merge(array(get_subsite_id()), $subsites);
  248. $set_data = array();
  249. foreach ($subsites as $k => $v) {
  250. if ($v !== null) {
  251. $set_data[] = array(
  252. 'article_id' => $form->model()->id,
  253. 'subsite_id'=> $v
  254. );
  255. }
  256. Cache::forget('article_index_list_home_'.$v); //清除缓存
  257. }
  258. SubsiteArticle::insert($set_data);
  259. });
  260. $form->footer(function ($footer) {
  261. $footer->disableViewCheck();
  262. $footer->disableEditingCheck();
  263. $footer->disableCreatingCheck();
  264. $footer->disableReset();
  265. });
  266. return $form;
  267. }
  268. protected function editForm($id)
  269. {
  270. $info = Article::find($id);
  271. $form = new ValidateForm(new Article);
  272. $form->text('title', '公告标题')->rules('required|max:100', array('required'=>'公告标题不能为空。','max'=>'公告标题长度不能大于100。'))->setWidth(4)->setMustMark();
  273. $form->color('tit_color', '标题颜色')->default('#000000')->rules('required|regex:/^#[a-fA-F0-9]{6}$/', array('required'=>'标题颜色不能为空。','regex'=>'标题颜色格式不正确。'));
  274. $form->image('small_img', '缩略图')->uniqueName()->rules('image|mimes:jpeg,bmp,png', array('image'=>'缩略图请选择图片文件。','mimes'=>'请选择jpeg,bmp,png格式的缩略图上传。'))->setWidth(3);
  275. $form->ignore(['relate_subsites']);
  276. $form->hidden('subsite_id')->value(get_subsite_id());
  277. $form->hidden('parent_id');
  278. $form->editor('content', '内容')->rules('required', array('required'=>'内容不能为空。'))->setMustMark();
  279. $display_option = [
  280. 'on' => ['value' => 1, 'text' => '是', 'color' => 'success'],
  281. 'off' => ['value' => 0, 'text' => '否', 'color' => 'danger'],
  282. ];
  283. $form->switch('is_display', '是否显示')->states($display_option)->setMustMark();
  284. $form->switch('tit_b', '标题加粗')->states($display_option)->setMustMark();
  285. $form->date('released_at', '发布日期')->format('YYYY-MM-DD')->rules('required|date', array('required'=>'发布日期不能为空。', 'date'=>'发布日期格式不正确。'));
  286. $form->number('list_order', '公告排序')->min(0)->default(0)->rules('required', array('required'=>'公告排序不能为空。'))->help('(数字越大越靠前)');
  287. $form->text('author', '作者')->rules('max:20', array('max'=>'作者长度不能大于20。'))->setWidth(3);
  288. $form->text('source', '来源')->rules('max:20', array('max'=>'来源长度不能大于20。'))->setWidth(3);
  289. $form->url('is_url', '外部链接')->rules('max:250', array('max'=>'外部链接长度不能超过250。'))->help('(请输入包含http://或https://的完整链接)');
  290. $form->text('seo_keywords', 'Keywords')->placeholder('合理设置Keywords有利于搜索引擎排名')->rules('max:80', array('max:Keywords不能大于80个字符。'));
  291. $form->textarea('seo_description', 'Description')->placeholder('合理设置Description有利于搜索引擎排名')->rules('max:80', array('max'=>'Description不能大于80个字符。'));
  292. $form->saving(function (Form $form) {
  293. if (!request()->has(Form\Field::FILE_DELETE_FLAG)) {
  294. $form->released_at = strtotime($form->released_at);
  295. $author = $form->author?$form->author:'';
  296. $source = $form->source?$form->source:'';
  297. $is_url = $form->is_url?$form->is_url:'';
  298. $seo_keywords = $form->seo_keywords?$form->seo_keywords:'';
  299. $seo_description = $form->seo_description?$form->seo_description:'';
  300. $form->author = $author;
  301. $form->source = $source;
  302. $form->is_url = $is_url;
  303. $form->seo_keywords = $seo_keywords;
  304. $form->seo_description = $seo_description;
  305. }
  306. });
  307. $form->saved(function (Form $form) {
  308. if (!request()->has(Form\Field::FILE_DELETE_FLAG)) {
  309. $subsites = \Illuminate\Support\Facades\Request::input('relate_subsites');
  310. if (empty($subsites)) {
  311. $subsites = [];
  312. }
  313. $subsites = array_merge(array($form->model()->subsite_id), $subsites);
  314. SubsiteArticle::where('article_id', $form->model()->id)->delete();
  315. $set_data = array();
  316. foreach ($subsites as $k => $v) {
  317. if ($v !== null) {
  318. $set_data[] = array(
  319. 'article_id' => $form->model()->id,
  320. 'subsite_id'=> $v
  321. );
  322. }
  323. Cache::forget('article_index_list_home_'.$v); //清除缓存
  324. }
  325. SubsiteArticle::insert($set_data);
  326. }
  327. });
  328. $form->footer(function ($footer) {
  329. $footer->disableViewCheck();
  330. $footer->disableEditingCheck();
  331. $footer->disableCreatingCheck();
  332. $footer->disableReset();
  333. });
  334. $form->tools(function (Form\Tools $tools) {
  335. $tools->disableDelete();
  336. $tools->disableView();
  337. });
  338. return $form;
  339. }
  340. public function update($id)
  341. {
  342. return $this->editForm($id)->update($id);
  343. }
  344. public function destroy($id)
  345. {
  346. $ids = array();
  347. if ($id) {
  348. $ids = explode(',', $id);
  349. }
  350. if (get_subsite_id() == 0) {
  351. $filter_id = $id;
  352. } else {
  353. $seletctors = Article::where(array('subsite_id'=>get_subsite_id()))->whereIn('id', $ids)->get()->pluck('id')->toarray();
  354. if ($seletctors) {
  355. $filter_id = implode(',', $seletctors);
  356. } else {
  357. $filter_id = '';
  358. }
  359. }
  360. //获取所有分站信息
  361. $subsites = Subsite::where(array('effective'=>1))->orderBy('order', 'asc')->get()->pluck('sitename', 'id')->toArray();
  362. if ($subsites) {
  363. foreach ($subsites as $key => $val) {
  364. Cache::forget('article_index_list_home_'.$key);
  365. }
  366. }
  367. if ($filter_id) {
  368. if ($this->form()->destroy($filter_id)) {
  369. $data = [
  370. 'status' => true,
  371. 'message' => trans('admin.delete_succeeded'),
  372. ];
  373. } else {
  374. $data = [
  375. 'status' => false,
  376. 'message' => trans('admin.delete_failed'),
  377. ];
  378. }
  379. } else {
  380. $data = [
  381. 'status' => false,
  382. 'message' => '不能删除其它分站数据!',
  383. ];
  384. }
  385. return response()->json($data);
  386. }
  387. }