ArticleController.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. namespace App\Admin\Controllers;
  3. use App\Article;
  4. use App\Articletype;
  5. use App\Http\Controllers\Controller;
  6. use Encore\Admin\Controllers\HasResourceActions;
  7. use Encore\Admin\Form;
  8. use Encore\Admin\Grid;
  9. use Encore\Admin\Layout\Content;
  10. use Encore\Admin\Show;
  11. class ArticleController extends Controller
  12. {
  13. use HasResourceActions;
  14. /**
  15. * Index interface.
  16. *
  17. * @param Content $content
  18. * @return Content
  19. */
  20. public function index(Content $content)
  21. {
  22. return $content
  23. ->header('文章管理')
  24. ->description('列表')
  25. ->body($this->grid());
  26. }
  27. /**
  28. * Show interface.
  29. *
  30. * @param mixed $id
  31. * @param Content $content
  32. * @return Content
  33. */
  34. public function show($id, Content $content)
  35. {
  36. return $content
  37. ->header('文章管理')
  38. ->description('详情')
  39. ->body($this->detail($id));
  40. }
  41. /**
  42. * Edit interface.
  43. *
  44. * @param mixed $id
  45. * @param Content $content
  46. * @return Content
  47. */
  48. public function edit($id, Content $content)
  49. {
  50. return $content
  51. ->header('文章管理')
  52. ->description('编辑')
  53. ->body($this->form()->edit($id));
  54. }
  55. /**
  56. * Create interface.
  57. *
  58. * @param Content $content
  59. * @return Content
  60. */
  61. public function create(Content $content)
  62. {
  63. return $content
  64. ->header('文章管理')
  65. ->description('创建')
  66. ->body($this->form());
  67. }
  68. /**
  69. * Make a grid builder.
  70. *
  71. * @return Grid
  72. */
  73. protected function grid()
  74. {
  75. $grid = new Grid(new Article);
  76. $grid->id('编号');
  77. $grid->name('标题');
  78. $grid->articletype_id('文章类型')->display(function ($tid) {
  79. return Articletype::where('id', $tid)->value('name') ?? 未分类;
  80. })->label();
  81. $grid->source('来源');
  82. $grid->author('作者');
  83. $grid->sort('排序')->editable();
  84. $grid->ispush("是否发布")->display(function ($push) {
  85. if ($push) {
  86. return "已发布";
  87. }
  88. return "未发布";
  89. })->label();
  90. $grid->updated_at('更新时间');
  91. $grid->filter(function ($filter) {
  92. // 去掉默认的id过滤器
  93. $filter->disableIdFilter();
  94. // 在这里添加字段过滤器
  95. $filter->like('name', '标题');
  96. });
  97. $grid->disableExport();
  98. $grid->actions(function (Grid\Displayers\Actions $actions) {
  99. $actions->disableView();
  100. });
  101. return $grid;
  102. }
  103. /**
  104. * Make a show builder.
  105. *
  106. * @param mixed $id
  107. * @return Show
  108. */
  109. protected function detail($id)
  110. {
  111. $show = new Show(Article::findOrFail($id));
  112. $show->id('编号');
  113. $show->name('标题');
  114. $show->picture();
  115. $show->content('内容')->unescape();
  116. $show->articletype_id('文章类型')->as(function ($tid) {
  117. $name = Articletype::where('id', $tid)->select('name')->first();
  118. if ($name) {
  119. return $name['name'];
  120. }
  121. return "未分类";
  122. });
  123. $show->source('来源');
  124. $show->author('作者');
  125. $show->ispush('是否发布')->as(function ($ispush) {
  126. if ($ispush) {
  127. return "已发布";
  128. }
  129. return "未发布";
  130. });
  131. $show->created_at('创建时间');
  132. $show->updated_at('更新时间');
  133. return $show;
  134. }
  135. /**
  136. * Make a form builder.
  137. *
  138. * @return Form
  139. */
  140. protected function form()
  141. {
  142. $form = new Form(new Article);
  143. $form->text('name', '文章名称')->rules('required');
  144. $form->image('picture', '文章封面')->uniqueName()->rules('required')->help('图片尺寸:172px * 115px');
  145. $form->editor('content', '内容')->rules('required');
  146. $form->select('articletype_id', '文章类型')->options('/admin/api/getArticleTypeApi')->rules('required');
  147. $form->text('author', '作者')->rules('required');
  148. $form->text('source', '来源');
  149. $form->number('sort', '排序')->help('注:数值越大越靠前');
  150. $states = [
  151. 'on' => ['value' => 1, 'text' => '已发布', 'color' => 'success'],
  152. 'off' => ['value' => 0, 'text' => '未发布', 'color' => 'danger'],
  153. ];
  154. $form->switch("ispush", "发布状态")->states($states)->default('1');
  155. $form->disableCreatingCheck();
  156. $form->disableEditingCheck();
  157. $form->disableViewCheck();
  158. return $form;
  159. }
  160. }