Files
streamline-emr/docker/_streamline-src/Modules/Patients/Http/Controllers/PatientDocumentController.php
T

244 lines
11 KiB
PHP
Executable File

<?php
namespace Modules\Patients\Http\Controllers;
use Illuminate\Http\Request;
use Streamline\Models\Alert;
use Streamline\Models\Allergy;
use Streamline\Models\DrugCategory;
use Streamline\Models\PatientDocument;
use Streamline\Models\Patient;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
class PatientDocumentController extends Controller {
public function __construct() {
$this->middleware('auth');
}
public function index() {
$patient_id = session()->get('patient_id');
$patient_episodes = \Illuminate\Support\Facades\DB::table('patient_episodes')
->where(['patient_id' => $patient_id])
->orderBy('id', 'desc')
->get();
$patient = Patient::where(['id' => $patient_id])->first();
$categories = DB::table('patient_categories')->where('available', 1)->pluck("name", "id");
$marital_statuses = DB::table('marital_statuses')->pluck("name", "id");
$diagnoses = DB::table('diagnoses')->where('available', 1)->pluck("name", "id");
$clinics = DB::table('clinics')->pluck("name", "id");
$relationships = DB::table('family_relations')->pluck('name', 'id');
$occupations = DB::table('occupations')->pluck('name', 'id');
$patient_categories = DB::table('patient_categories')->where('available', 1)->pluck('name', 'id');
$districts = DB::table('districts')->pluck('name', 'id');
$counties = DB::table('counties')->pluck('name', 'id');
$subcounties = DB::table('subcounties')->pluck('name', 'id');
$parishes = DB::table('parishes')->pluck('name', 'id');
$villages = DB::table('villages')->pluck('name', 'id');
$drug_categories = DrugCategory::orderBy('name', 'asc')->get();
$documents = PatientDocument::where('patient_id', $patient_id)->orderBy('created_at', 'desc')->get();
$known_patient_allergies = Allergy::where('patient_id', $patient_id)->orderBy('created_at', 'desc')->take(2)->get();
$known_patient_alerts = Alert::where('patient_id', $patient_id)->orderBy('created_at', 'desc')->take(2)->get();
$drug_categories_array = DB::table('drug_categories')->pluck('name', 'id');
return view('patients::patient_documents.index', compact('patient', 'clinics', 'relationships', 'occupations', 'patient_categories', 'diagnoses', 'patient_episodes', 'categories', 'marital_statuses', 'districts', 'counties', 'subcounties', 'parishes', 'villages', 'drug_categories', 'documents', 'known_patient_allergies', 'known_patient_alerts', 'drug_categories_array'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create() {
$patient_id = session()->get('patient_id');
$episode_id = session()->get('episode_id');
$patient = Patient::find($patient_id);
$categories = DB::table('patient_categories')->where('available', 1)->pluck("name", "id");
$drug_categories = DB::table('drug_categories')->get();
$marital_statuses = DB::table('marital_statuses')->pluck("name", "id");
$diagnoses = DB::table('diagnoses')->where('available', 1)->pluck("name", "id");
$clinics = DB::table('clinics')->pluck("name", "id");
$relationships = DB::table('family_relations')->pluck('name', 'id');
$occupations = DB::table('occupations')->pluck('name', 'id');
$patient_categories = DB::table('patient_categories')->where('available', 1)->pluck('name', 'id');
$districts = DB::table('districts')->pluck('name', 'id');
$counties = DB::table('counties')->pluck('name', 'id');
$subcounties = DB::table('subcounties')->pluck('name', 'id');
$parishes = DB::table('parishes')->pluck('name', 'id');
$villages = DB::table('villages')->pluck('name', 'id');
$documents = PatientDocument::where('patient_id', $patient_id)->orderBy('created_at', 'desc')->get();
$drug_categories = DB::table('drug_categories')->orderBy('name', 'asc')->get();
//allergies and alerts
$known_patient_allergies = \Streamline\Models\Allergy::where('patient_id', $patient_id)->orderBy('created_at', 'desc')->take(2)->get();
$known_patient_alerts = \Streamline\Models\Alert::where('patient_id', $patient_id)->orderBy('created_at', 'desc')->take(2)->get();
$drug_categories_array = DB::table('drug_categories')->pluck('name', 'id');
return view('patients::patient_documents.create', compact('patient', 'clinics', 'relationships', 'occupations', 'patient_categories', 'diagnoses', 'categories', 'marital_statuses', 'districts', 'counties', 'subcounties', 'parishes', 'villages', 'drug_categories', 'episode_id', 'documents', 'drug_categories_array', 'drug_categories', 'known_patient_allergies', 'known_patient_alerts'));
}
/**
* Store a newly created resource in storage.
*
*/
public function store(Request $request) {
request()->validate([
'documenttitle' => 'required'
]);
$patient_document = new PatientDocument;
$patient_document->patient_id = session()->get('patient_id');
$patient_document->episode_id = session()->get('episode_id');
$patient_document->title = $request->documenttitle;
$patient_document->description = $request->description;
if ($request->file('document')->isValid()) {
$file = $request->file('document');
$store = public_path() . '/uploads/patient-documents/';
$file_name = $file->getClientOriginalName();
$file->move($store, $file_name);
$patient_document->path = public_path() . '/uploads/patient-documents/' . $file_name;
}
$patient_document->date_taken = \Carbon\Carbon::createFromFormat('d/m/Y', $request->documentdate)->toDateString();
$patient_document->created_by = auth()->user()->id;
$patient_document->úpdated_by = auth()->user()->id;
$patient_document->save();
flash("document has been added.")->success();
// redirect to consultation or patient_episode page depending on where the user is from
if (session()->has('redirect_to_consultation')) {
$url = session()->get('redirect_to_consultation');
session()->forget('redirect_to_consultation');
return redirect($url);
} else {
return redirect("/patient_episodes");
}
}
public function modal_store(Request $request) {
$validator = Validator::make($request->all(), [
'documenttitle' => 'required',
]);
if ($validator->fails()) {
$string = "";
foreach ($validator->errors()->getMessages() as $item) {
$string .= "{$item[0]}<br>";
}
return back()->withErrors($validator)->withInput();
} else {
$patient_document = new PatientDocument;
$patient_document->patient_id = session()->get('patient_id');
$patient_document->episode_id = session()->get('episode_id');
$patient_document->title = $request->documenttitle;
$patient_document->description = $request->description;
if ($request->file('document')->isValid()) {
$file = $request->file('document');
$store = public_path() . '/uploads/patient-documents/';
$file_name = $file->getClientOriginalName();
$file->move($store, $file_name);
$patient_document->path = public_path() . '/uploads/patient-documents/' . $file_name;
}
$patient_document->date_taken = \Carbon\Carbon::createFromFormat('d/m/Y', $request->documentdate)->toDateString();
$patient_document->created_by = auth()->user()->id;
$patient_document->úpdated_by = auth()->user()->id;
$patient_document->save();
flash("document has been added.")->success();
return redirect('/consultation/route');
}
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id) {
$document = PatientDocument::find($id);
if (substr($document->path, 0, 25) === "../uploads/patient_upload") {
$document_path_cleaned = str_replace("../uploads/patient_uploads", "/var/www/html/uploads/patient-documents", $document->path);
} else {
$document_path_cleaned = $document->path;
}
$ext = pathinfo($document->path, PATHINFO_EXTENSION);
if ($ext == "pdf") {
return response()->file($document_path_cleaned, ['Content-Type' => 'application/pdf']);
} elseif ($ext == 'doc') {
return response()->file($document_path_cleaned, [
'Content-Type' => 'application/msword'
]);
} elseif ($ext == 'docx') {
return response()->file($document_path_cleaned, [
'Content-Type' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
]);
} elseif ($ext == 'xls') {
return response()->file($document_path_cleaned, [
'Content-Type' => 'application/vnd.ms-excel'
]);
} elseif ($ext == 'xlsx') {
return response()->file($document_path_cleaned, [
'Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
]);
} elseif ($ext == 'txt') {
return response()->file($document_path_cleaned, [
'Content-Type' => 'application/octet-stream'
]);
} elseif ($ext == 'png' || $ext == 'jpg' || $ext == 'JPG' || $ext == 'jpeg') {
return response()->file($document_path_cleaned, [
'Content-Type' => 'image/jpeg'
]);
}
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id) {
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id) {
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id) {
$document = PatientDocument::find($id);
if ($document->delete()):
flash("document has been deleted.")->error();
return redirect('/patient_documents/');
endif;
}
/*
* put the episode id in the session before creating the document
*/
public function set_episode_id($id) {
session()->put(['episode_id' => $id]);
}
}