EmailTemplateController.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. namespace App\Admin\Controllers\System;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\Email;
  5. use App\Models\EmailTemplate;
  6. use Encore\Admin\Controllers\HasResourceActions;
  7. use Encore\Admin\Facades\Admin;
  8. use Encore\Admin\Form;
  9. use Encore\Admin\Grid;
  10. use Encore\Admin\Layout\Content;
  11. use Encore\Admin\Show;
  12. class EmailTemplateController extends Controller
  13. {
  14. use HasResourceActions;
  15. /**
  16. * Index interface.
  17. *
  18. * @param Content $content
  19. * @return Content
  20. */
  21. public function index(Content $content)
  22. {
  23. return $content
  24. ->header('邮件模板')
  25. ->description('')
  26. ->body($this->grid());
  27. }
  28. /**
  29. * Show interface.
  30. *
  31. * @param mixed $id
  32. * @param Content $content
  33. * @return Content
  34. */
  35. public function show($id, Content $content)
  36. {
  37. return $content
  38. ->header('邮件模板')
  39. ->description('详细')
  40. ->body($this->detail($id));
  41. }
  42. /**
  43. * Edit interface.
  44. *
  45. * @param mixed $id
  46. * @param Content $content
  47. * @return Content
  48. */
  49. public function edit($id, Content $content)
  50. {
  51. return $content
  52. ->header('邮件模板')
  53. ->description('编辑')
  54. ->body($this->editForm()->edit($id));
  55. }
  56. /**
  57. * Create interface.
  58. *
  59. * @param Content $content
  60. * @return Content
  61. */
  62. public function create(Content $content)
  63. {
  64. return $content
  65. ->header('邮件模板')
  66. ->description('创建')
  67. ->body($this->createForm());
  68. }
  69. /**
  70. * Make a grid builder.
  71. *
  72. * @return Grid
  73. */
  74. protected function grid()
  75. {
  76. $grid = new Grid(new EmailTemplate);
  77. $grid->id('ID');
  78. $grid->alias('邮件规则')->width(200);
  79. $grid->name('模板名称')->width(200);
  80. $grid->title('模板标题')->width(300);
  81. $grid->created_at('添加时间');
  82. $grid->updated_at('更新时间');
  83. $grid->actions(function ($actions) use ($grid) {
  84. if (Admin::user()->can('system_email_templates_edit')) {
  85. $actions->disableEdit(false);
  86. }
  87. if (Admin::user()->can('system_email_templates_delete')) {
  88. $actions->disableDelete(false);
  89. }
  90. });
  91. if (Admin::user()->can('system_email_templates_delete')) {
  92. $grid->tools(function ($tools) {
  93. $tools->batch(function ($batch) {
  94. $batch->disableDelete(false);
  95. });
  96. });
  97. $grid->disableRowSelector(false);
  98. }
  99. if (Admin::user()->can('system_email_templates_create')) {
  100. $grid->disableCreateButton(false);
  101. }
  102. return $grid;
  103. }
  104. /**
  105. * Make a show builder.
  106. *
  107. * @param mixed $id
  108. * @return Show
  109. */
  110. protected function detail($id)
  111. {
  112. $show = new Show(EmailTemplate::findOrFail($id));
  113. $show->id('ID');
  114. $show->alias('模板别名');
  115. $show->name('模板名称');
  116. $show->title('模板标题');
  117. $show->value('模板内容');
  118. $show->created_at('添加时间');
  119. $show->updated_at('更新时间');
  120. return $show;
  121. }
  122. /**
  123. * Make a form builder.
  124. *
  125. * @return Form
  126. */
  127. protected function editForm()
  128. {
  129. $form = new Form(new EmailTemplate);
  130. $form->display('alias', '邮件规则')->setWidth(3);
  131. $form->text('name', '模板名称')->setWidth(3);
  132. $form->text('title', '模板标题')->rules([
  133. 'required',
  134. ])->setWidth(3)->setMustMark();
  135. $form->editor('value', '模板内容')->rules([
  136. 'required',
  137. ])->setWidth(8)->setMustMark();
  138. return $form;
  139. }
  140. protected function createForm()
  141. {
  142. $form = new Form(new EmailTemplate);
  143. $form->select('alias', '邮件规则')->options(
  144. Email::List()->pluck('name', 'alias')
  145. )->rules([
  146. 'required',
  147. 'unique:email_templates',
  148. ])->setWidth(3)->setMustMark();
  149. $form->text('name', '模板名称')->rules([
  150. 'required',
  151. ])->setWidth(3)->setMustMark();
  152. $form->text('title', '模板标题')->rules([
  153. 'required',
  154. ])->setWidth(3)->setMustMark();
  155. $form->editor('value', '模板内容')->rules([
  156. 'required',
  157. ])->setWidth(8)->setMustMark();
  158. return $form;
  159. }
  160. /**
  161. * Store a newly created resource in storage.
  162. *
  163. * @return mixed
  164. */
  165. public function store()
  166. {
  167. return $this->createForm()->store();
  168. }
  169. /**
  170. * Update the specified resource in storage.
  171. *
  172. * @param int $id
  173. *
  174. * @return \Illuminate\Http\Response|\Symfony\Component\HttpFoundation\Response
  175. */
  176. public function update($id)
  177. {
  178. return $this->editForm()->update($id);
  179. }
  180. /**
  181. * Make a form builder.
  182. *
  183. * @return Form
  184. */
  185. protected function form()
  186. {
  187. $form = new Form(new EmailTemplate);
  188. $form->display('ID');
  189. $form->display('添加时间');
  190. $form->display('更新时间');
  191. return $form;
  192. }
  193. }