PopupController.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. 'start_time' => !empty($json->start_time) ? $json->start_time : null,
  69. 'end_time' => !empty($json->end_time) ? $json->end_time : null,
  70. 'width' => $json->width ?? 400,
  71. 'height' => $json->height ?? 500,
  72. 'position_top' => $json->position_top ?? 100,
  73. 'position_left' => $json->position_left ?? 100,
  74. 'cookie_setting' => $cookieSetting,
  75. 'content' => $json->content ?? '',
  76. 'image_url' => $json->image_url ?? '',
  77. 'link_url' => $json->link_url ?? '',
  78. 'link_target' => $json->link_target ?? '_blank',
  79. 'is_active' => $json->is_active ?? 1,
  80. 'site' => $json->site ?? 'ford',
  81. 'created_at' => date('Y-m-d H:i:s')
  82. ];
  83. $builder = $this->getDB()->table('popups');
  84. $builder->insert($data);
  85. return $this->respondSuccess(['id' => $this->getDB()->insertID()], '팝업이 등록되었습니다.');
  86. }
  87. /**
  88. * Update popup
  89. */
  90. public function update($id = null)
  91. {
  92. $auth = $this->requireAuth();
  93. if ($auth instanceof ResponseInterface) {
  94. return $auth;
  95. }
  96. $json = $this->request->getJSON();
  97. // cookie_type 또는 cookie_setting 둘 다 지원
  98. $cookieSetting = $json->cookie_setting ?? $json->cookie_type ?? 'none';
  99. $data = [
  100. 'type' => $json->type ?? 'html',
  101. 'title' => $json->title ?? '',
  102. 'start_date' => $json->start_date ?? '',
  103. 'end_date' => $json->end_date ?? '',
  104. 'start_time' => !empty($json->start_time) ? $json->start_time : null,
  105. 'end_time' => !empty($json->end_time) ? $json->end_time : null,
  106. 'width' => $json->width ?? 400,
  107. 'height' => $json->height ?? 500,
  108. 'position_top' => $json->position_top ?? 100,
  109. 'position_left' => $json->position_left ?? 100,
  110. 'cookie_setting' => $cookieSetting,
  111. 'content' => $json->content ?? '',
  112. 'image_url' => $json->image_url ?? '',
  113. 'link_url' => $json->link_url ?? '',
  114. 'link_target' => $json->link_target ?? '_blank',
  115. 'is_active' => $json->is_active ?? 1,
  116. 'site' => $json->site ?? 'ford',
  117. 'updated_at' => date('Y-m-d H:i:s')
  118. ];
  119. $builder = $this->getDB()->table('popups');
  120. $builder->where('id', $id)->update($data);
  121. return $this->respondSuccess(null, '팝업이 수정되었습니다.');
  122. }
  123. /**
  124. * Delete popup
  125. */
  126. public function delete($id = null)
  127. {
  128. $auth = $this->requireAuth();
  129. if ($auth instanceof ResponseInterface) {
  130. return $auth;
  131. }
  132. $builder = $this->getDB()->table('popups');
  133. $builder->where('id', $id)->delete();
  134. return $this->respondSuccess(null, '팝업이 삭제되었습니다.');
  135. }
  136. /**
  137. * Get active popups (Public API - No Auth Required)
  138. */
  139. public function getActive()
  140. {
  141. $builder = $this->getDB()->table('popups');
  142. // 사이트 필터 (GET 파라미터로 전달)
  143. $site = $this->request->getGet('site');
  144. // 활성화되고 기간 내인 팝업만 조회
  145. // 시간 고려: start_time NULL → 00:00:00(당일 즉시), end_time NULL → 23:59:59(자정 직전)
  146. $now = date('Y-m-d H:i:s');
  147. $nowEsc = $this->getDB()->escape($now);
  148. $builder->where('is_active', 1)
  149. ->where("CONCAT(start_date, ' ', COALESCE(start_time, '00:00:00')) <= $nowEsc", null, false)
  150. ->where("CONCAT(end_date, ' ', COALESCE(end_time, '23:59:59')) >= $nowEsc", null, false);
  151. // 사이트 필터 적용
  152. if ($site && in_array($site, ['ford', 'lincoln'])) {
  153. $builder->where('site', $site);
  154. }
  155. $popups = $builder
  156. ->orderBy('id', 'DESC')
  157. ->get()
  158. ->getResult();
  159. // Fix localhost URLs
  160. $popups = $this->fixUrls($popups, ['image_url']);
  161. return $this->respondSuccess($popups);
  162. }
  163. }