Feature #4 : Villages
This commit is contained in:
49
app/Exports/VillagesExport.php
Normal file
49
app/Exports/VillagesExport.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Location\Exports;
|
||||
|
||||
use Maatwebsite\Excel\Concerns\FromCollection;
|
||||
use Maatwebsite\Excel\Concerns\WithColumnFormatting;
|
||||
use Maatwebsite\Excel\Concerns\WithHeadings;
|
||||
use Maatwebsite\Excel\Concerns\WithMapping;
|
||||
use Modules\Location\Models\Village;
|
||||
|
||||
class VillagesExport implements WithColumnFormatting, WithHeadings, FromCollection, withMapping
|
||||
{
|
||||
public function collection(){
|
||||
return Village::with('district.city.province')->get();
|
||||
}
|
||||
|
||||
public function map($row): array{
|
||||
return [
|
||||
$row->id,
|
||||
$row->code,
|
||||
$row->name,
|
||||
$row->postal_code,
|
||||
$row->district->name,
|
||||
$row->district->city->name,
|
||||
$row->district->city->province->name,
|
||||
$row->created_at
|
||||
];
|
||||
}
|
||||
public function headings(): array{
|
||||
return [
|
||||
'ID',
|
||||
'Code',
|
||||
'Name',
|
||||
'Postal Code',
|
||||
'District',
|
||||
'City',
|
||||
'Province',
|
||||
'Created At'
|
||||
];
|
||||
}
|
||||
|
||||
public function columnFormats(): array{
|
||||
return [
|
||||
'A' => \PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_NUMBER,
|
||||
'D' => \PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_NUMBER,
|
||||
'H' => \PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_DATE_DATETIME
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -132,4 +132,8 @@ class DistrictsController extends Controller
|
||||
public function export(Request $request){
|
||||
return Excel::download(new DistrictsExport, 'districts.xlsx');
|
||||
}
|
||||
|
||||
public function getDistrictsByCityId($id){
|
||||
return response()->json(District::where('city_code', $id)->get());
|
||||
}
|
||||
}
|
||||
|
||||
161
app/Http/Controllers/VillagesController.php
Normal file
161
app/Http/Controllers/VillagesController.php
Normal file
@@ -0,0 +1,161 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Location\Http\Controllers;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use Maatwebsite\Excel\Facades\Excel;
|
||||
use Modules\Location\Exports\VillagesExport;
|
||||
use Modules\Location\Http\Requests\VillageRequest;
|
||||
use Modules\Location\Models\City;
|
||||
use Modules\Location\Models\District;
|
||||
use Modules\Location\Models\Province;
|
||||
use Modules\Location\Models\Village;
|
||||
use Exception;
|
||||
|
||||
class VillagesController extends Controller
|
||||
{
|
||||
public $user;
|
||||
|
||||
public function index()
|
||||
{
|
||||
$provinces = Province::all();
|
||||
return view('location::villages.index',compact('provinces'));
|
||||
}
|
||||
|
||||
public function store(VillageRequest $request)
|
||||
{
|
||||
$validate = $request->validated();
|
||||
|
||||
if ($validate) {
|
||||
try {
|
||||
$village = Village::create($validate);
|
||||
return redirect()
|
||||
->route('locations.villages.index')
|
||||
->with('success', 'Village created successfully');
|
||||
} catch (Exception $e) {
|
||||
return redirect()->back()->with('error', 'Failed to create village. ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function create()
|
||||
{
|
||||
$provinces = Province::all();
|
||||
return view('location::villages.create', compact('provinces'));
|
||||
}
|
||||
|
||||
public function edit($id)
|
||||
{
|
||||
$village = Village::find($id);
|
||||
$provinces = Province::all();
|
||||
$cities = City::where('province_code', $village->province_code)->get();
|
||||
$districts = District::where('city_code', $village->city_code)->get();
|
||||
return view('location::villages.create', compact('village', 'provinces', 'cities', 'districts'));
|
||||
}
|
||||
|
||||
public function update(VillageRequest $request, $id)
|
||||
{
|
||||
$validate = $request->validated();
|
||||
|
||||
if ($validate) {
|
||||
try {
|
||||
$village = Village::find($id);
|
||||
$village->update($validate);
|
||||
return redirect()
|
||||
->route('locations.villages.index')
|
||||
->with('success', 'Village updated successfully');
|
||||
} catch (Exception $e) {
|
||||
return redirect()->back()->with('error', 'Failed to update village. ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function destroy($id)
|
||||
{
|
||||
try {
|
||||
Village::destroy($id);
|
||||
echo json_encode(['message' => 'Village deleted successfully', 'success' => true]);
|
||||
} catch (Exception $e) {
|
||||
echo json_encode(['message' => 'Failed to delete Village', 'success' => false]);
|
||||
}
|
||||
}
|
||||
|
||||
public function export(Request $request)
|
||||
{
|
||||
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.');
|
||||
}
|
||||
|
||||
// Retrieve data from the database
|
||||
$query = Village::query();
|
||||
|
||||
// Apply search filter if provided
|
||||
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[1]) &&!empty($search[1])){
|
||||
$query->where('city_code',$search[1]);
|
||||
}
|
||||
|
||||
if(isset($search[2]) &&!empty($search[2])){
|
||||
$query->where('district_code',$search[2]);
|
||||
}
|
||||
$query->where(function ($q) use ($search) {
|
||||
$q->where('code', 'LIKE', "%$search[3]%");
|
||||
$q->orWhere('name', 'LIKE', "%$search[3]%");
|
||||
});
|
||||
}
|
||||
|
||||
// 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->with('district.city.province')->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,
|
||||
]);
|
||||
}
|
||||
|
||||
}
|
||||
42
app/Http/Requests/VillageRequest.php
Normal file
42
app/Http/Requests/VillageRequest.php
Normal file
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Location\Http\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class VillageRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*/
|
||||
public function rules()
|
||||
: array
|
||||
{
|
||||
$rules = [
|
||||
'province_code' => 'required|exists:provinces,code',
|
||||
'city_code' => 'required|exists:cities,code',
|
||||
'district_code' => 'required|exists:districts,code',
|
||||
'postal_code' => 'required|string|max:5',
|
||||
'alt_name' => 'nullable|string|max:255',
|
||||
];
|
||||
|
||||
if ($this->method() === 'PUT') {
|
||||
$rules['code'] = 'required|string|max:6|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['name'] = 'required|string|max:2554|unique:villages,name';
|
||||
}
|
||||
|
||||
return $rules;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize()
|
||||
: bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
17
app/Models/Village.php
Normal file
17
app/Models/Village.php
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Location\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Modules\Location\Database\Factories\VillageFactory;
|
||||
|
||||
class Village extends Base
|
||||
{
|
||||
protected $fillable = ['province_code','city_code','district_code', 'code', 'name','alt_name','postal_code'];
|
||||
|
||||
public function district(){
|
||||
return $this->belongsTo(District::class, 'district_code', 'code');
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user