/** * Creates a new Attachments model. * If creation is successful, the browser will be redirected to the 'view' page. * @return string|\yii\web\Response * @throws ForbiddenHttpException */ public function actionCreate() { // Check RBAC Permission if ($this->userCanCreate()) { $model = new Attachments(); if ($model->load(Yii::$app->request->post())) { // Upload Attachments if is not Null $attachPath = Yii::getAlias(Yii::$app->controller->module->attachPath); $attachName = $model->title; $attachType = "original"; $attachField = "filename"; // Create UploadFile Instance $attach = $model->uploadFile($attachName, $attachType, $attachPath, $attachField); $model->filename = $attach->name; if ($model->save()) { // upload only if valid uploaded file instance found if ($attach !== false) { // Set Success Message Yii::$app->session->setFlash('success', Yii::t('articles', 'Attachment has been created!')); } return $this->redirect(['index']); } else { // Set Error Message Yii::$app->session->setFlash('error', Yii::t('articles', 'Attachment could not be saved!')); return $this->render('create', ['model' => $model]); } } else { return $this->render('create', ['model' => $model]); } } else { throw new ForbiddenHttpException(); } }
/** * Creates a new Attachments model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Attachments(); if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['index']); } else { return $this->render('create', ['model' => $model]); } }