424 lines
14 KiB
PHP
424 lines
14 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Collection;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Illuminate\Support\Str;
|
|
|
|
use App\Models\{Content, Galeri};
|
|
|
|
use DataTables;
|
|
use GuzzleHttp\Client;
|
|
use Carbon\Carbon;
|
|
|
|
class ContentController extends Controller
|
|
{
|
|
public function index(Request $request)
|
|
{
|
|
if ($request->ajax()) {
|
|
$data = Content::select('*');
|
|
// Convert the Eloquent Collection to a regular PHP array
|
|
$data->each(function ($item, $key) {
|
|
$item->rowIndex = $key + 1;
|
|
});
|
|
|
|
return Datatables::of($data)
|
|
->addIndexColumn()
|
|
->addColumn('title-post', function($row){
|
|
$text = '
|
|
<p class="mb-0">' . $row->title . '</p>
|
|
<p class="mb-0 small text-muted">Terakhir diperbarui pada ' . date_formatting($row->updated_at, 'timeago') . '</p>
|
|
';
|
|
return $text;
|
|
})
|
|
->addColumn('status', function($row){
|
|
if ($row->status == 1) {
|
|
return '<span class="mb-1 badge font-medium bg-primary text-white py-2 px-3 fs-7">Publish</span>';
|
|
} else {
|
|
return '<span class="mb-1 badge font-medium bg-light text-dark py-2 px-3 fs-7">Draft</span>';
|
|
}
|
|
})
|
|
->rawColumns(['title-post','status'])
|
|
->make(true);
|
|
}
|
|
|
|
$data = [
|
|
'subtitle' => 'Papan Informasi',
|
|
'button' => true,
|
|
'module' => [
|
|
'url' => route('papan-informasi.create'),
|
|
'name' => 'Tambah baru'
|
|
]
|
|
];
|
|
|
|
return view('admin.app.content.blog.index', compact('data'));
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function create()
|
|
{
|
|
$data = [
|
|
'subtitle' => 'Tambah baru',
|
|
];
|
|
return view('admin.app.content.blog.add', compact('data'));
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function store(Request $request)
|
|
{
|
|
$validator = Validator::make($request->all(), [
|
|
'title' => 'required',
|
|
'description' => 'required',
|
|
'is_status' => 'required',
|
|
'image' => 'image|mimes:jpg,jpeg,png,svg|max:7048',
|
|
], [
|
|
'image.mimes' => 'Tipe file yang diunggah harus jpg, jpeg, png, atau svg.',
|
|
]);
|
|
|
|
if ($validator->fails()) {
|
|
return redirect()->back()->withInput()->with('error', $validator->errors()->first());
|
|
}
|
|
|
|
$input = $request->all();
|
|
$titleSlug = Str::slug($input['title']);
|
|
$foto_namaBaru = null;
|
|
|
|
if ($request->hasFile('image') && $request->file('image')->isValid()) {
|
|
$foto_namaBaru = $request->file('image')->store('public/images');
|
|
}
|
|
|
|
$post = new Content([
|
|
'slug' => $titleSlug,
|
|
'title' => $input['title'], // Membersihkan input judul menggunakan Purifier
|
|
'description' => $input['description'], // Membersihkan input deskripsi menggunakan Purifier
|
|
'status' => $input['is_status'],
|
|
'is_thumbnail' => $foto_namaBaru,
|
|
]);
|
|
|
|
$check = Content::where('title', $input['title'])->count();
|
|
if ($check == 0) {
|
|
if ($post->save()) {
|
|
return redirect()->route('papan-informasi')->with('success', 'You have successfully added data');
|
|
} else {
|
|
return redirect()->route('papan-informasi')->with('error', 'An error occurred while adding data');
|
|
}
|
|
} else {
|
|
return redirect()->route('papan-informasi')->with('error', 'Data already exists');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Display the specified resource.
|
|
*
|
|
* @param int $id
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function show($id)
|
|
{
|
|
$data = [
|
|
'subtitle' => Content::where('id', $id)->first()->title,
|
|
'records' => Content::where('id', $id)->first()
|
|
];
|
|
return view('admin.app.content.blog.detail', compact('data'));
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*
|
|
* @param int $id
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function edit($id)
|
|
{
|
|
$data = [
|
|
'subtitle' => 'Edit: ' . Content::where('id', $id)->first()->title,
|
|
'records' => Content::where('id', $id)->first(),
|
|
];
|
|
$posts = Content::FindOrFail($id);
|
|
return view('admin.app.content.blog.edit', compact('data','posts'));
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @param int $id
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function update(Request $request, $id)
|
|
{
|
|
// Validasi input sebelum memperbarui data
|
|
$validator = Validator::make($request->all(), [
|
|
'title' => 'required',
|
|
'description' => 'required',
|
|
'is_status' => 'required',
|
|
'image' => 'image|mimes:jpg,jpeg,png,svg|max:7048',
|
|
], [
|
|
'image.mimes' => 'Tipe file yang diunggah harus jpg, jpeg, png, atau svg.',
|
|
]);
|
|
|
|
if ($validator->fails()) {
|
|
return redirect()->back()->withInput()->with('error', $validator->errors()->first());
|
|
}
|
|
|
|
// Cari data berdasarkan ID
|
|
$post = Content::find($id);
|
|
|
|
// Jika data ditemukan
|
|
if ($post) {
|
|
// Jika ada file baru yang diunggah, hapus file thumbnail yang lama
|
|
if ($request->hasFile('image') && $request->file('image')->isValid()) {
|
|
if ($post->is_thumbnail) {
|
|
Storage::delete($post->is_thumbnail);
|
|
}
|
|
}
|
|
|
|
// Update data dengan data baru dari form yang telah dibersihkan
|
|
$post->title = $request->input('title');
|
|
$post->slug = Str::slug($request->input('title'));;
|
|
$post->description = $request->input('description');
|
|
$post->status = $request->input('is_status');
|
|
|
|
// Jika ada file baru yang diunggah, simpan file baru di storage
|
|
if ($request->hasFile('image') && $request->file('image')->isValid()) {
|
|
$foto_namaBaru = $request->file('image')->store('public/images');
|
|
$post->is_thumbnail = $foto_namaBaru;
|
|
}
|
|
|
|
// Simpan perubahan pada database
|
|
$post->save();
|
|
return redirect()->route('papan-informasi')->with('success', 'You have successfully updated data');
|
|
} else {
|
|
return redirect()->route('papan-informasi')->with('error', 'Data not found');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*
|
|
* @param int $id
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function destroy($id)
|
|
{
|
|
// Cari data berdasarkan ID
|
|
$post = Content::find($id);
|
|
// Jika data ditemukan
|
|
if ($post) {
|
|
// Cek apakah ada file di kolom "is_thumbnail"
|
|
if ($post->is_thumbnail) {
|
|
// Hapus file thumbnail dari storage
|
|
Storage::delete($post->is_thumbnail);
|
|
}
|
|
// Hapus data dari database
|
|
$post->delete();
|
|
return redirect()->route('papan-informasi')->with('success', 'You have successfully deleted data');
|
|
} else {
|
|
return redirect()->route('papan-informasi')->with('error', 'Data not found');
|
|
}
|
|
}
|
|
|
|
public function galeri(Request $request)
|
|
{
|
|
if ($request->ajax()) {
|
|
$data = Galeri::select('*');
|
|
// Convert the Eloquent Collection to a regular PHP array
|
|
$data->each(function ($item, $key) {
|
|
$item->rowIndex = $key + 1;
|
|
});
|
|
|
|
return Datatables::of($data)
|
|
->addIndexColumn()
|
|
->addColumn('title-post', function($row){
|
|
$text = '
|
|
<p class="mb-0">' . $row->name . '</p>
|
|
<p class="mb-0 small text-muted">Terakhir diperbarui pada ' . date_formatting($row->updated_at, 'timeago') . '</p>
|
|
';
|
|
return $text;
|
|
})
|
|
->addColumn('status', function($row){
|
|
if ($row->status == 1) {
|
|
return '<span class="mb-1 badge font-medium bg-primary text-white py-2 px-3 fs-7">Publish</span>';
|
|
} else {
|
|
return '<span class="mb-1 badge font-medium bg-light text-dark py-2 px-3 fs-7">Draft</span>';
|
|
}
|
|
})
|
|
->rawColumns(['title-post','status'])
|
|
->filter(function ($query) use ($request) {
|
|
if ($request->has('search')) {
|
|
$search = $request->get('search')['value'];
|
|
if(!empty($search)) {
|
|
$query->where('name', 'LIKE', "%$search%");
|
|
} else {
|
|
$query->get();
|
|
}
|
|
}
|
|
})
|
|
->make(true);
|
|
}
|
|
|
|
$data = [
|
|
'subtitle' => 'Galeri',
|
|
'button' => true,
|
|
'module' => [
|
|
'url' => route('galeri.create'),
|
|
'name' => 'Tambah baru'
|
|
]
|
|
];
|
|
|
|
return view('admin.app.content.galeri.index', compact('data'));
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function createGaleri()
|
|
{
|
|
$data = [
|
|
'subtitle' => 'Tambah baru',
|
|
];
|
|
return view('admin.app.content.galeri.add', compact('data'));
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function storeGaleri(Request $request)
|
|
{
|
|
$validator = Validator::make($request->all(), [
|
|
'title' => 'required',
|
|
'is_status' => 'required',
|
|
'image' => 'image|mimes:jpg,jpeg,png,svg|max:7048',
|
|
], [
|
|
'image.mimes' => 'Tipe file yang diunggah harus jpg, jpeg, png, atau svg.',
|
|
]);
|
|
|
|
if ($validator->fails()) {
|
|
return redirect()->back()->withInput()->with('error', $validator->errors()->first());
|
|
}
|
|
|
|
$input = $request->all();
|
|
$foto_namaBaru = null;
|
|
|
|
if ($request->hasFile('image') && $request->file('image')->isValid()) {
|
|
$foto_namaBaru = $request->file('image')->store('public/images');
|
|
}
|
|
|
|
$post = new Galeri([
|
|
'id' => Str::uuid(),
|
|
'name' => $input['title'],
|
|
'description' => $foto_namaBaru,
|
|
'status' => $input['is_status'],
|
|
]);
|
|
|
|
$check = Galeri::where('name', $input['title'])->count();
|
|
if ($check == 0) {
|
|
if ($post->save()) {
|
|
return redirect()->route('galeri')->with('success', 'You have successfully added data');
|
|
} else {
|
|
return redirect()->route('galeri')->with('error', 'An error occurred while adding data');
|
|
}
|
|
} else {
|
|
return redirect()->route('papan-informasi')->with('error', 'Data already exists');
|
|
}
|
|
}
|
|
|
|
public function editGaleri($id)
|
|
{
|
|
$data = [
|
|
'subtitle' => 'Edit: ' . Galeri::where('id', $id)->first()->title,
|
|
'records' => Galeri::where('id', $id)->first(),
|
|
];
|
|
$posts = Galeri::find($id);
|
|
|
|
return view('admin.app.content.galeri.edit', compact('data','posts', 'id'));
|
|
}
|
|
|
|
public function updateGaleri(Request $request, $id)
|
|
{
|
|
// Validasi input sebelum memperbarui data
|
|
$validator = Validator::make($request->all(), [
|
|
'title' => 'required',
|
|
'is_status' => 'required',
|
|
'image' => 'image|mimes:jpg,jpeg,png,svg|max:7048',
|
|
], [
|
|
'image.mimes' => 'Tipe file yang diunggah harus jpg, jpeg, png, atau svg.',
|
|
]);
|
|
|
|
if ($validator->fails()) {
|
|
return redirect()->back()->withInput()->with('error', $validator->errors()->first());
|
|
}
|
|
|
|
// Cari data berdasarkan ID
|
|
$post = Galeri::find($id);
|
|
|
|
// Jika data ditemukan
|
|
if ($post) {
|
|
// Jika ada file baru yang diunggah, hapus file thumbnail yang lama
|
|
if ($request->hasFile('image') && $request->file('image')->isValid()) {
|
|
if ($post->description) {
|
|
Storage::delete($post->description);
|
|
}
|
|
}
|
|
|
|
// Update data dengan data baru dari form yang telah dibersihkan
|
|
$post->name = $request->input('title');
|
|
$post->status = $request->input('is_status');
|
|
|
|
// Jika ada file baru yang diunggah, simpan file baru di storage
|
|
if ($request->hasFile('image') && $request->file('image')->isValid()) {
|
|
$foto_namaBaru = $request->file('image')->store('public/images');
|
|
$post->description = $foto_namaBaru;
|
|
}
|
|
|
|
// Simpan perubahan pada database
|
|
$post->save();
|
|
return redirect()->route('galeri')->with('success', 'You have successfully updated data');
|
|
} else {
|
|
return redirect()->route('galeri')->with('error', 'Data not found');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*
|
|
* @param int $id
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function destroyGaleri($id)
|
|
{
|
|
// Cari data berdasarkan ID
|
|
$post = Galeri::find($id);
|
|
// Jika data ditemukan
|
|
if ($post) {
|
|
// Cek apakah ada file di kolom "is_thumbnail"
|
|
if ($post->description) {
|
|
// Hapus file thumbnail dari storage
|
|
Storage::delete($post->description);
|
|
}
|
|
// Hapus data dari database
|
|
$post->delete();
|
|
return redirect()->route('galeri')->with('success', 'You have successfully deleted data');
|
|
} else {
|
|
return redirect()->route('galeri')->with('error', 'Data not found');
|
|
}
|
|
}
|
|
}
|