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

198 lines
5.4 KiB
PHP

<?php
namespace Modules\ClinicalData\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Auth;
use Illuminate\Database\QueryException;
use Streamline\Models\Country;
class CountriesController extends Controller
{
public function __construct()
{
$this->middleware('auth');
$this->middleware('permission:countries-list', ['only' => ['index']]);
$this->middleware('permission:countries-detail', ['only' => ['show']]);
$this->middleware('permission:countries-create', ['only' => ['create', 'store']]);
$this->middleware('permission:countries-edit', ['only' => ['edit', 'edit_all', 'update', 'update_all', 'updatePatientEpisode']]);
$this->middleware('permission:countries-delete', ['only' => ['destroy']]);
$this->middleware('permission:countries-status', ['only' => ['activate, inactive']]);
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$countries = Country::orderBy('name', 'asc')->paginate(50);
return view('clinical_data::countries.index', compact('countries'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('clinical_data::countries.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]}<br>";
}
flash($string)->error();
return back()->withErrors($validator)->withInput();
}
else {
$country = new Country;
$country->name = $request->name;
$country->created_by = Auth::user()->id;
$country->updated_by = Auth::user()->id;
try {
$country->save();
flash($request->name . " Country has been saved")->success();
return redirect("/countries/");
} catch (QueryException $e) {
flash("An error occurred")->error();
return back()->withInput();
}
}
}
/**
* Display the specified resource.
*
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
*/
public function edit($id)
{
$country = Country::where(['id' => $id])->first();
if (!$country) {
flash()->error("There is no such country");
return redirect('/countries/');
} else {
return view('clinical_data::countries.edit', compact('country'));
}
}
/**
* Update the specified resource in storage.
*
*/
public function update(Request $request, Country $countries)
{
$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 {
$logged_in_user_id = Auth::user()->id;
$country = Country::find($id);
$country->name = $request->name;
$country->updated_by = $logged_in_user_id;
try {
$country->save();
flash($request->name . " Country has been updated")->success();
return redirect("/countries/");
} catch (QueryException $e) {
flash("An error occurred")->error();
return back()->withInput();
}
}
}
/**
* Remove the specified resource from storage.
*
*/
public function destroy($id)
{
$country = Country::find($id);
if ($country->delete()):
flash("Country has been deleted.")->success();
return redirect('/countries/');
endif;
}
/**
* Display a listing of the inactive resource(s).
*
* @return \Illuminate\Http\Response
*/
public function inactive()
{
$countries = Country::onlyTrashed()
->orderBy('name', 'asc')
->paginate(50);
if (count($countries) < 1) {
flash()->error("There is no inactive country");
return redirect('/countries/');
} else {
return view('clinical_data::countries.inactive', compact('countries'));
}
}
/**
* Activate the specified resource in storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function activate($id)
{
$country = Country::withTrashed()->find($id);
if ($country->restore()):
flash("Country has been activated.")->success();
return redirect('/countries/inactive');
endif;
}
}