コード例 #1
0
ファイル: BooksController.php プロジェクト: krausweb/yiibooks
 /**
  * Creates a new Books model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Books();
     if ($model->load(Yii::$app->request->post())) {
         if (!$model->validate()) {
             yii::$app->session->setFlash('create_bad', yii::t('app', 'New book not has been created'));
             return $this->redirect(['index']);
         }
         // upload new preview
         $model->upload_preview = UploadedFile::getInstance($model, 'upload_preview');
         if ($model->upload_preview) {
             if (($model->upload_preview->type == 'image/jpeg' or $model->upload_preview->type == 'image/png' or $model->upload_preview->type == 'image/gif' or $model->upload_preview->type == 'image/svg+xml') and $model->upload_preview->size < 999999) {
                 $preview_name_arr = preg_replace("~[ :-]~", "_", explode(".", $model->upload_preview->name));
                 $preview_name = $preview_name_arr[0] . '__' . time() . "." . $model->upload_preview->extension;
                 $model->upload_preview->saveAs('img/small/' . $preview_name, false);
                 $model->upload_preview->saveAs('img/original/' . $preview_name);
                 $model->preview = $preview_name;
             } else {
                 yii::$app->session->setFlash('upload_preview_bad', yii::t('app', 'Preview not uploaded: error type or big size'));
             }
         }
         // возвращаю к формату согласно БД
         $model->date_create = time();
         $model->date_update = time();
         $model->date = Yii::$app->formatter->asTimestamp($model->date);
         $model->save();
         yii::$app->session->setFlash('create_ok', yii::t('app', 'New book - <i>{name}</i> - has been successfully created', array('name' => $model->name)));
         return $this->redirect(Url::previous());
     } else {
         return $this->renderAjax('create', ['model' => $model]);
     }
 }
コード例 #2
0
ファイル: BooksController.php プロジェクト: fedman/books
 /**
  * Creates a new Books model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Books();
     if ($model->load(Yii::$app->request->post())) {
         $upload_file = $model->uploadFile();
         var_dump($model->validate());
         if ($model->validate()) {
             if ($model->save()) {
                 if ($upload_file !== false) {
                     $path = $model->getUploadedFile();
                     $upload_file->saveAs($path);
                 }
                 return $this->redirect(['index']);
             }
         }
     }
     return $this->render('create', ['model' => $model]);
 }
コード例 #3
0
 /**
  * Creates a new Books model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Books();
     if ($model->load(Yii::$app->request->post()) && $model->validate()) {
         if ($this->saveUpload($model)) {
             return $this->redirect(['view', 'id' => $model->id]);
         }
     }
     return $this->render('create', ['model' => $model]);
 }
コード例 #4
0
 /**
  * Creates a new Books model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Books();
     if ($model->load(Yii::$app->request->post()) && $model->validate()) {
         $file = UploadedFile::getInstance($model, 'image');
         $model->image = $file;
         // Если изображение выбрано, загрузить его
         if (!empty($model->image)) {
             $filename = uniqid();
             $model->image = $file;
             $model->preview = $filename . "." . $model->image->getExtension();
             if ($model->save()) {
                 $file->saveAs($model->getUplDir() . $filename . "." . $model->image->getExtension());
                 return $this->redirect(["index"]);
             }
         } else {
             if ($model->save()) {
                 return $this->redirect(["index"]);
             }
         }
     }
     return $this->render('create', ['model' => $model]);
 }