Files
law-api/app/Http/Controllers/Admin/BylawDocumentController.php

119 lines
4.6 KiB
PHP

<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Models\BylawDocument;
use App\Models\BylawLegalAuthority;
use App\Models\BylawSubjectCategory;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
class BylawDocumentController extends Controller
{
public function index(Request $request)
{
$query = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority']);
if ($request->filled('q')) {
$query->where('title', 'like', "%{$request->q}%");
}
$items = $query->latest()->paginate(15)->withQueryString();
return view('admin.bylaw-document.index', compact('items'));
}
public function create()
{
$categories = BylawSubjectCategory::all();
$authorities = BylawLegalAuthority::all();
return view('admin.bylaw-document.create', compact('categories', 'authorities'));
}
public function store(Request $request)
{
$data = $request->validate([
'title' => 'required|string|max:1000',
'category_id' => 'nullable|exists:bylaw_subject_categories,id',
'approval_authority_id' => 'nullable|exists:bylaw_legal_authorities,id',
'approval_date' => 'nullable|date',
'promulgation_authority_id' => 'nullable|exists:bylaw_legal_authorities,id',
'promulgation_date' => 'nullable|date',
'promulgation_number' => 'nullable|string|max:100',
'official_gazette_number' => 'nullable|string|max:100',
'official_gazette_date' => 'nullable|date',
'execution_date' => 'nullable|date',
'bylaw_type' => 'required|in:executive,delegated,independent',
'global_status' => 'required|in:valid,explicitly_repealed,implicitly_repealed,annulled,expired,draft',
'pdf_file' => 'nullable|file|mimes:pdf|max:20480',
'pdf_file_path' => 'nullable|string|max:500',
'is_published' => 'nullable|boolean',
'description' => 'nullable|string',
]);
if ($request->hasFile('pdf_file')) {
$data['pdf_file_path'] = $request->file('pdf_file')->store('pdfs', 'public');
}
$data['is_published'] = $request->boolean('is_published');
BylawDocument::create($data);
return redirect(route('bylaw-document.index'));
}
public function edit(BylawDocument $bylawDocument)
{
$categories = BylawSubjectCategory::all();
$authorities = BylawLegalAuthority::all();
return view('admin.bylaw-document.update', compact('bylawDocument', 'categories', 'authorities'));
}
public function show(BylawDocument $bylawDocument)
{
return redirect(route('bylaw-document.edit', $bylawDocument));
}
public function update(Request $request, BylawDocument $bylawDocument)
{
$data = $request->validate([
'title' => 'required|string|max:1000',
'category_id' => 'nullable|exists:bylaw_subject_categories,id',
'approval_authority_id' => 'nullable|exists:bylaw_legal_authorities,id',
'approval_date' => 'nullable|date',
'promulgation_authority_id' => 'nullable|exists:bylaw_legal_authorities,id',
'promulgation_date' => 'nullable|date',
'promulgation_number' => 'nullable|string|max:100',
'official_gazette_number' => 'nullable|string|max:100',
'official_gazette_date' => 'nullable|date',
'execution_date' => 'nullable|date',
'bylaw_type' => 'required|in:executive,delegated,independent',
'global_status' => 'required|in:valid,explicitly_repealed,implicitly_repealed,annulled,expired,draft',
'pdf_file' => 'nullable|file|mimes:pdf|max:20480',
'pdf_file_path' => 'nullable|string|max:500',
'is_published' => 'nullable|boolean',
'description' => 'nullable|string',
]);
if ($request->hasFile('pdf_file')) {
if ($bylawDocument->pdf_file_path && Storage::disk('public')->exists($bylawDocument->pdf_file_path)) {
Storage::disk('public')->delete($bylawDocument->pdf_file_path);
}
$data['pdf_file_path'] = $request->file('pdf_file')->store('pdfs', 'public');
}
$data['is_published'] = $request->boolean('is_published');
$bylawDocument->update($data);
return redirect(route('bylaw-document.edit', $bylawDocument));
}
public function destroy(BylawDocument $bylawDocument)
{
$bylawDocument->delete();
return redirect(route('bylaw-document.index'));
}
}