/**
  * Updates an existing Monitoring1 model.
  * If update is successful, the browser will be redirected to the 'view' page.
  * @param integer $id
  * @return mixed
  */
 public function actionUpdate($id)
 {
     $model = $this->findModel($id);
     $modelsStr = $model->mon1str;
     if (!Yii::$app->user->can('admin')) {
         if ($model->user_id != Yii::$app->user->id) {
             $this->redirect(['index']);
         }
     }
     if (!Yii::$app->user->can('admin')) {
         if ($model->status == 1) {
             $this->redirect(['index']);
         }
     }
     $months = Month::find()->all();
     $years = Year::find()->all();
     //        $model->user_id = Yii::$app->user->id;
     if ($model->load(Yii::$app->request->post())) {
         $model->date = date('Y-m-d', strtotime($model->date));
         if (!Monitoring1::find()->where(['date' => $model->date, 'user_id' => Yii::$app->user->id])->one()) {
             if ($model->save()) {
                 $oldIDs = ArrayHelper::map($modelsStr, 'id', 'id');
                 $modelsStr = Model::createMultiple(Mon1str::classname(), $modelsStr);
                 Model::loadMultiple($modelsStr, Yii::$app->request->post());
                 $deletedIDs = array_diff($oldIDs, array_filter(ArrayHelper::map($modelsStr, 'id', 'id')));
                 // ajax validation
                 if (Yii::$app->request->isAjax) {
                     Yii::$app->response->format = Response::FORMAT_JSON;
                     return ArrayHelper::merge(ActiveForm::validateMultiple($modelsStr), ActiveForm::validate($model));
                 }
                 // validate all models
                 $valid = $model->validate();
                 $valid = Model::validateMultiple($modelsStr) && $valid;
                 if ($valid) {
                     $transaction = \Yii::$app->db->beginTransaction();
                     try {
                         if ($flag = $model->save(false)) {
                             if (!empty($deletedIDs)) {
                                 Mon1str::deleteAll(['id' => $deletedIDs]);
                             }
                             foreach ($modelsStr as $modelStr) {
                                 $modelStr->monitoring1_id = $model->id;
                                 if (!($flag = $modelStr->save(false))) {
                                     $transaction->rollBack();
                                     break;
                                 }
                             }
                         }
                         if ($flag) {
                             $transaction->commit();
                             return $this->redirect(['view', 'id' => $model->id]);
                         }
                     } catch (Exception $e) {
                         $transaction->rollBack();
                     }
                 }
             }
         }
     }
     return $this->render('update', ['model' => $model, 'months' => $months, 'years' => $years, 'modelsStr' => empty($modelsStr) ? [new Mon1str()] : $modelsStr]);
 }