| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261 |
- <?php
- namespace App\Controllers\Api;
- use CodeIgniter\HTTP\ResponseInterface;
- class NoticeController extends BaseApiController
- {
- /**
- * Get notice list
- */
- public function index()
- {
- $auth = $this->requireAuth();
- if ($auth instanceof ResponseInterface) {
- return $auth;
- }
- $params = $this->getPaginationParams();
- $builder = $this->getDB()->table('notices');
- // 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('id', 'DESC');
- $result = $this->paginatedResponse($builder, $params);
- return $this->respondSuccess($result);
- }
- /**
- * Get single notice
- */
- public function show($id = null)
- {
- $auth = $this->requireAuth();
- if ($auth instanceof ResponseInterface) {
- return $auth;
- }
- $builder = $this->getDB()->table('notices');
- $notice = $builder->where('id', $id)->get()->getRow();
- if (!$notice) {
- return $this->respondError('공지사항을 찾을 수 없습니다.', ResponseInterface::HTTP_NOT_FOUND);
- }
- // Parse file_urls JSON
- $notice->file_urls = $this->normalizeFileUrls($notice->file_urls ?? '[]');
- // Fix image paths in content
- if (!empty($notice->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)://가 없는 경우만 도메인 추가
- $notice->content = preg_replace(
- '/src="(?!https?:\/\/)\/uploads\//',
- 'src="' . $currentDomain . '/uploads/',
- $notice->content
- );
- $notice->content = preg_replace(
- "/src='(?!https?:\/\/)\/uploads\//",
- "src='" . $currentDomain . "/uploads/",
- $notice->content
- );
- }
- // Increment view count
- $builder->where('id', $id)->set('views', 'views + 1', false)->update();
- return $this->respondSuccess($notice);
- }
- /**
- * Create notice
- */
- 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('notices');
- $builder->insert($data);
- return $this->respondSuccess(['id' => $this->getDB()->insertID()], '공지사항이 등록되었습니다.');
- }
- /**
- * Update notice
- */
- 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('notices');
- $builder->where('id', $id)->update($data);
- return $this->respondSuccess(null, '공지사항이 수정되었습니다.');
- }
- /**
- * Delete notice
- */
- public function delete($id = null)
- {
- $auth = $this->requireAuth();
- if ($auth instanceof ResponseInterface) {
- return $auth;
- }
- $builder = $this->getDB()->table('notices');
- $builder->where('id', $id)->delete();
- return $this->respondSuccess(null, '공지사항이 삭제되었습니다.');
- }
- /**
- * Normalize file_urls to always return object array
- * Handles both old format (string array) and new format (object array)
- */
- private function normalizeFileUrls($fileUrlsJson)
- {
- $fileUrls = json_decode($fileUrlsJson ?? '[]');
- if (empty($fileUrls) || !is_array($fileUrls)) {
- return [];
- }
- $normalized = [];
- foreach ($fileUrls as $item) {
- // If already an object with url property, keep it
- if (is_object($item) && isset($item->url)) {
- $normalized[] = $item;
- }
- // If it's a string (old format), convert to object
- elseif (is_string($item)) {
- $filename = basename($item);
- $normalized[] = (object)[
- 'name' => $filename,
- 'url' => $item,
- 'size' => 0 // Size unknown for migrated data
- ];
- }
- }
- return $normalized;
- }
- /**
- * Get public notice list (no auth required)
- */
- public function publicList()
- {
- $params = $this->getPaginationParams();
- $builder = $this->getDB()->table('notices');
- // Show all notices
- $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 = $this->normalizeFileUrls($item->file_urls ?? '[]');
- }
- }
- return $this->respondSuccess($result);
- }
- /**
- * Get public single notice (no auth required)
- */
- public function publicShow($id = null)
- {
- $builder = $this->getDB()->table('notices');
- $notice = $builder->where('id', $id)->get()->getRow();
- if (!$notice) {
- return $this->respondError('공지사항을 찾을 수 없습니다.', ResponseInterface::HTTP_NOT_FOUND);
- }
- // Parse file_urls JSON
- $notice->file_urls = $this->normalizeFileUrls($notice->file_urls ?? '[]');
- // Fix image paths in content
- if (!empty($notice->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)://가 없는 경우만 도메인 추가
- $notice->content = preg_replace(
- '/src="(?!https?:\/\/)\/uploads\//',
- 'src="' . $currentDomain . '/uploads/',
- $notice->content
- );
- $notice->content = preg_replace(
- "/src='(?!https?:\/\/)\/uploads\//",
- "src='" . $currentDomain . "/uploads/",
- $notice->content
- );
- }
- // Increment view count
- $builder->where('id', $id)->set('views', 'views + 1', false)->update();
- return $this->respondSuccess($notice);
- }
- }
|