Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions app/Exports/PekerjaanPmiExport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace App\Exports;

use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithColumnFormatting;
use Maatwebsite\Excel\Concerns\WithHeadings;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;

class PekerjaanPmiExport implements FromCollection, WithHeadings, ShouldAutoSize, WithColumnFormatting
{
protected $data;

public function __construct($data)
{
$this->data = $data;
}

public function collection()
{
return $this->data->map(function ($item, $index) {
return [
'no' => $index + 1, // Menambahkan nomor urut berdasarkan index
'nama_pekerjaan' => $item->nama,
];
});
}

public function headings(): array
{
return [
'No',
'Nama Pekerjaan PMI',
];
}

public function columnFormats(): array
{
return [
'A' => NumberFormat::FORMAT_NUMBER,
'B' => NumberFormat::FORMAT_TEXT,
];
}
}
117 changes: 117 additions & 0 deletions app/Http/Controllers/Admin/PekerjaanPmiController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php

namespace App\Http\Controllers\Admin;

use App\Exports\PekerjaanPmiExport;
use App\Http\Controllers\Controller;
use App\Http\Requests\PekerjaanPmiRequest;
use App\Imports\PekerjaanPmiImport;
use App\Models\PekerjaanPmi;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Maatwebsite\Excel\Facades\Excel;
use Yajra\DataTables\DataTables;

class PekerjaanPmiController extends Controller
{
public function index(Request $request)
{
if ($request->excel) {
$paramDatatable = json_decode($request->get('params'), 1);
$request->merge($paramDatatable);
}

if ($request->ajax() || $request->excel) {
$query = DataTables::of(PekerjaanPmi::query());
if ($request->excel) {
$query->filtering();

return Excel::download(new PekerjaanPmiExport($query->results()), 'Data-Pekerjaan-PMI.xlsx');
}

return $query->addIndexColumn()
->addColumn('action', function ($data) {
$edit = '<a href="' . route('pekerjaan-pmi.edit', $data->id) . '" class="btn btn-sm btn-warning btn-sm"><i class="fas fa-pencil-alt"></i></a>';
$delete = '<button data-href="' . route('pekerjaan-pmi.destroy', $data->id) . '" class="btn btn-sm btn-danger" data-toggle="modal" data-target="#confirm-delete"><i class="fas fa-trash"></i></button>';

return '<div class="btn btn-group">' . $edit . $delete . '</div>';
})
->rawColumns(['action'])
->make(true);
}

return view('admin.pekerjaan-pmi.index');
}

public function create()
{
return view('admin.pekerjaan-pmi.create');
}

public function store(PekerjaanPmiRequest $request)
{
$input = $request->all();

if (PekerjaanPmi::create($input)) {
return redirect('pekerjaan-pmi')->with('success', 'Data berhasil disimpan');
}

return back()->with('error', 'Data gagal disimpan');
}

public function edit($id)
{
return view('admin.pekerjaan-pmi.edit', [
'pekerjaanPmi' => PekerjaanPmi::findOrFail($id),
]);
}

public function update(PekerjaanPmiRequest $request, $id)
{
$input = $request->all();
$pekerjaanPmi = PekerjaanPmi::findOrFail($id);

if ($pekerjaanPmi->update($input)) {
return redirect('pekerjaan-pmi')->with('success', 'Data berhasil diubah');
}

return back()->with('error', 'Data gagal diubah');
}

public function import()
{
return view('admin.pekerjaan-pmi.import');
}

public function prosesImport(Request $request)
{
try {
Excel::import(new PekerjaanPmiImport, $request->file('file')->store('temp'));
} catch (\Exception $e) {
report($e);

return back()->with('error', 'Data gagal diimport');
}

// Hapus folder temp ketika sudah selesai
Storage::deleteDirectory('temp');

return redirect('pekerjaan-pmi')->with('success', 'Data berhasil diimport');
}

public function contohImport()
{
$file = public_path('/assets/import/data_pekerjaan_pmi.xlsx');

return response()->download($file);
}

public function destroy($id)
{
if (PekerjaanPmi::destroy($id)) {
return redirect('pekerjaan-pmi')->with('success', 'Data berhasil dihapus');
}

return redirect('pekerjaan-pmi')->with('error', 'Data gagal dihapus');
}
}
25 changes: 25 additions & 0 deletions app/Http/Controllers/Api/PekerjaanPmiController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\PekerjaanPmi;
use Illuminate\Http\Request;

class PekerjaanPmiController extends Controller
{
public function index(Request $request)
{
$search = $request->get('q');
$pekerjaanPmi = PekerjaanPmi::selectRaw('id, nama, nama as text')
->when($search, static fn($q) => $q->where('nama', 'like', "%{$search}%"))
->paginate();

return response()->json([
'results' => $pekerjaanPmi->items(),
'pagination' => [
'more' => $pekerjaanPmi->currentPage() < $pekerjaanPmi->lastPage(),
],
]);
}
}
45 changes: 45 additions & 0 deletions app/Http/Requests/PekerjaanPmiRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class PekerjaanPmiRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
$id = $this->route('pekerjaan_pmi');

return [
'nama' => 'required|string|max:255|unique:pekerjaan_pmi,nama,' . $id,
];
}

/**
* Get the error messages for the defined validation rules.
*
* @return array<string, string>
*/
public function messages(): array
{
return [
'nama.required' => 'Nama pekerjaan harus diisi.',
'nama.string' => 'Nama pekerjaan harus berupa teks.',
'nama.max' => 'Nama pekerjaan tidak boleh lebih dari 255 karakter.',
'nama.unique' => 'Nama pekerjaan sudah ada, silakan gunakan nama yang berbeda.',
];
}
}
39 changes: 39 additions & 0 deletions app/Imports/PekerjaanPmiImport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace App\Imports;

use App\Models\PekerjaanPmi;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithBatchInserts;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Maatwebsite\Excel\Concerns\WithUpserts;

class PekerjaanPmiImport implements ToModel, WithBatchInserts, WithUpserts, WithHeadingRow
{
public function __construct()
{
ini_set('memory_limit', '-1');
}

/**
* @param array $row
*
* @return \Illuminate\Database\Eloquent\Model|null
*/
public function model(array $row)
{
return new PekerjaanPmi([
'nama' => $row['nama_pekerjaan'] ?? $row['nama'],
]);
}

public function batchSize(): int
{
return 1000;
}

public function uniqueBy()
{
return 'nama';
}
}
22 changes: 22 additions & 0 deletions app/Models/PekerjaanPmi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class PekerjaanPmi extends Model
{
use HasFactory;

protected $table = 'pekerjaan_pmi';

protected $fillable = [
'nama',
];

protected $dates = [
'created_at',
'updated_at',
];
}
1 change: 1 addition & 0 deletions catatan_rilis.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Di rilis v2508.0.0 berisi perbaikan yang diminta Komunitas Open Desa.
#### Penambahan Fitur

1. [#513](https://github.com/OpenSID/pantau/issues/513) Penambahan CRUD provinsi dan kabupaten.
2. [#505](https://github.com/OpenSID/pantau/issues/505) Penambahan modul pekerjaan PMI.

#### Perbaikan Bug

Expand Down
Loading