171 lines
5.9 KiB
PHP
171 lines
5.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Dashboard;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\ReportRequest;
|
|
use App\Models\Announcement;
|
|
use App\Models\Report;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
class ReportController extends Controller
|
|
{
|
|
public function create($id){
|
|
return view('dashboard.reports.create', [
|
|
'title' => 'Buat Laporan',
|
|
'mediaOrderId' => $id
|
|
]);
|
|
}
|
|
|
|
public function store(ReportRequest $request, $id){
|
|
$validatedData = $request->validated();
|
|
$validatedData['media_id'] = Auth::user()->company->media->id;
|
|
$validatedData['order_id'] = decrypt_id($id);
|
|
|
|
$validatedData['proof'] = store_file($validatedData['proof'], '/uploads/reports')['randomFileName'];
|
|
|
|
$createdReport = Report::create($validatedData);
|
|
|
|
if($createdReport){
|
|
return redirect()->route('dashboard.cooperations.process', ['id' => $id, 'process' => 'reports'])->with('success-status', 'Berhasil menambahkan laporan.');
|
|
}else{
|
|
return redirect()->back()->with('failed-status', 'Gagal menambahkan laporan.');
|
|
}
|
|
}
|
|
|
|
public function edit($mediaOrderId, $id){
|
|
$report = Report::findOrFail(decrypt_id($id));
|
|
|
|
return view('dashboard.reports.edit', [
|
|
'title' => 'Buat Laporan',
|
|
'report' => $report,
|
|
'mediaOrderId' => $mediaOrderId
|
|
]);
|
|
}
|
|
|
|
public function update(ReportRequest $request, $mediaOrderId, $id){
|
|
$validatedData = $request->validated();
|
|
$validatedData['media_id'] = Auth::user()->company->media->id;
|
|
$validatedData['order_id'] = decrypt_id($mediaOrderId);
|
|
$validatedData['status'] = '0';
|
|
|
|
$report = Report::findOrFail(decrypt_id($id));
|
|
|
|
if($request->hasFile('image')){
|
|
$validatedData['proof'] = store_file($validatedData['proof'], '/uploads/reports')['randomFileName'];
|
|
|
|
delete_file('app/public/uploads/reports/'.$report->proof);
|
|
}else{
|
|
$validatedData['proof'] = $report->proof;
|
|
}
|
|
|
|
$updatedReport = $report->update($validatedData);
|
|
|
|
if($updatedReport){
|
|
return redirect()->back()->with('success-status', 'Berhasil mengubah laporan.');
|
|
}else{
|
|
return redirect()->back()->with('failed-status', 'Gagal mengubah laporan.');
|
|
}
|
|
}
|
|
|
|
public function show($mediaOrderId, $id){
|
|
$report = Report::findOrFail(decrypt_id($id));
|
|
|
|
return view('dashboard.reports.show', [
|
|
'title' => 'Detail Laporan',
|
|
'report' => $report,
|
|
'mediaOrderId' => $mediaOrderId
|
|
]);
|
|
}
|
|
|
|
public function accept($id){
|
|
$report = Report::findOrFail(decrypt_id($id));
|
|
|
|
$acceptedReport = $report->update([
|
|
'status' => '1'
|
|
]);
|
|
|
|
// Announcements
|
|
$announcementData = [
|
|
'headline' => 'Laporan bukti tayang kerjasama <u>'. $report->order->cooperation->name .'</u> telah <span class="text-primary">disetujui</span>',
|
|
'content' => '<p>Admin telah menyetujui laporan bukti tayang Anda.</p>',
|
|
'enhancer_id' => Auth::id()
|
|
];
|
|
$createdAnnouncement = Announcement::create($announcementData);
|
|
$createdAnnouncement->users()->attach($report->media->company->user->id, ['category' => 'system']);
|
|
// End Announcements
|
|
|
|
if ($acceptedReport) {
|
|
return redirect()->back()->with('success-status', 'Berhasil menyetujui laporan bukti tayang.');
|
|
} else {
|
|
return redirect()->back()->with('failed-status', 'Gagal menyetujui laporan bukti tayang');
|
|
}
|
|
}
|
|
|
|
public function reject($mediaOrderId, $id){
|
|
$report = Report::findOrFail(decrypt_id($id));
|
|
|
|
return view('dashboard.reports.reject', [
|
|
'title' => 'Tolak Laporan',
|
|
'report' => $report,
|
|
'mediaOrderId' => $mediaOrderId
|
|
]);
|
|
}
|
|
|
|
public function rejectAction(Request $request, $mediaOrderId, $id){
|
|
$validatedData = $request->validate([
|
|
'rejection_reason' => ['required'],
|
|
], [
|
|
'rejection_reason.required' => 'Alasan penolakan wajib diisi.',
|
|
]);
|
|
|
|
$report = Report::findOrFail(decrypt_id($id));
|
|
|
|
$rejectedReport = $report->update([
|
|
'status' => '2',
|
|
'rejection_reason' => $validatedData['rejection_reason']
|
|
]);
|
|
|
|
// Announcements
|
|
$announcementData = [
|
|
'headline' => 'Laporan bukti tayang kerjasama <u>'. $report->order->cooperation->name .'</u> telah <span class="text-danger">ditolak</span>',
|
|
'content' => '<p>Admin telah menolak laporan bukti tayang Anda.</p>',
|
|
'enhancer_id' => Auth::id()
|
|
];
|
|
$createdAnnouncement = Announcement::create($announcementData);
|
|
$createdAnnouncement->users()->attach($report->media->company->user->id, ['category' => 'system']);
|
|
// End Announcements
|
|
|
|
if ($rejectedReport) {
|
|
return redirect()->back()->with('success-status', 'Berhasil menolak laporan bukti tayang.');
|
|
} else {
|
|
return redirect()->back()->with('failed-status', 'Gagal menolak laporan bukti tayang');
|
|
}
|
|
}
|
|
|
|
public function destroy($id){
|
|
try {
|
|
$report = Report::findOrFail(decrypt_id($id));
|
|
delete_file('app/public/uploads/reports/'.$report->proof);
|
|
$report->forceDelete();
|
|
|
|
|
|
return response()->json(['message' => 'Berhasil menghapus laporan bukti tayang.']);
|
|
} catch (\Exception $e) {
|
|
return response()->json(['message' => 'Gagal menghapus laporan bukti tayang.'], 500);
|
|
}
|
|
}
|
|
|
|
public function checkLink(Request $request)
|
|
{
|
|
$linkExists = Report::where('link', $request->link)
|
|
->where('media_id', Auth::user()->company->media->id)
|
|
->exists();
|
|
|
|
return response()->json([
|
|
'exists' => $linkExists
|
|
]);
|
|
}
|
|
}
|