Files

237 lines
8.5 KiB
PHP
Executable File

<?php
namespace Modules\Investigations\Http\Controllers;
use Illuminate\Database\QueryException;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Streamline\Models\Investigation;
use Streamline\Models\InvestigationSpecialisedVariable;
use Streamline\Models\InvestigationTestCode;
use Streamline\Models\LabInstrument;
class InvestigationTestCodesController extends Controller {
/**
* Display a listing of the resource.
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\Foundation\Application|\Illuminate\View\View
*/
public function index() {
$test_codes = InvestigationTestCode::get();
$instruments = LabInstrument::pluck('name', 'id');
$investigations = Investigation::pluck('name', 'id');
$specialised_variables = InvestigationSpecialisedVariable::pluck('name', 'id');
return view('investigations::investigation_test_codes.index', compact('test_codes', 'investigations', 'instruments', 'specialised_variables'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\Foundation\Application|\Illuminate\View\View
*/
public function create() {
$investigations = Investigation::orderBy('name')
->pluck('name', 'id')
->prepend('- select -', '');
$instruments = LabInstrument::orderBy('name')
->pluck('name', 'id')
->prepend('- select -', '');
return view('investigations::investigation_test_codes.create', compact('investigations', 'instruments'));
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\RedirectResponse
*/
public function store(Request $request) {
request()->validate([
'investigation_id' => 'required',
'machine_name' => 'required',
'test_code' => 'required'
]);
$test_code = new InvestigationTestCode();
$logged_in_user_id = Auth::user()->id;
if ($request->is_specialised_variable == 1){
if (is_null($request->investigation_specialised_variable)) {
flash("Please include the specialised variable for this investigation")->error();
return back()->withInput();
} else {
$test_code->investigation_id = $request->investigation_specialised_variable;
}
} else {
$test_code->investigation_id = $request->investigation_id;
}
$test_code->machine_name = $request->machine_name;
$test_code->test_code = $request->test_code;
$test_code->is_specialised_variable = $request->is_specialised_variable;
$test_code->created_by = $logged_in_user_id;
$test_code->updated_by = $logged_in_user_id;
try {
$test_code->save();
flash("Test Code has been saved")->success();
return redirect("/investigation_test_codes/");
} catch (QueryException $e) {
flash("Something went wrong. Please try again")->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\Foundation\Application|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function edit($id) {
$test_code = InvestigationTestCode::where(['id' => $id])->first();
$investigations = Investigation::orderBy('name')
->pluck('name', 'id')
->prepend('- select -', '');
$instruments = LabInstrument::orderBy('name')
->pluck('name', 'id')
->prepend('- select -', '');
$specialised_variables = InvestigationSpecialisedVariable::orderBy('name')
->pluck('name', 'id')
->prepend('- select -', '');
if (!$test_code) {
flash()->error("Investigation test code not found");
return redirect('/investigation_test_codes/');
} else {
return view('investigations::investigation_test_codes.edit', compact('test_code', 'investigations', 'instruments', 'specialised_variables'));
}
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Foundation\Application|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function update(Request $request, $id) {
request()->validate([
'investigation_id' => 'required',
'machine_name' => 'required',
'test_code' => 'required'
]);
//validation passed
$test_code = InvestigationTestCode::find($id);
$logged_in_user_id = Auth::user()->id;
if ($request->is_specialised_variable == 1){
if (is_null($request->investigation_specialised_variable)) {
flash("Please include the specialised variable for this investigation")->error();
return back()->withInput();
} else {
$test_code->investigation_id = $request->investigation_specialised_variable;
}
} else {
$test_code->investigation_id = $request->investigation_id;
}
$test_code->machine_name = $request->machine_name;
$test_code->test_code = $request->test_code;
$test_code->is_specialised_variable = $request->is_specialised_variable;
$test_code->updated_by = $logged_in_user_id;
try {
$test_code->save();
flash("Test code has been updated")->success();
return redirect("/investigation_test_codes/");
} catch (QueryException $e) {
flash("An error occurred")->error();
return back()->withInput();
}
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Foundation\Application|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function destroy($id) {
$test_code = InvestigationTestCode::find($id);
if ($test_code->delete()) {
flash("Test Code has been deleted.")->success();
return redirect('/investigation_test_codes/');
}
}
/**
* Display a listing of the inactive resource(s).
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\Foundation\Application|\Illuminate\View\View
*/
public function inactive() {
$test_codes = InvestigationTestCode::onlyTrashed()->get();
$investigations = Investigation::pluck('name', 'id');
$instruments = LabInstrument::pluck('name', 'id');
$specialised_variables = InvestigationSpecialisedVariable::pluck('name', 'id');
if (count($test_codes) < 1) {
flash()->error("There is no inactive test code");
return redirect('/investigation_test_codes/');
} else {
return view('investigations::investigation_test_codes.inactive', compact('test_codes', 'investigations', 'instruments', 'specialised_variables'));
}
}
/**
* Activate the specified resource in storage.
*
* @param int $id
* @return \Illuminate\Foundation\Application|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function activate($id) {
$test_code = InvestigationTestCode::withTrashed()->find($id);
if($test_code->restore()){
flash("Test code has been activated.")->success();
return redirect('/investigation_test_codes/inactive');
}
}
public function check_investigation_speciality_type($id) {
if (get_name($id, 'id', 'type', 'investigations') == 1){
$code = "<select name='investigation_specialised_variable' id='investigation_specialised_variable' class='form-control select'>";
$code .= "<option value=''> --select-- </option>";
$variables = InvestigationSpecialisedVariable::where('investigation_id', $id)->get();
foreach ($variables as $variable){
$code .= "<option value='" . $variable->id . "'>" . $variable->name . "</option>";
}
$code .= "</select>";
} else {
$code = "0";
}
return $code;
}
}