148 lines
4.9 KiB
PHP
148 lines
4.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin\Setting;
|
|
|
|
use App\Enums\TaxonomyType;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Taxonomy;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Validation\Rule;
|
|
use Illuminate\View\View;
|
|
use Illuminate\Support\Str;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
class TaxonomyController extends Controller
|
|
{
|
|
public function index(Request $request): View
|
|
{
|
|
$type = TaxonomyType::tryFrom(
|
|
$request->query('type', TaxonomyType::CATEGORY->value)
|
|
) ?? TaxonomyType::CATEGORY;
|
|
|
|
$taxonomies = Taxonomy::where('type', $type)->get();
|
|
|
|
return view('admin.setting.taxonomy.index', compact('taxonomies', 'type'));
|
|
}
|
|
|
|
public function create(Request $request): View
|
|
{
|
|
$type = TaxonomyType::tryFrom(
|
|
$request->query('type', TaxonomyType::CATEGORY->value)
|
|
) ?? TaxonomyType::CATEGORY;
|
|
|
|
return view('admin.setting.taxonomy.create', compact('type'));
|
|
}
|
|
|
|
public function store(Request $request): RedirectResponse
|
|
{
|
|
$type = TaxonomyType::tryFrom(
|
|
$request->query('type', TaxonomyType::CATEGORY->value)
|
|
) ?? TaxonomyType::CATEGORY;
|
|
|
|
$validated = $this->validateTaxonomy($request);
|
|
|
|
Taxonomy::create([...$validated,
|
|
'slug' => Str::of($validated['name'])->slug('-'),
|
|
'created_by' => Auth::id(),
|
|
'updated_by' => Auth::id(),
|
|
]);
|
|
|
|
return redirect()->route('admin.settings.taxonomies.index', ['type' => $type->value()])
|
|
->with('success', 'Taxonomy berhasil dibuat.');
|
|
}
|
|
|
|
public function show(Request $request, Taxonomy $taxonomy): View
|
|
{
|
|
$type = TaxonomyType::tryFrom(
|
|
$request->query('type', TaxonomyType::CATEGORY->value)
|
|
) ?? TaxonomyType::CATEGORY;
|
|
|
|
return view('admin.setting.taxonomy.show', compact('taxonomy', 'type'));
|
|
}
|
|
|
|
public function edit(Request $request, Taxonomy $taxonomy): View
|
|
{
|
|
$type = TaxonomyType::tryFrom(
|
|
$request->query('type', TaxonomyType::CATEGORY->value)
|
|
) ?? TaxonomyType::CATEGORY;
|
|
|
|
$types = TaxonomyType::cases();
|
|
|
|
return view('admin.setting.taxonomy.edit', compact('taxonomy', 'types', 'type'));
|
|
}
|
|
|
|
public function update(Request $request, Taxonomy $taxonomy): RedirectResponse
|
|
{
|
|
$type = TaxonomyType::tryFrom(
|
|
$request->query('type', TaxonomyType::CATEGORY->value)
|
|
) ?? TaxonomyType::CATEGORY;
|
|
|
|
$validated = $this->validateTaxonomy($request, $taxonomy);
|
|
|
|
$taxonomy->update([...$validated,
|
|
'slug' => Str::of($validated['name'])->slug('-'),
|
|
'updated_by' => auth()->user()->id,
|
|
]);
|
|
|
|
return redirect()->route('admin.settings.taxonomies.index', ['type' => $type->value()])
|
|
->with('success', 'Taxonomy berhasil diperbarui.');
|
|
}
|
|
|
|
public function destroy(Request $request, Taxonomy $taxonomy): RedirectResponse
|
|
{
|
|
$type = TaxonomyType::tryFrom(
|
|
$request->query('type', TaxonomyType::CATEGORY->value)
|
|
) ?? TaxonomyType::CATEGORY;
|
|
|
|
$taxonomy->update([
|
|
'deleted_by' => auth()->user()->id,
|
|
]);
|
|
$taxonomy->delete();
|
|
|
|
return redirect()->route('admin.settings.taxonomies.index', ['type' => $type->value()])
|
|
->with('success', 'Taxonomy berhasil dihapus.');
|
|
}
|
|
|
|
protected function validateTaxonomy(Request $request, ?Taxonomy $taxonomy = null): array
|
|
{
|
|
$types = array_map(fn(TaxonomyType $type) => $type->value, TaxonomyType::cases());
|
|
|
|
$rules = [
|
|
'type' => ['required', Rule::in($types)],
|
|
'name' => ['required', 'string', 'max:255',
|
|
function ($attribute, $value, $fail) use ($taxonomy) {
|
|
$exists = DB::table('taxonomies')
|
|
->whereRaw('LOWER(name) = ?', [strtolower($value)])
|
|
->when($taxonomy, fn ($q) => $q->where('id', '!=', $taxonomy->id))
|
|
->exists();
|
|
|
|
if ($exists) {
|
|
$fail('Nama sudah digunakan.');
|
|
}
|
|
},
|
|
],
|
|
'slug' => [
|
|
'nullable',
|
|
'string',
|
|
'max:255',
|
|
Rule::unique('taxonomies', 'slug')->ignore($taxonomy?->id),
|
|
],
|
|
];
|
|
|
|
$messages = [
|
|
'type.required' => 'Tipe taxonomy harus diisi.',
|
|
'type.in' => 'Tipe taxonomy tidak valid.',
|
|
'name.required' => 'Nama harus diisi.',
|
|
'name.max' => 'Nama maksimal 255 karakter.',
|
|
'name.unique' => 'Nama sudah digunakan.',
|
|
'slug.max' => 'Slug maksimal 255 karakter.',
|
|
'slug.unique' => 'Slug sudah digunakan.',
|
|
];
|
|
|
|
return $request->validate($rules, $messages);
|
|
}
|
|
}
|