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 ?? '', 'start_time' => !empty($json->start_time) ? $json->start_time : null, 'end_time' => !empty($json->end_time) ? $json->end_time : null, '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 ?? '', 'link_target' => $json->link_target ?? '_blank', '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 ?? '', 'start_time' => !empty($json->start_time) ? $json->start_time : null, 'end_time' => !empty($json->end_time) ? $json->end_time : null, '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 ?? '', 'link_target' => $json->link_target ?? '_blank', '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'); // 활성화되고 기간 내인 팝업만 조회 // 시간 고려: start_time NULL → 00:00:00(당일 즉시), end_time NULL → 23:59:59(자정 직전) $now = date('Y-m-d H:i:s'); $nowEsc = $this->getDB()->escape($now); $builder->where('is_active', 1) ->where("CONCAT(start_date, ' ', COALESCE(start_time, '00:00:00')) <= $nowEsc", null, false) ->where("CONCAT(end_date, ' ', COALESCE(end_time, '23:59:59')) >= $nowEsc", null, false); // 사이트 필터 적용 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); } }