Files
sam-manage/app/Http/Controllers/Juil/PmisNoticeController.php
김보곤 2102f4a398 feat: [pmis] PMIS 자료실/안전관리/품질관리 기능 추가 및 개선
- 자료실 하위 3개 메뉴: 자료보관함, 매뉴얼, 공지사항
- 자료보관함: 폴더 트리 + 파일 업로드/다운로드/삭제
- 매뉴얼/공지사항: 게시판형 CRUD + 첨부파일
- 안전관리: 안전보건교육, TBM현황, 위험성평가, 재해예방조치
- 품질관리: 시정조치 UI 페이지
- 대시보드: 슈퍼관리자 전용 레거시 사이트 참고 카드
- 작업일보/출면일보 오류 수정 및 기능 개선
- 설비 사진 업로드, 근로계약서 종료일 수정
2026-03-12 21:11:21 +09:00

186 lines
5.9 KiB
PHP

<?php
namespace App\Http\Controllers\Juil;
use App\Http\Controllers\Controller;
use App\Models\Juil\PmisNotice;
use App\Models\Juil\PmisNoticeAttachment;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
class PmisNoticeController extends Controller
{
private function tenantId(): int
{
return (int) session('current_tenant_id', 1);
}
public function list(Request $request): JsonResponse
{
$search = $request->query('search', '');
$withTrashed = $request->boolean('with_trashed', false);
$query = PmisNotice::tenant($this->tenantId())
->with('author:id,name')
->withCount('attachments')
->orderByDesc('id');
if ($withTrashed && auth()->user()->isAdmin()) {
$query->withTrashed();
}
if ($search) {
$query->where('title', 'like', "%{$search}%");
}
$notices = $query->get()->map(fn ($n) => [
'id' => $n->id,
'title' => $n->title,
'author' => $n->author?->name ?? '관리자',
'createdAt' => $n->created_at->format('Y-m-d H:i:s'),
'views' => $n->views,
'hasAttachment' => $n->attachments_count > 0,
'isDeleted' => $n->trashed(),
]);
return response()->json(['notices' => $notices]);
}
public function show(int $id): JsonResponse
{
$notice = PmisNotice::tenant($this->tenantId())
->with(['author:id,name', 'attachments'])
->findOrFail($id);
$notice->increment('views');
return response()->json([
'notice' => [
'id' => $notice->id,
'title' => $notice->title,
'content' => $notice->content,
'author' => $notice->author?->name ?? '관리자',
'createdAt' => $notice->created_at->format('Y-m-d H:i:s'),
'views' => $notice->views,
'attachments' => $notice->attachments->map(fn ($a) => [
'id' => $a->id,
'fileName' => $a->original_name,
'size' => PmisNoticeAttachment::formatSize($a->file_size),
'downloadUrl' => "/juil/construction-pmis/api/notices/attachments/{$a->id}/download",
]),
],
]);
}
public function store(Request $request): JsonResponse
{
$user = auth()->user();
if (! $user->isAdmin()) {
return response()->json(['message' => '권한이 없습니다.'], 403);
}
$request->validate([
'title' => 'required|string|max:300',
'content' => 'nullable|string',
'files' => 'nullable|array',
'files.*' => 'file|max:51200',
]);
$notice = PmisNotice::create([
'tenant_id' => $this->tenantId(),
'title' => $request->title,
'content' => $request->content ?? '',
'author_id' => $user->id,
]);
if ($request->hasFile('files')) {
foreach ($request->file('files') as $file) {
$path = $file->store('pmis/notices', 'public');
$notice->attachments()->create([
'original_name' => $file->getClientOriginalName(),
'file_path' => $path,
'file_size' => $file->getSize(),
]);
}
}
return response()->json(['notice' => $notice->load('attachments'), 'message' => '등록되었습니다.'], 201);
}
public function update(Request $request, int $id): JsonResponse
{
$user = auth()->user();
if (! $user->isAdmin()) {
return response()->json(['message' => '권한이 없습니다.'], 403);
}
$notice = PmisNotice::tenant($this->tenantId())->findOrFail($id);
$request->validate([
'title' => 'required|string|max:300',
'content' => 'nullable|string',
'files' => 'nullable|array',
'files.*' => 'file|max:51200',
]);
$notice->update([
'title' => $request->title,
'content' => $request->content ?? '',
]);
if ($request->hasFile('files')) {
foreach ($request->file('files') as $file) {
$path = $file->store('pmis/notices', 'public');
$notice->attachments()->create([
'original_name' => $file->getClientOriginalName(),
'file_path' => $path,
'file_size' => $file->getSize(),
]);
}
}
return response()->json(['notice' => $notice->load('attachments'), 'message' => '수정되었습니다.']);
}
public function destroy(int $id): JsonResponse
{
$user = auth()->user();
if (! $user->isAdmin()) {
return response()->json(['message' => '권한이 없습니다.'], 403);
}
$notice = PmisNotice::tenant($this->tenantId())->findOrFail($id);
foreach ($notice->attachments as $att) {
Storage::disk('public')->delete($att->file_path);
}
$notice->delete();
return response()->json(['message' => '삭제되었습니다.']);
}
public function attachmentDownload(int $id)
{
$att = PmisNoticeAttachment::findOrFail($id);
$fullPath = Storage::disk('public')->path($att->file_path);
return response()->download($fullPath, $att->original_name);
}
public function attachmentDestroy(int $id): JsonResponse
{
$user = auth()->user();
if (! $user->isAdmin()) {
return response()->json(['message' => '권한이 없습니다.'], 403);
}
$att = PmisNoticeAttachment::findOrFail($id);
Storage::disk('public')->delete($att->file_path);
$att->delete();
return response()->json(['message' => '삭제되었습니다.']);
}
}