Compare commits

...

14 Commits

Author SHA1 Message Date
Daeng Deni Mardaeni
d8767174ca feat(location): perbaiki middleware auth dan optimalkan metode controllers
- **Middleware Auth**:
  - Mengganti penanganan middleware auth menggunakan `this->middleware('auth')` pada semua controller.
  - Menambahkan middleware closure untuk menginisialisasi properti user dengan `Auth::user()` setelah auth diverifikasi.

- **Controller Updates**:
  - **CitiesController**:
    - Menambahkan variabel `$provinces` untuk mendapatkan semua data provinsi.
    - Menambahkan data `$provinces` ke dalam view `location::cities.index`.
  - **DistrictsController**:
    - Menambahkan variabel `$provinces` untuk mendukung akses data provinsi dalam view.
    - Menyertakan data provinsi ke dalam view `location::districts.index`.
  - **ProvincesController** dan **VillagesController**:
    - Penyesuaian middleware auth sama seperti controller lainnya. Tidak ada perubahan pada struktur utama lainnya.

- **Seeder Optimization**:
  - Menghapus metode `crudActions` dalam `PermissionSeeder` yang sebelumnya menghasilkan daftar tindakan CRUD.
  - Mengurangi redundansi kode seeding dengan langsung membuat grup permission tanpa tindakan tambahan.
  - Membersihkan logika tambahan terkait CRUD actions yang tidak dipakai.
2025-06-22 20:51:20 +07:00
Daeng Deni Mardaeni
53a2c3a38e feat(auth): tambah validasi peran pengguna untuk modul lokasi
- **Autentikasi dan Otorisasi**:
  - Menambah validasi akses berdasarkan peran pengguna menggunakan `Auth::guard('web')->user()`.
  - Menentukan izin seperti `location.read`, `location.create`, `location.update`, `location.delete`, dan `location.export` untuk setiap metode di controller.
  - Menambahkan pesan error khusus untuk user yang tidak memiliki izin akses tertentu.

- **Peningkatan Controller**:
  - Mengubah properti `user` dari publik menjadi `protected`.
  - Menambahkan validasi peran pengguna pada metode di controller berikut:
    - `ProvincesController`: Pembatasan akses untuk operasi CRUD dan export.
    - `CitiesController`: Restriksi yang sama seperti pada `ProvincesController`.
    - `DistrictsController`: Menambahkan pengecekan otorisasi berdasarkan peran.
    - `VillagesController`: Validasi akses CRUD, export, dan data villager.

- **Seeder**:
  - Menambahkan `PermissionSeeder` untuk menghasilkan izin CRUD dengan grup `location`.
  - Memastikan setiap action CRUD (`read`, `create`, `update`, dll.) terdaftar dalam basis data.

- **Perbaikan API Response**:
  - Mengganti penggunaan `echo json_encode` dengan metode `response()->json()`.
  - Penanganan response untuk berbagai metode seperti `destroy`, dan validasi akses lainnya.

Signed-off-by: Daeng Deni Mardaeni <ddeni05@gmail.com>
2025-06-22 19:16:11 +07:00
Daeng Deni Mardaeni
cd10e17259 fix(models): ganti penggunaan trait Userstamps
- Mengubah penggunaan trait dari `Wildside\Userstamps\Userstamps` menjadi `Mattiverse\Userstamps\Traits\Userstamps`.
- Penyesuaian ini dilakukan untuk mendukung versi terbaru dari dependensi yang digunakan.
- Memastikan kompatibilitas dengan implementasi terkini pada model.
- Tidak ada perubahan pada logika utama aplikasi, hanya penggantian trait.
2025-06-05 16:55:06 +07:00
Daeng Deni Mardaeni
80e592c639 fix(cities): perbaiki pemetaan kolom untuk tabel kota
- Menambahkan indeks pada kolom 'code' untuk meningkatkan performa pencarian.
- Menghapus indeks yang tidak diperlukan untuk kolom 'province_code'.
2025-04-27 16:40:13 +07:00
Daeng Deni Mardaeni
4cf14d46b4 fix(cities): perbaiki pemetaan indeks untuk tabel kota
- Memisahkan indeks untuk kolom 'code' dan 'province_code' pada tabel kota.
- Meningkatkan performa pencarian dengan menambahkan indeks terpisah.
2025-04-27 16:34:29 +07:00
Daeng Deni Mardaeni
817eb3360b fix(cities): perbaiki pemetaan nama provinsi untuk kota
- Memperbarui pemetaan nama provinsi berdasarkan kode kota tertentu.
- Menambahkan logika untuk menetapkan nama provinsi jika kode kota termasuk dalam daftar tertentu.
- Mengubah referensi dari `$row->province->name` menjadi `$row->province_name` untuk konsistensi.

fix(districts): perbaiki pemetaan nama provinsi untuk distrik

- Memperbarui pemetaan nama provinsi berdasarkan kode kota dalam distrik.
- Menambahkan logika untuk menetapkan nama provinsi jika kode kota termasuk dalam daftar tertentu.
- Mengubah referensi dari `$row->city->province->name` menjadi `$row->province_name` untuk konsistensi.

fix(villages): perbaiki pemetaan nama provinsi untuk desa

- Memperbarui pemetaan nama provinsi berdasarkan kode kota dalam desa.
- Menambahkan logika untuk menetapkan nama provinsi jika kode kota termasuk dalam daftar tertentu.
- Mengubah referensi dari `$row->district->city->province->name` menjadi `$row->province_name` untuk konsistensi.
2025-04-27 16:00:19 +07:00
Daeng Deni Mardaeni
d92b66185c fix(villages): perbaiki pencarian dan pemetaan provinsi untuk desa
- Tambahkan penanganan khusus untuk kode kota di provinsi Papua.
- Perbarui aturan validasi untuk kode desa agar mendukung panjang maksimum 13 karakter.
- Perbaiki tampilan nama provinsi pada tabel desa dengan menggunakan nama yang sesuai.
2025-04-27 12:14:50 +07:00
Daeng Deni Mardaeni
094f9fa6d6 fix(districts): perbaiki pencarian dan pemetaan provinsi untuk distrik
- Tambahkan penanganan khusus untuk kode kota Papua.
- Perbarui aturan validasi untuk kolom kode distrik.
- Perbaiki tampilan nama provinsi pada tabel distrik.
2025-04-27 12:14:40 +07:00
Daeng Deni Mardaeni
bf288013f4 fix(cities): perbaiki pencarian dan pemetaan provinsi untuk kota
- Tambahkan logika untuk menangani kode kota khusus di provinsi Papua.
- Perbarui pemetaan nama provinsi berdasarkan kode kota.
- Sesuaikan tampilan data kota di tabel dengan nama provinsi yang benar.
2025-04-27 12:14:22 +07:00
Daeng Deni Mardaeni
1a7703a6cc fix(provinces): perbarui tampilan dan fungsionalitas tabel provinsi
- Hapus dropdown filter yang tidak diperlukan.
- Tambahkan garis batas untuk pemisahan elemen.
- Modifikasi render kolom kode untuk menampilkan dua karakter pertama.
2025-04-27 12:14:07 +07:00
Daeng Deni Mardaeni
dc931dbb4b fix(villages): perbaiki nama desa Cikampek Utara
- Mengubah nama desa dari 'Cilkampek Utara' menjadi 'Cikampek Utara'
- Memastikan konsistensi penamaan dalam database
2025-04-27 12:13:48 +07:00
Daeng Deni Mardaeni
d74dbb37a7 fix(migration): tambahkan index pada tabel villages
- Menambahkan index pada kolom province_code, city_code, district_code, dan code.
- Meningkatkan performa query yang melibatkan kolom-kolom tersebut.
2025-04-27 12:13:20 +07:00
Daeng Deni Mardaeni
00356f2366 fix(migration): tambahkan index pada tabel districts
- Menambahkan index pada kolom 'province_code', 'city_code', dan 'code' di tabel districts.
- Meningkatkan performa query yang melibatkan kolom-kolom tersebut.
2025-04-27 12:13:15 +07:00
Daeng Deni Mardaeni
0be26178ff fix(migration): tambahkan index pada tabel cities
- Menambahkan index pada kolom 'province_code' dan 'code' untuk meningkatkan performa query.
2025-04-27 12:13:07 +07:00
20 changed files with 391 additions and 84 deletions

View File

@@ -11,7 +11,18 @@ use Modules\Location\Models\City;
class CitiesExport implements WithColumnFormatting, WithHeadings, FromCollection, withMapping
{
public function collection(){
return City::with('province')->get();
$data = City::with('province')->get();
$data = $data->map(function ($item) {
if (in_array($item->code, ['92.01', '92.04', '92.05', '92.09', '92.10', '92.71'])) {
$item->province_name = Province::where('code', '92.1')->first()->name;
} else {
$item->province_name = $item->province->name;
}
return $item;
});
return $data;
}
public function map($row): array{
@@ -19,7 +30,7 @@ class CitiesExport implements WithColumnFormatting, WithHeadings, FromCollection
$row->id,
$row->code,
$row->name,
$row->province->name,
$row->province_name,
$row->created_at
];
}

View File

@@ -7,11 +7,21 @@ use Maatwebsite\Excel\Concerns\WithColumnFormatting;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
use Modules\Location\Models\District;
use Modules\Location\Models\Province;
class DistrictsExport implements WithColumnFormatting, WithHeadings, FromCollection, withMapping
{
public function collection(){
return District::with('city.province')->get();
$data = District::with('city.province')->get();
$data = $data->map(function ($item) {
if (in_array($item->city_code, ['92.01', '92.04', '92.05', '92.09', '92.10', '92.71'])) {
$item->province_name = Province::where('code', '92.1')->first()->name;
} else {
$item->province_name = $item->city->province->name;
}
return $item;
});
}
public function map($row): array{
@@ -20,7 +30,7 @@ class DistrictsExport implements WithColumnFormatting, WithHeadings, FromCollect
$row->code,
$row->name,
$row->city->name,
$row->city->province->name,
$row->province_name,
$row->created_at
];
}

View File

@@ -6,12 +6,22 @@ use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithColumnFormatting;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
use Modules\Location\Models\Province;
use Modules\Location\Models\Village;
class VillagesExport implements WithColumnFormatting, WithHeadings, FromCollection, withMapping
{
public function collection(){
return Village::with('district.city.province')->get();
$data = Village::with('district.city.province')->get();
$data = $data->map(function ($item) {
if (in_array($item->city_code, ['92.01', '92.04', '92.05', '92.09', '92.10', '92.71'])) {
$item->province_name = Province::where('code', '92.1')->first()->name;
} else {
$item->province_name = $item->district->city->province->name;
}
return $item;
});
}
public function map($row): array{
@@ -22,7 +32,7 @@ class VillagesExport implements WithColumnFormatting, WithHeadings, FromCollecti
$row->postal_code,
$row->district->name,
$row->district->city->name,
$row->district->city->province->name,
$row->province_name,
$row->created_at
];
}

View File

@@ -3,30 +3,55 @@
namespace Modules\Location\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Auth;
use Maatwebsite\Excel\Facades\Excel;
use Modules\Location\Exports\CitiesExport;
use Modules\Location\Http\Requests\CityRequest;
use Modules\Location\Models\City;
use Modules\Location\Models\Province;
class CitiesController extends Controller
{
public $user;
protected $user;
public function __construct()
{
// Mengatur middleware auth
$this->middleware('auth');
// Mengatur user setelah middleware auth dijalankan
$this->middleware(function ($request, $next) {
$this->user = Auth::user();
return $next($request);
});
}
public function index(){
if (is_null($this->user) || !$this->user->can('location.read')) {
abort(403, 'Sorry! You are not allowed to view cities.');
}
$provinces = Province::all();
return view('location::cities.index', compact('provinces'));
}
public function create(){
if (is_null($this->user) || !$this->user->can('location.create')) {
abort(403, 'Sorry! You are not allowed to create cities.');
}
$provinces = Province::all();
return view('location::cities.create',compact('provinces'));
}
public function store(CityRequest $request){
if (is_null($this->user) || !$this->user->can('location.create')) {
abort(403, 'Sorry! You are not allowed to create cities.');
}
$validate = $request->validated();
if($validate){
@@ -40,12 +65,20 @@ class CitiesController extends Controller
}
public function edit($id){
if (is_null($this->user) || !$this->user->can('location.update')) {
abort(403, 'Sorry! You are not allowed to update cities.');
}
$city = City::find($id);
$provinces = Province::all();
return view('location::cities.create', compact('city', 'provinces'));
}
public function update(CityRequest $request, $id){
if (is_null($this->user) || !$this->user->can('location.update')) {
abort(403, 'Sorry! You are not allowed to update cities.');
}
$validate = $request->validated();
if($validate){
@@ -60,17 +93,21 @@ class CitiesController extends Controller
}
public function destroy($id){
if (is_null($this->user) || !$this->user->can('provinces.delete')) {
return response()->json(['success' => false, 'message' => 'Sorry! You are not allowed to delete cities.']);
}
try {
City::destroy($id);
echo json_encode(['message' => 'City deleted successfully', 'success' => true]);
return response()->json(['message' => 'City deleted successfully', 'success' => true]);
} catch (\Exception $e) {
echo json_encode(['message' => 'Failed to delete city', 'success' => false]);
return response()->json(['message' => 'Failed to delete city', 'success' => false]);
}
}
public function dataForDatatables(Request $request){
if (is_null($this->user) || !$this->user->can('provinces.view')) {
//abort(403, 'Sorry! You are not allowed to view users.');
if (is_null($this->user) || !$this->user->can('location.read')) {
return response()->json(['success' => false, 'message' => 'Sorry! You are not allowed to view cities.'], 403);
}
// Retrieve data from the database
@@ -80,8 +117,21 @@ class CitiesController extends Controller
if ($request->has('search') && !empty($request->get('search'))) {
$search = $request->get('search');
$search = explode('|', $search);
if(isset($search[0]) &&!empty($search[0])){
$query->where('province_code',$search[0]);
if(isset($search[0]) && !empty($search[0])){
// Define special city codes once
$specialCityCodes = ['92.01', '92.04', '92.05', '92.09', '92.10', '92.71'];
// Handle Papua province special cases
if($search[0] == '92'){
$query->where('province_code', '92')
->whereNotIn('code', $specialCityCodes);
} else if($search[0] == '92.1'){
$query->where('province_code', '92')
->whereIn('code', $specialCityCodes);
} else {
// For all other provinces
$query->where('province_code', $search[0]);
}
}
$query->where(function ($q) use ($search) {
$q->where('code', 'LIKE', "%$search[1]%");
@@ -121,6 +171,19 @@ class CitiesController extends Controller
// Calculate the current page number
$currentPage = 0 + 1;
//maping data for datatables,
//jika city code adalah '92.01', '92.04', '92.05', '92.09', '92.10', '92.71' maka province code yang dipilih adalah 92.1
$data = $data->map(function ($item) {
if (in_array($item->code, ['92.01', '92.04', '92.05', '92.09', '92.10', '92.71'])) {
$item->province_name = Province::where('code', '92.1')->first()->name;
} else {
$item->province_name = $item->province->name;
}
return $item;
});
// Apply the search filter again if there is a search filter
// Return the response data as a JSON object
return response()->json([
'draw' => $request->get('draw'),
@@ -135,10 +198,30 @@ class CitiesController extends Controller
public function export()
{
if (is_null($this->user) || !$this->user->can('location.read')) {
abort(403, 'Sorry! You are not allowed to export cities.');
}
return Excel::download(new CitiesExport, 'cities.xlsx');
}
public function getCitiesByProvinceId($id){
return response()->json(City::where('province_code', $id)->get());
public function getCitiesByProvinceId($id)
{
if (is_null($this->user) || !$this->user->can('location.read')) {
return response()->json(['success' => false, 'message' => 'Sorry! You are not allowed to view cities.'], 403);
}
$query = City::query();
$provinceCode = substr($id, 0, 2);
$query->where('province_code', $provinceCode);
if ($id == '92.1') {
$query->whereIn('code', ['92.01', '92.04', '92.05', '92.09', '92.10', '92.71']);
} elseif ($id == '92') {
$query->whereNotIn('code', ['92.01', '92.04', '92.05', '92.09', '92.10', '92.71']);
}
return response()->json($query->get());
}
}

View File

@@ -10,27 +10,52 @@ use Modules\Location\Http\Requests\DistrictRequest;
use Modules\Location\Models\City;
use Modules\Location\Models\District;
use Modules\Location\Models\Province;
use Illuminate\Support\Facades\Auth;
class DistrictsController extends Controller
{
public $user;
protected $user;
public function __construct()
{
// Mengatur middleware auth
$this->middleware('auth');
// Mengatur user setelah middleware auth dijalankan
$this->middleware(function ($request, $next) {
$this->user = Auth::user();
return $next($request);
});
}
public function index(){
if (is_null($this->user) || !$this->user->can('location.read')) {
abort(403, 'Sorry! You are not allowed to view districts.');
}
$provinces = Province::all();
return view('location::districts.index', compact('provinces'));
}
public function create(){
if (is_null($this->user) || !$this->user->can('location.create')) {
abort(403, 'Sorry! You are not allowed to create districts.');
}
$provinces = Province::all();
return view('location::districts.create', compact('provinces'));
}
public function store(DistrictRequest $request){
if (is_null($this->user) || !$this->user->can('location.create')) {
abort(403, 'Sorry! You are not allowed to create districts.');
}
$validate = $request->validated();
if($validate){
try{
$district = District::create($validate);
District::create($validate);
return redirect()->route('locations.districts.index')->with('success', 'District created successfully');
} catch(\Exception $e){
return redirect()->back()->with('error', 'Failed to create district. '.$e->getMessage());
@@ -39,6 +64,10 @@ class DistrictsController extends Controller
}
public function edit($id){
if (is_null($this->user) || !$this->user->can('location.update')) {
abort(403, 'Sorry! You are not allowed to edit districts.');
}
$district = District::find($id);
$provinces = Province::all();
$cities = City::where('province_code', $district->province_code)->get();
@@ -46,6 +75,10 @@ class DistrictsController extends Controller
}
public function update(DistrictRequest $request, $id){
if (is_null($this->user) || !$this->user->can('location.update')) {
abort(403, 'Sorry! You are not allowed to update districts.');
}
$validate = $request->validated();
if($validate){
@@ -60,17 +93,22 @@ class DistrictsController extends Controller
}
public function destroy($id){
if (is_null($this->user) || !$this->user->can('location.delete')) {
return response()->json(['success' => false, 'message' => 'Sorry! You are not allowed to delete districts.'], 403);
}
try{
District::destroy($id);
echo json_encode(['message' => 'District deleted successfully','success'=>true ]);
return response()->json(['message' => 'District deleted successfully', 'success' => true]);
} catch(\Exception $e){
echo json_encode(['message' => 'Failed to delete District', 'success' => false]);
return response()->json(['message' => 'Failed to delete District', 'success' => false]);
}
}
public function dataForDatatables(Request $request){
if (is_null($this->user) || !$this->user->can('provinces.view')) {
//abort(403, 'Sorry! You are not allowed to view users.');
// Check if the authenticated user has the required permission to view audit logs
if (is_null($this->user) || !$this->user->can('location.read')) {
return response()->json(['success' => false, 'message' => 'Sorry! You are not allowed to view districts.'], 403);
}
// Retrieve data from the database
@@ -80,9 +118,23 @@ class DistrictsController extends Controller
if ($request->has('search') && !empty($request->get('search'))) {
$search = $request->get('search');
$search = explode('|', $search);
if(isset($search[0]) &&!empty($search[0])){
$query->where('province_code',$search[0]);
if(isset($search[0]) && !empty($search[0])){
// Define special city codes once
$specialCityCodes = ['92.01', '92.04', '92.05', '92.09', '92.10', '92.71'];
// Handle Papua province special cases
if($search[0] == '92'){
$query->where('province_code', '92')
->whereNotIn('city_code', $specialCityCodes);
} else if($search[0] == '92.1'){
$query->where('province_code', '92')
->whereIn('city_code', $specialCityCodes);
} else {
// For all other provinces
$query->where('province_code', $search[0]);
}
}
if(isset($search[1]) &&!empty($search[1])){
$query->where('city_code',$search[1]);
}
@@ -123,6 +175,15 @@ class DistrictsController extends Controller
// Calculate the current page number
$currentPage = 0 + 1;
$data = $data->map(function ($item) {
if (in_array($item->city_code, ['92.01', '92.04', '92.05', '92.09', '92.10', '92.71'])) {
$item->province_name = Province::where('code', '92.1')->first()->name;
} else {
$item->province_name = $item->city->province->name;
}
return $item;
});
// Return the response data as a JSON object
return response()->json([
'draw' => $request->get('draw'),
@@ -136,10 +197,18 @@ class DistrictsController extends Controller
}
public function export(Request $request){
if (is_null($this->user) || !$this->user->can('location.export')) {
abort(403, 'Sorry! You are not allowed to export districts.');
}
return Excel::download(new DistrictsExport, 'districts.xlsx');
}
public function getDistrictsByCityId($id){
if (is_null($this->user) || !$this->user->can('location.read')) {
return response()->json(['success' => false, 'message' => 'Sorry! You are not allowed to view districts.'], 403);
}
return response()->json(District::where('city_code', $id)->get());
}
}

View File

@@ -2,32 +2,57 @@
namespace Modules\Location\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Maatwebsite\Excel\Facades\Excel;
use Modules\Location\Models\Province;
use Modules\Location\Exports\ProvincesExport;
use Modules\Location\Http\Requests\ProvinceRequest;
use Modules\Location\Models\Province;
class ProvincesController extends Controller
{
public $user;
protected $user;
public function __construct()
{
// Mengatur middleware auth
$this->middleware('auth');
// Mengatur user setelah middleware auth dijalankan
$this->middleware(function ($request, $next) {
$this->user = Auth::user();
return $next($request);
});
}
public function index(){
if (is_null($this->user) || !$this->user->can('location.read')) {
abort(403, 'Sorry! You are not allowed to view provinces.');
}
return view('location::provinces.index');
}
public function create(){
if (is_null($this->user) || !$this->user->can('location.create')) {
abort(403, 'Sorry! You are not allowed to create provinces.');
}
return view('location::provinces.create');
}
public function store(ProvinceRequest $request){
if (is_null($this->user) || !$this->user->can('location.create')) {
abort(403, 'Sorry! You are not allowed to create provinces.');
}
$validate = $request->validated();
if($validate){
try{
// Save to database
$province = Province::create($validate);
Province::create($validate);
return redirect()->route('locations.provinces.index')->with('success', 'Province created successfully');
} catch (\Exception $e){
return redirect()->route('locations.provinces.create')->with('error', 'Failed to create province');
@@ -36,11 +61,19 @@ class ProvincesController extends Controller
}
public function edit($id){
if (is_null($this->user) || !$this->user->can('location.update')) {
abort(403, 'Sorry! You are not allowed to update provinces.');
}
$province = Province::find($id);
return view('location::provinces.create', compact('province'));
}
public function update(ProvinceRequest $request, $id){
if (is_null($this->user) || !$this->user->can('location.update')) {
abort(403, 'Sorry! You are not allowed to update provinces.');
}
$validate = $request->validated();
if($validate){
@@ -56,20 +89,24 @@ class ProvincesController extends Controller
}
public function destroy($id){
if (is_null($this->user) || !$this->user->can('location.delete')) {
return response()->json(['success' => false, 'message' => 'Sorry! You are not allowed to delete provinces.']);
}
try{
// Delete from database
$province = Province::find($id);
$province->delete();
echo json_encode(['success' => true, 'message' => 'Province deleted successfully']);
return response()->json(['success' => true, 'message' => 'Province deleted successfully']);
} catch (\Exception $e){
echo json_encode(['success' => false, 'message' => 'Failed to delete province']);
return response()->json(['success' => false, 'message' => 'Failed to delete province']);
}
}
public function dataForDatatables(Request $request){
if (is_null($this->user) || !$this->user->can('provinces.view')) {
//abort(403, 'Sorry! You are not allowed to view users.');
if (is_null($this->user) || !$this->user->can('location.read')) {
return response()->json(['success' => false, 'message' => 'Sorry! You are not allowed to view provinces.']);
}
// Retrieve data from the database
@@ -129,6 +166,10 @@ class ProvincesController extends Controller
public function export()
{
if (is_null($this->user) || !$this->user->can('location.export')) {
return response()->json(['success' => false, 'message' => 'Sorry! You are not allowed to export provinces.']);
}
return Excel::download(new ProvincesExport, 'provinces.xlsx');
}
}

View File

@@ -2,6 +2,7 @@
namespace Modules\Location\Http\Controllers;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;
@@ -15,21 +16,44 @@
class VillagesController extends Controller
{
public $user;
protected $user;
public function __construct()
{
// Mengatur middleware auth
$this->middleware('auth');
// Mengatur user setelah middleware auth dijalankan
$this->middleware(function ($request, $next) {
$this->user = Auth::user();
return $next($request);
});
}
/**
* Display a listing of the resource.
*/
public function index()
{
if (is_null($this->user) || !$this->user->can('location.read')) {
abort(403, 'Sorry! You are not allowed to view villages.');
}
$provinces = Province::all();
return view('location::villages.index',compact('provinces'));
}
public function store(VillageRequest $request)
{
if (is_null($this->user) || !$this->user->can('location.create')) {
abort(403, 'Sorry! You are not allowed to create villages.');
}
$validate = $request->validated();
if ($validate) {
try {
$village = Village::create($validate);
Village::create($validate);
return redirect()
->route('locations.villages.index')
->with('success', 'Village created successfully');
@@ -41,12 +65,20 @@
public function create()
{
if (is_null($this->user) || !$this->user->can('location.create')) {
abort(403, 'Sorry! You are not allowed to create villages.');
}
$provinces = Province::all();
return view('location::villages.create', compact('provinces'));
}
public function edit($id)
{
if (is_null($this->user) || !$this->user->can('location.update')) {
abort(403, 'Sorry! You are not allowed to update villages.');
}
$village = Village::find($id);
$provinces = Province::all();
$cities = City::where('province_code', $village->province_code)->get();
@@ -56,6 +88,10 @@
public function update(VillageRequest $request, $id)
{
if (is_null($this->user) || !$this->user->can('location.update')) {
abort(403, 'Sorry! You are not allowed to update villages.');
}
$validate = $request->validated();
if ($validate) {
@@ -73,23 +109,31 @@
public function destroy($id)
{
if (is_null($this->user) || !$this->user->can('location.delete')) {
return response()->json(['success' => false,'message' => 'Sorry! You are not allowed to delete villages.'], 403);
}
try {
Village::destroy($id);
echo json_encode(['message' => 'Village deleted successfully', 'success' => true]);
return json_encode(['message' => 'Village deleted successfully', 'success' => true]);
} catch (Exception $e) {
echo json_encode(['message' => 'Failed to delete Village', 'success' => false]);
return json_encode(['message' => 'Failed to delete Village', 'success' => false]);
}
}
public function export(Request $request)
{
if (is_null($this->user) || !$this->user->can('location.export')) {
abort(403, 'Sorry! You are not allowed to export villages.');
}
return Excel::download(new VillagesExport, 'villages.xlsx');
}
public function dataForDatatables(Request $request)
{
if (is_null($this->user) || !$this->user->can('provinces.view')) {
//abort(403, 'Sorry! You are not allowed to view users.');
if (is_null($this->user) || !$this->user->can('location.read')) {
return response()->json(['success' => false,'message' => 'Sorry! You are not allowed to view villages.'], 403);
}
// Retrieve data from the database
@@ -99,8 +143,21 @@
if ($request->has('search') && !empty($request->get('search'))) {
$search = $request->get('search');
$search = explode('|', $search);
if(isset($search[0]) &&!empty($search[0])){
$query->where('province_code',$search[0]);
if(isset($search[0]) && !empty($search[0])){
// Define special city codes once
$specialCityCodes = ['92.01', '92.04', '92.05', '92.09', '92.10', '92.71'];
// Handle Papua province special cases
if($search[0] == '92'){
$query->where('province_code', '92')
->whereNotIn('city_code', $specialCityCodes);
} else if($search[0] == '92.1'){
$query->where('province_code', '92')
->whereIn('city_code', $specialCityCodes);
} else {
// For all other provinces
$query->where('province_code', $search[0]);
}
}
if(isset($search[1]) &&!empty($search[1])){
$query->where('city_code',$search[1]);
@@ -146,6 +203,15 @@
// Calculate the current page number
$currentPage = 0 + 1;
$data = $data->map(function ($item) {
if (in_array($item->city_code, ['92.01', '92.04', '92.05', '92.09', '92.10', '92.71'])) {
$item->province_name = Province::where('code', '92.1')->first()->name;
} else {
$item->province_name = $item->district->city->province->name;
}
return $item;
});
// Return the response data as a JSON object
return response()->json([
'draw' => $request->get('draw'),
@@ -159,10 +225,18 @@
}
public function getVillagesByDistrictId($id){
if (is_null($this->user) || !$this->user->can('location.read')) {
return response()->json(['success' => false,'message' => 'Sorry! You are not allowed to view villages.'], 403);
}
return response()->json(Village::where('district_code', $id)->get());
}
public function getPostalCodesByVillageId($id){
if (is_null($this->user) || !$this->user->can('location.read')) {
return response()->json(['success' => false,'message' => 'Sorry! You are not allowed to view villages.'], 403);
}
return response()->json(Village::where('code', $id)->first());
}

View File

@@ -18,10 +18,10 @@
];
if ($this->method() === 'PUT') {
$rules['code'] = 'required|string|max:6|unique:districts,code,' . $this->id;
$rules['code'] = 'required|string|max:9|unique:districts,code,' . $this->id;
$rules['name'] = 'required|string|max:2554|unique:districts,name,' . $this->id;
} else {
$rules['code'] = 'required|string|max:6|unique:districts,code';
$rules['code'] = 'required|string|max:9|unique:districts,code';
$rules['name'] = 'required|string|max:2554|unique:districts,name';
}

View File

@@ -21,10 +21,10 @@
];
if ($this->method() === 'PUT') {
$rules['code'] = 'required|string|max:6|unique:villages,code,' . $this->id;
$rules['code'] = 'required|string|max:13|unique:villages,code,' . $this->id;
$rules['name'] = 'required|string|max:2554|unique:villages,name,' . $this->id;
} else {
$rules['code'] = 'required|string|max:6|unique:villages,code';
$rules['code'] = 'required|string|max:13|unique:villages,code';
$rules['name'] = 'required|string|max:2554|unique:villages,name';
}

View File

@@ -5,7 +5,7 @@
use Illuminate\Database\Eloquent\SoftDeletes;
use Spatie\Activitylog\LogOptions;
use Spatie\Activitylog\Traits\LogsActivity;
use Wildside\Userstamps\Userstamps;
use Mattiverse\Userstamps\Traits\Userstamps;
/**

View File

@@ -14,7 +14,7 @@ return new class extends Migration
Schema::create('cities', function (Blueprint $table) {
$table->id();
$table->string('province_code')->index();
$table->string('code');
$table->string('code')->index();
$table->string('name');
$table->timestamps();
$table->softDeletes();

View File

@@ -15,7 +15,7 @@ return new class extends Migration
$table->id();
$table->string('province_code')->index();
$table->string('city_code')->index();
$table->string('code');
$table->string('code')->index();
$table->string('name');
$table->timestamps();
$table->softDeletes();

View File

@@ -16,7 +16,7 @@ return new class extends Migration
$table->string('province_code')->index();
$table->string('city_code')->index();
$table->string('district_code')->index();
$table->string('code');
$table->string('code')->index();
$table->string('name');
$table->string('alt_name')->nullable();
$table->string('postal_code', 5)->nullable();

View File

@@ -13,6 +13,7 @@ class LocationDatabaseSeeder extends Seeder
public function run(): void
{
$this->call([
PermissionSeeder::class,
ProvinceSeeder::class,
CitySeeder::class,
DistrictSeeder::class,

View File

@@ -0,0 +1,32 @@
<?php
namespace Modules\Location\Database\Seeders;
use Illuminate\Database\Seeder;
use Illuminate\Support\Str;
use Modules\Usermanagement\Models\PermissionGroup;
class PermissionSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run()
{
$data = $this->data();
foreach ($data as $value) {
PermissionGroup::updateOrCreate([
'name' => $value['name'],
'slug' => Str::slug($value['name'])
]);
}
}
public function data()
{
return [
['name' => 'location']
];
}
}

View File

@@ -30632,7 +30632,7 @@ INSERT INTO villages (province_code, city_code, district_code, code, name, posta
INSERT INTO villages (province_code, city_code, district_code, code, name, postal_code) VALUES ('32', '32.15', '32.15.25', '32.15.25.2006', 'Jomin Timur', 41376);
INSERT INTO villages (province_code, city_code, district_code, code, name, postal_code) VALUES ('32', '32.15', '32.15.25', '32.15.25.2007', 'Jomin Barat', 41376);
INSERT INTO villages (province_code, city_code, district_code, code, name, postal_code) VALUES ('32', '32.15', '32.15.25', '32.15.25.2008', 'Sarimulya', 41376);
INSERT INTO villages (province_code, city_code, district_code, code, name, postal_code) VALUES ('32', '32.15', '32.15.25', '32.15.25.2009', 'Cilkampek Utara', 41376);
INSERT INTO villages (province_code, city_code, district_code, code, name, postal_code) VALUES ('32', '32.15', '32.15.25', '32.15.25.2009', 'Cikampek Utara', 41376);
INSERT INTO villages (province_code, city_code, district_code, code, name, postal_code) VALUES ('32', '32.15', '32.15.26', '32.15.26.1001', 'Karawang Wetan', 41314);
INSERT INTO villages (province_code, city_code, district_code, code, name, postal_code) VALUES ('32', '32.15', '32.15.26', '32.15.26.1002', 'Adiarsa Timur', 41314);
INSERT INTO villages (province_code, city_code, district_code, code, name, postal_code) VALUES ('32', '32.15', '32.15.26', '32.15.26.1003', 'Palumbonsari', 41314);

View File

@@ -38,9 +38,8 @@
</label>
<button class="search btn btn-sm btn-outline btn-primary">
<i class="ki-filled ki-setting-4"> </i>
<Filters></Filters>
</button>
<div class="h-[24px] border border-r-gray-200"></div>
<div class="border border-r-gray-200"></div>
<a class="btn btn-sm btn-light" href="{{ route('locations.cities.export') }}"> Export to Excel </a>
<a class="btn btn-sm btn-primary" href="{{ route('locations.cities.create') }}"> Add City </a>
</div>
@@ -150,7 +149,7 @@
province: {
title: 'Province',
render: (item, data) => {
return data.province.name;
return data.province_name;
}
},
actions: {

View File

@@ -42,9 +42,8 @@
</label>
<button class="search btn btn-sm btn-outline btn-primary">
<i class="ki-filled ki-setting-4"> </i>
<Filters></Filters>
</button>
<div class="h-[24px] border border-r-gray-200"></div>
<div class="border border-r-gray-200"></div>
<a class="btn btn-sm btn-light" href="{{ route('locations.districts.export') }}"> Export to Excel </a>
<a class="btn btn-sm btn-primary" href="{{ route('locations.districts.create') }}"> Add District </a>
</div>
@@ -165,7 +164,7 @@
province: {
title: 'Province',
render: (item, data) => {
return data.city.province.name;
return data.province_name;
}
},
actions: {

View File

@@ -15,34 +15,10 @@
<div class="flex">
<label class="input input-sm"> <i class="ki-filled ki-magnifier"> </i>
<input placeholder="Search provinces" id="search" type="text" value="">
</label>
</div>
<div class="border border-r-gray-200"></div>
<div class="flex flex-wrap gap-2.5">
<select class="select select-sm w-28">
<option value="1">
Active
</option>
<option value="2">
Disabled
</option>
<option value="2">
Pending
</option>
</select>
<select class="select select-sm w-28">
<option value="desc">
Latest
</option>
<option value="asc">
Oldest
</option>
</select>
<button class="btn btn-sm btn-outline btn-primary">
<i class="ki-filled ki-setting-4"> </i>
<Filters></Filters>
</button>
<div class="h-[24px] border border-r-gray-200"></div>
<a class="btn btn-sm btn-light" href="{{ route('locations.provinces.export') }}"> Export to Excel </a>
<a class="btn btn-sm btn-primary" href="{{ route('locations.provinces.create') }}"> Add Province </a>
</div>
@@ -140,6 +116,9 @@
},
code: {
title: 'Code',
render: (item, data) => {
return `${data.code.substring(0, 2)}`;
},
},
name: {
title: 'Province',

View File

@@ -47,9 +47,8 @@
<button class="search btn btn-sm btn-outline btn-primary">
<i class="ki-filled ki-setting-4"> </i>
<Filters></Filters>
</button>
<div class="h-[24px] border border-r-gray-200"></div>
<div class="border border-r-gray-200"></div>
<a class="btn btn-sm btn-light" href="{{ route('locations.villages.export') }}"> Export to Excel </a>
<a class="btn btn-sm btn-primary" href="{{ route('locations.villages.create') }}"> Add District </a>
</div>
@@ -192,7 +191,7 @@
province: {
title: 'Province',
render: (item, data) => {
return data.district.city.province.name;
return data.province_name;
}
},
actions: {