149 lines
5.1 KiB
PHP
149 lines
5.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin\Content;
|
|
|
|
use App\Enums\ContentStatus;
|
|
use App\Enums\ContentType;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Content;
|
|
use App\Models\Media;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Str;
|
|
use Illuminate\View\View;
|
|
use App\Models\Taxonomy;
|
|
|
|
class ContentController extends Controller
|
|
{
|
|
public function index(Request $request): View
|
|
{
|
|
$type = ContentType::tryFrom(
|
|
$request->query('type', ContentType::PAGE->value)
|
|
) ?? ContentType::PAGE;
|
|
|
|
$contents = Content::query()
|
|
->orderByDesc('published_at')
|
|
->get();
|
|
|
|
return view('admin.content.content.index', compact('contents', 'type'));
|
|
}
|
|
|
|
public function create(Request $request): View
|
|
{
|
|
$type = ContentType::tryFrom(
|
|
$request->query('type', ContentType::PAGE->value)
|
|
) ?? ContentType::PAGE;
|
|
|
|
$taxonomies = Taxonomy::all();
|
|
|
|
return view('admin.content.content.create', compact('type', 'taxonomies'));
|
|
}
|
|
|
|
public function store(Request $request): RedirectResponse
|
|
{
|
|
$validated = $this->validateContent($request);
|
|
//dd($request->all(), $validated);
|
|
|
|
$taxonomies = [];
|
|
if (!empty($validated['category'])) {
|
|
$taxonomies['category'] = $validated['category'];
|
|
}
|
|
if (!empty($validated['tag'])) {
|
|
$taxonomies['tag'] = $validated['tag'];
|
|
}
|
|
|
|
Content::create([...$validated,
|
|
'slug' => Str::slug($validated['title']),
|
|
'taxonomies' => $taxonomies,
|
|
'created_by' => Auth::id(),
|
|
'updated_by' => Auth::id(),
|
|
]);
|
|
|
|
return redirect()->route('admin.contents.contents.index')
|
|
->with('success', 'Konten berhasil dibuat.');
|
|
}
|
|
|
|
public function show(Request $request, Content $content): View
|
|
{
|
|
return view('admin.content.show', compact('content'));
|
|
}
|
|
|
|
public function edit(Request $request, Content $content): View
|
|
{
|
|
$type = ContentType::tryFrom(
|
|
$request->query('type', ContentType::PAGE->value)
|
|
) ?? ContentType::PAGE;
|
|
|
|
$taxonomies = Taxonomy::all();
|
|
|
|
return view('admin.content.content.edit', compact('content', 'type', 'taxonomies'));
|
|
}
|
|
|
|
public function update(Request $request, Content $content): RedirectResponse
|
|
{
|
|
$validated = $this->validateContent($request, $content);
|
|
|
|
$taxonomies = [];
|
|
if (!empty($validated['category'])) {
|
|
$taxonomies['category'] = $validated['category'];
|
|
}
|
|
if (!empty($validated['tag'])) {
|
|
$taxonomies['tag'] = $validated['tag'];
|
|
}
|
|
|
|
$content->update([...$validated,
|
|
'slug' => Str::slug($validated['title']),
|
|
'taxonomies' => $taxonomies,
|
|
'updated_by' => Auth::id(),
|
|
]);
|
|
|
|
return redirect()->route('admin.contents.contents.index')
|
|
->with('success', 'Konten berhasil diperbarui.');
|
|
}
|
|
|
|
public function destroy(Content $content): RedirectResponse
|
|
{
|
|
$content->update([
|
|
'deleted_by' => Auth::id(),
|
|
]);
|
|
$content->delete();
|
|
|
|
return redirect()->route('admin.contents.contents.index')
|
|
->with('success', 'Konten berhasil dihapus.');
|
|
}
|
|
|
|
|
|
protected function validateContent(Request $request, ?Content $content = null): array
|
|
{
|
|
return $request->validate([
|
|
'type' => ['required', 'string', 'in:' . implode(',', array_map(fn(ContentType $item) => $item->value(), ContentType::cases()))],
|
|
'title' => ['required', 'string', 'max:255'],
|
|
'excerpt' => ['nullable', 'string'],
|
|
'content' => ['nullable', 'string'],
|
|
'category' => ['nullable', 'exists:taxonomies,id'],
|
|
'tag' => ['nullable', 'array'],
|
|
'tag.*' => ['exists:taxonomies,id', 'distinct'],
|
|
'meta_title' => ['nullable', 'string', 'max:255'],
|
|
'meta_description' => ['nullable', 'string'],
|
|
'meta_keywords' => ['nullable', 'array'],
|
|
'meta_keywords.*' => ['string'],
|
|
'status' => ['required', 'string', 'in:' . implode(',', array_map(fn(ContentStatus $item) => $item->value(), ContentStatus::cases()))],
|
|
'featured_image_id' => ['nullable', 'exists:media,id'],
|
|
'published_at' => ['nullable', 'date'],
|
|
], [
|
|
'type.required' => 'Tipe harus diisi.',
|
|
'type.in' => 'Tipe tidak valid.',
|
|
'title.required' => 'Judul harus diisi.',
|
|
'title.max' => 'Judul maksimal 255 karakter.',
|
|
'category.exists' => 'Kategori tidak ditemukan.',
|
|
'tag.exists' => 'Tag tidak ditemukan.',
|
|
'meta_title.max' => 'Meta title maksimal 255 karakter.',
|
|
'status.required' => 'Status harus diisi.',
|
|
'status.in' => 'Status tidak valid.',
|
|
'featured_image_id.exists' => 'Featured image tidak ditemukan.',
|
|
'published_at.date' => 'Tanggal publikasi tidak valid.',
|
|
]);
|
|
}
|
|
}
|