Files
confirms/app/Controllers/Board/Notice.php
yangsh 38444fcb4f
Some checks failed
Close Pull Request / main (pull_request_target) Has been cancelled
공지사항 파일업로드 수정
2025-12-30 16:40:40 +09:00

366 lines
11 KiB
PHP

<?php
namespace App\Controllers\board;
use App\Controllers\BaseController;
use App\Libraries\MyUpload;
use App\Models\board\NoticeModel;
class Notice extends BaseController
{
private $notice;
public function __construct()
{
$this->notice = new NoticeModel();
}
public function notice(): string
{
return view('pages/board/notice');
}
public function getNoticeList()
{
$start = (int) $this->request->getGet('start') ?: 0;
$end = (int) $this->request->getGet('length') ?: 10;
$data = [
'srchType' => $this->request->getGet('srchType') ?: '',
'srchTxt' => $this->request->getGet('srchTxt') ?: '',
];
$totalCount = $this->notice->getTotalCount($data);
$datas = $this->notice->getNoticeList($start, $end, $data);
return $this->response->setJSON(body: [
'recordsTotal' => $totalCount,
'recordsFiltered' => $totalCount,
'data' => $datas,
]);
}
// 공지사항 상세
public function detail($id = null)
{
$id = (int) $id;
if ($id <= 0) {
throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound();
}
// 데이터 조회
$data = $this->notice->getNoticeData($id);
if (!$data) {
throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound();
}
return view('pages/board/noticeDetail', $data);
}
// 첨부파일 다운로드
public function download($fileSq = null)
{
$fileSq = (int) $fileSq;
if ($fileSq <= 0) {
throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound();
}
// 1) DB에서 파일 정보 조회
$fileInfo = $this->notice->getFile($fileSq);
if (!$fileInfo) {
throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound();
}
// DB에 이렇게 저장했다고 가정:
// file_path : /var/www/html/writable/upload/notice
// file_name : 실제 서버 파일명 (orig or new)
// dd($fileInfo);
$filePath = rtrim($fileInfo['file_path'], '/\\') . DIRECTORY_SEPARATOR . $fileInfo['file_name'];
if (!is_file($filePath)) {
throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound();
}
// 브라우저에 보여줄 다운로드 파일명 (원본 이름)
$downloadName = $fileInfo['orig_name'];
// 2) CI4 내장 다운로드 헬퍼 사용
return $this->response->download($filePath, null)->setFileName($downloadName);
}
// 공지사항 작성 화면
public function write(): string
{
return view('pages/board/noticeWrite');
}
// 공지사항 작성
public function actWrite()
{
$lib = new MyUpload();
try {
$data = [
'subject' => $this->request->getPost('subject'),
'content' => $this->request->getPost('content'),
'insert_usr' => session()->get('usr_id'),
'insert_nm' => session()->get('usr_nm'),
];
$file = $this->request->getFile('file');
if ($file && $file->isValid() && !$file->hasMoved()) {
$uploadPath = "/upload/notice/" . date('Ymd') . "/";
$arrUploadfile = [];
if ($file->isValid() && !$file->hasMoved()) {
$uploadData = $lib->do_upload2($file, $uploadPath);
if ($uploadData !== false) {
$arrUploadfile[] = $uploadData;
}
}
if (!empty($arrUploadfile)) {
foreach ($arrUploadfile as $key => $uploadFile) {
$data['file'] = [
'file_sq' => $this->request->getPost('file_sq'),
'orig_name' => $uploadFile['origin_name'],
'new_name' => $uploadFile['file_name'],
'file_path' => $uploadPath, // 필요에 따라 상대경로로만 저장
'ext' => '.' . $uploadFile['ext'],
'size' => $file->getSize(),
'img_yn' => null,
// 높이/폭은 나중에 getimagesize 등으로 구해도 됨
'img_height' => null,
'img_width' => null,
];
}
}
/*
$origName = $file->getClientName();
$ext = $file->getClientExtension();
$size = $file->getSize();
$mime = $file->getMimeType();
$type = $file->getClientMimeType();
$tempName = $file->getTempName();
$imgYn = (strpos($mime, 'image/') === 0) ? 'Y' : 'N';
// 저장 경로
$saveDir = WRITEPATH . 'upload/notice';
if (!is_dir($saveDir)) {
mkdir($saveDir, 0777, true);
}
// 서버 저장 이름 (덮어쓰기 방지를 위해 랜덤으로 추천)
$newName = $file->getRandomName();
$file->move($saveDir, $newName);
// 모델로 넘길 파일 정보
$data['file'] = [
'orig_name' => $origName,
'new_name' => $newName,
'file_path' => $saveDir,
'ext' => $ext,
'size' => $size,
'img_yn' => $imgYn,
'img_height' => null,
'img_width' => null,
];
*/
}
$result = $this->notice->write($data);
return $this->response->setJSON([
'code' => '0',
'msg' => 'success'
]);
} catch (\Exception $e) {
log_message('error', '[LOGIN ERROR] ' . $e->getMessage());
return $this->response->setJSON([
'code' => '9',
'msg' => $e->getMessage(),
]);
}
}
// 공지사항 수정
public function modify($id = null): string
{
$id = (int) $id;
if ($id <= 0) {
throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound();
}
// 데이터 조회
$data = $this->notice->getNoticeData($id);
if (!$data) {
throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound();
}
return view('pages/board/noticeModify', $data);
}
// 공지사항 수정요청
public function actModify()
{
$lib = new MyUpload();
try {
$data = [
'bbs_sq' => $this->request->getPost('bbs_sq'),
'subject' => $this->request->getPost('subject'),
'content' => $this->request->getPost('content'),
'update_usr' => session()->get('usr_id'),
'update_nm' => session()->get('usr_nm'),
];
$file = $this->request->getFile('file');
if ($file && $file->isValid() && !$file->hasMoved()) {
$uploadPath = "/upload/notice/" . date('Ymd') . "/";
$arrUploadfile = [];
if ($file->isValid() && !$file->hasMoved()) {
$uploadData = $lib->do_upload2($file, $uploadPath);
if ($uploadData !== false) {
$arrUploadfile[] = $uploadData;
}
}
if (!empty($arrUploadfile)) {
foreach ($arrUploadfile as $key => $uploadFile) {
$data['file'] = [
'file_sq' => $this->request->getPost('file_sq'),
'orig_name' => $uploadFile['origin_name'],
'new_name' => $uploadFile['file_name'],
'file_path' => $uploadPath, // 필요에 따라 상대경로로만 저장
'ext' => '.' . $uploadFile['ext'],
'size' => $file->getSize(),
'img_yn' => null,
// 높이/폭은 나중에 getimagesize 등으로 구해도 됨
'img_height' => null,
'img_width' => null,
];
}
}
/*
$origName = $file->getClientName();
$ext = $file->getClientExtension();
$size = $file->getSize();
$mime = $file->getMimeType(); // ★ move() 전에!
$type = $file->getClientMimeType();
$tempName = $file->getTempName();
$imgYn = (strpos($mime, 'image/') === 0) ? 'Y' : 'N';
// 저장 경로
$saveDir = WRITEPATH . 'upload/notice';
if (!is_dir($saveDir)) {
mkdir($saveDir, 0777, true);
}
// 서버 저장 이름 (덮어쓰기 방지를 위해 랜덤으로 추천)
$newName = $file->getRandomName();
$file->move($saveDir, $newName);
// 모델로 넘길 파일 정보
$data['file'] = [
'file_sq' => $this->request->getPost('file_sq'),
'orig_name' => $origName,
'new_name' => $newName,
'file_path' => $saveDir, // 필요에 따라 상대경로로만 저장
'ext' => $ext,
'size' => $size,
'img_yn' => $imgYn,
// 높이/폭은 나중에 getimagesize 등으로 구해도 됨
'img_height' => null,
'img_width' => null,
];
*/
}
$result = $this->notice->modify($data);
return $this->response->setJSON([
'code' => '0',
'msg' => 'success'
]);
} catch (\Exception $e) {
log_message('error', '[LOGIN ERROR] ' . $e->getMessage());
return $this->response->setJSON([
'code' => '9',
'msg' => $e->getMessage(),
]);
}
}
// 공지사항 삭제요청
public function actRemove()
{
try {
$data = [
'bbs_sq' => $this->request->getPost('bbs_sq'),
'update_usr' => session()->get('usr_id'),
'update_nm' => session()->get('usr_nm'),
];
$this->notice->remove($data);
return $this->response->setJSON([
'code' => '0',
'msg' => 'success'
]);
} catch (\Exception $e) {
log_message('error', '[LOGIN ERROR] ' . $e->getMessage());
return $this->response->setJSON([
'code' => '9',
'msg' => $e->getMessage(),
]);
}
}
}