feat(custom-field): tambahkan fitur custom field
- Menambahkan model CustomField dengan atribut mass assignable. - Membuat request validation untuk custom field. - Menambahkan route dan breadcrumb untuk custom field. - Membuat migration untuk tabel custom_fields. - Menambahkan export functionality untuk custom field. - Membuat view untuk menambah dan mengedit custom field.
This commit is contained in:
49
app/Exports/CustomFieldExport.php
Normal file
49
app/Exports/CustomFieldExport.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Lpj\Exports;
|
||||
|
||||
use Maatwebsite\Excel\Concerns\FromCollection;
|
||||
use Maatwebsite\Excel\Concerns\WithColumnFormatting;
|
||||
use Maatwebsite\Excel\Concerns\WithHeadings;
|
||||
use Maatwebsite\Excel\Concerns\WithMapping;
|
||||
use Modules\Lpj\Models\CustomField;
|
||||
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
|
||||
|
||||
class CustomFieldExport implements WithColumnFormatting, WithHeadings, FromCollection, WithMapping
|
||||
{
|
||||
public function collection()
|
||||
{
|
||||
return CustomField::all();
|
||||
}
|
||||
|
||||
public function map($row): array
|
||||
{
|
||||
return [
|
||||
$row->id,
|
||||
$row->name,
|
||||
$row->type,
|
||||
$row->created_at,
|
||||
$row->updated_at,
|
||||
];
|
||||
}
|
||||
|
||||
public function headings(): array
|
||||
{
|
||||
return [
|
||||
'ID',
|
||||
'Name',
|
||||
'Type',
|
||||
'Created At',
|
||||
'Updated At',
|
||||
];
|
||||
}
|
||||
|
||||
public function columnFormats(): array
|
||||
{
|
||||
return [
|
||||
'A' => NumberFormat::FORMAT_NUMBER,
|
||||
'D' => NumberFormat::FORMAT_DATE_DDMMYYYY,
|
||||
'E' => NumberFormat::FORMAT_DATE_DDMMYYYY,
|
||||
];
|
||||
}
|
||||
}
|
||||
150
app/Http/Controllers/CustomFieldController.php
Normal file
150
app/Http/Controllers/CustomFieldController.php
Normal file
@@ -0,0 +1,150 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Lpj\Http\Controllers;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Exception;
|
||||
use Illuminate\Http\Request;
|
||||
use Maatwebsite\Excel\Facades\Excel;
|
||||
use Modules\Lpj\Exports\CustomFieldExport;
|
||||
use Modules\Lpj\Http\Requests\CustomFieldRequest;
|
||||
use Modules\Lpj\Models\CustomField;
|
||||
|
||||
class CustomFieldController extends Controller
|
||||
{
|
||||
public $user;
|
||||
|
||||
public function index()
|
||||
{
|
||||
return view('lpj::custom_fields.index');
|
||||
}
|
||||
|
||||
public function store(CustomFieldRequest $request)
|
||||
{
|
||||
$validate = $request->validated();
|
||||
|
||||
if ($validate) {
|
||||
try {
|
||||
// Save to database
|
||||
CustomField::create($validate);
|
||||
return redirect()
|
||||
->route('basicdata.custom-field.index')
|
||||
->with('success', 'Custom Field created successfully');
|
||||
} catch (Exception $e) {
|
||||
return redirect()
|
||||
->route('basicdata.custom-field.create')
|
||||
->with('error', $e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function create()
|
||||
{
|
||||
return view('lpj::custom_fields.create');
|
||||
}
|
||||
|
||||
public function edit($id)
|
||||
{
|
||||
$customField = CustomField::find($id);
|
||||
return view('lpj::custom_fields.create', compact('customField'));
|
||||
}
|
||||
|
||||
public function update(CustomFieldRequest $request, $id)
|
||||
{
|
||||
$validate = $request->validated();
|
||||
|
||||
if ($validate) {
|
||||
try {
|
||||
// Update in database
|
||||
$customField = CustomField::find($id);
|
||||
$customField->update($validate);
|
||||
return redirect()
|
||||
->route('basicdata.custom-field.index')
|
||||
->with('success', 'Custom Field updated successfully');
|
||||
} catch (Exception $e) {
|
||||
return redirect()
|
||||
->route('basicdata.custom-field.edit', $id)
|
||||
->with('error', 'Failed to update custom field');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function destroy($id)
|
||||
{
|
||||
try {
|
||||
// Delete from database
|
||||
$customField = CustomField::find($id);
|
||||
$customField->delete();
|
||||
|
||||
echo json_encode(['success' => true, 'message' => 'Custom Field deleted successfully']);
|
||||
} catch (Exception $e) {
|
||||
echo json_encode(['success' => false, 'message' => 'Failed to delete custom field']);
|
||||
}
|
||||
}
|
||||
|
||||
public function dataForDatatables(Request $request)
|
||||
{
|
||||
if (is_null($this->user) || !$this->user->can('custom_fields.view')) {
|
||||
//abort(403, 'Sorry! You are not allowed to view custom fields.');
|
||||
}
|
||||
|
||||
// Retrieve data from the database
|
||||
$query = CustomField::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('name', 'LIKE', "%$search%");
|
||||
$q->orWhere('type', '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 = 0 + 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()
|
||||
{
|
||||
return Excel::download(new CustomFieldExport, 'custom_fields.xlsx');
|
||||
}
|
||||
}
|
||||
33
app/Http/Requests/CustomFieldRequest.php
Normal file
33
app/Http/Requests/CustomFieldRequest.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Lpj\Http\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class CustomFieldRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'name' => 'required|max:255',
|
||||
'type' => 'required|in:text,select,radio,checkbox',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function prepareValidationData($data){
|
||||
if(!$this->type){
|
||||
$this->merge(['type' => 'text']);
|
||||
}
|
||||
}
|
||||
}
|
||||
24
app/Models/CustomField.php
Normal file
24
app/Models/CustomField.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Lpj\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
// use Modules\Lpj\Database\Factories\CustomFieldFactory;
|
||||
|
||||
class customField extends Base
|
||||
{
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*/
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'type'
|
||||
];
|
||||
|
||||
// protected static function newFactory(): CustomFieldFactory
|
||||
// {
|
||||
// // return CustomFieldFactory::new();
|
||||
// }
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('custom_fields', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->string('name');
|
||||
$table->string('type');
|
||||
$table->timestamps();
|
||||
$table->softDeletes();
|
||||
|
||||
$table->unsignedBigInteger('created_by')->nullable();
|
||||
$table->unsignedBigInteger('updated_by')->nullable();
|
||||
$table->unsignedBigInteger('deleted_by')->nullable();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('custom_fields');
|
||||
}
|
||||
};
|
||||
11
module.json
11
module.json
@@ -851,6 +851,17 @@
|
||||
"administrator",
|
||||
"admin"
|
||||
]
|
||||
},
|
||||
{
|
||||
"title": "Custom Field",
|
||||
"path": "basicdata.custom-field",
|
||||
"classes": "",
|
||||
"attributes": [],
|
||||
"permission": "",
|
||||
"roles": [
|
||||
"administrator",
|
||||
"admin"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
67
resources/views/custom_fields/create.blade.php
Normal file
67
resources/views/custom_fields/create.blade.php
Normal file
@@ -0,0 +1,67 @@
|
||||
@php
|
||||
$route = explode('.', Route::currentRouteName());
|
||||
@endphp
|
||||
|
||||
@extends('layouts.main')
|
||||
|
||||
@section('breadcrumbs')
|
||||
{{ Breadcrumbs::render(request()->route()->getName()) }}
|
||||
@endsection
|
||||
|
||||
@section('content')
|
||||
<div class="w-full grid gap-5 lg:gap-7.5 mx-auto">
|
||||
@if(isset($customField->id))
|
||||
<form action="{{ route('basicdata.custom-field.update', $customField->id) }}" method="POST">
|
||||
<input type="hidden" name="id" value="{{ $customField->id }}">
|
||||
@method('PUT')
|
||||
@else
|
||||
<form method="POST" action="{{ route('basicdata.custom-field.store') }}">
|
||||
@endif
|
||||
@csrf
|
||||
<div class="card border border-agi-100 pb-2.5">
|
||||
<div class="card-header bg-agi-50" id="basic_settings">
|
||||
<h3 class="card-title">
|
||||
{{ isset($customField->id) ? 'Edit' : 'Tambah' }} Custom Field
|
||||
</h3>
|
||||
<div class="flex items-center gap-2">
|
||||
<a href="{{ route('basicdata.custom-field.index') }}" class="btn btn-xs btn-info"><i class="ki-filled ki-exit-left"></i> Back</a>
|
||||
</div>
|
||||
</div>
|
||||
<div class="card-body grid gap-5">
|
||||
<div class="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
|
||||
<label class="form-label max-w-56">
|
||||
Name
|
||||
</label>
|
||||
<div class="flex flex-wrap items-baseline w-full">
|
||||
<input class="input @error('name') border-danger bg-danger-light @enderror" type="text" name="name" value="{{ $customField->name ?? '' }}">
|
||||
@error('name')
|
||||
<em class="alert text-danger text-sm">{{ $message }}</em>
|
||||
@enderror
|
||||
</div>
|
||||
</div>
|
||||
<div class="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
|
||||
<label class="form-label max-w-56">
|
||||
Type
|
||||
</label>
|
||||
<div class="flex flex-wrap items-baseline w-full">
|
||||
<select class="input @error('type') border-danger bg-danger-light @enderror" name="type">
|
||||
<option value="text" {{ (isset($customField->type) && $customField->type == 'text') ? 'selected' : '' }}>Text</option>
|
||||
<option value="radio" {{ (isset($customField->type) && $customField->type == 'radio') ? 'selected' : '' }}>Radio</option>
|
||||
<option value="number" {{ (isset($customField->type) && $customField->type == 'select') ? 'selected' : '' }}>Select</option>
|
||||
<option value="option" {{ (isset($customField->type) && $customField->type == 'checkbox') ? 'selected' : '' }}>Checkbox</option>
|
||||
</select>
|
||||
@error('type')
|
||||
<em class="alert text-danger text-sm">{{ $message }}</em>
|
||||
@enderror
|
||||
</div>
|
||||
</div>
|
||||
<div class="flex justify-end">
|
||||
<button type="submit" class="btn btn-primary">
|
||||
Save
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
@endsection
|
||||
146
resources/views/custom_fields/index.blade.php
Normal file
146
resources/views/custom_fields/index.blade.php
Normal file
@@ -0,0 +1,146 @@
|
||||
@extends('layouts.main')
|
||||
|
||||
@section('breadcrumbs')
|
||||
{{ Breadcrumbs::render('basicdata.custom-field') }}
|
||||
@endsection
|
||||
|
||||
@section('content')
|
||||
<div class="grid">
|
||||
<div class="card border border-agi-100 card-grid min-w-full" data-datatable="false" data-datatable-page-size="10" data-datatable-state-save="false" id="custom-field-table" data-api-url="{{ route('basicdata.custom-field.datatables') }}">
|
||||
<div class="card-header bg-agi-50 py-5 flex-wrap">
|
||||
<h3 class="card-title">
|
||||
Daftar Custom Field
|
||||
</h3>
|
||||
<div class="flex flex-wrap gap-2 lg:gap-5">
|
||||
<div class="flex">
|
||||
<label class="input input-sm"> <i class="ki-filled ki-magnifier"> </i>
|
||||
<input placeholder="Search Custom Field" id="search" type="text" value="">
|
||||
</label>
|
||||
</div>
|
||||
<div class="flex flex-wrap gap-2.5">
|
||||
<div class="h-[24px] border border-r-gray-200"></div>
|
||||
<a class="btn btn-sm btn-light" href="{{ route('basicdata.custom-field.export') }}"> Export to Excel </a>
|
||||
<a class="btn btn-sm btn-primary" href="{{ route('basicdata.custom-field.create') }}"> Tambah Custom Field </a>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="card-body">
|
||||
<div class="scrollable-x-auto">
|
||||
<table class="table table-auto table-border align-middle text-gray-700 font-medium text-sm" data-datatable-table="true">
|
||||
<thead>
|
||||
<tr>
|
||||
<th class="w-14">
|
||||
<input class="checkbox checkbox-sm" data-datatable-check="true" type="checkbox"/>
|
||||
</th>
|
||||
<th class="min-w-[250px]" data-datatable-column="name">
|
||||
<span class="sort"> <span class="sort-label"> Custom Field </span>
|
||||
<span class="sort-icon"> </span> </span>
|
||||
</th>
|
||||
<th class="min-w-[250px]" data-datatable-column="type">
|
||||
<span class="sort"> <span class="sort-label"> Type </span>
|
||||
<span class="sort-icon"> </span> </span>
|
||||
</th>
|
||||
<th class="min-w-[50px] text-center" data-datatable-column="actions">Action</th>
|
||||
</tr>
|
||||
</thead>
|
||||
</table>
|
||||
</div>
|
||||
<div class="card-footer justify-center md:justify-between flex-col md:flex-row gap-3 text-gray-600 text-2sm font-medium">
|
||||
<div class="flex items-center gap-2">
|
||||
Show
|
||||
<select class="select select-sm w-16" data-datatable-size="true" name="perpage"> </select> per page
|
||||
</div>
|
||||
<div class="flex items-center gap-4">
|
||||
<span data-datatable-info="true"> </span>
|
||||
<div class="pagination" data-datatable-pagination="true">
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@endsection
|
||||
|
||||
@push('scripts')
|
||||
<script type="text/javascript">
|
||||
function deleteData(data) {
|
||||
Swal.fire({
|
||||
title: 'Are you sure?',
|
||||
text: "You won't be able to revert this!",
|
||||
icon: 'warning',
|
||||
showCancelButton: true,
|
||||
confirmButtonColor: '#3085d6',
|
||||
cancelButtonColor: '#d33',
|
||||
confirmButtonText: 'Yes, delete it!'
|
||||
}).then((result) => {
|
||||
if (result.isConfirmed) {
|
||||
$.ajaxSetup({
|
||||
headers: {
|
||||
'X-CSRF-TOKEN': '{{ csrf_token() }}'
|
||||
}
|
||||
});
|
||||
|
||||
$.ajax(`basic-data/custom-field/${data}`, {
|
||||
type: 'DELETE'
|
||||
}).then((response) => {
|
||||
swal.fire('Deleted!', 'User has been deleted.', 'success').then(() => {
|
||||
window.location.reload();
|
||||
});
|
||||
}).catch((error) => {
|
||||
console.error('Error:', error);
|
||||
Swal.fire('Error!', 'An error occurred while deleting the file.', 'error');
|
||||
});
|
||||
}
|
||||
})
|
||||
}
|
||||
</script>
|
||||
<script type="module">
|
||||
const element = document.querySelector('#custom-field-table');
|
||||
const searchInput = document.getElementById('search');
|
||||
|
||||
const apiUrl = element.getAttribute('data-api-url');
|
||||
const dataTableOptions = {
|
||||
apiEndpoint: apiUrl,
|
||||
pageSize: 5,
|
||||
columns: {
|
||||
select: {
|
||||
render: (item, data, context) => {
|
||||
const checkbox = document.createElement('input');
|
||||
checkbox.className = 'checkbox checkbox-sm';
|
||||
checkbox.type = 'checkbox';
|
||||
checkbox.value = data.id.toString();
|
||||
checkbox.setAttribute('data-datatable-row-check', 'true');
|
||||
return checkbox.outerHTML.trim();
|
||||
},
|
||||
},
|
||||
name: {
|
||||
title: 'Custom Field',
|
||||
},
|
||||
type: {
|
||||
title: 'Type',
|
||||
},
|
||||
actions: {
|
||||
title: 'Status',
|
||||
render: (item, data) => {
|
||||
return `<div class="flex flex-nowrap justify-center">
|
||||
<a class="btn btn-sm btn-icon btn-clear btn-info" href="basic-data/custom-field/${data.id}/edit">
|
||||
<i class="ki-outline ki-notepad-edit"></i>
|
||||
</a>
|
||||
<a onclick="deleteData(${data.id})" class="delete btn btn-sm btn-icon btn-clear btn-danger">
|
||||
<i class="ki-outline ki-trash"></i>
|
||||
</a>
|
||||
</div>`;
|
||||
},
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
let dataTable = new KTDataTable(element, dataTableOptions);
|
||||
// Custom search functionality
|
||||
searchInput.addEventListener('input', function () {
|
||||
const searchValue = this.value.trim();
|
||||
dataTable.search(searchValue, true);
|
||||
});
|
||||
</script>
|
||||
@endpush
|
||||
|
||||
@@ -9,6 +9,22 @@
|
||||
});
|
||||
}
|
||||
|
||||
Breadcrumbs::for('basicdata.custom-field', function (BreadcrumbTrail $trail) {
|
||||
$trail->parent('basicdata');
|
||||
$trail->push('Custom Field', route('basicdata.custom-field.index'));
|
||||
});
|
||||
|
||||
Breadcrumbs::for('basicdata.custom-field.create', function (BreadcrumbTrail $trail) {
|
||||
$trail->parent('basicdata.custom-field');
|
||||
$trail->push('Tambah Custom Field', route('basicdata.custom-field.create'));
|
||||
});
|
||||
|
||||
Breadcrumbs::for('basicdata.custom-field.edit', function (BreadcrumbTrail $trail) {
|
||||
$trail->parent('basicdata.custom-field');
|
||||
$trail->push('Edit Custom Field');
|
||||
});
|
||||
|
||||
|
||||
Breadcrumbs::for('basicdata.jenis-fasilitas-kredit', function (BreadcrumbTrail $trail) {
|
||||
$trail->parent('basicdata');
|
||||
$trail->push('Jenis Fasilitas Kredit', route('basicdata.jenis-fasilitas-kredit.index'));
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use Modules\Lpj\Http\Controllers\ActivityController;
|
||||
use Modules\Lpj\Http\Controllers\ArahMataAnginController;
|
||||
use Modules\Lpj\Http\Controllers\CustomFieldController;
|
||||
use Modules\Lpj\Http\Controllers\DebitureController;
|
||||
use Modules\Lpj\Http\Controllers\DokumenJaminanController;
|
||||
use Modules\Lpj\Http\Controllers\HubunganPemilikJaminanController;
|
||||
@@ -51,6 +52,16 @@ Route::middleware(['auth'])->group(function () {
|
||||
Route::get('api/check-penawaran/{nomor_registrasi}', [TenderController::class, 'checkPenawaranExistence']);
|
||||
|
||||
Route::name('basicdata.')->prefix('basic-data')->group(function () {
|
||||
|
||||
Route::name('custom-field.')->prefix('custom-field')->group(function () {
|
||||
Route::get('restore/{id}', [CustomFieldController::class, 'restore'])->name('restore');
|
||||
Route::get('datatables', [CustomFieldController::class, 'dataForDatatables'])->name(
|
||||
'datatables',
|
||||
);
|
||||
Route::get('export', [CustomFieldController::class, 'export'])->name('export');
|
||||
});
|
||||
Route::resource('custom-field', CustomFieldController::class);
|
||||
|
||||
Route::name('jenis-fasilitas-kredit.')->prefix('jenis-fasilitas-kredit')->group(function () {
|
||||
Route::get('restore/{id}', [JenisFasilitasKreditController::class, 'restore'])->name('restore');
|
||||
Route::get('datatables', [JenisFasilitasKreditController::class, 'dataForDatatables'])->name(
|
||||
|
||||
Reference in New Issue
Block a user