From 18cbb0bbc5e2c63317ea8e02963d174096477053 Mon Sep 17 00:00:00 2001 From: Daeng Deni Mardaeni Date: Thu, 30 Jan 2025 16:14:48 +0700 Subject: [PATCH] feat(custom-field): tambahkan kolom label dan urutan prioritas - Menambahkan kolom 'label' pada tabel custom_fields. - Menambahkan kolom 'urutan_prioritas' pada tabel custom_fields. - Memperbarui model CustomField untuk menyertakan kolom baru. - Memperbarui form input untuk label dan urutan prioritas di tampilan create dan index. - Menambahkan validasi untuk label dan urutan prioritas di CustomFieldRequest. --- .../Controllers/CustomFieldController.php | 271 +++++++++--------- app/Http/Requests/CustomFieldRequest.php | 25 ++ app/Models/CustomField.php | 4 +- ...rutan_prioritas_to_custom_fields_table.php | 28 ++ ...91021_add_label_to_custom_fields_table.php | 28 ++ .../views/custom_fields/create.blade.php | 22 ++ resources/views/custom_fields/index.blade.php | 9 +- 7 files changed, 251 insertions(+), 136 deletions(-) create mode 100644 database/migrations/2025_01_30_085835_add_urutan_prioritas_to_custom_fields_table.php create mode 100644 database/migrations/2025_01_30_091021_add_label_to_custom_fields_table.php diff --git a/app/Http/Controllers/CustomFieldController.php b/app/Http/Controllers/CustomFieldController.php index ad41ee9..b42ab89 100644 --- a/app/Http/Controllers/CustomFieldController.php +++ b/app/Http/Controllers/CustomFieldController.php @@ -1,150 +1,153 @@ validated(); + public function index() + { + return view('lpj::custom_fields.index'); + } - 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 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 create() + { + $urutan_prioritas = CustomField::max('urutan_prioritas')+1; + return view('lpj::custom_fields.create', compact('urutan_prioritas')); } - } - public function destroy($id) - { - try { - // Delete from database + public function edit($id) + { $customField = CustomField::find($id); - $customField->delete(); + $urutan_prioritas = $customField->urutan_prioritas ?? CustomField::max('urutan_prioritas')+1; + return view('lpj::custom_fields.create', compact('customField', 'urutan_prioritas' )); + } - 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 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('label', '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'); } } - - 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'); - } -} diff --git a/app/Http/Requests/CustomFieldRequest.php b/app/Http/Requests/CustomFieldRequest.php index 08c5740..0cf1cf8 100644 --- a/app/Http/Requests/CustomFieldRequest.php +++ b/app/Http/Requests/CustomFieldRequest.php @@ -3,6 +3,8 @@ namespace Modules\Lpj\Http\Requests; use Illuminate\Foundation\Http\FormRequest; +use Modules\Lpj\Models\customField; +use Illuminate\Validation\Rule; class CustomFieldRequest extends FormRequest { @@ -14,6 +16,12 @@ class CustomFieldRequest extends FormRequest return [ 'name' => 'required|max:255', 'type' => 'required|in:text,select,radio,checkbox', + 'label' => 'nullable|max:255', + 'urutan_prioritas' => [ + 'nullable', + 'integer', + Rule::unique('custom_fields')->ignore($this->route('custom_field')), + ], ]; } @@ -29,5 +37,22 @@ class CustomFieldRequest extends FormRequest if(!$this->type){ $this->merge(['type' => 'text']); } + + if (!$this->urutan_prioritas) { + $maxPrioritas = CustomField::max('urutan_prioritas') ?? 0; + $this->merge(['urutan_prioritas' => $maxPrioritas + 1]); + } + } + + /** + * Get custom messages for validator errors. + * + * @return array + */ + public function messages() + { + return [ + 'urutan_prioritas.unique' => 'Urutan prioritas sudah digunakan. Silakan pilih nomor lain.', + ]; } } diff --git a/app/Models/CustomField.php b/app/Models/CustomField.php index 94c43be..8efcd66 100644 --- a/app/Models/CustomField.php +++ b/app/Models/CustomField.php @@ -14,7 +14,9 @@ class customField extends Base */ protected $fillable = [ 'name', - 'type' + 'type', + 'urutan_prioritas', + 'label' ]; // protected static function newFactory(): CustomFieldFactory diff --git a/database/migrations/2025_01_30_085835_add_urutan_prioritas_to_custom_fields_table.php b/database/migrations/2025_01_30_085835_add_urutan_prioritas_to_custom_fields_table.php new file mode 100644 index 0000000..f4250e3 --- /dev/null +++ b/database/migrations/2025_01_30_085835_add_urutan_prioritas_to_custom_fields_table.php @@ -0,0 +1,28 @@ +integer('urutan_prioritas')->nullable()->after('type'); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('custom_fields', function (Blueprint $table) { + $table->dropColumn('urutan_prioritas'); + }); + } +}; diff --git a/database/migrations/2025_01_30_091021_add_label_to_custom_fields_table.php b/database/migrations/2025_01_30_091021_add_label_to_custom_fields_table.php new file mode 100644 index 0000000..39f8b30 --- /dev/null +++ b/database/migrations/2025_01_30_091021_add_label_to_custom_fields_table.php @@ -0,0 +1,28 @@ +string('label')->nullable()->after('name'); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('custom_fields', function (Blueprint $table) { + $table->dropColumn('label'); + }); + } +}; diff --git a/resources/views/custom_fields/create.blade.php b/resources/views/custom_fields/create.blade.php index 31220b0..448cdb2 100644 --- a/resources/views/custom_fields/create.blade.php +++ b/resources/views/custom_fields/create.blade.php @@ -39,6 +39,17 @@ @enderror +
+ +
+ + @error('label') + {{ $message }} + @enderror +
+
+
+ +
+ + @error('urutan_prioritas') + {{ $message }} + @enderror +
+