Files
streamline-emr/docker/streamline-src/Modules/ClinicalData/Http/Controllers/DrugUnitController.php
T

173 lines
4.7 KiB
PHP
Executable File

<?php
namespace Modules\ClinicalData\Http\Controllers;
use Illuminate\Http\Request;
use Streamline\Models\DrugUnit;
use Illuminate\Support\Facades\Auth;
use Illuminate\Database\QueryException;
class DrugUnitController extends Controller {
public function __construct() {
$this->middleware('auth');
$this->middleware('permission:drug-unit-list', ['only' => ['index']]);
$this->middleware('permission:drug-unit-create', ['only' => ['create', 'store']]);
$this->middleware('permission:drug-unit-edit', ['only' => ['edit', 'update']]);
$this->middleware('permission:drug-unit-delete', ['only' => ['destroy', 'inactive', 'activate']]);
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index() {
$drug_units = DrugUnit::orderBy('name', 'asc')
->paginate(50);
return view('clinical_data::drug_units.index', compact('drug_units'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create() {
return view('clinical_data::drug_units.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request) {
request()->validate([
'name' => 'required'
]);
$logged_in_user_id = Auth::user()->id;
$drug_unit = new DrugUnit;
$drug_unit->name = $request->name;
$drug_unit->created_by = $logged_in_user_id;
$drug_unit->updated_by = $logged_in_user_id;
try {
$drug_unit->save();
flash($request->name . " Drug Unit has been saved")->success();
return redirect("/drug_units/");
} 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) {
$drug_unit = DrugUnit::where(['id' => $id])->first();
if (!$drug_unit) {
flash()->error("There is no such Unit");
return redirect('/drug_units/');
} else {
return view('clinical_data::drug_units.edit', compact('drug_unit'));
}
}
/**
* 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'
]);
$logged_in_user_id = Auth::user()->id;
$drug_unit = DrugUnit::find($id);
$drug_unit->name = $request->name;
$drug_unit->updated_by = $logged_in_user_id;
try {
$drug_unit->save();
flash($request->name . " Drug Unit has been updated")->success();
return redirect("/drug_units/");
} 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) {
$drug_unit = DrugUnit::find($id);
if ($drug_unit->delete()):
flash("Drug Unit has been deleted.")->success();
return redirect('/drug_units/');
endif;
}
/**
* Display a listing of the inactive resource(s).
*
* @return \Illuminate\Http\Response
*/
public function inactive() {
$drug_units = DrugUnit::onlyTrashed()
->orderBy('name', 'asc')
->paginate(50);
if (count($drug_units) < 1) {
flash()->error("There is no inactive drug unit");
return redirect('/drug_units/');
} else {
return view('clinical_data::drug_units.inactive', compact('drug_units'));
}
}
/**
* Activate the specified resource in storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function activate($id) {
$drug_unit = DrugUnit::withTrashed()->find($id);
if ($drug_unit->restore()){
flash("Drug Unit has been activated.")->success();
return redirect('/drug_units/');
}
}
}