simedkom/app/Http/Controllers/Dashboard/ContentRecapController.php
2025-05-05 08:08:15 +07:00

172 lines
6.3 KiB
PHP

<?php
namespace App\Http\Controllers\Dashboard;
use App\Exports\ContentRecapExport;
use App\Http\Controllers\Controller;
use App\Http\Requests\ContentRecapRequest;
use App\Models\ContentRecap;
use App\Models\ContentRecapClassification;
use Barryvdh\DomPDF\Facade\Pdf;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;
use Maatwebsite\Excel\Facades\Excel;
class ContentRecapController extends Controller
{
public function index(Request $request){
return view('dashboard.content-recaps.index', [
'title' => 'Rekap Konten',
'classifications' => ContentRecapClassification::all(),
'channel' => $request->input('channel', null),
'classification' => $request->input('classification', null),
'social_media' => $request->input('social_media', null),
'posted_year' => $request->input('posted_year', null),
'posted_month' => $request->input('posted_month', null),
'posted_date' => $request->input('posted_date', null),
]);
}
public function create(){
return view('dashboard.content-recaps.create', [
'title' => 'Tambah Rekap Konten',
'classifications' => ContentRecapClassification::all()
]);
}
public function store(ContentRecapRequest $request){
$validatedData = $request->validated();
try{
$createdContentRecap = ContentRecap::create($validatedData);
if($request->has('image') && filled($validatedData['image'])){
$validatedData['image'] = store_file($validatedData['image'], '/uploads/content-recaps')['randomFileName'];
}else{
$validatedData['image'] = null;
}
}catch(\Exception $e){
Log::error("ContentRecaps (store): ". $e);
return redirect()->back()->with('failed-status', 'Gagal menambahkan rekap konten.');
}
return redirect()->route('dashboard.content.recaps.index')->with('success-status', 'Berhasil menambahkan rekap konten.')
->with('new_data', encrypt_id($createdContentRecap->id));
}
public function edit($id){
$contentRecap = ContentRecap::findOrFail(decrypt_id($id));
return view('dashboard.content-recaps.edit', [
'title' => 'Ubah Rekap Konten',
'contentRecap' => $contentRecap,
'classifications' => ContentRecapClassification::all()
]);
}
public function update(ContentRecapRequest $request, $id){
$validatedData = $request->validated();
try{
$contentRecap = ContentRecap::findOrFail(decrypt_id($id));
if($request->has('image') && filled($validatedData['image'])){
delete_file('app/public/uploads/content-recaps/'.$contentRecap->image);
$validatedData['image'] = store_file($validatedData['image'], '/uploads/content-recaps')['randomFileName'];
}else{
$validatedData['image'] = $contentRecap->image;
}
$contentRecap->update($validatedData);
}catch(\Exception $e){
Log::error("ContentRecaps (update): ". $e);
return redirect()->back()->with('failed-status', 'Gagal mengubah rekap konten.');
}
return redirect()->route('dashboard.content.recaps.show', ['id' => encrypt_id($contentRecap->id)])->with('success-status', 'Berhasil mengubah rekap konten.');
}
public function show($id){
$contentRecap = ContentRecap::findOrFail(decrypt_id($id));
return view('dashboard.content-recaps.show', [
'title' => 'Detail Rekap Konten',
'contentRecap' => $contentRecap
]);
}
public function destroy($id){
try {
$contentRecap = ContentRecap::findOrFail(decrypt_id($id));
$contentRecap->delete();
} catch (\Exception $e) {
Log::error("ContentRecaps (destroy): ". $e);
return response()->json(['message' => 'Gagal menghapus rekap konten.'], 500);
}
return response()->json(['message' => 'Berhasil menghapus rekap konten.']);
}
public function checkLink(Request $request)
{
$linkExists = ContentRecap::where('link', $request->link)->exists();
return response()->json([
'exists' => $linkExists
]);
}
public function filter(Request $request){
try{
$filteredRequest = $request->except('_token');
$filterField = array_filter($filteredRequest, function ($value) {
return filled($value);
});
}catch(\Exception $e){
Log::error("ContentRecaps (filter): ". $e);
return redirect()->back()->with('failed-status', 'Gagal melakukan filter data.');
}
return redirect()->route('dashboard.content.recaps.index', $filterField);
}
public function export(Request $request)
{
try{
$classification = $request->input('classification', 'all');
$channel = $request->input('channel', 'all');
$social_media = $request->input('social_media', 'all');
$posted_year = $request->input('posted_year', null);
$posted_month = $request->input('posted_month', null);
$posted_date = $request->input('posted_date', null);
$formattedNow = Carbon::parse(now())->locale('id')->translatedFormat('d-m-Y');
return Excel::download(new ContentRecapExport($classification, $channel, $social_media, $posted_year, $posted_month, $posted_date), 'content-recaps-'.$formattedNow.'.xlsx');
}catch(\Exception $e){
Log::error("ContentRecaps (export): ". $e);
return redirect()->back()->with('failed-status', 'Gagal melakukan export data.');
}
}
public function printData($id){
try{
$contentRecap = ContentRecap::with(['enhancer', 'classification'])->findOrFail(decrypt_id($id));
$pdf = Pdf::loadView('dashboard.content-recaps.print', compact('contentRecap'));
return $pdf->stream(slugify('rekap konten '. $contentRecap->content_theme).'.pdf');
}catch(\Exception $e){
Log::error("ContentRecaps (printData): ". $e);
return redirect()->back()->with('failed-status', 'Gagal melakukan print data.');
}
}
}