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

176 lines
4.9 KiB
PHP
Executable File

<?php
namespace Modules\Investigations\Http\Controllers;
use Illuminate\Database\QueryException;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;
use Streamline\Models\LabForm;
use Illuminate\Http\Request;
class LabFormController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$lab_forms = LabForm::get();
return view('investigations::lab_forms.index', compact('lab_forms'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('investigations::lab_forms.create');
}
/**
* 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',
//'mobile_number' => 'regex:/07\d{2} \d{3} \d{3}/'
//'mobile_number' => 'regex:/(07)[0-9]{8}/'
]);
if ($validator->fails()) {
$string = "";
foreach ($validator->errors()->getMessages() as $item) {
$string .= "{$item[0]}<br>";
}
return back()->withErrors($validator)->withInput();
} else {
$logged_in_user_id = Auth::user()->id;
$lab_form = new LabForm;
$lab_form->name = $request->name;
$lab_form->created_by = $logged_in_user_id;
$lab_form->updated_by = $logged_in_user_id;
try {
$lab_form->save();
flash($request->name . " LabForm has been saved")->success();
return redirect("/lab_forms/");
} catch (QueryException $e) {
flash("An error occurred")->error();
return back()->withInput();
}
}
}
/**
* Display the specified resource.
*
* @param \Streamline\Models\LabForm $labForm
* @return \Illuminate\Http\Response
*/
public function show(LabForm $labForm)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param \Streamline\Models\LabForm $labForm
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
$lab_form = LabForm::where(['id' => $id])->first();
return view('investigations::lab_forms.edit', compact('lab_form'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \Streamline\Models\LabForm $labForm
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$validator = Validator::make($request->all(), [
'name' => 'required',
]);
if ($validator->fails()) {
$string = "";
foreach ($validator->errors()->getMessages() as $item) {
$string .= "{$item[0]}<br>";
}
return back()->withErrors($validator)->withInput();
} else {
$logged_in_user_id = Auth::user()->id;
$lab_form = LabForm::find($id);
$lab_form->name = $request->name;
$lab_form->updated_by = $logged_in_user_id;
try {
$lab_form->save();
flash($request->name . " LabForm has been updated")->success();
return redirect("/lab_forms/");
} catch (QueryException $e) {
flash("An error occurred")->error();
return back()->withInput();
}
}
}
/**
* Remove the specified resource from storage.
*
* @param \Streamline\Models\LabForm $labForm
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
$lab = LabForm::find($id);
if ($lab->delete()) {
flash("Lab has been deleted.")->success();
return redirect('labs');
}
}
public function inactive() {
$lab_forms = LabForm::onlyTrashed()
->orderBy('name', 'asc')
->get();
if (count($lab_forms) < 1) {
flash()->error("There is no inactive lab forms");
return redirect('lab_forms');
}
return view('investigations::lab_forms.inactive', compact('lab_forms'));
}
/**
* Activate the specified resource in storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function activate($id) {
$lab_form = LabForm::withTrashed()->find($id);
if($lab_form->restore()){
flash("Lab Form has been activated.")->success();
return redirect('/lab_forms/inactive');
}
}
}