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