/** * Creates a new Timesheet model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Timesheet(); if ($model->load(Yii::$app->request->post())) { $model->created_at = time(); $model->updated_at = time(); $model->user_id = Yii::$app->user->identity->id; if ($model->save()) { return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('create', ['model' => $model]); } } else { return $this->render('create', ['model' => $model]); } }
/** * Creates a new Work model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $id = Yii::$app->user->identity->id; $model = new Timesheet(['user_id' => $id, 'date' => date('Y-m-d')]); $modelDetails = [new Work(), new Work(), new Work()]; $formDetails = Yii::$app->request->post('Work', []); foreach ($formDetails as $i => $formDetail) { if (isset($modelDetails[$i])) { $modelDetail = $modelDetails[$i]; $modelDetail->setAttributes($formDetail); } else { $modelDetail = new Work(); $modelDetail->setAttributes($formDetail); $modelDetails[] = $modelDetail; } } //handling if the addRow button has been pressed if (Yii::$app->request->post('addRow') == 'true') { $model->load(Yii::$app->request->post()); $modelDetails[] = new Work(); return $this->render('createTimesheet', ['model' => $model, 'modelDetails' => $modelDetails]); } if ($model->load(Yii::$app->request->post())) { //if (Model::validateMultiple($modelDetails) && $model->validate()) { if ($model->validate()) { $newmodel = Timesheet::findTimesheet($id, $model->date); if ($newmodel == null) { if (strtotime($model->date) > strtotime(date('Y-m-d'))) { Yii::$app->session->setFlash("WrongDate"); $model->date = date('Y-m-d'); return $this->render('createTimesheet', ['model' => $model, 'modelDetails' => $modelDetails]); } else { Yii::$app->session->setFlash("CreateMode"); $model->save(); } } else { if ($newmodel->point !== null) { Yii::$app->session->setFlash("NoModify"); return $this->render('createTimesheet', ['model' => $newmodel, 'modelDetails' => $modelDetails]); } $model = $newmodel; } foreach ($modelDetails as $modelDetail) { $modelDetail->timesheet_id = $model->id; if ($modelDetail->validate()) { $modelDetail->save(); } } return $this->redirect(['index', 'id' => $model->id]); } } return $this->render('createTimesheet', ['model' => $model, 'modelDetails' => $modelDetails]); }