/** * Creates a new Request model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $request = new Request(['scenario' => 'Create']); $areasRequest = new AreasRequest(); $categoryRequest = new CategoryRequest(); //$usersRequest = new UsersRequest(); if ($request->load(Yii::$app->request->post())) { $request->requestFile = UploadedFile::getInstances($request, 'requestFile'); $valid = true; //$valid = $valid && $request->validate(); if ($valid) { if ($request->save()) { if ($valid && !empty($request->requestFile)) { $request->upload(); } $areasRequest->request_id = $request->id; $areasRequest->area_id = $request->area_id; $categoryRequest->request_id = $request->id; $categoryRequest->category_id = $request->category_id; if (!empty($request->category_id)) { $categoryRequest->save(); } if ($areasRequest->save()) { if ($valid) { return $this->redirect(['view', 'id' => $request->id]); } else { return $this->render('create', ['request' => $request]); } } else { return $this->render('create', ['request' => $request]); } } else { return $this->render('create', ['request' => $request]); } } else { return $this->render('create', ['request' => $request]); } } else { return $this->render('create', ['request' => $request]); } }
public function assignCategories() { foreach ($this->listCategories as $category) { //$this->fileNameAttached = uniqid() . '.' . $file->extension; //$file->saveAs('files/'.$this->fileNameAttached); $category_request = new CategoryRequest(); $category_request->request_id = $this->id; $category_request->category_id = $category; $category_request->save(); } return true; }
/** * Creates a new request model. * For ajax request will return json object * and for non-ajax request if creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $request = Yii::$app->request; $areasRequest = new AreasRequest(); $categoryRequest = new CategoryRequest(); $model = new Request(); if ($request->isAjax) { /* * Process for ajax request */ Yii::$app->response->format = Response::FORMAT_JSON; if ($request->isGet) { $model = $this->completeModel($model); return ['title' => "Create new request", 'content' => $this->renderAjax('create', ['model' => $model]), 'footer' => Html::button('Close', ['class' => 'btn btn-default pull-left', 'data-dismiss' => "modal"]) . Html::button('Save', ['class' => 'btn btn-primary', 'type' => "submit"])]; } else { if ($model->load($request->post()) && $model->save()) { $areasRequest->request_id = $model->id; $areasRequest->area_id = $model->area_id; $categoryRequest->request_id = $model->id; $categoryRequest->category_id = $model->category_id; if (!empty($model->category_id) || $model->category_id != 0) { $categoryRequest->save(); } $areasRequest->save(); return ['forceReload' => '#crud-datatable-pjax', 'title' => "Create new request", 'content' => '<span class="text-success">Create request success</span>', 'footer' => Html::button('Close', ['class' => 'btn btn-default pull-left', 'data-dismiss' => "modal"]) . Html::a('Create More', ['create'], ['class' => 'btn btn-primary', 'role' => 'modal-remote'])]; } else { $model = $this->completeModel($model); return ['title' => "Create new request", 'content' => $this->renderAjax('create', ['model' => $model]), 'footer' => Html::button('Close', ['class' => 'btn btn-default pull-left', 'data-dismiss' => "modal"]) . Html::button('Save', ['class' => 'btn btn-primary', 'type' => "submit"])]; } } } else { /* * Process for non-ajax request */ if ($model->load($request->post()) && $model->save()) { $areasRequest->request_id = $model->id; $areasRequest->area_id = $model->area_id; $categoryRequest->request_id = $model->id; $categoryRequest->category_id = $model->category_id; if (!empty($model->category_id) || $model->category_id != 0) { $categoryRequest->save(); } $areasRequest->save(); if (Yii::$app->user->isGuest) { Yii::$app->session->setFlash('requestFormSubmitted'); return $this->refresh(); } else { return $this->redirect(['view', 'id' => $model->id]); } } else { $model = $this->completeModel($model); return $this->render('create', ['model' => $model]); } } }