173 lines
6.0 KiB
PHP
173 lines
6.0 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Dashboard;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\JournalistCooperationProposalRequest;
|
|
use App\Models\Cooperation;
|
|
use App\Models\CooperationProposal;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
class CooperationRequestController extends Controller
|
|
{
|
|
public function index(){
|
|
return view('dashboard.cooperation-requests.index', [
|
|
'title' => 'Ajuan Kerjasama'
|
|
]);
|
|
}
|
|
|
|
public function show($id){
|
|
$cooperation = Cooperation::with(['media'])->findOrFail(decrypt_id($id));
|
|
|
|
$mediaCooperationPivot = $cooperation->media()
|
|
->where('media.id', Auth::user()->company->media->id)
|
|
->first()->pivot;
|
|
|
|
return view('dashboard.cooperation-requests.show', [
|
|
'title' => 'Tolak Kerjasama',
|
|
'cooperation' => $cooperation,
|
|
'mediaCooperationPivot' => $mediaCooperationPivot
|
|
]);
|
|
}
|
|
|
|
public function mediaProposal(){
|
|
return view('dashboard.cooperation-requests.media-proposals.index',[
|
|
'title' => 'Ajuan Kerjasama Saya'
|
|
]);
|
|
}
|
|
|
|
public function mediaProposalShow($id){
|
|
$proposal = CooperationProposal::with(['cooperation', 'media', 'journalists'])->findOrFail(decrypt_id($id));
|
|
|
|
return view('dashboard.cooperation-requests.media-proposals.show',[
|
|
'title' => 'Detail Ajuan Kerjasama',
|
|
'proposal' => $proposal
|
|
]);
|
|
}
|
|
|
|
public function accept(Request $request, $id){
|
|
|
|
if(!Auth::check() || Auth::user()->role_id !== 3){
|
|
return abort(403, 'Unauthorized action.');
|
|
}
|
|
|
|
$cooperation = Cooperation::with('media')->findOrFail(decrypt_id($id));
|
|
|
|
$mediaCooperation = $cooperation->media()
|
|
->where('media.id', Auth::user()->company->media->id)
|
|
->first();
|
|
|
|
if (!$mediaCooperation) {
|
|
return abort(404, 'Media not found for the cooperation.');
|
|
}
|
|
|
|
|
|
// dd($mediaCooperation->pivot);
|
|
|
|
// Pastikan data yang ingin diperbarui ada dan statusnya belum '3'
|
|
if ($mediaCooperation->pivot->status != '3') {
|
|
$affectedRows = $cooperation->media()->updateExistingPivot($mediaCooperation->id, [
|
|
'status' => '1',
|
|
]);
|
|
|
|
if ($affectedRows > 0) {
|
|
return redirect()->back()->with('success-status', 'Berhasil menerima kerjasama.');
|
|
} else {
|
|
return redirect()->back()->with('failed-status', 'Gagal menerima kerjasama');
|
|
}
|
|
} else {
|
|
return redirect()->back()->with('failed-status', 'Kerjasama sudah diterima sebelumnya.');
|
|
}
|
|
|
|
}
|
|
|
|
public function proposal($id){
|
|
$cooperation = Cooperation::with(['media'])->findOrFail(decrypt_id($id));
|
|
|
|
$mediaCooperationPivot = $cooperation->media()
|
|
->where('media.id', Auth::user()->company->media->id)
|
|
->first()->pivot;
|
|
|
|
return view('dashboard.cooperation-requests.proposal', [
|
|
'title' => 'Ajukan Kerjasama',
|
|
'user' => Auth::user(),
|
|
'cooperation' => $cooperation,
|
|
'mediaCooperationPivot' => $mediaCooperationPivot
|
|
]);
|
|
}
|
|
|
|
public function proposalAction(JournalistCooperationProposalRequest $request, $id){
|
|
$validatedData = $request->validated();
|
|
$validatedData['cooperation_id'] = decrypt_id($id);
|
|
$validatedData['media_id'] = Auth::user()->company->media->id;
|
|
$validatedData['attachment'] = store_file($validatedData['attachment'], '/uploads/cooperation-proposals', 'public')['randomFileName'];
|
|
|
|
$createdCooperationProposal = CooperationProposal::create($validatedData);
|
|
$createdCooperationProposal->journalists()->attach($validatedData['journalist_ids']);
|
|
|
|
if ($createdCooperationProposal->journalists()->count() > 0) {
|
|
return redirect()->back()->with('success-status', 'Berhasil melakukan pengajuan kerjasama.');
|
|
} else {
|
|
return redirect()->back()->with('failed-status', 'Gagal melakukan pengajuan kerjasama');
|
|
}
|
|
}
|
|
|
|
public function reject($id){
|
|
$cooperation = Cooperation::with(['media'])->findOrFail(decrypt_id($id));
|
|
|
|
$mediaCooperationPivot = $cooperation->media()
|
|
->where('media.id', Auth::user()->company->media->id)
|
|
->first()->pivot;
|
|
|
|
return view('dashboard.cooperation-requests.reject', [
|
|
'title' => 'Tolak Kerjasama',
|
|
'cooperation' => $cooperation,
|
|
'mediaCooperationPivot' => $mediaCooperationPivot
|
|
]);
|
|
}
|
|
|
|
public function rejectAction(Request $request, $id){
|
|
|
|
if(!Auth::check() || Auth::user()->role_id !== 3){
|
|
return abort(403, 'Unauthorized action.');
|
|
}
|
|
|
|
$validatedData = $request->validate([
|
|
'rejection_reason' => ['required'],
|
|
], [
|
|
'rejection_reason.required' => 'Alasan penolakan wajib diisi.',
|
|
]);
|
|
|
|
$cooperation = Cooperation::with('media')->findOrFail(decrypt_id($id));
|
|
|
|
$mediaCooperation = $cooperation->media()
|
|
->where('media.id', Auth::user()->company->media->id)
|
|
->first();
|
|
|
|
if (!$mediaCooperation) {
|
|
return abort(404, 'Media not found for the cooperation.');
|
|
}
|
|
|
|
|
|
// dd($mediaCooperation->pivot);
|
|
|
|
// Pastikan data yang ingin diperbarui ada dan statusnya belum '3'
|
|
if ($mediaCooperation->pivot->status != '3') {
|
|
$affectedRows = $cooperation->media()->updateExistingPivot($mediaCooperation->id, [
|
|
'status' => '2',
|
|
'rejection_reason' => $validatedData['rejection_reason'],
|
|
]);
|
|
|
|
if ($affectedRows > 0) {
|
|
return redirect()->back()->with('success-status', 'Berhasil menolak kerjasama.');
|
|
} else {
|
|
return redirect()->back()->with('failed-status', 'Gagal menolak kerjasama');
|
|
}
|
|
} else {
|
|
return redirect()->back()->with('failed-status', 'Kerjasama sudah ditolak sebelumnya.');
|
|
}
|
|
|
|
}
|
|
}
|