123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434 |
- <?php
- namespace App\Admin\Controllers\Content;
- use App\Admin\Extensions\Form\ValidateForm;
- use App\Http\Controllers\Controller;
- use App\Models\Link;
- use App\Models\LinkCategory;
- use App\Models\Subsite;
- use App\Models\SubsiteLink;
- use Encore\Admin\Controllers\HasResourceActions;
- use Encore\Admin\Facades\Admin as userAdmin;
- use Encore\Admin\Form;
- use Encore\Admin\Grid;
- use Encore\Admin\Grid\Filter;
- use Encore\Admin\Layout\Content;
- use Encore\Admin\Show;
- use Illuminate\Support\Facades\Cache;
- class LinkControllser extends Controller
- {
- use HasResourceActions;
- /**
- * Index interface.
- *
- * @param Content $content
- * @return Content
- */
- public function index(Content $content)
- {
- return $content
- ->header('友情链接')
- ->description(' ')
- ->body(view('admin.content.link')->with(['grid'=>$this->grid()]));
- }
- /**
- * Show interface.
- *
- * @param mixed $id
- * @param Content $content
- * @return Content
- */
- public function show($id, Content $content)
- {
- return $content
- ->header('友情链接')
- ->description(' ')
- ->body($this->detail($id));
- }
- /**
- * Edit interface.
- *
- * @param mixed $id
- * @param Content $content
- * @return Content
- */
- public function edit($id, Content $content)
- {
- return $content
- ->header('友情链接')
- ->description(' ')
- ->body($this->editForm($id)->edit($id));
- }
- /**
- * Create interface.
- *
- * @param Content $content
- * @return Content
- */
- public function create(Content $content)
- {
- return $content
- ->header('友情链接')
- ->description(' ')
- ->body($this->form());
- }
- /**
- * Make a grid builder.
- *
- * @return Grid
- */
- protected function grid()
- {
- $grid = new Grid(new Link);
- $grid->model()->when(get_subsite_id()>0, function ($query) {
- $query->where('subsite_id', get_subsite_id());
- })->orderBy('list_order', 'desc')->orderBy('created_at', 'desc');
- $grid->link_title('链接标题')->display(function () {
- $link_url = $this->link_url;
- $note = $this->note;
- $link_logo = $this->link_logo;
- $note_html = '';
- if ($note) {
- $note_html = '<span title='.str_replace(PHP_EOL, '', $note).'><i class="fa-commenting-o fa" style="color: #3c8dbc;"></i></span>';
- }
- $logo_html = '';
- if ($link_logo) {
- //判断是否是图片
- if (preg_match('/^http/', $this->link_logo)) {
- $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>';
- } else {
- $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>';
- }
- }
- $html = '<a href='.$link_url.' style="color: #0066CC;" target="_blank">'.$this->link_title.'</a>'.' '.$note_html.$logo_html;
- return $html;
- })->width(200);
- $grid->link_url('链接地址')->display(function () {
- return '<a href='.$this->link_url.' style="color:#000000;">'.$this->link_url.'</a>';
- })->width(300);
- $states = [
- '1' => ['text' => '是'],
- '0' => ['text' => '否'],
- ];
- $grid->is_display('是否显示')->switch($states);
- $grid->list_order('顺序');
- $grid->show_category()->category_name('位置');
- if(get_subsite_open()){
- $grid->subsite_id('所属分站')->display(function () {
- $subsites = Cache::get('subsites_list');
- if (array_has($subsites, $this->subsite_id)) {
- return $subsites[$this->subsite_id]['sitename'];
- }
- return '';
- });
- }
- $grid->created_at('添加时间');
- //新增按钮
- if (userAdmin::user()->can('content_link_list_create')) {
- $grid->disableCreateButton(false);
- }
- //批量删除
- if (userAdmin::user()->can('content_link_list_delete')) {
- $grid->tools(function ($tools) {
- $tools->batch(function ($batch) {
- $batch->disableDelete(false);
- });
- });
- } else {
- $grid->disableRowSelector();
- }
- $grid->actions(function ($actions) {
- if (userAdmin::user()->can('content_link_list_edit')) {
- if ($actions->row['subsite_id']== get_subsite_id() || get_subsite_id()==0) {
- $actions->disableEdit(false);
- }
- }
- if (userAdmin::user()->can('content_link_list_delete')) {
- if ($actions->row['subsite_id']== get_subsite_id() || get_subsite_id()==0) {
- $actions->disableDelete(false);
- }
- }
- });
- $grid->filter(function (Filter $filter) {
- $filter->disableIdFilter();
- $subsites = Subsite::where(array('effective'=>1))->orderBy('order', 'asc')->get()->pluck('sitename', 'id')->toArray();
- if ($subsites) {
- $subsites = array('' => '不限', '0' => '总站') + $subsites;
- $filter->equal('subsite_id', '所属分站')->select($subsites);
- }
- $cate_option = LinkCategory::all()->pluck('category_name', 'id');
- $filter->equal('type_id', '链接位置')->select($cate_option);
- $filter->like('link_title', '链接标题');
- $filter->like('link_url', '链接地址');
- });
- return $grid;
- }
- /**
- * Make a show builder.
- *
- * @param mixed $id
- * @return Show
- */
- protected function detail($id)
- {
- $show = new Show(Link::findOrFail($id));
- $show->id('ID');
- $show->is_display('是否显示')->as(function ($is_display) {
- return $is_display?'是':'否';
- });
- $show->link_title('链接名称');
- $show->link_url('链接地址');
- $show->type_id('链接分类')->as(function ($type_id) {
- $cate_info = LinkCategory::where(array('id'=>$type_id))->first();
- return $cate_info?$cate_info->category_name:' ';
- });
- $show->list_order('显示顺序');
- $show->link_logo('链接LOGO')->image();
- if(get_subsite_open()){
- $show->subsite_id('所属分站')->as(function ($subsite_id) {
- if ($subsite_id) {
- $Subsite = Subsite::findOrFail($subsite_id);
- return $Subsite->sitename;
- }
- return '总站';
- });
- }
- $show->note('备注信息');
- $show->created_at('添加时间');
- $show->updated_at('更新时间');
- return $show;
- }
- /**
- * Make a form builder.
- *
- * @return Form
- */
- protected function form()
- {
- $form = new ValidateForm(new Link);
- $display_option = [
- 'on' => ['value' => 1, 'text' => '是', 'color' => 'success'],
- 'off' => ['value' => 0, 'text' => '否', 'color' => 'danger'],
- ];
- $form->switch('is_display', '是否显示')->states($display_option)->default('1')->setMustMark();
- $form->text('link_title', '链接名称')->rules('required|max:50', array('required'=>'链接名称不能为空。','max'=>'链接名称长度不能大于50。'))->setWidth(4)->setMustMark();
- $form->url('link_url', '链接地址')->rules('required|max:250', array('required'=>'链接地址不能为空。','max'=>'链接地址长度不能大于250。'))->setWidth(4)->help('(请输入包含http://或https://的完整链接)')->setMustMark();
- $cate_option = LinkCategory::all()->pluck('category_name', 'id');
- $form->select('type_id', '链接分类')->options($cate_option)->rules('required', array('required'=>'请选择链接分类。'))->setWidth(4)->setMustMark();
- $form->number('list_order', '显示顺序')->min(0)->default(0)->rules('required', array('required'=>'显示顺序不能为空。'))->attribute('maxlength', '10')->help('(数字越大越靠前)')->setMustMark();
- $form->image('link_logo', '链接LOGO')->uniqueName()->setWidth(4)
- ->rules('mimes:gif,jpeg,bmp,png', array('mimes'=>'链接LOGO文件格式不正确。'))
- ->help('(允许格式为gif/jpg/bmp/png)');
- $form->url('logo_url', '或LOGO地址')->help('(请输入包含http://或https://的完整链接,上传链接LOGO或者填写LOGO地址满足一项即可)');
- /*if (get_subsite_id()==0) {
- $subsites = Subsite::where(array('effective'=>1))->orderBy('order', 'asc')->get()->pluck('sitename', 'id');
- if ($subsites) {
- $form->multipleSelect('relate_subsites', '同步分站')->options($subsites);
- }
- } else {
- $form->hidden('relate_subsites');
- }*/
- if (get_subsite_id()==0) {
- $subsites = Subsite::where(array('effective'=>1))->orderBy('order', 'asc')->get()->pluck('sitename', 'id');
- } else {
- $subsites = Subsite::where(array(array('effective','=',1),array('id','<>', get_subsite_id())))->orderBy('order', 'asc')->get()->pluck('sitename', 'id')->toArray();
- $subsites['0']= '总站';
- $subsites = collect($subsites);
- }
- if ($subsites->isNotEmpty() && get_subsite_id()==0) {
- $form->multipleSelect('relate_subsites', '同步分站')->options($subsites);
- } else {
- $form->hidden('relate_subsites');
- }
- $form->ignore(['relate_subsites']);
- $form->hidden('subsite_id')->value(get_subsite_id());
- //$form->ignore(['up_file']);
- $form->ignore(['logo_url']);
- $form->textarea('note', '备注信息')->rows(3)->help('(备注不在前台显示,只作为管理员的备忘标记)');
- $form->saved(function (Form $form) {
- //如果没有上传logo,判断是否
- $logo_url = \Illuminate\Support\Facades\Request::input('logo_url');
- if (!$form->model()->link_logo && $logo_url) {
- $form->model()->link_logo = $logo_url;
- $form->model()->save();
- }
- $subsites = \Illuminate\Support\Facades\Request::input('relate_subsites');
- if (empty($subsites)) {
- $subsites = [];
- }
- $subsites = array_merge(array(get_subsite_id()), $subsites);
- /*if (get_subsite_id() == 0) {
- $subsites = array_merge(array(get_subsite_id()), $subsites);
- } else {
- $subsites = array(get_subsite_id());
- }*/
- $set_data = array();
- foreach ($subsites as $k => $v) {
- if ($v !== null) {
- $set_data[] = array(
- 'link_id' => $form->model()->id,
- 'subsite_id'=> $v
- );
- }
- }
- SubsiteLink::insert($set_data);
- });
- $form->footer(function ($footer) {
- $footer->disableViewCheck();
- $footer->disableEditingCheck();
- $footer->disableCreatingCheck();
- $footer->disableReset();
- });
- return $form;
- }
- protected function editForm($id)
- {
- $form = new ValidateForm(new Link);
- //获取友情链接详情
- $info = Link::find($id);
- $display_option = [
- 'on' => ['value' => 1, 'text' => '是', 'color' => 'success'],
- 'off' => ['value' => 0, 'text' => '否', 'color' => 'danger'],
- ];
- $form->switch('is_display', '是否显示')->states($display_option)->setMustMark();
- $form->text('link_title', '链接名称')->rules('required|max:50', array('required'=>'链接名称不能为空。','max'=>'链接名称长度不能大于50。'))->setWidth(4)->setMustMark();
- $form->url('link_url', '链接地址')->rules('required|max:250', array('required'=>'链接地址不能为空。','max'=>'链接地址长度不能大于250。'))->setWidth(4)->help('(请输入包含http://或https://的完整链接)')->setMustMark();
- $cate_option = LinkCategory::all()->pluck('category_name', 'id');
- $form->select('type_id', '链接分类')->options($cate_option)->rules('required', array('required'=>'请选择链接分类。'))->setWidth(4)->setMustMark();
- $form->number('list_order', '显示顺序')->min(0)->default(0)->rules('required', array('required'=>'显示顺序不能为空。'))->attribute('maxlength', '10')->help('(数字越大越靠前)')->setMustMark();
- $form->image('link_logo', '链接LOGO')->uniqueName()->setWidth(4)->rules('mimes:gif,jpeg,bmp,png', array('mimes'=>'链接LOGO文件格式不正确。'))->help('(允许格式为gif/jpg/bmp/png)');;
- $form->url('logo_url', '或LOGO地址')->help('(请输入包含http://或https://的完整链接,上传链接LOGO或者填写LOGO地址满足一项即可)');
- /*if (get_subsite_id()==0 && $info->subsite_id == get_subsite_id()) {
- $subsites = Subsite::where(array('effective'=>1))->orderBy('order', 'asc')->get()->pluck('sitename', 'id');
- if ($subsites) {
- $relations = SubsiteLink::where(array('link_id'=>$id))->get()->pluck('subsite_id')->toArray();
- $form->multipleSelect('relate_subsites', '同步分站')->options($subsites)->default($relations);
- }
- } else {
- $form->hidden('relate_subsites');
- }*/
- if ($info->subsite_id==0) {
- $subsites = Subsite::where(array('effective'=>1))->orderBy('order', 'asc')->get()->pluck('sitename', 'id');
- } else {
- $subsites = Subsite::where(array(array('effective','=',1),array('id','<>', $info->subsite_id)))->orderBy('order', 'asc')->get()->pluck('sitename', 'id')->toArray();
- $subsites['0']= '总站';
- $subsites = collect($subsites);
- }
- if ($subsites->isNotEmpty() && get_subsite_id()==0) {
- $relations = SubsiteLink::where(array('link_id'=>$id))->get()->pluck('subsite_id')->toArray();
- $form->multipleSelect('relate_subsites', '同步分站')->options($subsites)->default($relations);
- } else {
- $form->hidden('relate_subsites');
- }
- $form->ignore(['relate_subsites']);
- $form->hidden('subsite_id')->value(get_subsite_id());
- $form->ignore(['logo_url']);
- $form->textarea('note', '备注信息')->rows(3)->help('(备注不在前台显示,只作为管理员的备忘标记)');
- $form->saved(function (Form $form) {
- $logo_url = \Illuminate\Support\Facades\Request::input('logo_url');
- if (!$form->model()->link_logo && $logo_url) {
- $form->model()->link_logo = $logo_url;
- $form->model()->save();
- }
- $subsites = \Illuminate\Support\Facades\Request::input('relate_subsites');
- if (empty($subsites)) {
- $subsites = [];
- }
- $subsites = array_merge(array($form->model()->subsite_id), $subsites);
- SubsiteLink::where('link_id', $form->model()->id)->delete();
- $set_data = array();
- foreach ($subsites as $k => $v) {
- if ($v !== null) {
- $set_data[] = array(
- 'link_id' => $form->model()->id,
- 'subsite_id'=> $v
- );
- }
- }
- SubsiteLink::insert($set_data);
- });
- $form->footer(function ($footer) {
- $footer->disableViewCheck();
- $footer->disableEditingCheck();
- $footer->disableCreatingCheck();
- $footer->disableReset();
- });
- $form->tools(function (Form\Tools $tools) {
- $tools->disableDelete();
- $tools->disableView();
- });
- return $form;
- }
- public function update($id)
- {
- return $this->editForm($id)->update($id);
- }
- public function destroy($id)
- {
- $ids = array();
- if ($id) {
- $ids = explode(',', $id);
- }
- if (get_subsite_id() == 0) {
- $filter_id = $id;
- } else {
- $seletctors = Link::where(array('subsite_id'=>get_subsite_id()))->whereIn('id', $ids)->get()->pluck('id')->toarray();
- if ($seletctors) {
- $filter_id = implode(',', $seletctors);
- } else {
- $filter_id = '';
- }
- }
- if ($filter_id) {
- if ($this->form()->destroy($filter_id)) {
- $data = [
- 'status' => true,
- 'message' => trans('admin.delete_succeeded'),
- ];
- } else {
- $data = [
- 'status' => false,
- 'message' => trans('admin.delete_failed'),
- ];
- }
- } else {
- $data = [
- 'status' => false,
- 'message' => '不能删除其它分站数据!',
- ];
- }
- return response()->json($data);
- }
- }
|