Files
streamline-emr/docker/statistics/Modules/ClinicalData/Http/Controllers/SubcountyController.php
T
2025-03-31 03:46:05 +03:00

196 lines
5.7 KiB
PHP
Executable File

<?php
namespace Modules\ClinicalData\Http\Controllers;
use Illuminate\Database\QueryException;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;
use Streamline\Models\County;
use Streamline\Models\Subcounty;
class SubcountyController extends Controller {
public function __construct() {
$this->middleware('auth');
$this->middleware('permission:subcounty-list', ['only' => ['index']]);
$this->middleware('permission:subcounty-create', ['only' => ['create', 'store']]);
$this->middleware('permission:subcounty-edit', ['only' => ['edit', 'update']]);
$this->middleware('permission:subcounty-delete', ['only' => ['destroy', 'inactive', 'activate']]);
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index() {
$sub_counties = Subcounty::orderBy('name', 'asc')->get();
$counties = County::pluck('name', 'id');
return view('clinical_data::sub_counties.index', compact('sub_counties', 'counties'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create() {
$counties = County::all(['id', 'name', 'district_id'])->pluck("name_with_district", "id")->prepend('- select -', '')->toArray();
return view('clinical_data::sub_counties.create', compact('counties'));
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request) {
$validator = Validator::make($request->all(), [
'name' => 'required',
'county_id' => 'required'
]);
if ($validator->fails()) {
$string = "";
foreach ($validator->errors()->getMessages() as $item) {
$string .= "{$item[0]}<br>";
}
flash($string)->error();
return back()->withErrors($validator)->withInput();
}else{
$user_id = Auth::user()->id;
$sub_county = new Subcounty;
$sub_county->name = $request->name;
$sub_county->county_id = $request->county_id;
$sub_county->created_by = $user_id;
$sub_county->updated_by = $user_id;
try {
$sub_county->save();
flash($request->name . " Sub county has been saved")->success();
return redirect("/subcounties/");
} catch (QueryException $e) {
flash("An error occurred")->error();
return back()->withInput();
}
}
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id) {
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id) {
$sub_county = Subcounty::where(['id' => $id])->first();
$counties = County::all(['id', 'name', 'district_id'])->pluck("name_with_district", "id")->prepend('- select -', '')->toArray();
if (!$sub_county) {
flash()->error("That sub county is not registered");
return redirect('/sub_counties/');
} else {
return view('clinical_data::sub_counties.edit', compact('sub_county', 'counties'));
}
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id) {
request()->validate([
'name' => 'required',
'county_id' => 'required'
]);
$sub_county = Subcounty::find($id);
$sub_county->name = $request->name;
$sub_county->county_id = $request->county_id;
$sub_county->updated_by = Auth::user()->id;
try {
$sub_county->save();
flash($request->name . " Sub county has been updated")->success();
return redirect("/subcounties/");
} catch (QueryException $e) {
flash("An error occurred")->error();
return back()->withInput();
}
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id) {
$sub_county = Subcounty::find($id);
if ($sub_county->delete()) {
flash("Sub county has been deleted.")->success();
return redirect('/subcounties/');
}
}
/**
* Display a listing of the inactive resource(s).
*
* @return \Illuminate\Http\Response
*/
public function inactive() {
$sub_counties = Subcounty::onlyTrashed()
->orderBy('name', 'asc')
->get();
$counties = County::pluck('name', 'id');
if (count($sub_counties) < 1) {
flash()->error("There is no inactive sub counties");
}
return view('clinical_data::sub_counties.inactive', compact('counties', 'sub_counties'));
}
/**
* Activate the specified resource in storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function activate($id) {
$sub_county = Subcounty::withTrashed()->find($id);
if($sub_county->restore()){
flash("Sub county has been activated.")->success();
return redirect('/subcounties/inactive');
}
}
}