requireAuth(); if ($auth instanceof ResponseInterface) { return $auth; } $params = $this->getPaginationParams(); $builder = $this->getDB()->table('news'); // 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 === 'name') { $builder->like('name', $searchKeyword); } elseif ($searchType === 'content') { $builder->like('content', $searchKeyword); } } $builder->orderBy('is_notice', 'DESC'); $builder->orderBy('created_at', 'DESC'); $builder->orderBy('id', 'DESC'); $result = $this->paginatedResponse($builder, $params); return $this->respondSuccess($result); } /** * Get single news */ public function show($id = null) { $auth = $this->requireAuth(); if ($auth instanceof ResponseInterface) { return $auth; } $builder = $this->getDB()->table('news'); $news = $builder->where('id', $id)->get()->getRow(); if (!$news) { return $this->respondError('뉴스를 찾을 수 없습니다.', ResponseInterface::HTTP_NOT_FOUND); } // Parse file_urls JSON $news->file_urls = json_decode($news->file_urls ?? '[]'); // Fix image paths in content if (!empty($news->content)) { // YouTube iframe 경로 수정: /embed/ID -> https://www.youtube.com/embed/ID $news->content = str_replace('src="/embed/', 'src="https://www.youtube.com/embed/', $news->content); $news->content = str_replace("src='/embed/", "src='https://www.youtube.com/embed/", $news->content); // 도메인 추가: src="/uploads -> src="http://도메인/uploads // 단, 이미 http:// 또는 https://로 시작하는 URL은 제외 $protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https://' : 'http://'; $currentDomain = $protocol . ($_SERVER['HTTP_HOST'] ?? 'localhost'); // 정규표현식으로 /uploads로 시작하고 앞에 http(s)://가 없는 경우만 도메인 추가 $news->content = preg_replace( '/src="(?!https?:\/\/)\/uploads\//', 'src="' . $currentDomain . '/uploads/', $news->content ); $news->content = preg_replace( "/src='(?!https?:\/\/)\/uploads\//", "src='" . $currentDomain . "/uploads/", $news->content ); } // Increment view count $builder->where('id', $id)->set('views', 'views + 1', false)->update(); return $this->respondSuccess($news); } /** * Create news */ public function create() { $auth = $this->requireAuth(); if ($auth instanceof ResponseInterface) { return $auth; } $json = $this->request->getJSON(); $data = [ 'allow_comment' => isset($json->allow_comment) ? (int)$json->allow_comment : 0, 'is_notice' => isset($json->is_notice) ? (int)$json->is_notice : 0, 'name' => $json->name ?? '', 'email' => $json->email ?? '', 'url' => $json->url ?? '', 'title' => $json->title ?? '', 'content' => $json->content ?? '', 'file_urls' => json_encode($json->file_urls ?? []), 'views' => 0, 'created_at' => date('Y-m-d H:i:s') ]; $builder = $this->getDB()->table('news'); $builder->insert($data); return $this->respondSuccess(['id' => $this->getDB()->insertID()], '뉴스가 등록되었습니다.'); } /** * Update news */ public function update($id = null) { $auth = $this->requireAuth(); if ($auth instanceof ResponseInterface) { return $auth; } $json = $this->request->getJSON(); $data = [ 'allow_comment' => isset($json->allow_comment) ? (int)$json->allow_comment : 0, 'is_notice' => isset($json->is_notice) ? (int)$json->is_notice : 0, 'name' => $json->name ?? '', 'email' => $json->email ?? '', 'url' => $json->url ?? '', 'title' => $json->title ?? '', 'content' => $json->content ?? '', 'file_urls' => json_encode($json->file_urls ?? []), 'updated_at' => date('Y-m-d H:i:s') ]; $builder = $this->getDB()->table('news'); $builder->where('id', $id)->update($data); return $this->respondSuccess(null, '뉴스가 수정되었습니다.'); } /** * Delete news */ public function delete($id = null) { $auth = $this->requireAuth(); if ($auth instanceof ResponseInterface) { return $auth; } $builder = $this->getDB()->table('news'); $builder->where('id', $id)->delete(); return $this->respondSuccess(null, '뉴스가 삭제되었습니다.'); } /** * Get public news list (no auth required) */ public function publicList() { $params = $this->getPaginationParams(); $builder = $this->getDB()->table('news'); $builder->orderBy('is_notice', 'DESC'); $builder->orderBy('created_at', 'DESC'); $builder->orderBy('id', 'DESC'); $result = $this->paginatedResponse($builder, $params); // Parse file_urls for each item if (!empty($result['items'])) { foreach ($result['items'] as &$item) { $item->file_urls = json_decode($item->file_urls ?? '[]'); } } return $this->respondSuccess($result); } /** * Get single news (no auth required) */ public function publicShow($id = null) { $builder = $this->getDB()->table('news'); $news = $builder->where('id', $id)->get()->getRow(); if (!$news) { return $this->respondError('뉴스를 찾을 수 없습니다.', ResponseInterface::HTTP_NOT_FOUND); } // Parse file_urls JSON $news->file_urls = json_decode($news->file_urls ?? '[]'); // Fix image paths in content if (!empty($news->content)) { // YouTube iframe 경로 수정: /embed/ID -> https://www.youtube.com/embed/ID $news->content = str_replace('src="/embed/', 'src="https://www.youtube.com/embed/', $news->content); $news->content = str_replace("src='/embed/", "src='https://www.youtube.com/embed/", $news->content); // 도메인 추가: src="/uploads -> src="http://도메인/uploads // 단, 이미 http:// 또는 https://로 시작하는 URL은 제외 $protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https://' : 'http://'; $currentDomain = $protocol . ($_SERVER['HTTP_HOST'] ?? 'localhost'); // 정규표현식으로 /uploads로 시작하고 앞에 http(s)://가 없는 경우만 도메인 추가 $news->content = preg_replace( '/src="(?!https?:\/\/)\/uploads\//', 'src="' . $currentDomain . '/uploads/', $news->content ); $news->content = preg_replace( "/src='(?!https?:\/\/)\/uploads\//", "src='" . $currentDomain . "/uploads/", $news->content ); } // Increment view count $builder->where('id', $id)->set('views', 'views + 1', false)->update(); return $this->respondSuccess($news); } }