ListController.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. namespace App\Http\Controllers\Admin\Sms\Book;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Validation\Rule;
  5. use App\Http\Controllers\Controller;
  6. use App\Models\SmsBook;
  7. use App\Models\DTO\SearchData;
  8. class ListController extends Controller
  9. {
  10. private SmsBook $smsBookModel;
  11. public function __construct(SmsBook $smsBook)
  12. {
  13. $this->smsBookModel = $smsBook;
  14. }
  15. /**
  16. * 주소록 관리 목록
  17. * @method GET
  18. * @see /admin/sms/send
  19. */
  20. public function index(Request $request)
  21. {
  22. $params = SearchData::fromRequest($request);
  23. $smsBookData = $this->smsBookModel->data($params);
  24. if ($smsBookData->rows > 0) {
  25. $num = listNum($smsBookData->total, $params->page, $params->perPage);
  26. foreach ($smsBookData->list as $i => $row) {
  27. $row->num = $num--;
  28. $row->bookUserListURL = route('admin.sms.book.user.index') . '?book=' . $row->id;
  29. $row->editURL = route('admin.sms.book.list.edit', $row->id);
  30. $smsBookData->list[$i] = $row;
  31. }
  32. }
  33. return view('admin.sms.book.list.index', [
  34. 'smsBookData' => $smsBookData
  35. ]);
  36. }
  37. /**
  38. * 주소록 관리 등록
  39. * @method GET
  40. * @see /admin/sms/send/create
  41. */
  42. public function create()
  43. {
  44. return view('admin.sms.book.list.write', [
  45. 'actionURL' => route('admin.sms.book.list.store'),
  46. 'smsBookData' => [],
  47. 'smsBookID' => null
  48. ]);
  49. }
  50. /**
  51. * 주소록 관리 수정
  52. * @method GET
  53. * @see /admin/sms/send/{pk}/edit
  54. */
  55. public function edit(int $smsBookID)
  56. {
  57. return view('admin.sms.book.list.write', [
  58. 'actionURL' => route('admin.sms.book.list.update', $smsBookID),
  59. 'smsBookData' => $this->smsBookModel->find($smsBookID),
  60. 'smsBookID' => $smsBookID
  61. ]);
  62. }
  63. /**
  64. * 주소록 관리 등록
  65. * @method POST
  66. * @see /admin/sms/send/create
  67. */
  68. public function store(Request $request)
  69. {
  70. $rules = [
  71. 'name' => 'required|string|max:255|unique:tb_sms_book,name',
  72. 'order' => 'required|numeric'
  73. ];
  74. $attributes = [
  75. 'name' => '이름',
  76. 'order' => '순서'
  77. ];
  78. $posts = $this->validate($request, $rules, [], $attributes);
  79. $this->smsBookModel->insert([
  80. 'name' => $posts['name'],
  81. 'order' => $posts['order'],
  82. 'created_at' => now()
  83. ]);
  84. $message = '주소록이 등록되었습니다.';
  85. return redirect()->route('admin.sms.book.list.index')->with('message', $message);
  86. }
  87. /**
  88. * 주소록 관리 수정
  89. * @method PUT
  90. * @see /admin/sms/send
  91. */
  92. public function update(Request $request)
  93. {
  94. $smsBookID = $request->post('sms_book_id');
  95. $rules = [
  96. 'sms_book_id' => 'required|numeric|exists:tb_sms_book,id',
  97. 'name' => 'required|string|max:255' . Rule::unique('tb_sms_book', 'name')->ignore($smsBookID, 'id'),
  98. 'order' => 'required|numeric'
  99. ];
  100. $attributes = [
  101. 'sms_book_id' => 'PK',
  102. 'name' => '이름',
  103. 'order' => '순서'
  104. ];
  105. $posts = $this->validate($request, $rules, [], $attributes);
  106. $this->smsBookModel->find($posts['sms_book_id'])->update([
  107. 'name' => $posts['name'],
  108. 'order' => $posts['order'],
  109. 'updated_at' => now()
  110. ]);
  111. $message = '주소록이 수정되었습니다.';
  112. return redirect()->route('admin.sms.book.list.edit', $posts['sms_book_id'])->with('message', $message);
  113. }
  114. /**
  115. * 주소록 관리 삭제
  116. * @method DELETE
  117. * @see /admin/sms/send/destroy
  118. */
  119. public function destroy(Request $request)
  120. {
  121. $chk = $request->post('chk');
  122. if ($chk) {
  123. foreach ($chk as $smsBookID) {
  124. $smsBook = $this->smsBookModel->find($smsBookID);
  125. // 연락처 그룹에 회원이 등록되면 삭제 불가
  126. $userRows = $smsBook->smsUser->count();
  127. if($userRows > 0) {
  128. return back()->withErrors("{$smsBookID}번 주소록은 삭제할 수 없습니다.")->withInput();
  129. }
  130. $smsBook->delete();
  131. }
  132. }
  133. $message = '주소록이 삭제되었습니다.';
  134. return redirect()->route('admin.sms.book.list.index')->with('message', $message);
  135. }
  136. }