143 lines
5.2 KiB
PHP
143 lines
5.2 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 Carbon\Carbon;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
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();
|
|
$validatedData['enhancer_id'] = Auth::id();
|
|
|
|
if($request->has('image') && filled($validatedData['image'])){
|
|
$validatedData['image'] = store_file($validatedData['image'], '/uploads/content-recaps', 'public')['randomFileName'];
|
|
}else{
|
|
$validatedData['image'] = null;
|
|
}
|
|
|
|
|
|
try{
|
|
$createdContentRecap = ContentRecap::create($validatedData);
|
|
|
|
return redirect()->route('dashboard.content.recaps.index')->with('success-status', 'Berhasil menambahkan rekap konten.')
|
|
->with('new_data', encrypt_id($createdContentRecap->id));
|
|
}catch(\Exception $e){
|
|
return redirect()->back()->with('failed-status', 'Gagal menambahkan rekap konten.');
|
|
}
|
|
}
|
|
|
|
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();
|
|
$validatedData['enhancer_id'] = Auth::id();
|
|
|
|
$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', 'public')['randomFileName'];
|
|
}else{
|
|
$validatedData['image'] = $contentRecap->image;
|
|
}
|
|
|
|
try{
|
|
$contentRecap->update($validatedData);
|
|
return redirect()->route('dashboard.content.recaps.show', ['id' => encrypt_id($contentRecap->id)])->with('success-status', 'Berhasil mengubah rekap konten.');
|
|
}catch(\Exception $e){
|
|
return redirect()->back()->with('failed-status', 'Gagal 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();
|
|
|
|
return response()->json(['message' => 'Berhasil menghapus rekap konten.']);
|
|
} catch (\Exception $e) {
|
|
return response()->json(['message' => 'Gagal menghapus rekap konten.'], 500);
|
|
}
|
|
}
|
|
|
|
public function checkLink(Request $request)
|
|
{
|
|
$linkExists = ContentRecap::where('link', $request->link)->exists();
|
|
|
|
return response()->json([
|
|
'exists' => $linkExists
|
|
]);
|
|
}
|
|
|
|
public function filter(Request $request){
|
|
|
|
$filteredRequest = $request->except('_token');
|
|
|
|
$filterField = array_filter($filteredRequest, function ($value) {
|
|
return filled($value);
|
|
});
|
|
|
|
return redirect()->route('dashboard.content.recaps.index', $filterField);
|
|
}
|
|
|
|
public function export(Request $request)
|
|
{
|
|
$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');
|
|
}
|
|
}
|