middleware('auth');
$this->middleware('permission:departments-list', ['only' => ['index']]);
$this->middleware('permission:departments-detail', ['only' => ['show']]);
$this->middleware('permission:departments-create', ['only' => ['create', 'store']]);
$this->middleware('permission:departments-edit', ['only' => ['edit', 'edit_all', 'update', 'update_all', 'updatePatientEpisode']]);
$this->middleware('permission:departments-delete', ['only' => ['destroy']]);
$this->middleware('permission:departments-status', ['only' => ['activate, inactive']]);
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$departments = Department::orderBy('name', 'asc')->paginate(50);
return view('clinical_data::departments.index', compact('departments'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('clinical_data::departments.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'
]);
if ($validator->fails()) {
$string = "";
foreach ($validator->errors()->getMessages() as $item) {
$string .= "{$item[0]}
";
}
flash($string)->error();
return back()->withErrors($validator)->withInput();
} else {
$logged_in_user_id = Auth::user()->id;
$department = new Department;
$department->name = $request->name;
$department->created_by = $logged_in_user_id;
$department->updated_by = $logged_in_user_id;
try {
$department->save();
flash($request->name . " Department has been saved")->success();
return redirect("/departments/");
} 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)
{
$department = Department::where(['id' => $id])->first();
if (!$department) {
flash()->error("There is no such department");
return redirect('/departments/');
} else {
return view('clinical_data::departments.edit', compact('department'));
}
}
/**
* 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]}
";
}
// flash($string)->error();
return back()->withErrors($validator)->withInput();
} else {
$logged_in_user_id = Auth::user()->id;
$department = Department::find($id);
$department->name = $request->name;
$department->updated_by = $logged_in_user_id;
try {
$department->save();
flash($request->name . " Department has been updated")->success();
return redirect("/departments/");
} 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)
{
$department = Department::find($id);
if ($department->delete()):
flash("Department has been deleted.")->success();
return redirect('/departments/');
endif;
}
/**
* Display a listing of the inactive resource(s).
*
* @return \Illuminate\Http\Response
*/
public function inactive()
{
$departments = Department::onlyTrashed()
->orderBy('name', 'asc')
->paginate(50);
if (count($departments) < 1) {
flash()->error("There is no inactive department");
return redirect('/departments/');
} else {
return view('clinical_data::departments.inactive', compact('departments'));
}
}
/**
* Activate the specified resource in storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function activate($id)
{
$department = Department::withTrashed()->find($id);
if ($department->restore()):
flash("Department has been activated.")->success();
return redirect('/departments/inactive');
endif;
}
/**
* Display a listing of the active resources for bulk editing.
*
* @return \Illuminate\Http\Response
*/
public function edit_all()
{
$departments = Department::orderBy('name', 'asc')->paginate(25);
if (count($departments) < 1) {
flash()->error("There is no active department");
return redirect('/departments/');
} else {
return view('clinical_data::departments.edit.all', compact('departments'));
}
}
/**
* Update all the resources in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function update_all(Request $request)
{
$validator = Validator::make($request->all(), [
'name' => 'required'
]);
if ($validator->fails()) {
$string = "";
foreach ($validator->errors()->getMessages() as $item) {
$string .= "{$item[0]}
";
}
// flash($string)->error();
return back()->withErrors($validator)->withInput();
} else {
$logged_in_user_id = Auth::user()->id;
$id_array = $request->id;
$name_array = $request->name;
for ($x = 0; $x < count($id_array); $x++):
$department = Department::find($id_array[$x]);
$department->name = $name_array[$x];
$department->updated_by = $logged_in_user_id;
try {
$department->save();
} catch (QueryException $e) {
flash("An error occurred")->error();
return back()->withInput();
}
endfor;
flash("Departments have been updated")->success();
return redirect("/departments/");
}
}
}