/** * Creates a new model. * If creation is successful, the browser will be redirected to the 'view' page. */ public function actionCreate() { $model = new Mittaus(); // Uncomment the following line if AJAX validation is needed // $this->performAjaxValidation($model); if (isset($_POST['Mittaus'])) { $model->attributes = $_POST['Mittaus']; if ($model->save()) { $this->redirect(array('view', 'id' => $model->id)); } } $this->render('create', array('model' => $model)); }
public function actionMittaus_ajax() { $pvm = date('Y-m-d'); if (isset($_POST['pvm2'])) { $pvm = date('Y-m-d', strtotime($_POST['pvm2'])); } elseif (isset($_POST['Mittaus'])) { $pvm = date('Y-m-d', strtotime($_POST['Mittaus']['pvm'])); } $criteria = new CDbCriteria(); $criteria->condition = " user_id='" . Yii::app()->user->id . "' AND pvm='" . $pvm . "' "; $m = Mittaus::model()->find($criteria); if (isset($m->id)) { $model = $m; } else { $model = new Mittaus(); } // Uncomment the following line if AJAX validation is needed // $this->performAjaxValidation($model); if (isset($_POST['Mittaus'])) { foreach ($_POST['Mittaus'] as $k => $d) { $_POST['Mittaus'][$k] = str_replace(',', '.', $d); } $model->attributes = $_POST['Mittaus']; if ($model->save()) { $criteria = new CDbCriteria(); $criteria->order = " pvm DESC "; $criteria->condition = " user_id='" . Yii::app()->user->id . "' "; $mi = Mittaus::model()->find($criteria); if (isset($mi->id) and $mi->aamupaino > 0) { $pr = Profile::model()->updatebypk(Yii::app()->user->id, array('paino' => $mi->aamupaino)); } elseif (!isset($mi->id) and $_POST['Mittaus']['aamupaino'] > 0) { $pr = Profile::model()->updatebypk(Yii::app()->user->id, array('paino' => $_POST['Mittaus']['aamupaino'])); } } $criteria = new CDbCriteria(); $criteria->order = " DATE(pvm) ASC "; $criteria->condition = " user_id='" . Yii::app()->user->id . "' AND aamupaino!='' "; $e = Mittaus::model()->find($criteria); $tulos = ''; if (isset($e->aamupaino) and $e->aamupaino > 0) { $tulos .= $e->aamupaino; $pr = Profile::model()->findbypk(Yii::app()->user->id); $j = pow($pr->pituus / 100, 2.5); $m = (double) $model->aamupaino - (double) $e->aamupaino; $prosenti = round((double) $model->aamupaino / $e->aamupaino * 100); $prosenti1 = round((double) $prosenti - 100); $bmi = round(1.3 * (double) $model->aamupaino / $j, 1); $bmiTekstti = ''; if ($bmi < 14.9) { $bmiTekstti = 'Sairaalloinen alipaino'; } elseif ($bmi > 15 and $bmi < 18) { $bmiTekstti = 'Merkittävä alipaino'; } elseif ($bmi > 18 and $bmi < 19) { $bmiTekstti = 'Lievä alipaino'; } elseif ($bmi > 19 and $bmi < 25) { $bmiTekstti = 'Normaali paino'; } elseif ($bmi > 25 and $bmi < 30) { $bmiTekstti = 'Lievä ylipaino'; } elseif ($bmi > 30 and $bmi < 35) { $bmiTekstti = 'Merkittävä ylipaino'; } elseif ($bmi > 35 and $bmi < 40) { $bmiTekstti = 'Vaikea ylipaino'; } elseif ($bmi > 40) { $bmiTekstti = 'Sairaalloinen ylipaino'; } $tulos = 'Paino muutos lähtöpainosta ' . $m . ' kg (' . $prosenti1 . '%). BMI ' . $bmi . ', ' . $bmiTekstti . '.'; } echo $tulos; exit; } $this->renderPartial('mittaus_ajax', array('model' => $model, 'pvm' => $pvm)); }