| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- <?php
- namespace App\Controllers\Api;
- use CodeIgniter\HTTP\ResponseInterface;
- class NewsController extends BaseApiController
- {
- /**
- * Get news list
- */
- public function index()
- {
- $auth = $this->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);
- }
- }
|