<?php
namespace App\Http\Controllers;
use App\Models\berita;
use App\Rules\YoutubeLink;
use Illuminate\Contracts\Encryption\DecryptException;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\File;
class BeritaController extends Controller
{
public function index()
{
$data['berita'] = berita::latest()->get();
return view('admin.berita.index')->with($data);
}
public function beritaAdd()
{
return view('admin.berita.add');
}
public function show(string $id)
{
$decryptedId = Crypt::decryptString($id);
$data['berita'] = berita::where('id', $decryptedId)->first();
return view('admin.berita.edit')->with($data);
}
public function store(Request $request)
{
$rx = '#^(?:https?://)?(?:www[.])?(?:youtube[.]com/watch[?]v=|youtu[.]be/)([^&]{11})#';
$request->validate([
'title' => 'required',
'deskripsi' => 'required',
'tag' => 'required',
'link' => ['nullable', 'regex:' . $rx],
'photo' => 'mimes:jpg,jpeg,png,gif,svg|max:300000|required',
], ['photo.max' => 'Foto yang tidak boleh diatas 30000mb!', 'link.regex' => 'Link bukan berasal dari Youtube ! ']);
$data = [
'tanggal' => now(),
'title' => $request->title,
'tag' => $request->tag,
'banner' => 0,
'pengirim' => Auth()->user()->username,
'deskripsi' => nl2br($request->deskripsi),
];
if ($request->link) {
$data['link'] = $request->link;
}
if ($request->hasFile('photo')) {
$foto_file = $request->file('photo');
$foto_ekstensi = $foto_file->extension();
$foto_baru = "Berita-" . date('ymdhis') . ".$foto_ekstensi";
$foto_file->move(public_path('images/berita'), $foto_baru);
$data['photo'] = $foto_baru;
}
berita::create($data);
return redirect()->route('beritas.index')->with('success', 'berita berhasil dibuat!');
}
public function update(request $request, string $id)
{
try {
$decryptedId = Crypt::decryptString($id);
$rx = '#^(?:https?://)?(?:www[.])?(?:youtube[.]com/watch[?]v=|youtu[.]be/)([^&]{11})#';
$request->validate([
'photo' => 'mimes:jpg,jpeg,png,gif,svg|max:300000|nullable',
'title' => 'required',
'link' => ['nullable', 'regex:' . $rx],
'deskripsi' => 'required',
], ['photo.max' => 'Foto yang tidak boleh diatas 300mb!', 'link.regex' => 'Link bukan berasal dari Youtube ! ']);
$data = [
'tanggal' => now(),
'title' => $request->title,
'link' => $request->link,
'tag' => $request->tag,
'deskripsi' => nl2br($request->deskripsi),
];
if ($request->hasFile('photo')) {
$foto_file = $request->file('photo');
$foto_ekstensi = $foto_file->extension();
$foto_baru = "Berita-" . date('ymdhis') . ".$foto_ekstensi";
$foto_file->move(public_path('images/berita'), $foto_baru);
$data['photo'] = $foto_baru;
$getfoto = berita::where('id', $decryptedId)->first();
$foto_lama = $getfoto->photo;
File::delete(public_path('images/berita') . "/" . $foto_lama);
}
berita::where('id', $decryptedId)->update($data);
return redirect()->route('beritas.index')->with('success', 'Data successfully changed');
} catch (DecryptException $e) {
return redirect()->back()->with('error', 'Invalid ID');
}
}
public function destroy(string $id)
{
try {
$decryptedId = Crypt::decryptString($id);
$berita = berita::where('id', $decryptedId);
$foto = $berita->first();
File::delete(public_path('images/berita') . "/" . $foto->photo);
$berita->delete();
return redirect()->route('beritas.index')->with('delete', 'Data successfully deleted');
} catch (DecryptException $e) {
return redirect()->back()->with('error', 'Invalid ID');
}
}
// public function banner(string $id)
// {
// $decryptedId = Crypt::decryptString($id);
// $data = ['banner' => 1];
// berita::where('id', $decryptedId)->update($data);
// return redirect()->route('beritas.index')->with('success', 'Berita berhasil dipasang kebanner!');
// }
// public function no_banner(string $id)
// {
// $decryptedId = Crypt::decryptString($id);
// $data = ['banner' => 0];
// berita::where('id', $decryptedId)->update($data);
// return redirect()->route('beritas.index')->with('delete', 'Berita dilepas dari banner!');
// }
}
Anons79 File Manager Version 1.0, Coded By Anons79
Email: [email protected]