Feature #1 : Provinces
This commit is contained in:
41
app/Exports/ProvincesExport.php
Normal file
41
app/Exports/ProvincesExport.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?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\Province;
|
||||
use Modules\Usermanagement\Models\Role;
|
||||
|
||||
class ProvincesExport implements WithColumnFormatting, WithHeadings, FromCollection, withMapping
|
||||
{
|
||||
public function collection(){
|
||||
return Province::all();
|
||||
}
|
||||
|
||||
public function map($row): array{
|
||||
return [
|
||||
$row->id,
|
||||
$row->code,
|
||||
$row->name,
|
||||
$row->created_at
|
||||
];
|
||||
}
|
||||
public function headings(): array{
|
||||
return [
|
||||
'ID',
|
||||
'Code',
|
||||
'Name',
|
||||
'Created At'
|
||||
];
|
||||
}
|
||||
|
||||
public function columnFormats(): array{
|
||||
return [
|
||||
'A' => \PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_NUMBER,
|
||||
'C' => \PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_DATE_DATETIME
|
||||
];
|
||||
}
|
||||
}
|
||||
133
app/Http/Controllers/ProvincesController.php
Normal file
133
app/Http/Controllers/ProvincesController.php
Normal file
@@ -0,0 +1,133 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Location\Http\Controllers;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use Maatwebsite\Excel\Facades\Excel;
|
||||
use Modules\Location\Exports\ProvincesExport;
|
||||
use Modules\Location\Http\Requests\ProvinceRequest;
|
||||
use Modules\Location\Models\Province;
|
||||
|
||||
class ProvincesController extends Controller
|
||||
{
|
||||
public $user;
|
||||
|
||||
public function index(){
|
||||
return view('location::provinces.index');
|
||||
}
|
||||
|
||||
public function create(){
|
||||
return view('location::provinces.create');
|
||||
}
|
||||
|
||||
public function store(ProvinceRequest $request){
|
||||
$validate = $request->validated();
|
||||
|
||||
if($validate){
|
||||
try{
|
||||
// Save to database
|
||||
$province = 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');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function edit($id){
|
||||
$province = Province::find($id);
|
||||
return view('location::provinces.create', compact('province'));
|
||||
}
|
||||
|
||||
public function update(ProvinceRequest $request, $id){
|
||||
$validate = $request->validated();
|
||||
|
||||
if($validate){
|
||||
try{
|
||||
// Update in database
|
||||
$province = Province::find($id);
|
||||
$province->update($validate);
|
||||
return redirect()->route('locations.provinces.index')->with('success', 'Province updated successfully');
|
||||
} catch (\Exception $e){
|
||||
return redirect()->route('locations.provinces.edit', $id)->with('error', 'Failed to update province');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function destroy($id){
|
||||
try{
|
||||
// Delete from database
|
||||
$province = Province::find($id);
|
||||
$province->delete();
|
||||
|
||||
echo json_encode(['success' => true, 'message' => 'Province deleted successfully']);
|
||||
} catch (\Exception $e){
|
||||
echo json_encode(['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.');
|
||||
}
|
||||
|
||||
// Retrieve data from the database
|
||||
$query = Province::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%");
|
||||
});
|
||||
}
|
||||
|
||||
// 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
|
||||
$roles = $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' => $roles,
|
||||
]);
|
||||
}
|
||||
|
||||
public function export()
|
||||
{
|
||||
return Excel::download(new ProvincesExport, 'provinces.xlsx');
|
||||
}
|
||||
}
|
||||
33
app/Http/Requests/ProvinceRequest.php
Normal file
33
app/Http/Requests/ProvinceRequest.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Location\Http\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class ProvinceRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
|
||||
if ($this->method() === 'PUT') {
|
||||
$rules['code'] = 'required|string|max:4|unique:provinces,code,' . $this->id;
|
||||
$rules['name'] = 'required|string|max:2554|unique:provinces,name,' . $this->id;
|
||||
} else {
|
||||
$rules['code'] = 'required|string|max:4|unique:provinces,code';
|
||||
$rules['name'] = 'required|string|max:2554|unique:provinces,name';
|
||||
}
|
||||
|
||||
return $rules;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
50
app/Models/Base.php
Normal file
50
app/Models/Base.php
Normal file
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
namespace Modules\Location\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use Spatie\Activitylog\LogOptions;
|
||||
use Spatie\Activitylog\Traits\LogsActivity;
|
||||
use Wildside\Userstamps\Userstamps;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
class Base extends Model
|
||||
{
|
||||
use LogsActivity, SoftDeletes, Userstamps;
|
||||
|
||||
protected $connection;
|
||||
|
||||
/**
|
||||
* Constructs a new instance of the class.
|
||||
*
|
||||
* @param array $attributes Optional attributes to initialize the object with.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(array $attributes = [])
|
||||
{
|
||||
parent::__construct($attributes);
|
||||
|
||||
// Retrieve the module configuration from the module.json file
|
||||
$modulePath = dirname(__FILE__, 3) . '/module.json';
|
||||
$module = file_get_contents($modulePath);
|
||||
$module = json_decode($module);
|
||||
|
||||
// Set the connection property to the database connection specified in the module configuration
|
||||
$this->connection = $module->database;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the activity log options for the User Management.
|
||||
*
|
||||
* @return LogOptions The activity log options.
|
||||
*/
|
||||
public function getActivitylogOptions()
|
||||
: LogOptions
|
||||
{
|
||||
return LogOptions::defaults()->logAll()->useLogName('Locations : ');
|
||||
}
|
||||
}
|
||||
22
app/Models/Province.php
Normal file
22
app/Models/Province.php
Normal file
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Location\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use Modules\Location\Models\City;
|
||||
use Modules\Location\Models\District;
|
||||
use Modules\Location\Models\Village;
|
||||
use Spatie\Activitylog\LogOptions;
|
||||
use Spatie\Activitylog\Traits\LogsActivity;
|
||||
use Spatie\Permission\Models\Role as SpatieRole;
|
||||
|
||||
class Province extends Base
|
||||
{
|
||||
protected $fillable = ['code','name'];
|
||||
|
||||
public function cities(){
|
||||
return $this->hasMany(City::class, 'province_code', 'code');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -22,6 +22,10 @@ class LocationServiceProvider extends ServiceProvider
|
||||
$this->registerConfig();
|
||||
$this->registerViews();
|
||||
$this->loadMigrationsFrom(module_path($this->moduleName, 'database/migrations'));
|
||||
|
||||
if (class_exists('Breadcrumbs')) {
|
||||
require __DIR__ . '/../../routes/breadcrumbs.php';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user