291 lines
12 KiB
PHP
291 lines
12 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Dashboard;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\JournalistCooperationProposalRequest;
|
|
use App\Models\Announcement;
|
|
use App\Models\Cooperation;
|
|
use App\Models\CooperationProposal;
|
|
use App\Models\User;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
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){
|
|
|
|
try{
|
|
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.');
|
|
}
|
|
|
|
// Pastikan data yang ingin diperbarui ada dan statusnya belum '3'
|
|
if ($mediaCooperation->pivot->status == '0') {
|
|
$affectedRows = $cooperation->media()->updateExistingPivot($mediaCooperation->id, [
|
|
'status' => '1',
|
|
]);
|
|
|
|
// Announcements
|
|
$announcementData = [
|
|
'headline' => 'Ajuan Kerjasama <u>'.$cooperation->name.'</u> <span class="text-primary">diterima</span> oleh media <u>'.$mediaCooperation->name. '</u>',
|
|
'content' => '<p>Ajuan Kerjasama <u>'.$cooperation->name.'</u> telah <span class="text-primary">diterima</span> oleh media <u>'.$mediaCooperation->name.'</u></p>',
|
|
'enhancer_id' => Auth::id()
|
|
];
|
|
|
|
$adminIds = User::where('role_id', 1)->pluck('id')->mapWithKeys(function ($id) {
|
|
return [$id => ['category' => 'system']];
|
|
})->toArray();
|
|
|
|
$createdAnnouncement = Announcement::create($announcementData);
|
|
$createdAnnouncement->users()->attach($adminIds);
|
|
// End Announcements
|
|
|
|
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.');
|
|
}
|
|
}catch(\Exception $e){
|
|
Log::error("CooperationRequest (accept): ". $e);
|
|
return redirect()->back()->with('failed-status', 'Proses gagal.');
|
|
}
|
|
|
|
}
|
|
|
|
public function createProposal($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.create-proposal', [
|
|
'title' => 'Ajukan Kerjasama',
|
|
'user' => Auth::user(),
|
|
'cooperation' => $cooperation,
|
|
'mediaCooperationPivot' => $mediaCooperationPivot
|
|
]);
|
|
}
|
|
|
|
public function storeProposal(JournalistCooperationProposalRequest $request, $id){
|
|
$validatedData = $request->validated();
|
|
|
|
try{
|
|
$cooperation = Cooperation::findOrFail(decrypt_id($id));
|
|
$validatedData['cooperation_id'] = decrypt_id($id);
|
|
$validatedData['media_id'] = Auth::user()->company->media->id;
|
|
$validatedData['attachment'] = store_file($validatedData['attachment'], '/uploads/cooperation-proposals')['randomFileName'];
|
|
|
|
$createdCooperationProposal = CooperationProposal::create($validatedData);
|
|
$createdCooperationProposal->journalists()->attach($validatedData['journalist_ids']);
|
|
|
|
// Announcements
|
|
$announcementData = [
|
|
'headline' => 'Media <u>'.Auth::user()->company->media->name.'</u> telah mengirimkan formulir pengajuan kerjasama <u>'.$cooperation->name. '</u>',
|
|
'content' => 'Media <u>'.Auth::user()->company->media->name.'</u> telah mengirimkan formulir pengajuan kerjasama <u>'.$cooperation->name. '</u> pada <b>'.idn_date($createdCooperationProposal->created_at, 'l, d F Y H:i').'</b>',
|
|
'enhancer_id' => Auth::id()
|
|
];
|
|
|
|
$adminIds = User::where('role_id', 1)->pluck('id')->mapWithKeys(function ($id) {
|
|
return [$id => ['category' => 'system']];
|
|
})->toArray();
|
|
|
|
$createdAnnouncement = Announcement::create($announcementData);
|
|
$createdAnnouncement->users()->attach($adminIds);
|
|
// End Announcements
|
|
|
|
if ($createdCooperationProposal->journalists()->count() <= 0) {
|
|
abort(404);
|
|
}
|
|
|
|
}catch(\Exception $e){
|
|
return redirect()->back()->with('failed-status', 'Gagal melakukan pengajuan kerjasama');
|
|
}
|
|
|
|
return redirect()->route('dashboard.cooperations.requests.index')->with('success-status', 'Berhasil 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){
|
|
|
|
try{
|
|
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.');
|
|
}
|
|
|
|
|
|
if ($mediaCooperation->pivot->status != '3') {
|
|
$affectedRows = $cooperation->media()->updateExistingPivot($mediaCooperation->id, [
|
|
'status' => '2',
|
|
'rejection_reason' => $validatedData['rejection_reason'],
|
|
]);
|
|
|
|
// Announcements
|
|
$announcementData = [
|
|
'headline' => 'Ajuan Kerjasama <u>'.$cooperation->name.'</u> <span class="text-danger fw-bold">ditolak</span> oleh media <u>'.$mediaCooperation->name. '</u>',
|
|
'content' => '<p>Ajuan Kerjasama <u>'.$cooperation->name.'</u> <span class="text-danger">ditolak</span> oleh media <u>'.$mediaCooperation->name.'</u></p>
|
|
<p>Alasan: <br> '.$validatedData['rejection_reason'].'</p>
|
|
',
|
|
'enhancer_id' => Auth::id()
|
|
];
|
|
|
|
$adminIds = User::where('role_id', 1)->pluck('id')->mapWithKeys(function ($id) {
|
|
return [$id => ['category' => 'system']];
|
|
})->toArray();
|
|
|
|
$createdAnnouncement = Announcement::create($announcementData);
|
|
$createdAnnouncement->users()->attach($adminIds);
|
|
// End Announcements
|
|
|
|
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.');
|
|
}
|
|
}catch(\Exception $e){
|
|
Log::error("CooperationRequest (rejectAction): ". $e);
|
|
return redirect()->back()->with('failed-status', 'Proses gagal.');
|
|
}
|
|
|
|
}
|
|
|
|
public function editProposal($id){
|
|
$proposal = CooperationProposal::with(['cooperation', 'media', 'journalists'])->findOrFail(decrypt_id($id));
|
|
|
|
return view('dashboard.cooperation-requests.media-proposals.edit',[
|
|
'title' => 'Ubah Ajuan Kerjasama',
|
|
'proposal' => $proposal,
|
|
'selectedJournalists' => $proposal->journalists->pluck('id')->toArray()
|
|
]);
|
|
}
|
|
|
|
public function updateProposal(JournalistCooperationProposalRequest $request, $id){
|
|
$validatedData = $request->validated();
|
|
$validatedData['media_id'] = Auth::user()->company->media->id;
|
|
|
|
try{
|
|
$proposal = CooperationProposal::with(['cooperation', 'media', 'journalists'])->findOrFail(decrypt_id($id));
|
|
$validatedData['cooperation_id'] = $proposal->cooperation_id;
|
|
|
|
if($request->hasFile('attachment')){
|
|
$validatedData['attachment'] = store_file($validatedData['attachment'], '/uploads/cooperation-proposals')['randomFileName'];
|
|
delete_file('app/public/uploads/cooperation-proposals/'.$proposal->image);
|
|
}else{
|
|
$validatedData['attachment'] = $proposal->attachment;
|
|
}
|
|
|
|
$proposal->update($validatedData);
|
|
|
|
if($request->has('journalist_ids')){
|
|
$proposal->journalists()->sync($validatedData['journalist_ids']);
|
|
}
|
|
|
|
if ($proposal->journalists()->count() > 0) {
|
|
return redirect()->back()->with('success-status', 'Berhasil mengubah pengajuan kerjasama.');
|
|
} else {
|
|
return redirect()->back()->with('failed-status', 'Gagal mengubah pengajuan kerjasama');
|
|
}
|
|
}catch(\Exception $e){
|
|
Log::error("CooperationRequest (updateProposal): ". $e);
|
|
return redirect()->back()->with('failed-status', 'Gagal mengubah pengajuan kerjasama');
|
|
}
|
|
|
|
}
|
|
|
|
public function destroyProposal($id){
|
|
try {
|
|
$cooperationProposal = CooperationProposal::findOrFail(decrypt_id($id));
|
|
delete_file('app/public/uploads/cooperation-proposals/'.$cooperationProposal->attachment);
|
|
$cooperationProposal->delete();
|
|
|
|
|
|
return response()->json(['message' => 'Berhasil menghapus ajuan kerjasama.']);
|
|
} catch (\Exception $e) {
|
|
Log::error("CooperationRequest (destroyProposal): ". $e);
|
|
return response()->json(['message' => 'Gagal menghapus ajuan kerjasama.'], 500);
|
|
}
|
|
}
|
|
}
|