<?php
namespace App\Http\Controllers;
use App\Models\cabor;
use App\Models\event;
use Carbon\Carbon;
use Carbon\CarbonTimeZone;
use Illuminate\Contracts\Encryption\DecryptException;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\File;
class EventController extends Controller
{
public function index()
{
$data['event'] = event::latest()->get();
$data['cabor'] = cabor::get();
return view('admin.event.index')->with($data);
}
public function store(Request $request)
{
$request->validate([
'nama' => 'required',
'tanggal_mulai' => 'required',
'tanggal_berakhir' => 'required',
'batas_daftar' => 'required',
'cabor' => 'required',
'link' => 'required',
'tempat' => 'required',
'photo' => 'required|mimes:jpg,jpeg,png,gif,svg|max:300000',
], ['photo.max' => 'Foto harus dibawah 300mb.']);
//nomor tanding
$no_tanding = $request->input('nomor_tanding', []);
$nomor_tanding = implode(',', $no_tanding);
//nomor tanding
$data = [
'nama' => $request->nama,
'tanggal_mulai' => $request->tanggal_mulai,
'tanggal_berakhir' => $request->tanggal_berakhir,
'batas_daftar' => $request->batas_daftar,
'cabor' => $request->cabor,
'tempat' => $request->tempat,
'link' => $request->link,
'nomor_tanding' => $nomor_tanding,
];
if ($request->hasFile('photo')) {
$foto_file = $request->file('photo');
$foto_ekstensi = $foto_file->extension();
$foto_baru = "event-" . date('ymdhis') . ".$foto_ekstensi";
$foto_file->move(public_path('images/event'), $foto_baru);
$data['photo'] = $foto_baru;
}
$create = event::create($data);
if ($create) {
return redirect()->route('events.index')->with('success', 'Data baru telah ditambahkan');
} else {
return redirect()->route('events.index')->with('error', 'Data gagal ditambahkan');
}
}
public function update(Request $request, String $id)
{
$decryptedId = Crypt::decryptString($id);
try {
$request->validate([
'nama' => 'required',
'tanggal_mulai' => 'required',
'tanggal_berakhir' => 'required',
'batas_daftar' => 'required',
'cabor' => 'required',
'tempat' => 'required',
'link' => 'required',
'photo' => 'nullable|mimes:jpg,jpeg,png,gif,svg|max:300000'
], ['photo.max' => 'Foto harus dibawah 300mb.']);
//nomor tanding
$no_tanding = $request->input('nomor_tanding', []);
$nomor_tanding = implode(',', $no_tanding);
//nomor tanding
$data = [
'nama' => $request->nama,
'tanggal_mulai' => $request->tanggal_mulai,
'tanggal_berakhir' => $request->tanggal_berakhir,
'batas_daftar' => $request->batas_daftar,
'cabor' => $request->cabor,
'tempat' => $request->tempat,
'link' => $request->link,
'nomor_tanding' => $nomor_tanding,
];
if ($request->hasFile('photo')) {
$foto_file = $request->file('photo');
$foto_ekstensi = $foto_file->extension();
$foto_baru = "event-" . date('ymdhis') . ".$foto_ekstensi";
$foto_file->move(public_path('images/event'), $foto_baru);
$data['photo'] = $foto_baru;
$getfoto = event::where('id', $decryptedId)->first();
$foto_lama = $getfoto->photo;
File::delete(public_path('images/event') . "/" . $foto_lama);
}
$update = event::where('id', $decryptedId)->update($data);
if ($update) {
return redirect()->route('events.index')->with('success', 'Data dari telah diedit');
} else {
return redirect()->route('events.index')->with('error', 'Data gagal diedit');
}
} catch (DecryptException $e) {
return redirect()->route('events.index')->with('error', 'Data gagal diedit');
}
}
public function destroy(String $id)
{
try {
$decryptedId = Crypt::decryptString($id);
$user = event::where('id', $decryptedId);
$foto = $user->first();
File::delete(public_path('images/event') . "/" . $foto->photo);
$user->delete();
return redirect()->route('events.index')->with('delete', 'Data berhasil dihapus!');
} catch (DecryptException $e) {
return redirect()->back()->with('error', 'Invalid ID');
}
}
public function userEvent(String $cabor)
{
$sekarang = now();
$data['now'] = $sekarang->format('Y-m-d');
$data['event'] = event::where('cabor', $cabor)->latest()->get();
return view('admin.event.userEvent')->with($data);
}
}
Anons79 File Manager Version 1.0, Coded By Anons79
Email: [email protected]