99 lines
3.6 KiB
PHP
99 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Dashboard;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\CooperationRequest;
|
|
use App\Models\Cooperation;
|
|
use App\Models\Media;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
class MediaCooperationController extends Controller
|
|
{
|
|
public function index(){
|
|
return view('dashboard.media-cooperations.index', [
|
|
'title' => 'Kerjasama'
|
|
]);
|
|
}
|
|
|
|
public function create(){
|
|
return view('dashboard.media-cooperations.create', [
|
|
'title' => 'Tambah Kerjasama',
|
|
'media' => Media::all()
|
|
]);
|
|
}
|
|
|
|
public function store(CooperationRequest $request){
|
|
$validatedData = $request->validated();
|
|
$validatedData['enhancer_id'] = Auth::id();
|
|
|
|
$validatedData['banner'] = store_file($validatedData['banner'], '/uploads/cooperations', 'public')['randomFileName'];
|
|
$validatedData['offer_file_template'] = store_file($validatedData['offer_file_template'], '/uploads/cooperations', 'public')['randomFileName'];
|
|
|
|
$createdCooperation = Cooperation::create($validatedData);
|
|
$createdCooperation->media()->attach($validatedData['media_ids']);
|
|
|
|
if ($createdCooperation->media()->count() > 0) {
|
|
return redirect()->back()->with('success-status', 'Berhasil menambahkan kerjasama.');
|
|
} else {
|
|
return redirect()->back()->with('failed-status', 'Gagal menambahkan kerjasama');
|
|
}
|
|
}
|
|
|
|
public function edit($id){
|
|
$cooperation = Cooperation::with(['media'])->findOrFail(decrypt_id($id));
|
|
return view('dashboard.media-cooperations.edit', [
|
|
'title' => 'Ubah Kerjasama',
|
|
'cooperation' => $cooperation,
|
|
'media' => Media::all(),
|
|
'selectedMediaIds' => $cooperation->media->pluck('id')->toArray()
|
|
]);
|
|
}
|
|
|
|
public function update(CooperationRequest $request, $id){
|
|
$validatedData = $request->validated();
|
|
$cooperation = Cooperation::with(['media'])->findOrFail(decrypt_id($id));
|
|
|
|
$validatedData['enhancer_id'] = Auth::id();
|
|
|
|
if($request->hasFile('banner')){
|
|
delete_file('app/public/uploads/cooperations/'.$cooperation->banner);
|
|
$validatedData['banner'] = store_file($validatedData['banner'], '/uploads/cooperations', 'public')['randomFileName'];
|
|
}else{
|
|
$validatedData['banner'] = $cooperation->banner;
|
|
}
|
|
|
|
if($request->hasFile('offer_file_template')){
|
|
delete_file('app/public/uploads/cooperations/'.$cooperation->offer_file_template);
|
|
$validatedData['offer_file_template'] = store_file($validatedData['offer_file_template'], '/uploads/cooperations', 'public')['randomFileName'];
|
|
}else{
|
|
$validatedData['offer_file_template'] = $cooperation->offer_file_template;
|
|
}
|
|
|
|
$cooperation->update($validatedData);
|
|
if ($request->has('media_ids')) {
|
|
$cooperation->media()->sync($validatedData['media_ids']);
|
|
}
|
|
|
|
if ($cooperation->media()->count() > 0) {
|
|
return redirect()->back()->with('success-status', 'Berhasil mengubah kerjasama.');
|
|
} else {
|
|
return redirect()->back()->with('failed-status', 'Gagal mengubah kerjasama');
|
|
}
|
|
|
|
|
|
}
|
|
|
|
public function show($id){
|
|
$mediaCooperation = Cooperation::with(['media'])->findOrFail(decrypt_id($id));
|
|
|
|
return view('dashboard.media-cooperations.show', [
|
|
'title' => 'Detail Kerjasama',
|
|
'mediaCooperation' => $mediaCooperation
|
|
]);
|
|
}
|
|
|
|
public function destroy(){}
|
|
}
|