mirror of
https://git.meshkee.com/cssd/cssd-back.git
synced 2026-10-10 19:41:07 +03:30
335 lines
15 KiB
PHP
335 lines
15 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use App\Http\Requests\StoreTestResultRequest;
|
|
use App\TestResult;
|
|
use App\InputValue;
|
|
use App\Input;
|
|
use App\Gallery;
|
|
use App\Uploadable;
|
|
use Intervention\Image\ImageManagerStatic as Image;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
class TestResultController extends Controller
|
|
{
|
|
public function index(Request $request)
|
|
{
|
|
$TestResult = TestResult::with('test_type', 'user', 'user.medical_center')
|
|
->when($request->has('verified'), function ($query) use ($request)
|
|
{
|
|
return $query->where('verified', $request->verified);
|
|
})->paginate(10);
|
|
return \MessageResponse::data($TestResult);
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
//
|
|
}
|
|
|
|
// public function CreateChildFormValue2($Input,$TestResultObj,$TestResultInfo)
|
|
// {
|
|
// if ($Input['type'] != 'Form') {
|
|
// $Value = $TestResultInfo[$Input->name];
|
|
// $InputValueObj = InputValue::create(['test_result_id' => $TestResultObj->id , 'input_id' => $Input->id , 'value' => $Value]);
|
|
// }
|
|
// else {
|
|
// foreach ($Input->children as $InputChild) {
|
|
// $this->CreateChildFormValue2($InputChild,$TestResultObj,$TestResultInfo[$Input->name]);
|
|
// }
|
|
// }
|
|
//
|
|
// }
|
|
|
|
public function store(StoreTestResultRequest $request)
|
|
{
|
|
|
|
try {
|
|
$return_value = \DB::transaction(function () use ($request) {
|
|
|
|
$TestResult = [
|
|
'user_id' => auth('api')->user()->id,
|
|
'autoclave_id' => $request->autoclave_id,
|
|
'test_type_id' => $request->test_type_id,
|
|
'result' => $request->result,
|
|
'verified' => 'NotVerified',
|
|
];
|
|
$TestResultObj = TestResult::create($TestResult);
|
|
$TestResultInfo = $request->all();
|
|
|
|
$Inputs = Input::with('options','children','children.children')->whereNull('parent_id')->where('test_type_id',$request->test_type_id)->get();
|
|
|
|
///////////////////////////////////////////////////////////////////////////Inputs
|
|
foreach ($Inputs as $Input) {
|
|
if ($Input['type'] != 'Form') {
|
|
$Value = $TestResultInfo[$Input->name];
|
|
|
|
if ($Input['type'] == 'Checkbox' || $Input['type'] == 'Select' ) {
|
|
|
|
foreach ($Value as $Option) {
|
|
$InputValueObj = InputValue::create(['test_result_id' => $TestResultObj->id, 'input_id' => $Input->id, 'option_id' => $Option]);
|
|
}
|
|
}
|
|
else {
|
|
$InputValueObj = InputValue::create(['test_result_id' => $TestResultObj->id, 'input_id' => $Input->id, 'value' => $Value]);
|
|
}
|
|
}
|
|
else {
|
|
foreach ($Input->children as $InputChild) {
|
|
$Value = $TestResultInfo[$InputChild->name];
|
|
|
|
if ($InputChild['type'] == 'Checkbox' || $InputChild['type'] == 'Select') {
|
|
foreach ($Value as $Option) {
|
|
$InputValueObj = InputValue::create(['test_result_id' => $TestResultObj->id, 'input_id' => $InputChild->id, 'option_id' => $Option]);
|
|
}
|
|
}
|
|
else {
|
|
$InputValueObj = InputValue::create(['test_result_id' => $TestResultObj->id, 'input_id' => $InputChild->id, 'value' => $Value]);
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
}
|
|
///////////////////////////////////////////////////////////////////////////Inputs
|
|
///////////////////////////////////////////////////////////////////////////Image
|
|
// if ($request->hasFile('img')) {
|
|
//
|
|
// $Image = $request->file('img');
|
|
if ($request->img != null) {
|
|
|
|
// $Image = $request->file('img');
|
|
$Image = $request->img;
|
|
$InterventionImage = Image::make($Image);
|
|
$Mime = explode('/', $InterventionImage->mime());
|
|
|
|
$ImageName = time().'.'.$Mime[1];
|
|
$Path = 'uploads'.'/TestResults'.'/'.$TestResultObj->id .'/'.$ImageName;
|
|
|
|
Storage::disk('public')->makeDirectory('TestResults'.'/'.$TestResultObj->id);
|
|
$InterventionImage->save(public_path($Path));
|
|
|
|
$GalleryObj = Gallery::create(['user_id' => auth('api')->user()->id, 'path' => $Path]);
|
|
$Uploadable = Uploadable::create(['uploadable_id' => $TestResultObj->id, 'uploadable_type' => 'App\TestResult', 'gallery_id' => $GalleryObj->id, 'image_role' => 'TitleImage']);
|
|
}
|
|
///////////////////////////////////////////////////////////////////////////Image
|
|
|
|
});
|
|
} catch (\Exception $e) {
|
|
return response()->json($e->getMessage(), 441);
|
|
}
|
|
return \MessageResponse::success(null,trans('messages.create'));
|
|
|
|
}
|
|
|
|
public function show($id)
|
|
{
|
|
$TestResult = TestResult::with('input_values', 'user')->select('result', 'verified', 'autoclave_id','user_id','id')->findOrFail($id);
|
|
$options = [];
|
|
foreach ($TestResult->input_values as $key => $value) {
|
|
$input = $value->input;
|
|
|
|
if ($input->parent_id != null && $input->parent['type'] == 'Form') {
|
|
//----------------------------------------------------------------------FormType
|
|
if ($input->type == 'Checkbox' || $input->type == 'Select') {
|
|
$input_options = InputValue::with('option')->where('test_result_id', $TestResult->id)->where('input_id', $input->id)->get();
|
|
$options = [];
|
|
foreach ($input_options as $input_option) {
|
|
$options[] = $input_option->option->title;
|
|
$input_values[$input->name] = $options;
|
|
}
|
|
// dd($input_values);
|
|
$children[$input->parent->name] = $input_values;
|
|
$TestResult['children'] = $children;
|
|
}
|
|
else {
|
|
|
|
$children[$input->parent->name][$input->name] = $value->value;
|
|
$TestResult['children'] = $children;
|
|
}
|
|
//-----------------------------------------------------------------------FormType
|
|
}
|
|
else {
|
|
//-----------------------------------------------------------------------Anothertype
|
|
if ($input->type == 'Checkbox' || $input->type == 'Select') {
|
|
$input_options = InputValue::with('option')->where('test_result_id', $TestResult->id)->where('input_id', $input->id)->get();
|
|
$options = [];
|
|
foreach ($input_options as $input_option) {
|
|
$options[] = $input_option->option->title;
|
|
}
|
|
|
|
$values[$input->name] = $options;
|
|
$TestResult['options'] = $values;
|
|
}
|
|
else {
|
|
$inputs[$input->name] = $value->value;
|
|
$TestResult['inputs'] = $inputs;
|
|
}
|
|
//-----------------------------------------------------------------------Anothertype
|
|
}
|
|
}
|
|
|
|
if (count($TestResult->TitleImage)) {
|
|
$TestResult['imagePath'] = $TestResult->TitleImage[0]->path;
|
|
}
|
|
// dd($TestResult);
|
|
return \MessageResponse::data($TestResult);
|
|
}
|
|
|
|
public function edit(TestResult $TestResult)
|
|
{
|
|
foreach ($TestResult->input_values as $key => $value) {
|
|
$input = $value->input;
|
|
if ($input->type == 'Checkbox' || $input->type == 'Select') {
|
|
$input_options = InputValue::where('test_result_id', $TestResult->id)->where('input_id', $input->id)->get()->pluck('option_id')->toArray();
|
|
$TestResult[$input->id] = $input_options;
|
|
}
|
|
else {
|
|
$TestResult[$input->id] = $value->value;
|
|
}
|
|
}
|
|
if (count($TestResult->TitleImage)) {
|
|
$TestResult['imagePath'] = $TestResult->TitleImage[0]->path;
|
|
}
|
|
return \MessageResponse::data($TestResult);
|
|
}
|
|
|
|
public static function storage_path($path)
|
|
{
|
|
if (starts_with($path, 'uploads/')) {
|
|
$path = substr($path, 8);
|
|
}
|
|
return $path;
|
|
}
|
|
|
|
public function update(StoreTestResultRequest $request, TestResult $TestResult)
|
|
{
|
|
// dd($request->all());
|
|
$TestResultData = [
|
|
'user_id' => auth('api')->user()->id,
|
|
'autoclave_id' => $request->autoclave_id,
|
|
'test_type_id' => $request->test_type_id,
|
|
'result' => $request->result,
|
|
'verified' => 'NotVerified',
|
|
];
|
|
$TestResult->update($TestResultData);
|
|
$TestResultInfo = $request->all();
|
|
|
|
$Inputs = Input::with('options', 'children', 'children.options')->whereNull('parent_id')->where('test_type_id',$request->test_type_id)->get();
|
|
|
|
foreach ($Inputs as $Input) {
|
|
if ($Input['type'] != 'Form') {
|
|
$Value = $TestResultInfo[$Input->name];
|
|
|
|
if ($Input['type'] == 'Checkbox' || $Input['type'] == 'Select' ) {
|
|
|
|
$InputOptions = InputValue::where('test_result_id', $TestResult->id)->where('input_id', $Input->id)->get()->each->delete();
|
|
|
|
// $option_array = $Input->options->pluck('id')->toArray();
|
|
foreach ($Value as $Option) {//--1
|
|
$InputValueObj = InputValue::create(['test_result_id' => $TestResult->id, 'input_id' => $Input->id, 'option_id' => $Option]);
|
|
}
|
|
// foreach ($Input->options as $Option) {//--2
|
|
// if(in_array($Option->id,$Value)){
|
|
// $InputValueObj = InputValue::create(['test_result_id' => $TestResult->id, 'input_id' => $Input->id, 'option_id' => $Option]);
|
|
// }
|
|
// }
|
|
}
|
|
else {
|
|
$InputValueObj = InputValue::where('test_result_id', $TestResult->id)->where('input_id', $Input['id'])->update(['value' => $Value]);
|
|
}
|
|
}
|
|
else {
|
|
foreach ($Input->children as $InputChild) {
|
|
|
|
$Value = $TestResultInfo[$InputChild->name];
|
|
|
|
if ($InputChild['type'] == 'Checkbox' || $InputChild['type'] == 'Select') {
|
|
$InputOptions = InputValue::where('test_result_id', $TestResult->id)->where('input_id', $InputChild->id)->get()->each->delete();
|
|
|
|
foreach ($Value as $Option) {
|
|
$InputValueObj = InputValue::create(['test_result_id' => $TestResult->id, 'input_id' => $InputChild->id, 'option_id' => $Option]);
|
|
}
|
|
// foreach ($InputChild->options as $Option) {//--2
|
|
// if(in_array($Option->id,$Value)){
|
|
// $InputValueObj = InputValue::create(['test_result_id' => $TestResult->id, 'input_id' => $InputChild->id, 'option_id' => $Option->id]);
|
|
// }
|
|
// }
|
|
}
|
|
else {
|
|
$InputValueObj = InputValue::where('test_result_id', $TestResult->id)->where('input_id', $InputChild['id'])->update(['value' => $Value]);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
/////////////////////////////////////////////////////////////////////Image
|
|
$image = '';
|
|
if (count($TestResult->TitleImage))
|
|
$image = $TestResult->TitleImage[0];
|
|
// dd($request->img);
|
|
// if ($request->hasFile('img')) {
|
|
if ($request->img != null) {
|
|
|
|
// $Image = $request->file('img');
|
|
$Image = $request->img;
|
|
$InterventionImage = Image::make($Image);
|
|
$Mime = explode('/', $InterventionImage->mime());
|
|
|
|
$ImageName = time().'.'.$Mime[1];
|
|
$Path = 'uploads'.'/TestResults'.'/'.$TestResult->id .'/'.$ImageName;
|
|
|
|
Storage::disk('public')->makeDirectory('TestResults'.'/'.$TestResult->id);
|
|
$InterventionImage->save(public_path($Path));
|
|
|
|
|
|
if ($image !== ''){
|
|
Storage::disk('public')->delete(static::storage_path($image->path));
|
|
Uploadable::where('gallery_id', $image->id)->where('uploadable_id', $TestResult->id)->where('uploadable_type', get_class($TestResult))->delete();
|
|
|
|
//--------------------------------------------------------
|
|
$GalleryObj = Gallery::where('id', $image->id)->update(['path' => $Path]);
|
|
$Uploadable = Uploadable::create(['uploadable_id' => $TestResult->id, 'uploadable_type' => get_class($TestResult), 'gallery_id' => $image->id, 'image_role' => 'TitleImage']);
|
|
//--------------------------------------------------------
|
|
|
|
}
|
|
else {
|
|
//--------------------------------------------------------
|
|
$GalleryObj = Gallery::create(['user_id' => auth('api')->user()->id, 'path' => $Path]);
|
|
$Uploadable = Uploadable::create(['uploadable_id' => $TestResult->id, 'uploadable_type' => 'App\TestResult', 'gallery_id' => $GalleryObj->id, 'image_role' => 'TitleImage']);
|
|
//--------------------------------------------------------
|
|
}
|
|
}
|
|
//////////////////////////////////////////////////////////////////Image
|
|
|
|
return \MessageResponse::success(null,trans('messages.update'));
|
|
}
|
|
|
|
public function destroy(TestResult $TestResult)
|
|
{
|
|
if (!auth('api')->user()->can('delete', $TestResult)) {
|
|
return \MessageResponse::error(trans('messages.access_denied'));
|
|
}
|
|
$TestResult->delete();
|
|
return \MessageResponse::success(null, trans('messages.success'));
|
|
}
|
|
|
|
public function verifyTestResult(TestResult $TestResult)
|
|
{
|
|
if (!auth('api')->user()->can('verifyTestResult', $TestResult)) {
|
|
return \MessageResponse::error(trans('messages.access_denied'));
|
|
}
|
|
if ($TestResult->verified == 'Verified') {
|
|
$TestResult->verified = 'NotVerified';
|
|
} else {
|
|
$TestResult->verified = 'Verified';
|
|
}
|
|
$TestResult->save();
|
|
$TestResult = $TestResult->load('user.medical_center');
|
|
return \MessageResponse::success(trans('messages.success'), null, $TestResult);
|
|
}
|
|
|
|
}
|