| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- <?php
- namespace App\Controllers\Api;
- use CodeIgniter\HTTP\ResponseInterface;
- class PopupController extends BaseApiController
- {
- /**
- * Get popup list
- */
- public function index()
- {
- $auth = $this->requireAuth();
- if ($auth instanceof ResponseInterface) {
- return $auth;
- }
- $params = $this->getPaginationParams();
- $builder = $this->getDB()->table('popups');
- // Search
- $searchType = $this->request->getGet('search_type');
- $searchKeyword = $this->request->getGet('search_keyword');
- if ($searchType && $searchKeyword) {
- if ($searchType === 'title') {
- $builder->like('title', $searchKeyword);
- } elseif ($searchType === 'status') {
- $builder->like('status', $searchKeyword);
- }
- }
- $builder->orderBy('id', 'DESC');
- $result = $this->paginatedResponse($builder, $params);
- // Fix localhost URLs in items
- $result['items'] = $this->fixUrls($result['items'], ['image_url']);
- return $this->respondSuccess($result);
- }
- /**
- * Get single popup
- */
- public function show($id = null)
- {
- $auth = $this->requireAuth();
- if ($auth instanceof ResponseInterface) {
- return $auth;
- }
- $builder = $this->getDB()->table('popups');
- $popup = $builder->where('id', $id)->get()->getRow();
- if (!$popup) {
- return $this->respondError('팝업을 찾을 수 없습니다.', ResponseInterface::HTTP_NOT_FOUND);
- }
- // Fix localhost URLs
- $popup = $this->fixUrls($popup, ['image_url']);
- return $this->respondSuccess($popup);
- }
- /**
- * Create popup
- */
- public function create()
- {
- $auth = $this->requireAuth();
- if ($auth instanceof ResponseInterface) {
- return $auth;
- }
- $json = $this->request->getJSON();
- // cookie_type 또는 cookie_setting 둘 다 지원
- $cookieSetting = $json->cookie_setting ?? $json->cookie_type ?? 'none';
- $data = [
- 'type' => $json->type ?? 'html',
- 'title' => $json->title ?? '',
- 'start_date' => $json->start_date ?? '',
- 'end_date' => $json->end_date ?? '',
- 'width' => $json->width ?? 400,
- 'height' => $json->height ?? 500,
- 'position_top' => $json->position_top ?? 100,
- 'position_left' => $json->position_left ?? 100,
- 'cookie_setting' => $cookieSetting,
- 'content' => $json->content ?? '',
- 'image_url' => $json->image_url ?? '',
- 'link_url' => $json->link_url ?? '',
- 'is_active' => $json->is_active ?? 1,
- 'site' => $json->site ?? 'ford',
- 'created_at' => date('Y-m-d H:i:s')
- ];
- $builder = $this->getDB()->table('popups');
- $builder->insert($data);
- return $this->respondSuccess(['id' => $this->getDB()->insertID()], '팝업이 등록되었습니다.');
- }
- /**
- * Update popup
- */
- public function update($id = null)
- {
- $auth = $this->requireAuth();
- if ($auth instanceof ResponseInterface) {
- return $auth;
- }
- $json = $this->request->getJSON();
- // cookie_type 또는 cookie_setting 둘 다 지원
- $cookieSetting = $json->cookie_setting ?? $json->cookie_type ?? 'none';
- $data = [
- 'type' => $json->type ?? 'html',
- 'title' => $json->title ?? '',
- 'start_date' => $json->start_date ?? '',
- 'end_date' => $json->end_date ?? '',
- 'width' => $json->width ?? 400,
- 'height' => $json->height ?? 500,
- 'position_top' => $json->position_top ?? 100,
- 'position_left' => $json->position_left ?? 100,
- 'cookie_setting' => $cookieSetting,
- 'content' => $json->content ?? '',
- 'image_url' => $json->image_url ?? '',
- 'link_url' => $json->link_url ?? '',
- 'is_active' => $json->is_active ?? 1,
- 'site' => $json->site ?? 'ford',
- 'updated_at' => date('Y-m-d H:i:s')
- ];
- $builder = $this->getDB()->table('popups');
- $builder->where('id', $id)->update($data);
- return $this->respondSuccess(null, '팝업이 수정되었습니다.');
- }
- /**
- * Delete popup
- */
- public function delete($id = null)
- {
- $auth = $this->requireAuth();
- if ($auth instanceof ResponseInterface) {
- return $auth;
- }
- $builder = $this->getDB()->table('popups');
- $builder->where('id', $id)->delete();
- return $this->respondSuccess(null, '팝업이 삭제되었습니다.');
- }
- /**
- * Get active popups (Public API - No Auth Required)
- */
- public function getActive()
- {
- $builder = $this->getDB()->table('popups');
- // 사이트 필터 (GET 파라미터로 전달)
- $site = $this->request->getGet('site');
- // 활성화되고 기간 내인 팝업만 조회
- $today = date('Y-m-d');
- $builder->where('is_active', 1)
- ->where('start_date <=', $today)
- ->where('end_date >=', $today);
- // 사이트 필터 적용
- if ($site && in_array($site, ['ford', 'lincoln'])) {
- $builder->where('site', $site);
- }
- $popups = $builder
- ->orderBy('id', 'DESC')
- ->get()
- ->getResult();
- // Fix localhost URLs
- $popups = $this->fixUrls($popups, ['image_url']);
- return $this->respondSuccess($popups);
- }
- }
|