PopupController.php.bak 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. namespace App\Controllers\Api;
  3. use CodeIgniter\HTTP\ResponseInterface;
  4. class PopupController extends BaseApiController
  5. {
  6. /**
  7. * Get popup list
  8. */
  9. public function index()
  10. {
  11. $auth = $this->requireAuth();
  12. if ($auth instanceof ResponseInterface) {
  13. return $auth;
  14. }
  15. $params = $this->getPaginationParams();
  16. $builder = $this->getDB()->table('popups');
  17. // Search
  18. $searchType = $this->request->getGet('search_type');
  19. $searchKeyword = $this->request->getGet('search_keyword');
  20. if ($searchType && $searchKeyword) {
  21. if ($searchType === 'title') {
  22. $builder->like('title', $searchKeyword);
  23. } elseif ($searchType === 'status') {
  24. $builder->like('status', $searchKeyword);
  25. }
  26. }
  27. $builder->orderBy('id', 'DESC');
  28. $result = $this->paginatedResponse($builder, $params);
  29. // Fix localhost URLs in items
  30. $result['items'] = $this->fixUrls($result['items'], ['image_url']);
  31. return $this->respondSuccess($result);
  32. }
  33. /**
  34. * Get single popup
  35. */
  36. public function show($id = null)
  37. {
  38. $auth = $this->requireAuth();
  39. if ($auth instanceof ResponseInterface) {
  40. return $auth;
  41. }
  42. $builder = $this->getDB()->table('popups');
  43. $popup = $builder->where('id', $id)->get()->getRow();
  44. if (!$popup) {
  45. return $this->respondError('팝업을 찾을 수 없습니다.', ResponseInterface::HTTP_NOT_FOUND);
  46. }
  47. // Fix localhost URLs
  48. $popup = $this->fixUrls($popup, ['image_url']);
  49. return $this->respondSuccess($popup);
  50. }
  51. /**
  52. * Create popup
  53. */
  54. public function create()
  55. {
  56. $auth = $this->requireAuth();
  57. if ($auth instanceof ResponseInterface) {
  58. return $auth;
  59. }
  60. $json = $this->request->getJSON();
  61. // cookie_type 또는 cookie_setting 둘 다 지원
  62. $cookieSetting = $json->cookie_setting ?? $json->cookie_type ?? 'none';
  63. $data = [
  64. 'type' => $json->type ?? 'html',
  65. 'title' => $json->title ?? '',
  66. 'start_date' => $json->start_date ?? '',
  67. 'end_date' => $json->end_date ?? '',
  68. 'width' => $json->width ?? 400,
  69. 'height' => $json->height ?? 500,
  70. 'position_top' => $json->position_top ?? 100,
  71. 'position_left' => $json->position_left ?? 100,
  72. 'cookie_setting' => $cookieSetting,
  73. 'content' => $json->content ?? '',
  74. 'image_url' => $json->image_url ?? '',
  75. 'link_url' => $json->link_url ?? '',
  76. 'is_active' => $json->is_active ?? 1,
  77. 'site' => $json->site ?? 'ford',
  78. 'created_at' => date('Y-m-d H:i:s')
  79. ];
  80. $builder = $this->getDB()->table('popups');
  81. $builder->insert($data);
  82. return $this->respondSuccess(['id' => $this->getDB()->insertID()], '팝업이 등록되었습니다.');
  83. }
  84. /**
  85. * Update popup
  86. */
  87. public function update($id = null)
  88. {
  89. $auth = $this->requireAuth();
  90. if ($auth instanceof ResponseInterface) {
  91. return $auth;
  92. }
  93. $json = $this->request->getJSON();
  94. // cookie_type 또는 cookie_setting 둘 다 지원
  95. $cookieSetting = $json->cookie_setting ?? $json->cookie_type ?? 'none';
  96. $data = [
  97. 'type' => $json->type ?? 'html',
  98. 'title' => $json->title ?? '',
  99. 'start_date' => $json->start_date ?? '',
  100. 'end_date' => $json->end_date ?? '',
  101. 'width' => $json->width ?? 400,
  102. 'height' => $json->height ?? 500,
  103. 'position_top' => $json->position_top ?? 100,
  104. 'position_left' => $json->position_left ?? 100,
  105. 'cookie_setting' => $cookieSetting,
  106. 'content' => $json->content ?? '',
  107. 'image_url' => $json->image_url ?? '',
  108. 'link_url' => $json->link_url ?? '',
  109. 'is_active' => $json->is_active ?? 1,
  110. 'site' => $json->site ?? 'ford',
  111. 'updated_at' => date('Y-m-d H:i:s')
  112. ];
  113. $builder = $this->getDB()->table('popups');
  114. $builder->where('id', $id)->update($data);
  115. return $this->respondSuccess(null, '팝업이 수정되었습니다.');
  116. }
  117. /**
  118. * Delete popup
  119. */
  120. public function delete($id = null)
  121. {
  122. $auth = $this->requireAuth();
  123. if ($auth instanceof ResponseInterface) {
  124. return $auth;
  125. }
  126. $builder = $this->getDB()->table('popups');
  127. $builder->where('id', $id)->delete();
  128. return $this->respondSuccess(null, '팝업이 삭제되었습니다.');
  129. }
  130. /**
  131. * Get active popups (Public API - No Auth Required)
  132. */
  133. public function getActive()
  134. {
  135. $builder = $this->getDB()->table('popups');
  136. // 사이트 필터 (GET 파라미터로 전달)
  137. $site = $this->request->getGet('site');
  138. // 활성화되고 기간 내인 팝업만 조회
  139. $today = date('Y-m-d');
  140. $builder->where('is_active', 1)
  141. ->where('start_date <=', $today)
  142. ->where('end_date >=', $today);
  143. // 사이트 필터 적용
  144. if ($site && in_array($site, ['ford', 'lincoln'])) {
  145. $builder->where('site', $site);
  146. }
  147. $popups = $builder
  148. ->orderBy('id', 'DESC')
  149. ->get()
  150. ->getResult();
  151. // Fix localhost URLs
  152. $popups = $this->fixUrls($popups, ['image_url']);
  153. return $this->respondSuccess($popups);
  154. }
  155. }