From 74fe0c05a61d63c85462f0c63817fc6fc9d77a89 Mon Sep 17 00:00:00 2001 From: myqnrrhmn Date: Wed, 28 May 2025 10:00:18 +0700 Subject: [PATCH] feat: cooperation archive --- app/Exports/CooperationArchiveExport.php | 109 +++++++ app/Helpers/CustomHelper.php | 294 +++++++++--------- .../CooperationArchiveController.php | 52 ++++ .../Dashboard/DatatableController.php | 102 ++++++ .../cooperation-archives/index.blade.php | 163 ++++++++++ .../views/partials/app/datatable.blade.php | 39 +++ .../partials/dashboard/sidebar.blade.php | 2 +- routes/web.php | 14 + 8 files changed, 628 insertions(+), 147 deletions(-) create mode 100644 app/Exports/CooperationArchiveExport.php create mode 100644 app/Http/Controllers/Dashboard/CooperationArchiveController.php create mode 100644 resources/views/dashboard/cooperation-archives/index.blade.php diff --git a/app/Exports/CooperationArchiveExport.php b/app/Exports/CooperationArchiveExport.php new file mode 100644 index 0000000..4d9565b --- /dev/null +++ b/app/Exports/CooperationArchiveExport.php @@ -0,0 +1,109 @@ +year = $year; + $this->month = $month; + $this->date = $date; + } + + public function collection() + { + $query = Cooperation::with(['media', 'mediaOrder']); + + if (is_role(['1'])) { + // Hanya untuk role 1: filter kerja sama lebih dari 1 tahun + if (!$this->year && !$this->month && !$this->date) { + $query->where('created_at', '<=', Carbon::now()->subYear()); + } + } else if (is_role(['3'])) { + $mediaId = Auth::user()->company->media->id; + + $query->whereHas('proposals', function ($query) use ($mediaId) { + $query->where('status', 1)->where('media_id', $mediaId); + }); + + if (!$this->year && !$this->month && !$this->date) { + $query->where('created_at', '<=', Carbon::now()->subYear()); + } + } + + if ($this->year) { + $query->whereYear('created_at', $this->year); + } + + if ($this->month) { + $monthYear = Carbon::parse($this->month); + $query->whereYear('created_at', $monthYear->year) + ->whereMonth('created_at', $monthYear->month); + } + + if ($this->date) { + $query->whereDate('created_at', $this->date); + } + + return $query->get(); + } + + public function map($cooperationArchive): array + { + return [ + $this->no++, + $cooperationArchive->name, + Carbon::parse($cooperationArchive->start_date)->locale('id')->translatedFormat('l, d F Y'), + Carbon::parse($cooperationArchive->end_date)->locale('id')->translatedFormat('l, d F Y'), + $cooperationArchive->description, + Carbon::parse($cooperationArchive->created_at)->locale('id')->translatedFormat('l, d F Y'), + ]; + } + + public function headings(): array + { + return [ + ['Data Rekap Konten', '', '', '', '', ''], + ['NO', 'Nama Kerjasama', 'Taanggal Mulai', 'Tanggal Selesai', 'Deskripsi', 'Dibuat Pada'] + ]; + } + + public function styles(Worksheet $sheet) + { + $sheet->mergeCells('A1:I1'); // A1 sampai D1 digabung + + // Set alignment untuk pusat horizontal dan vertikal + $sheet->getStyle('A1:J1')->getAlignment()->setHorizontal('center'); + $sheet->getStyle('A1:F1')->getAlignment()->setVertical('center'); + + $sheet->getColumnDimension('A')->setWidth(7); // Set column A width + $sheet->getColumnDimension('B')->setAutoSize(true); + $sheet->getColumnDimension('C')->setAutoSize(true); + $sheet->getColumnDimension('D')->setAutoSize(true); + $sheet->getColumnDimension('E')->setAutoSize(true); + $sheet->getColumnDimension('F')->setAutoSize(true); + + // Menambahkan gaya lain jika diperlukan + $sheet->getStyle('A1:F1')->getFont()->setBold(true); + $sheet->getStyle('A1:F1')->getFont()->setSize(14); + } +} diff --git a/app/Helpers/CustomHelper.php b/app/Helpers/CustomHelper.php index 535927a..e726635 100644 --- a/app/Helpers/CustomHelper.php +++ b/app/Helpers/CustomHelper.php @@ -17,182 +17,184 @@ // } // } -// if (! function_exists('store_file')) { -// function store_file($file = null, $path, $disk = null) -// { -// $disk = $disk ?? env('FILESYSTEM_DISK', 'public'); +if(env('FILESYSTEM_DISK') === 's3'){ + if (! function_exists('store_file')) { + function store_file($file = null, $path, $disk = null) + { + $disk = $disk ?? env('FILESYSTEM_DISK', 'public'); -// if (filled($file) && $file instanceof \Illuminate\Http\UploadedFile) { -// $randomFilename = Str::random(35) . time() . '.' . $file->getClientOriginalExtension(); -// $originalFileName = Str::slug(pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME)) . '.' . $file->getClientOriginalExtension(); + if (filled($file) && $file instanceof \Illuminate\Http\UploadedFile) { + $randomFilename = Str::random(35) . time() . '.' . $file->getClientOriginalExtension(); + $originalFileName = Str::slug(pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME)) . '.' . $file->getClientOriginalExtension(); -// $stored = $file->storeAs($path, $randomFilename, $disk); + $stored = $file->storeAs($path, $randomFilename, $disk); -// if ($stored) { -// $url = Storage::disk($disk)->url($stored); + if ($stored) { + $url = Storage::disk($disk)->url($stored); -// return [ -// 'randomFileName' => $randomFilename, -// 'originalFileName' => $originalFileName, -// 'url' => $url, -// 'isSuccess' => true, -// ]; -// } -// } + return [ + 'randomFileName' => $randomFilename, + 'originalFileName' => $originalFileName, + 'url' => $url, + 'isSuccess' => true, + ]; + } + } -// return [ -// 'randomFileName' => null, -// 'originalFileName' => null, -// 'url' => null, -// 'isSuccess' => false, -// ]; -// } -// } - -if (! function_exists('store_file')) { - function store_file($file = null, $path, $disk = 'public') - { - if (filled($file) && $file && $file instanceof \Illuminate\Http\UploadedFile) { - $randomFilename = Str::random(35).time().'.'.$file->getClientOriginalExtension(); - $originalFileName = Str::slug(pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME)).'.'.$file->getClientOriginalExtension(); - - $file->storeAs($path, $randomFilename, $disk); - - return [ - 'randomFileName' => $randomFilename, - 'originalFileName' => $originalFileName, - 'isSuccess' => true, - ]; - } else { return [ 'randomFileName' => null, 'originalFileName' => null, + 'url' => null, 'isSuccess' => false, ]; } } -} -// if (!function_exists('display_file')) { -// function display_file($dir, $filename, $disk = null, $expirationMinutes = 60): string -// { -// $disk = $disk ?? env('FILESYSTEM_DISK', 'public'); + if (!function_exists('display_file')) { + function display_file($dir, $filename, $disk = null, $expirationMinutes = 60): string + { + $disk = $disk ?? env('FILESYSTEM_DISK', 'public'); -// if (!filled($filename)) { -// return '

Belum ditambahkan.

'; -// } + if (!filled($filename)) { + return '

Belum ditambahkan.

'; + } -// $path = "uploads/{$dir}/{$filename}"; + $path = "uploads/{$dir}/{$filename}"; -// try { -// $fileExists = is_file_exists($path, $disk); + try { + $fileExists = is_file_exists($path, $disk); -// if (!$fileExists) { -// return '

File tidak ditemukan.

'; -// } + if (!$fileExists) { + return '

File tidak ditemukan.

'; + } -// if ($disk === 's3') { -// $fileUrl = Storage::disk($disk)->temporaryUrl($path, now()->addMinutes($expirationMinutes)); -// } else { -// $fileUrl = Storage::disk($disk)->url($path); -// } + if ($disk === 's3') { + $fileUrl = Storage::disk($disk)->temporaryUrl($path, now()->addMinutes($expirationMinutes)); + } else { + $fileUrl = Storage::disk($disk)->url($path); + } -// $ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION)); -// $mimeType = match ($ext) { -// 'jpg', 'jpeg', 'png', 'gif', 'webp' => 'image/' . $ext, -// 'pdf' => 'application/pdf', -// default => 'unknown', -// }; + $ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION)); + $mimeType = match ($ext) { + 'jpg', 'jpeg', 'png', 'gif', 'webp' => 'image/' . $ext, + 'pdf' => 'application/pdf', + default => 'unknown', + }; -// if (str_contains($mimeType, 'image')) { -// return "File Image"; -// } elseif ($mimeType === 'application/pdf') { -// return ""; -// } + if (str_contains($mimeType, 'image')) { + return "File Image"; + } elseif ($mimeType === 'application/pdf') { + return ""; + } -// return '

Format file tidak didukung

'; -// } catch (\Exception $e) { -// return '

Terjadi kesalahan saat menampilkan file.

'; -// } -// } -// } - -if (!function_exists('display_file')) { - function display_file($dir, $filename) - { - $path = storage_path("app/public/uploads/{$dir}/{$filename}"); - - if(!filled($filename)){ - return '

Belum ditambahkan.

'; + return '

Format file tidak didukung

'; + } catch (\Exception $e) { + return '

Terjadi kesalahan saat menampilkan file.

'; + } } - - if (!file_exists($path)) { - return '

File tidak ditemukan.

'; - } - - $mimeType = mime_content_type($path); - $fileUrl = asset("storage/uploads/{$dir}/{$filename}"); - - if (str_contains($mimeType, 'image')) { - return "File Image"; - } elseif ($mimeType === 'application/pdf') { - return ""; - } - - return '

Format file tidak didukung

'; } -} -// if (! function_exists('delete_file')) { -// function delete_file($path, $disk = null): bool -// { -// $disk = $disk ?? env('FILESYSTEM_DISK', 'public'); + if (! function_exists('delete_file')) { + function delete_file($path, $disk = null): bool + { + $disk = $disk ?? env('FILESYSTEM_DISK', 'public'); -// try { -// if(is_file_exists($path)){ -// return Storage::disk($disk)->delete($path); -// }else{ -// return false; -// } -// } catch (\Exception $e) { -// return false; -// } -// } -// } - -if (! function_exists('delete_file')) { - function delete_file($path) - { - // Menggunakan public_path untuk file yang berada di dalam storage/public - $fullPath = public_path('storage/' . $path); - - if (File::exists($fullPath)) { - File::delete($fullPath); - return true; + try { + if(is_file_exists($path)){ + return Storage::disk($disk)->delete($path); + }else{ + return false; + } + } catch (\Exception $e) { + return false; + } } - - return false; } -} -// if (!function_exists('is_file_exists')) { -// function is_file_exists($path, $disk = null): bool -// { -// $disk = $disk ?? env('FILESYSTEM_DISK', 'public'); + if (!function_exists('is_file_exists')) { + function is_file_exists($path, $disk = null): bool + { + $disk = $disk ?? env('FILESYSTEM_DISK', 'public'); -// try { -// $file = Storage::disk($disk)->get($path); -// return !empty($file); -// } catch (\Throwable $e) { -// return false; -// } -// } -// } + try { + $file = Storage::disk($disk)->get($path); + return !empty($file); + } catch (\Throwable $e) { + return false; + } + } + } +}else if(env('FILESYSTEM_DISK') === 'local'){ + if (! function_exists('store_file')) { + function store_file($file = null, $path, $disk = 'public') + { + if (filled($file) && $file && $file instanceof \Illuminate\Http\UploadedFile) { + $randomFilename = Str::random(35).time().'.'.$file->getClientOriginalExtension(); + $originalFileName = Str::slug(pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME)).'.'.$file->getClientOriginalExtension(); -if (! function_exists('is_file_exists')) { - function is_file_exists($path) - { - return File::exists(public_path('storage/' . $path)); + $file->storeAs($path, $randomFilename, $disk); + + return [ + 'randomFileName' => $randomFilename, + 'originalFileName' => $originalFileName, + 'isSuccess' => true, + ]; + } else { + return [ + 'randomFileName' => null, + 'originalFileName' => null, + 'isSuccess' => false, + ]; + } + } + } + + if (!function_exists('display_file')) { + function display_file($dir, $filename) + { + $path = storage_path("app/public/uploads/{$dir}/{$filename}"); + + if(!filled($filename)){ + return '

Belum ditambahkan.

'; + } + + if (!file_exists($path)) { + return '

File tidak ditemukan.

'; + } + + $mimeType = mime_content_type($path); + $fileUrl = asset("storage/uploads/{$dir}/{$filename}"); + + if (str_contains($mimeType, 'image')) { + return "File Image"; + } elseif ($mimeType === 'application/pdf') { + return ""; + } + + return '

Format file tidak didukung

'; + } + } + + if (! function_exists('delete_file')) { + function delete_file($path) + { + // Menggunakan public_path untuk file yang berada di dalam storage/public + $fullPath = public_path('storage/' . $path); + + if (File::exists($fullPath)) { + File::delete($fullPath); + return true; + } + + return false; + } + } + + if (! function_exists('is_file_exists')) { + function is_file_exists($path) + { + return File::exists(public_path('storage/' . $path)); + } } } diff --git a/app/Http/Controllers/Dashboard/CooperationArchiveController.php b/app/Http/Controllers/Dashboard/CooperationArchiveController.php new file mode 100644 index 0000000..d23b9dc --- /dev/null +++ b/app/Http/Controllers/Dashboard/CooperationArchiveController.php @@ -0,0 +1,52 @@ + 'Arsip Kerjasama', + 'date' => $request->input('date', null), + 'month' => $request->input('month', null), + 'year' => $request->input('year', null), + ]); + } + + public function export(Request $request) + { + + try{ + $year = $request->input('year', null); + $month = $request->input('month', null); + $date = $request->input('date', null); + + return Excel::download(new CooperationArchiveExport($year, $month, $date), 'cooperation-archive.xlsx'); + }catch(\Exception $e){ + return redirect()->back()->with('failed-status', 'Gagal melakukan proses export data.'); + } + + } + + public function filter(Request $request){ + + try{ + $filteredRequest = $request->except('_token'); + + $filterField = array_filter($filteredRequest, function ($value) { + return filled($value); + }); + + return redirect()->route('dashboard.cooperation.archives.index', $filterField); + }catch(\Exception $e){ + return redirect()->back()->with('status-failed', 'Gagal melakukan proses filtering data.'); + } + + + } +} diff --git a/app/Http/Controllers/Dashboard/DatatableController.php b/app/Http/Controllers/Dashboard/DatatableController.php index 39c10c9..a3b8c42 100644 --- a/app/Http/Controllers/Dashboard/DatatableController.php +++ b/app/Http/Controllers/Dashboard/DatatableController.php @@ -2092,6 +2092,108 @@ class="text-dark" } + public function cooperationArchive(Request $request){ + if ($request->ajax()) { + + $year = $request->input('year', null); + $month = $request->input('month', null); + $date = $request->input('date', null); + + $query = Cooperation::with(['media', 'mediaOrder']); + + if (is_role(['1'])) { + // Hanya untuk role 1: filter kerja sama lebih dari 1 tahun + if (!$year && !$month && !$date) { + $query->where('created_at', '<=', Carbon::now()->subYear()); + } + } else if (is_role(['3'])) { + $mediaId = Auth::user()->company->media->id; + + $query->whereHas('proposals', function ($query) use ($mediaId) { + $query->where('status', 1)->where('media_id', $mediaId); + }); + + if (!$year && !$month && !$date) { + $query->where('created_at', '<=', Carbon::now()->subYear()); + } + } + + if ($year) { + $query->whereYear('created_at', $year); + } + + if ($month) { + $monthYear = Carbon::parse($month); + $query->whereYear('created_at', $monthYear->year) + ->whereMonth('created_at', $monthYear->month); + } + + if ($date) { + $query->whereDate('created_at', $date); + } + + $data = $query->get(); + + + return DataTables::of($data) + ->addIndexColumn() + ->addColumn('name', function ($data) { + return '
' . $data->name . '
'; + }) + // ->addColumn('start_date', function ($data) { + // return idn_date($data->start_date, 'l, d F Y'); + // }) + // ->addColumn('end_date', function ($data) { + // return idn_date($data->end_date, 'l, d F Y'); + // }) + ->addColumn('posted_at', function ($data) { + return idn_date($data->created_at, 'l, d F Y'); + }) + ->addColumn('status', function ($data) { + $isHaveMediaOrder = !is_null($data->mediaOrder); + if ($isHaveMediaOrder) { + return 'Media Order
Sudah Media Order'; + }else{ + return 'Pengajuan Kerjasama
Belum Media Order'; + } + }) + ->addColumn('action', function ($data) { + + $isHaveMediaOrder = $data->mediaorder; + + $showUrl = route('dashboard.cooperations.show', ['id' => encrypt_id($data->id)]); + + $processUrl = route('dashboard.cooperations.process', ['id' => encrypt_id($data->id), 'process' => 'cooperation']); + + $createMediaOrderUrl = route('dashboard.media.orders.create', ['id' => encrypt_id($data->id)]); + + $proposalsUrl = route('dashboard.cooperations.media.proposal', ['id' => encrypt_id($data->id)]); + + $editUrl = route('dashboard.cooperations.edit', ['id' => encrypt_id($data->id)]); + + $deleteUrl = route('dashboard.cooperations.destroy', ['id' => encrypt_id($data->id)]); + + $dropdown = ''; + + return $dropdown; + }) + + ->rawColumns(['status', 'name', 'action']) + ->make(true); + } + + return response()->json(['error' => 'Unauthorized'], 401); + + } + public function contentRecap(Request $request){ if ($request->ajax()) { diff --git a/resources/views/dashboard/cooperation-archives/index.blade.php b/resources/views/dashboard/cooperation-archives/index.blade.php new file mode 100644 index 0000000..a0ee844 --- /dev/null +++ b/resources/views/dashboard/cooperation-archives/index.blade.php @@ -0,0 +1,163 @@ +@include('partials.dashboard.head') +@include('partials.dashboard.sidebar', ['page' => 'cooperation_archives']) +@include('partials.dashboard.header') + +
+
+
+
+
+
+
+

{{$title}}

+
+ @if (is_role(['1'])) +
+
+ +
+ +
+
+
+ @endif +
+
+
+
+
+
+ + + + + + {{-- + --}} + + + + + +
NoNama KegiatanTanggal Mulai PengajuanTanggal Akhir PengajuanDibuat PadaStep/Status
+
+
+
+
+ + +
+
+
+
+ +@include('partials.dashboard.footer' ,['datatable' => 'cooperation_archive']) diff --git a/resources/views/partials/app/datatable.blade.php b/resources/views/partials/app/datatable.blade.php index f42c2b2..767b1e0 100644 --- a/resources/views/partials/app/datatable.blade.php +++ b/resources/views/partials/app/datatable.blade.php @@ -973,6 +973,45 @@ @endif +@if ($datatable === 'cooperation_archive') + +@endif + @if ($datatable === 'content_recap')