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

192 lines
5.7 KiB
PHP
Executable File

<?php
namespace Modules\ClinicalData\Http\Controllers;
use Illuminate\Http\Request;
use Streamline\Models\Occupation;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Auth;
use Illuminate\Database\QueryException;
class OccupationController extends Controller {
public function __construct() {
$this->middleware('auth');
$this->middleware('permission:occupation-list', ['only' => ['index']]);
$this->middleware('permission:occupation-detail', ['only' => ['show']]);
$this->middleware('permission:occupation-create', ['only' => ['create', 'store']]);
$this->middleware('permission:occupation-edit', ['only' => ['edit', 'update']]);
$this->middleware('permission:occupation-delete', ['only' => ['destroy']]);
$this->middleware('permission:occupation-status', ['only'=>['inactive', 'activate']]);
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index() {
$occupations = Occupation::orderBy('name', 'asc')
->paginate(50);
return view('clinical_data::occupations.index', compact('occupations'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create() {
return view('clinical_data::occupations.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|min:4"
]);
if ($validator->fails()) {
$string = "";
foreach ($validator->errors()->getMessages() as $item) {
$string .= "{$item[0]}<br>";
}
flash($string)->error();
return back()->withErrors($validator)->withInput();
} else {
$occupation = new Occupation;
$occupation->name = $request->name;
$occupation->created_by = Auth::user()->id;
$occupation->updated_by = Auth::user()->id;
try {
$occupation->save();
flash($request->name . " Occupation has been saved")->success();
return redirect("/occupations/");
} 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) {
$occupation = Occupation::where(['id' => $id])->first();
if (!$occupation) {
flash()->error("Occupation not found");
return redirect('/occupations/');
} else {
return view('clinical_data::occupations.edit', compact('occupation'));
}
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @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>";
}
// flash($string)->error();
return back()->withErrors($validator)->withInput();
} else {
//validation passed
$occupation = Occupation::find($id);
$occupation->name = $request->name;
try {
$occupation->save();
flash($request->name . " Occupation has been updated")->success();
return redirect("/occupations/");
} 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) {
$occupation = Occupation::find($id);
if ($occupation->delete()):
flash("Occupation has been deleted.")->success();
return redirect('/occupations/');
endif;
}
/**
* Display a listing of the inactive resource(s).
*
* @return \Illuminate\Http\Response
*/
public function inactive() {
$occupations = Occupation::onlyTrashed()
->orderBy('name', 'asc')
->paginate(50);
if (count($occupations) < 1) {
flash()->error("There is no inactive occupation");
return redirect('/occupations/');
} else {
// Log::info($occupations);
return view('clinical_data::occupations.inactive', compact('occupations'));
}
}
/**
* Activate the specified resource in storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function activate($id) {
$occupation = Occupation::withTrashed()->find($id);
if ($occupation->restore()):
flash("Occupation has been activated.")->success();
return redirect('/occupations/inactive');
endif;
}
}