GroupController.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace App\Http\Controllers\Admin\Page\Banner;
  3. use Illuminate\Support\Facades\Validator;
  4. use Illuminate\Http\Request;
  5. use App\Http\Controllers\Controller;
  6. use App\Models\BannerGroup;
  7. use App\Models\DTO\SearchData;
  8. use Exception;
  9. class GroupController extends Controller
  10. {
  11. private BannerGroup $bannerGroupModel;
  12. public function __construct(BannerGroup $bannerGroup)
  13. {
  14. $this->bannerGroupModel = $bannerGroup;
  15. }
  16. /**
  17. * 배너 그룹 관리
  18. * @method GET
  19. * @see /admin/page/banner/group
  20. */
  21. public function index(Request $request)
  22. {
  23. $params = SearchData::fromRequest($request);
  24. $bannerGroupData = $this->bannerGroupModel->data($params);
  25. if($bannerGroupData->rows > 0) {
  26. $num = listNum($bannerGroupData->total, $params->page, $params->perPage);
  27. foreach($bannerGroupData->list as $i => $row) {
  28. $row->num = $num++;
  29. $row->bannerCount = $row->banner->count();
  30. $bannerGroupData->list[$i] = $row;
  31. }
  32. }
  33. return view('admin.page.banner.group.index', [
  34. 'actionURL' => route('admin.page.banner.group.store'),
  35. 'bannerGroupData' => $bannerGroupData
  36. ]);
  37. }
  38. /**
  39. * 배너 그룹 저장
  40. * @method POST
  41. * @see /admin/page/banner/group
  42. */
  43. public function store(Request $request)
  44. {
  45. $rules = [
  46. 'name' => 'required|string|unique:tb_banner_group,name',
  47. ];
  48. $attributes = [
  49. 'name' => '배너 위치명',
  50. ];
  51. $posts = $this->validate($request, $rules, [], $attributes);
  52. $this->bannerGroupModel->insert([
  53. 'name' => $posts['name'],
  54. 'ip_address' => IP_ADDRESS,
  55. 'user_agent' => USER_AGENT,
  56. 'updated_user_id' => null,
  57. 'updated_at' => null,
  58. 'created_at' => now()
  59. ]);
  60. $message = '배너 그룹이 등록되었습니다.';
  61. return redirect()->route('admin.page.banner.group.index')->with('message', $message);
  62. }
  63. /**
  64. * 배너 그룹 수정
  65. * @method PUT
  66. * @see /admin/page/banner/group/{pk}
  67. */
  68. public function update(Request $request)
  69. {
  70. $chk = $request->post('chk');
  71. $name = $request->post('name');
  72. if($chk) {
  73. foreach($chk as $groupID) {
  74. $updateData = [
  75. 'name' => ($name[$groupID] ?? ''),
  76. 'ip_address' => IP_ADDRESS,
  77. 'user_agent' => USER_AGENT,
  78. 'updated_user_id' => UID
  79. ];
  80. $rules = [
  81. 'name' => 'required|string',
  82. ];
  83. $attributes = [
  84. 'name' => '위치명',
  85. ];
  86. $validator = Validator::make($updateData, $rules, [], $attributes);
  87. if($validator->fails()) {
  88. return back()->withErrors($validator)->withInput();
  89. }
  90. $this->bannerGroupModel->find($groupID)->update($updateData);
  91. }
  92. }
  93. $message = '배너 그룹 정보가 변경되었습니다.';
  94. return redirect()->route('admin.page.banner.group.index')->with('message', $message);
  95. }
  96. /**
  97. * 배너 그룹 삭제
  98. * @method DELETE
  99. * @see /admin/page/banner/group/destroy
  100. */
  101. public function destroy(Request $request)
  102. {
  103. try {
  104. $chk = $request->post('chk');
  105. if ($chk) {
  106. foreach ($chk as $bannerGroupID) {
  107. $bannerGroup = $this->bannerGroupModel->findOrNew($bannerGroupID);
  108. if(!$bannerGroup->exists)
  109. {
  110. throw new Exception($bannerGroupID . "번 배너 그룹은 존재하지 않습니다.");
  111. }
  112. if($bannerGroup->banner->count() > 0) {
  113. throw new Exception($bannerGroupID . "번 배너 그룹은 삭제할 수 없습니다.");
  114. }
  115. $bannerGroup->delete();
  116. }
  117. }
  118. $message = '배너 그룹 정보가 삭제되었습니다.';
  119. return redirect()->route('admin.page.banner.group.index')->with('message', $message);
  120. } catch (Exception $e) {
  121. return back()->withErrors($e->getMessage())->withInput();
  122. }
  123. }
  124. }