feat(jenis-lampiran): tambahkan fitur jenis lampiran
- Menambahkan model JenisLampiran dengan relasi ke LampiranDokumen. - Membuat migrasi untuk tabel jenis_lampiran. - Menambahkan rute dan kontroler untuk operasi CRUD jenis lampiran. - Menambahkan permintaan validasi untuk jenis lampiran. - Menambahkan tampilan untuk membuat dan mengedit jenis lampiran. - Menambahkan breadcrumb untuk navigasi jenis lampiran. - Menambahkan fitur ekspor data jenis lampiran ke Excel.
This commit is contained in:
46
app/Exports/JenisLampiranExport.php
Normal file
46
app/Exports/JenisLampiranExport.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Lpj\app\Exports;
|
||||
|
||||
use Maatwebsite\Excel\Concerns\FromCollection;
|
||||
use Maatwebsite\Excel\Concerns\WithColumnFormatting;
|
||||
use Maatwebsite\Excel\Concerns\WithHeadings;
|
||||
use Maatwebsite\Excel\Concerns\WithMapping;
|
||||
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
|
||||
use Modules\Lpj\app\Models\JenisLampiran;
|
||||
|
||||
class JenisLampiranExport implements WithColumnFormatting, WithHeadings, FromCollection, WithMapping
|
||||
{
|
||||
public function collection()
|
||||
{
|
||||
return JenisLampiran::all();
|
||||
}
|
||||
|
||||
public function map($row): array
|
||||
{
|
||||
return [
|
||||
$row->id,
|
||||
$row->nama,
|
||||
$row->deskripsi,
|
||||
$row->created_at
|
||||
];
|
||||
}
|
||||
|
||||
public function headings(): array
|
||||
{
|
||||
return [
|
||||
'ID',
|
||||
'Nama',
|
||||
'Deskripsi',
|
||||
'Created At'
|
||||
];
|
||||
}
|
||||
|
||||
public function columnFormats(): array
|
||||
{
|
||||
return [
|
||||
'A' => NumberFormat::FORMAT_NUMBER,
|
||||
'D' => NumberFormat::FORMAT_DATE_DATETIME
|
||||
];
|
||||
}
|
||||
}
|
||||
172
app/Http/Controllers/JenisLampiranController.php
Normal file
172
app/Http/Controllers/JenisLampiranController.php
Normal file
@@ -0,0 +1,172 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Lpj\Http\Controllers;
|
||||
|
||||
use Exception;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Routing\Controller;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Maatwebsite\Excel\Facades\Excel;
|
||||
use Modules\Lpj\Exports\JenisLampiranExport;
|
||||
use Modules\Lpj\Http\Requests\JenisLampiranRequest;
|
||||
use Modules\Lpj\Models\JenisLampiran;
|
||||
|
||||
class JenisLampiranController extends Controller
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
$jenisLampirans = JenisLampiran::all();
|
||||
return view('lpj::jenis_lampiran.index', compact('jenisLampirans'));
|
||||
}
|
||||
|
||||
public function store(JenisLampiranRequest $request)
|
||||
{
|
||||
DB::beginTransaction();
|
||||
try {
|
||||
$validated = $request->validated();
|
||||
$validated['created_by'] = Auth::id();
|
||||
|
||||
$jenisLampiran = JenisLampiran::create($validated);
|
||||
|
||||
DB::commit();
|
||||
return redirect()
|
||||
->route('basicdata.jenis-lampiran.index')
|
||||
->with('success', 'Jenis Lampiran berhasil ditambahkan.');
|
||||
} catch (Exception $e) {
|
||||
DB::rollBack();
|
||||
return redirect()
|
||||
->back()
|
||||
->with('error', 'Gagal menambahkan Jenis Lampiran: ' . $e->getMessage())
|
||||
->withInput();
|
||||
}
|
||||
}
|
||||
|
||||
public function create()
|
||||
{
|
||||
return view('lpj::jenis_lampiran.create');
|
||||
}
|
||||
|
||||
public function show($id)
|
||||
{
|
||||
$jenisLampiran = JenisLampiran::findOrFail($id);
|
||||
return view('lpj::jenis_lampiran.show', compact('jenisLampiran'));
|
||||
}
|
||||
|
||||
public function edit($id)
|
||||
{
|
||||
$jenisLampiran = JenisLampiran::findOrFail($id);
|
||||
return view('lpj::jenis_lampiran.create', compact('jenisLampiran'));
|
||||
}
|
||||
|
||||
public function update(JenisLampiranRequest $request, $id)
|
||||
{
|
||||
DB::beginTransaction();
|
||||
try {
|
||||
$jenisLampiran = JenisLampiran::findOrFail($id);
|
||||
$validated = $request->validated();
|
||||
$validated['updated_by'] = Auth::id();
|
||||
|
||||
$jenisLampiran->update($validated);
|
||||
|
||||
DB::commit();
|
||||
return redirect()
|
||||
->route('basicdata.jenis-lampiran.index')
|
||||
->with('success', 'Jenis Lampiran berhasil diperbarui.');
|
||||
} catch (Exception $e) {
|
||||
DB::rollBack();
|
||||
return redirect()
|
||||
->back()
|
||||
->with('error', 'Gagal memperbarui Jenis Lampiran: ' . $e->getMessage())
|
||||
->withInput();
|
||||
}
|
||||
}
|
||||
|
||||
public function destroy($id)
|
||||
{
|
||||
DB::beginTransaction();
|
||||
try {
|
||||
$jenisLampiran = JenisLampiran::findOrFail($id);
|
||||
$jenisLampiran->deleted_by = Auth::id();
|
||||
$jenisLampiran->save();
|
||||
|
||||
$jenisLampiran->delete();
|
||||
|
||||
DB::commit();
|
||||
echo json_encode(['success' => true, 'message' => 'Jenis Lampiran berhasil dihapus.']);
|
||||
|
||||
} catch (Exception $e) {
|
||||
DB::rollBack();
|
||||
echo json_encode([
|
||||
'success' => false,
|
||||
'message' => 'Gagal menghapus Jenis Lampiran: ' . $e->getMessage()
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
public function dataForDatatables(Request $request)
|
||||
{
|
||||
// Retrieve data from the database
|
||||
$query = JenisLampiran::query();
|
||||
|
||||
// Apply search filter if provided
|
||||
if ($request->has('search') && !empty($request->get('search'))) {
|
||||
$search = $request->get('search');
|
||||
$query->where(function ($q) use ($search) {
|
||||
$q->where('nama', 'LIKE', "%$search%")
|
||||
->orWhere('deskripsi', 'LIKE', "%$search%");
|
||||
});
|
||||
}
|
||||
|
||||
// Apply sorting if provided
|
||||
if ($request->has('sortOrder') && !empty($request->get('sortOrder'))) {
|
||||
$order = $request->get('sortOrder');
|
||||
$column = $request->get('sortField');
|
||||
$query->orderBy($column, $order);
|
||||
}
|
||||
|
||||
// Get the total count of records
|
||||
$totalRecords = $query->count();
|
||||
|
||||
// Apply pagination if provided
|
||||
if ($request->has('page') && $request->has('size')) {
|
||||
$page = $request->get('page');
|
||||
$size = $request->get('size');
|
||||
$offset = ($page - 1) * $size; // Calculate the offset
|
||||
|
||||
$query->skip($offset)->take($size);
|
||||
}
|
||||
|
||||
// Get the filtered count of records
|
||||
$filteredRecords = $query->count();
|
||||
|
||||
// Get the data for the current page
|
||||
$data = $query->get();
|
||||
|
||||
// Calculate the page count
|
||||
$pageCount = ceil($totalRecords / $request->get('size'));
|
||||
|
||||
// Calculate the current page number
|
||||
$currentPage = $request->get('page', 1);
|
||||
|
||||
// Return the response data as a JSON object
|
||||
return response()->json([
|
||||
'draw' => $request->get('draw'),
|
||||
'recordsTotal' => $totalRecords,
|
||||
'recordsFiltered' => $filteredRecords,
|
||||
'pageCount' => $pageCount,
|
||||
'page' => $currentPage,
|
||||
'totalCount' => $totalRecords,
|
||||
'data' => $data,
|
||||
]);
|
||||
}
|
||||
|
||||
public function export()
|
||||
{
|
||||
if (is_null($this->user) || !$this->user->can('jenis_lampiran.export')) {
|
||||
abort(403, 'Sorry! You are not allowed to export jenis lampiran.');
|
||||
}
|
||||
|
||||
return Excel::download(new JenisLampiranExport, 'jenis_lampiran.xlsx');
|
||||
}
|
||||
}
|
||||
54
app/Http/Requests/JenisLampiranRequest.php
Normal file
54
app/Http/Requests/JenisLampiranRequest.php
Normal file
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Lpj\Http\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class JenisLampiranRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function authorize()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
$rules = [
|
||||
'nama' => [
|
||||
'required',
|
||||
'string',
|
||||
'max:255',
|
||||
Rule::unique('jenis_lampiran')->where(function ($query) {
|
||||
return $query->whereNull('deleted_at');
|
||||
})->ignore($this->route('jenis_lampiran')),
|
||||
],
|
||||
'deskripsi' => 'nullable|string',
|
||||
];
|
||||
|
||||
return $rules;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get custom messages for validator errors.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function messages()
|
||||
{
|
||||
return [
|
||||
'nama.required' => 'Nama jenis lampiran harus diisi.',
|
||||
'nama.max' => 'Nama jenis lampiran tidak boleh lebih dari 255 karakter.',
|
||||
];
|
||||
}
|
||||
}
|
||||
20
app/Models/JenisLampiran.php
Normal file
20
app/Models/JenisLampiran.php
Normal file
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Lpj\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use Modules\Lpj\Models\Base;
|
||||
use Modules\Lpj\Models\LampiranDokumen;
|
||||
|
||||
class JenisLampiran extends Base
|
||||
{
|
||||
|
||||
protected $table = 'jenis_lampiran';
|
||||
protected $fillable = ['nama', 'deskripsi'];
|
||||
|
||||
public function lampiranDokumen()
|
||||
{
|
||||
return $this->hasMany(LampiranDokumen::class);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user