LinkControllser.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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\Link;
  6. use App\Models\LinkCategory;
  7. use App\Models\Subsite;
  8. use App\Models\SubsiteLink;
  9. use Encore\Admin\Controllers\HasResourceActions;
  10. use Encore\Admin\Facades\Admin as userAdmin;
  11. use Encore\Admin\Form;
  12. use Encore\Admin\Grid;
  13. use Encore\Admin\Grid\Filter;
  14. use Encore\Admin\Layout\Content;
  15. use Encore\Admin\Show;
  16. use Illuminate\Support\Facades\Cache;
  17. class LinkControllser extends Controller
  18. {
  19. use HasResourceActions;
  20. /**
  21. * Index interface.
  22. *
  23. * @param Content $content
  24. * @return Content
  25. */
  26. public function index(Content $content)
  27. {
  28. return $content
  29. ->header('友情链接')
  30. ->description(' ')
  31. ->body(view('admin.content.link')->with(['grid'=>$this->grid()]));
  32. }
  33. /**
  34. * Show interface.
  35. *
  36. * @param mixed $id
  37. * @param Content $content
  38. * @return Content
  39. */
  40. public function show($id, Content $content)
  41. {
  42. return $content
  43. ->header('友情链接')
  44. ->description(' ')
  45. ->body($this->detail($id));
  46. }
  47. /**
  48. * Edit interface.
  49. *
  50. * @param mixed $id
  51. * @param Content $content
  52. * @return Content
  53. */
  54. public function edit($id, Content $content)
  55. {
  56. return $content
  57. ->header('友情链接')
  58. ->description(' ')
  59. ->body($this->editForm($id)->edit($id));
  60. }
  61. /**
  62. * Create interface.
  63. *
  64. * @param Content $content
  65. * @return Content
  66. */
  67. public function create(Content $content)
  68. {
  69. return $content
  70. ->header('友情链接')
  71. ->description(' ')
  72. ->body($this->form());
  73. }
  74. /**
  75. * Make a grid builder.
  76. *
  77. * @return Grid
  78. */
  79. protected function grid()
  80. {
  81. $grid = new Grid(new Link);
  82. $grid->model()->when(get_subsite_id()>0, function ($query) {
  83. $query->where('subsite_id', get_subsite_id());
  84. })->orderBy('list_order', 'desc')->orderBy('created_at', 'desc');
  85. $grid->link_title('链接标题')->display(function () {
  86. $link_url = $this->link_url;
  87. $note = $this->note;
  88. $link_logo = $this->link_logo;
  89. $note_html = '';
  90. if ($note) {
  91. $note_html = '<span title='.str_replace(PHP_EOL, '', $note).'><i class="fa-commenting-o fa" style="color: #3c8dbc;"></i></span>';
  92. }
  93. $logo_html = '';
  94. if ($link_logo) {
  95. //判断是否是图片
  96. if (preg_match('/^http/', $this->link_logo)) {
  97. $logo_html = '<span style="padding-left:5px;color: #3c8dbc;" class="vtip" title="<img src=\''.$this->link_logo.'\' height=120>"><i class="fa fa-file-image-o"></i></span>';
  98. } else {
  99. $logo_html = '<span style="padding-left:5px;color: #3c8dbc;" class="vtip" title="<img src=\''.upload_asset($this->link_logo).'\' height=120>"><i class="fa fa-file-image-o"></i></span>';
  100. }
  101. }
  102. $html = '<a href='.$link_url.' style="color: #0066CC;" target="_blank">'.$this->link_title.'</a>'.' '.$note_html.$logo_html;
  103. return $html;
  104. })->width(200);
  105. $grid->link_url('链接地址')->display(function () {
  106. return '<a href='.$this->link_url.' style="color:#000000;">'.$this->link_url.'</a>';
  107. })->width(300);
  108. $states = [
  109. '1' => ['text' => '是'],
  110. '0' => ['text' => '否'],
  111. ];
  112. $grid->is_display('是否显示')->switch($states);
  113. $grid->list_order('顺序');
  114. $grid->show_category()->category_name('位置');
  115. if(get_subsite_open()){
  116. $grid->subsite_id('所属分站')->display(function () {
  117. $subsites = Cache::get('subsites_list');
  118. if (array_has($subsites, $this->subsite_id)) {
  119. return $subsites[$this->subsite_id]['sitename'];
  120. }
  121. return '';
  122. });
  123. }
  124. $grid->created_at('添加时间');
  125. //新增按钮
  126. if (userAdmin::user()->can('content_link_list_create')) {
  127. $grid->disableCreateButton(false);
  128. }
  129. //批量删除
  130. if (userAdmin::user()->can('content_link_list_delete')) {
  131. $grid->tools(function ($tools) {
  132. $tools->batch(function ($batch) {
  133. $batch->disableDelete(false);
  134. });
  135. });
  136. } else {
  137. $grid->disableRowSelector();
  138. }
  139. $grid->actions(function ($actions) {
  140. if (userAdmin::user()->can('content_link_list_edit')) {
  141. if ($actions->row['subsite_id']== get_subsite_id() || get_subsite_id()==0) {
  142. $actions->disableEdit(false);
  143. }
  144. }
  145. if (userAdmin::user()->can('content_link_list_delete')) {
  146. if ($actions->row['subsite_id']== get_subsite_id() || get_subsite_id()==0) {
  147. $actions->disableDelete(false);
  148. }
  149. }
  150. });
  151. $grid->filter(function (Filter $filter) {
  152. $filter->disableIdFilter();
  153. $subsites = Subsite::where(array('effective'=>1))->orderBy('order', 'asc')->get()->pluck('sitename', 'id')->toArray();
  154. if ($subsites) {
  155. $subsites = array('' => '不限', '0' => '总站') + $subsites;
  156. $filter->equal('subsite_id', '所属分站')->select($subsites);
  157. }
  158. $cate_option = LinkCategory::all()->pluck('category_name', 'id');
  159. $filter->equal('type_id', '链接位置')->select($cate_option);
  160. $filter->like('link_title', '链接标题');
  161. $filter->like('link_url', '链接地址');
  162. });
  163. return $grid;
  164. }
  165. /**
  166. * Make a show builder.
  167. *
  168. * @param mixed $id
  169. * @return Show
  170. */
  171. protected function detail($id)
  172. {
  173. $show = new Show(Link::findOrFail($id));
  174. $show->id('ID');
  175. $show->is_display('是否显示')->as(function ($is_display) {
  176. return $is_display?'是':'否';
  177. });
  178. $show->link_title('链接名称');
  179. $show->link_url('链接地址');
  180. $show->type_id('链接分类')->as(function ($type_id) {
  181. $cate_info = LinkCategory::where(array('id'=>$type_id))->first();
  182. return $cate_info?$cate_info->category_name:' ';
  183. });
  184. $show->list_order('显示顺序');
  185. $show->link_logo('链接LOGO')->image();
  186. if(get_subsite_open()){
  187. $show->subsite_id('所属分站')->as(function ($subsite_id) {
  188. if ($subsite_id) {
  189. $Subsite = Subsite::findOrFail($subsite_id);
  190. return $Subsite->sitename;
  191. }
  192. return '总站';
  193. });
  194. }
  195. $show->note('备注信息');
  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 Link);
  208. $display_option = [
  209. 'on' => ['value' => 1, 'text' => '是', 'color' => 'success'],
  210. 'off' => ['value' => 0, 'text' => '否', 'color' => 'danger'],
  211. ];
  212. $form->switch('is_display', '是否显示')->states($display_option)->default('1')->setMustMark();
  213. $form->text('link_title', '链接名称')->rules('required|max:50', array('required'=>'链接名称不能为空。','max'=>'链接名称长度不能大于50。'))->setWidth(4)->setMustMark();
  214. $form->url('link_url', '链接地址')->rules('required|max:250', array('required'=>'链接地址不能为空。','max'=>'链接地址长度不能大于250。'))->setWidth(4)->help('(请输入包含http://或https://的完整链接)')->setMustMark();
  215. $cate_option = LinkCategory::all()->pluck('category_name', 'id');
  216. $form->select('type_id', '链接分类')->options($cate_option)->rules('required', array('required'=>'请选择链接分类。'))->setWidth(4)->setMustMark();
  217. $form->number('list_order', '显示顺序')->min(0)->default(0)->rules('required', array('required'=>'显示顺序不能为空。'))->attribute('maxlength', '10')->help('(数字越大越靠前)')->setMustMark();
  218. $form->image('link_logo', '链接LOGO')->uniqueName()->setWidth(4)
  219. ->rules('mimes:gif,jpeg,bmp,png', array('mimes'=>'链接LOGO文件格式不正确。'))
  220. ->help('(允许格式为gif/jpg/bmp/png)');
  221. $form->url('logo_url', '或LOGO地址')->help('(请输入包含http://或https://的完整链接,上传链接LOGO或者填写LOGO地址满足一项即可)');
  222. /*if (get_subsite_id()==0) {
  223. $subsites = Subsite::where(array('effective'=>1))->orderBy('order', 'asc')->get()->pluck('sitename', 'id');
  224. if ($subsites) {
  225. $form->multipleSelect('relate_subsites', '同步分站')->options($subsites);
  226. }
  227. } else {
  228. $form->hidden('relate_subsites');
  229. }*/
  230. if (get_subsite_id()==0) {
  231. $subsites = Subsite::where(array('effective'=>1))->orderBy('order', 'asc')->get()->pluck('sitename', 'id');
  232. } else {
  233. $subsites = Subsite::where(array(array('effective','=',1),array('id','<>', get_subsite_id())))->orderBy('order', 'asc')->get()->pluck('sitename', 'id')->toArray();
  234. $subsites['0']= '总站';
  235. $subsites = collect($subsites);
  236. }
  237. if ($subsites->isNotEmpty() && get_subsite_id()==0) {
  238. $form->multipleSelect('relate_subsites', '同步分站')->options($subsites);
  239. } else {
  240. $form->hidden('relate_subsites');
  241. }
  242. $form->ignore(['relate_subsites']);
  243. $form->hidden('subsite_id')->value(get_subsite_id());
  244. //$form->ignore(['up_file']);
  245. $form->ignore(['logo_url']);
  246. $form->textarea('note', '备注信息')->rows(3)->help('(备注不在前台显示,只作为管理员的备忘标记)');
  247. $form->saved(function (Form $form) {
  248. //如果没有上传logo,判断是否
  249. $logo_url = \Illuminate\Support\Facades\Request::input('logo_url');
  250. if (!$form->model()->link_logo && $logo_url) {
  251. $form->model()->link_logo = $logo_url;
  252. $form->model()->save();
  253. }
  254. $subsites = \Illuminate\Support\Facades\Request::input('relate_subsites');
  255. if (empty($subsites)) {
  256. $subsites = [];
  257. }
  258. $subsites = array_merge(array(get_subsite_id()), $subsites);
  259. /*if (get_subsite_id() == 0) {
  260. $subsites = array_merge(array(get_subsite_id()), $subsites);
  261. } else {
  262. $subsites = array(get_subsite_id());
  263. }*/
  264. $set_data = array();
  265. foreach ($subsites as $k => $v) {
  266. if ($v !== null) {
  267. $set_data[] = array(
  268. 'link_id' => $form->model()->id,
  269. 'subsite_id'=> $v
  270. );
  271. }
  272. }
  273. SubsiteLink::insert($set_data);
  274. });
  275. $form->footer(function ($footer) {
  276. $footer->disableViewCheck();
  277. $footer->disableEditingCheck();
  278. $footer->disableCreatingCheck();
  279. $footer->disableReset();
  280. });
  281. return $form;
  282. }
  283. protected function editForm($id)
  284. {
  285. $form = new ValidateForm(new Link);
  286. //获取友情链接详情
  287. $info = Link::find($id);
  288. $display_option = [
  289. 'on' => ['value' => 1, 'text' => '是', 'color' => 'success'],
  290. 'off' => ['value' => 0, 'text' => '否', 'color' => 'danger'],
  291. ];
  292. $form->switch('is_display', '是否显示')->states($display_option)->setMustMark();
  293. $form->text('link_title', '链接名称')->rules('required|max:50', array('required'=>'链接名称不能为空。','max'=>'链接名称长度不能大于50。'))->setWidth(4)->setMustMark();
  294. $form->url('link_url', '链接地址')->rules('required|max:250', array('required'=>'链接地址不能为空。','max'=>'链接地址长度不能大于250。'))->setWidth(4)->help('(请输入包含http://或https://的完整链接)')->setMustMark();
  295. $cate_option = LinkCategory::all()->pluck('category_name', 'id');
  296. $form->select('type_id', '链接分类')->options($cate_option)->rules('required', array('required'=>'请选择链接分类。'))->setWidth(4)->setMustMark();
  297. $form->number('list_order', '显示顺序')->min(0)->default(0)->rules('required', array('required'=>'显示顺序不能为空。'))->attribute('maxlength', '10')->help('(数字越大越靠前)')->setMustMark();
  298. $form->image('link_logo', '链接LOGO')->uniqueName()->setWidth(4)->rules('mimes:gif,jpeg,bmp,png', array('mimes'=>'链接LOGO文件格式不正确。'))->help('(允许格式为gif/jpg/bmp/png)');;
  299. $form->url('logo_url', '或LOGO地址')->help('(请输入包含http://或https://的完整链接,上传链接LOGO或者填写LOGO地址满足一项即可)');
  300. /*if (get_subsite_id()==0 && $info->subsite_id == get_subsite_id()) {
  301. $subsites = Subsite::where(array('effective'=>1))->orderBy('order', 'asc')->get()->pluck('sitename', 'id');
  302. if ($subsites) {
  303. $relations = SubsiteLink::where(array('link_id'=>$id))->get()->pluck('subsite_id')->toArray();
  304. $form->multipleSelect('relate_subsites', '同步分站')->options($subsites)->default($relations);
  305. }
  306. } else {
  307. $form->hidden('relate_subsites');
  308. }*/
  309. if ($info->subsite_id==0) {
  310. $subsites = Subsite::where(array('effective'=>1))->orderBy('order', 'asc')->get()->pluck('sitename', 'id');
  311. } else {
  312. $subsites = Subsite::where(array(array('effective','=',1),array('id','<>', $info->subsite_id)))->orderBy('order', 'asc')->get()->pluck('sitename', 'id')->toArray();
  313. $subsites['0']= '总站';
  314. $subsites = collect($subsites);
  315. }
  316. if ($subsites->isNotEmpty() && get_subsite_id()==0) {
  317. $relations = SubsiteLink::where(array('link_id'=>$id))->get()->pluck('subsite_id')->toArray();
  318. $form->multipleSelect('relate_subsites', '同步分站')->options($subsites)->default($relations);
  319. } else {
  320. $form->hidden('relate_subsites');
  321. }
  322. $form->ignore(['relate_subsites']);
  323. $form->hidden('subsite_id')->value(get_subsite_id());
  324. $form->ignore(['logo_url']);
  325. $form->textarea('note', '备注信息')->rows(3)->help('(备注不在前台显示,只作为管理员的备忘标记)');
  326. $form->saved(function (Form $form) {
  327. $logo_url = \Illuminate\Support\Facades\Request::input('logo_url');
  328. if (!$form->model()->link_logo && $logo_url) {
  329. $form->model()->link_logo = $logo_url;
  330. $form->model()->save();
  331. }
  332. $subsites = \Illuminate\Support\Facades\Request::input('relate_subsites');
  333. if (empty($subsites)) {
  334. $subsites = [];
  335. }
  336. $subsites = array_merge(array($form->model()->subsite_id), $subsites);
  337. SubsiteLink::where('link_id', $form->model()->id)->delete();
  338. $set_data = array();
  339. foreach ($subsites as $k => $v) {
  340. if ($v !== null) {
  341. $set_data[] = array(
  342. 'link_id' => $form->model()->id,
  343. 'subsite_id'=> $v
  344. );
  345. }
  346. }
  347. SubsiteLink::insert($set_data);
  348. });
  349. $form->footer(function ($footer) {
  350. $footer->disableViewCheck();
  351. $footer->disableEditingCheck();
  352. $footer->disableCreatingCheck();
  353. $footer->disableReset();
  354. });
  355. $form->tools(function (Form\Tools $tools) {
  356. $tools->disableDelete();
  357. $tools->disableView();
  358. });
  359. return $form;
  360. }
  361. public function update($id)
  362. {
  363. return $this->editForm($id)->update($id);
  364. }
  365. public function destroy($id)
  366. {
  367. $ids = array();
  368. if ($id) {
  369. $ids = explode(',', $id);
  370. }
  371. if (get_subsite_id() == 0) {
  372. $filter_id = $id;
  373. } else {
  374. $seletctors = Link::where(array('subsite_id'=>get_subsite_id()))->whereIn('id', $ids)->get()->pluck('id')->toarray();
  375. if ($seletctors) {
  376. $filter_id = implode(',', $seletctors);
  377. } else {
  378. $filter_id = '';
  379. }
  380. }
  381. if ($filter_id) {
  382. if ($this->form()->destroy($filter_id)) {
  383. $data = [
  384. 'status' => true,
  385. 'message' => trans('admin.delete_succeeded'),
  386. ];
  387. } else {
  388. $data = [
  389. 'status' => false,
  390. 'message' => trans('admin.delete_failed'),
  391. ];
  392. }
  393. } else {
  394. $data = [
  395. 'status' => false,
  396. 'message' => '不能删除其它分站数据!',
  397. ];
  398. }
  399. return response()->json($data);
  400. }
  401. }