2024-12-25 03:04:59 +00:00
< ? php
2025-01-02 09:35:59 +00:00
namespace frontend\modules\frozen\controllers ;
2024-12-25 03:04:59 +00:00
use Yii ;
use yii\db\Expression ;
use yii\base\Exception ;
use yii\web\Controller ;
use yii\filters\VerbFilter ;
use common\models\IdCaseForm ;
use yii\web\NotFoundHttpException ;
use common\models\SurgicalProcessAfter ;
use common\models\SurgicalProcessBefore ;
use frontend\modules\surgical\models\SurgicalProcessAfterSearch ;
use frontend\modules\histo\models\SurgicalTissueProcessingSearch ;
/**
* SurgicalProcessAfterController implements the CRUD actions for SurgicalProcessAfter model .
*/
class SurgicalProcessAfterController extends Controller
{
/**
* { @ inheritdoc }
*/
public function behaviors ()
{
return [
'verbs' => [
'class' => VerbFilter :: className (),
'actions' => [
'delete' => [ 'POST' ],
],
],
];
}
/**
* Lists all SurgicalProcessAfter models .
* @ return mixed
*/
public function actionIndex ()
{
$searchModel = new SurgicalProcessAfterSearch ();
$dataProvider = $searchModel -> search ( Yii :: $app -> request -> queryParams );
return $this -> render ( 'index' , [
'searchModel' => $searchModel ,
'dataProvider' => $dataProvider ,
]);
}
/**
* Displays a single SurgicalProcessAfter model .
* @ param integer $id
* @ return mixed
* @ throws NotFoundHttpException if the model cannot be found
*/
public function actionView ()
{
$searchModel = new SurgicalTissueProcessingSearch ();
$dataProvider = $searchModel -> search ( Yii :: $app -> request -> queryParams );
return $this -> render ( 'view' , [
'searchModel' => $searchModel ,
'dataProvider' => $dataProvider ,
]);
/* return $this -> render ( 'view' , [
'model' => $this -> findModel ( $id ),
]); */
}
/**
* Creates a new SurgicalProcessAfter model .
* If creation is successful , the browser will be redirected to the 'view' page .
* @ return mixed
*/
public function actionCreate ()
{
$model = new SurgicalProcessAfter ();
if ( $model -> load ( Yii :: $app -> request -> post ()) && $model -> save ()) {
return $this -> redirect ([ 'view' , 'id' => $model -> id ]);
}
return $this -> render ( 'create' , [
'model' => $model ,
]);
}
/**
* Updates an existing SurgicalProcessAfter model .
* If update is successful , the browser will be redirected to the 'view' page .
* @ param integer $id
* @ return mixed
* @ throws NotFoundHttpException if the model cannot be found
*/
public function actionUpdate ( $id )
{
$model = $this -> findModel ( $id );
if ( $model -> load ( Yii :: $app -> request -> post ()) && $model -> save ()) {
return $this -> redirect ([ 'view' , 'id' => $model -> id ]);
}
return $this -> render ( 'update' , [
'model' => $model ,
]);
}
/*
* H & E Embed Case
*/
public function actionBlockProcess ()
{
$searchCase = new IdCaseForm ();
$session = Yii :: $app -> session ;
if ( ! $session [ 'aftercase' ]) {
$session [ 'aftercase' ] = [];
}
if ( $searchCase -> load ( Yii :: $app -> request -> post ())) {
$case = Yii :: $app -> pathology -> getCase ( $searchCase -> id_case );
if ( ! $case ) { //ไม่พบ Case ที่ค้นหา
Yii :: $app -> session -> setFlash ( 'error' , 'ไม่พบ Case ที่ค้นหา' );
return $this -> redirect ([ 'block-process' ]);
} else {
$check_before = SurgicalProcessAfter :: findOne ([ 'id_case' => $case -> id_case ]);
$surgical_block = SurgicalProcessAfter :: find () -> where ([ 'id_case' => $case -> id_case ])
-> andWhere ( 'after_id > 1' )
-> andWhere ( 'after_id <> ' . Yii :: $app -> user -> getId ())
-> one ();
if ( $surgical_block ) {
Yii :: $app -> session -> setFlash ( 'error' , 'มีการ Process Case นี้แล้วโดย ' . isset ( $surgical_block -> processAfter ) ? $surgical_block -> processAfter -> realname : '' );
return $this -> redirect ([ 'block-process' ]);
}
$case_session = Yii :: $app -> session [ 'aftercase' ];
/* var_dump ( $case_session );
die (); */
if ( ! in_array ( $case -> id_case , $case_session )) {
$case_session [] = $case -> id_case ;
} else {
Yii :: $app -> session -> setFlash ( 'warning' , 'เลือก Case นี้แล้ว' );
return $this -> redirect ([ 'block-process' ]);
}
$before = SurgicalProcessBefore :: findOne ([ 'id_case' => $case -> id_case ]);
if ( empty ( $before -> before_id )) {
Yii :: $app -> session -> setFlash ( 'error' , 'Case นี้ยังไม่ได้ Process Tissue Before' );
return $this -> redirect ([ 'block-process' ]);
}
$session [ 'aftercase' ] = $case_session ;
$searchCase -> id_case = null ;
}
}
//var_dump(Yii::$app->request->post());
//die();
if ( Yii :: $app -> request -> post ( 'after' )) {
$transaction = Yii :: $app -> db -> beginTransaction ();
try {
$case_all = Yii :: $app -> session [ 'aftercase' ];
$time_now = date ( 'Y-m-d H:i:s' );
foreach ( Yii :: $app -> request -> post ( 'after' ) as $c ) {
$process = SurgicalProcessAfter :: findOne ([ 'id_case' => $c [ 'id_case' ]]);
if ( ! $process ) {
$process = new SurgicalProcessAfter ();
$process -> id_case = $c [ 'id_case' ];
$process -> after_at = $time_now ;
$process -> after_id = Yii :: $app -> user -> getId ();
}
$process -> after_amount = $c [ 'after_amount' ];
$process -> after_remark = $c [ 'after_remark' ];
//echo $c['id_case'];
//echo '<pre>';
//var_dump($c);
//echo '</pre>';
//$embed->validate();
$process -> save ();
}
//die();
$transaction -> commit ();
unset ( Yii :: $app -> session [ 'aftercase' ]);
Yii :: $app -> session -> setFlash ( 'success' , 'บันทึกข้อมูล เรียบร้อยแล้ว' );
return $this -> redirect ([ 'block-process' ]);
} catch ( Exception $e ) {
$transaction -> rollBack ();
Yii :: $app -> session -> setFlash ( 'error' , 'เกิดข้อผิดพลาด' );
return $this -> redirect ([ 'block-process' ]);
}
}
//DataProvider
$searchModel = new SurgicalProcessAfterSearch ();
//$searchModel->mode = 'after';
$dataProvider = $searchModel -> search ( Yii :: $app -> request -> queryParams );
$dataProvider -> query -> andFilterWhere ([ 'is not' , 'after_id' , new Expression ( 'NULL' )]);
return $this -> render ( 'block-process' , [
'searchCase' => $searchCase ,
'cases' => Yii :: $app -> session [ 'aftercase' ],
'searchModel' => $searchModel ,
'dataProvider' => $dataProvider ,
//'check_before' => $check_before,
]);
}
/**
* Deletes an existing SurgicalBlockProcess model .
* If deletion is successful , the browser will be redirected to the 'index' page .
* @ param integer $id
* @ return mixed
* @ throws NotFoundHttpException if the model cannot be found
*/
public function actionDelete ( $id )
{
$this -> findModel ( $id ) -> delete ();
return $this -> redirect ([ 'index' ]);
}
/**
* Finds the SurgicalBlockProcess model based on its primary key value .
* If the model is not found , a 404 HTTP exception will be thrown .
* @ param integer $id
* @ return SurgicalProcessAfter the loaded model
* @ throws NotFoundHttpException if the model cannot be found
*/
protected function findModel ( $id )
{
if (( $model = SurgicalProcessAfter :: findOne ( $id )) !== null ) {
return $model ;
}
throw new NotFoundHttpException ( 'The requested page does not exist.' );
}
}