requireAuth(); if ($auth instanceof ResponseInterface) { return $auth; } $params = $this->getPaginationParams(); $builder = $this->getDB()->table('events'); // 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 event */ public function show($id = null) { $auth = $this->requireAuth(); if ($auth instanceof ResponseInterface) { return $auth; } $builder = $this->getDB()->table('events'); $event = $builder->where('id', $id)->get()->getRow(); if (!$event) { return $this->respondError('이벤트를 찾을 수 없습니다.', ResponseInterface::HTTP_NOT_FOUND); } // Parse file_urls JSON $event->file_urls = $this->normalizeFileUrls($event->file_urls ?? '[]'); // Fix image paths in content: /event/image.jpg -> /uploads/bbs/event/image.jpg if (!empty($event->content)) { // src="/event/ 형태를 src="/uploads/bbs/event/ 로 변경 $event->content = str_replace('src="/event/', 'src="/uploads/bbs/event/', $event->content); // src='/event/ 형태도 처리 $event->content = str_replace("src='/event/", "src='/uploads/bbs/event/", $event->content); // YouTube iframe 경로 수정: /embed/ID -> https://www.youtube.com/embed/ID $event->content = str_replace('src="/embed/', 'src="https://www.youtube.com/embed/', $event->content); $event->content = str_replace("src='/embed/", "src='https://www.youtube.com/embed/", $event->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)://가 없는 경우만 도메인 추가 $event->content = preg_replace( '/src="(?!https?:\/\/)\/uploads\//', 'src="' . $currentDomain . '/uploads/', $event->content ); $event->content = preg_replace( "/src='(?!https?:\/\/)\/uploads\//", "src='" . $currentDomain . "/uploads/", $event->content ); } // Increment view count $builder->where('id', $id)->set('views', 'views + 1', false)->update(); return $this->respondSuccess($event); } /** * Create event */ public function create() { $auth = $this->requireAuth(); if ($auth instanceof ResponseInterface) { return $auth; } $json = $this->request->getJSON(); $data = [ 'site' => $json->site ?? 'common', 'category' => $json->category ?? '', '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 ?? '', 'start_date' => $json->start_date ?? '', 'end_date' => $json->end_date ?? '', '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('events'); $builder->insert($data); return $this->respondSuccess(['id' => $this->getDB()->insertID()], '이벤트가 등록되었습니다.'); } /** * Update event */ public function update($id = null) { $auth = $this->requireAuth(); if ($auth instanceof ResponseInterface) { return $auth; } $json = $this->request->getJSON(); $data = [ 'site' => $json->site ?? 'common', 'category' => $json->category ?? '', '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 ?? '', 'start_date' => $json->start_date ?? '', 'end_date' => $json->end_date ?? '', '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('events'); $builder->where('id', $id)->update($data); return $this->respondSuccess(null, '이벤트가 수정되었습니다.'); } /** * Delete event */ public function delete($id = null) { $auth = $this->requireAuth(); if ($auth instanceof ResponseInterface) { return $auth; } $builder = $this->getDB()->table('events'); $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 event list (no auth required) */ public function publicList() { $params = $this->getPaginationParams(); $builder = $this->getDB()->table('events'); // Filter by site (ford or lincoln) + common $site = $this->request->getGet('site'); if ($site && in_array($site, ['ford', 'lincoln'])) { $builder->where("(site = '{$site}' OR site = 'common')"); } // Show all events (including 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 event (no auth required) */ public function publicShow($id = null) { $builder = $this->getDB()->table('events'); $event = $builder->where('id', $id)->get()->getRow(); if (!$event) { return $this->respondError('이벤트를 찾을 수 없습니다.', ResponseInterface::HTTP_NOT_FOUND); } // Parse file_urls JSON $event->file_urls = $this->normalizeFileUrls($event->file_urls ?? '[]'); // Fix image paths in content: /event/image.jpg -> /uploads/bbs/event/image.jpg if (!empty($event->content)) { // src="/event/ 형태를 src="/uploads/bbs/event/ 로 변경 $event->content = str_replace('src="/event/', 'src="/uploads/bbs/event/', $event->content); // src='/event/ 형태도 처리 $event->content = str_replace("src='/event/", "src='/uploads/bbs/event/", $event->content); // YouTube iframe 경로 수정: /embed/ID -> https://www.youtube.com/embed/ID $event->content = str_replace('src="/embed/', 'src="https://www.youtube.com/embed/', $event->content); $event->content = str_replace("src='/embed/", "src='https://www.youtube.com/embed/", $event->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)://가 없는 경우만 도메인 추가 $event->content = preg_replace( '/src="(?!https?:\/\/)\/uploads\//', 'src="' . $currentDomain . '/uploads/', $event->content ); $event->content = preg_replace( "/src='(?!https?:\/\/)\/uploads\//", "src='" . $currentDomain . "/uploads/", $event->content ); } // Increment view count $builder->where('id', $id)->set('views', 'views + 1', false)->update(); return $this->respondSuccess($event); } }