DistMessageController.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace App\Distributor\Controllers;
  3. use App\Distributor\Repositories\DistCustomMessage;
  4. use App\Distributor\Repositories\DistMessage;
  5. use App\Distributor\Repositories\DistReadStatus;
  6. use Dcat\Admin\Admin;
  7. use Dcat\Admin\Form;
  8. use Dcat\Admin\Grid;
  9. use Dcat\Admin\Http\Controllers\AdminController;
  10. use Dcat\Admin\Layout\Content;
  11. use Dcat\Admin\Show;
  12. class DistMessageController extends AdminController
  13. {
  14. /**
  15. * page index
  16. */
  17. public function index(Content $content)
  18. {
  19. return $content
  20. ->header('列表')
  21. ->description('全部')
  22. ->breadcrumb(['text'=>'列表','url'=>''])
  23. ->body($this->grid());
  24. }
  25. /**
  26. * Make a grid builder.
  27. *
  28. * @return Grid
  29. */
  30. protected function grid()
  31. {
  32. return Grid::make(new DistCustomMessage(), function (Grid $grid) {
  33. // $grid->setActionClass(Grid\Displayers\Actions::class);
  34. // // 添加“查看”按钮
  35. // $grid->actions(function (Grid\Displayers\Actions $actions) {
  36. // $actions->append('<a href="/dist/messages/' . $actions->row->id . '" class="btn btn-sm btn-primary">View Detail</a>');
  37. // });
  38. $grid->column('created_at','time');
  39. $grid->column('title','message_title')->display(function ($title) {
  40. $isRead = $this->is_read;
  41. $style = $isRead == 0 ? 'font-weight: bold;' : '';
  42. $label = $isRead == 0 ? ' (Unread) ' : '';
  43. return "<span style='{$style}'>{$title} {$label}</span>";
  44. });
  45. $grid->column('content');
  46. $grid->column('is_read');
  47. $grid->column('custom_column', admin_trans_field('action'))->display(function () {
  48. return view('admin.grid.actions-view', ['id' => $this->id,'text'=>admin_trans('admin.view')]);
  49. });
  50. $grid->disableCreateButton();
  51. $grid->disableDeleteButton();
  52. $grid->disableEditButton();
  53. $grid->disableActions();
  54. });
  55. }
  56. /**
  57. * Make a show builder.
  58. *
  59. * @param mixed $id
  60. *
  61. * @return Show
  62. */
  63. protected function detail($id)
  64. {
  65. $this->markAsRead($id);
  66. return Show::make($id, new DistMessage(), function (Show $show) {
  67. //$show->field('id');
  68. //$show->field('sender_id');
  69. $show->field('created_at');
  70. $show->field('title');
  71. $show->field('content');
  72. // Disable all toolbar actions and buttons
  73. $show->panel()
  74. ->tools(function (Show\Tools $tools) {
  75. $tools->disableEdit(); // Disable edit button
  76. $tools->disableDelete(); // Disable delete button
  77. });
  78. });
  79. }
  80. //
  81. // /**
  82. // * Make a form builder.
  83. // *
  84. // * @return Form
  85. // */
  86. // protected function form()
  87. // {
  88. // return Form::make(new DistMessage(), function (Form $form) {
  89. // $form->display('id');
  90. // $form->text('title');
  91. // $form->text('content');
  92. // $form->text('sender_id');
  93. // $form->text('target_type');
  94. // $form->text('target_ids');
  95. //
  96. // $form->display('created_at');
  97. // $form->display('updated_at');
  98. // });
  99. // }
  100. /**
  101. * 标记消息为已读
  102. */
  103. public function markAsRead($messageId)
  104. {
  105. $userId = getDistributorId();
  106. if (DistReadStatus::markAsRead($messageId, $userId)) {
  107. return true;
  108. }
  109. return false;
  110. }
  111. }