コード例 #1
0
 /**
  * Creates a new Books model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     if (Yii::$app->user->isGuest) {
         Yii::$app->session->setFlash('alert', ['options' => ['class' => 'alert-danger'], 'body' => Yii::t('app', 'You are not authorized for this action.')]);
         return $this->redirect(Url::toRoute('/site/login'));
     }
     $model = new Books();
     if ($model->load(Yii::$app->request->post())) {
         $image = UploadedFile::getInstance($model, 'preview');
         if (!empty($image)) {
             $dir = Yii::getAlias('@app/web/img');
             $uploaded = $image->saveAs($dir . '/' . $image->name);
             $model->preview = $image->name;
         }
         $model->date_create = self::getCurrentDateTime();
         $model->date_update = self::getCurrentDateTime();
         if ($model->validate() && $model->save()) {
             return $this->redirect(['view', 'id' => $model->id]);
         } else {
             Yii::$app->session->setFlash('alert', ['options' => ['class' => 'alert-danger'], 'body' => Yii::t('app', 'Error. Incorrect data')]);
             return $this->redirect(['index']);
         }
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
コード例 #2
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();
     $session = Yii::$app->session;
     if (strripos($_SERVER['HTTP_REFERER'], 'BooksSearch') || strripos($_SERVER['HTTP_REFERER'], 'sort')) {
         $session->set('redirect_param', $_SERVER['HTTP_REFERER']);
     }
     if ($model->load(Yii::$app->request->post())) {
         $file_image = UploadedFile::getInstance($model, 'image');
         if ($file_image && $file_image->tempName) {
             $model->image = $file_image;
             if ($model->validate(['image'])) {
                 $base_patch = 'uploads/';
                 $dir = Yii::getAlias('../web/' . $base_patch);
                 $fileName = $model->image->baseName . '.' . $model->image->extension;
                 $model->image->saveAs($dir . $fileName);
                 $model->image = $fileName;
                 $model->image = $dir . $fileName;
                 $model->preview = $base_patch . $fileName;
             }
         }
         if ($model->save()) {
             return isset($session['redirect_param']) ? $this->redirect($session->get('redirect_param')) : $this->redirect(['index']);
             //return $this->redirect(['view', 'id' => $model->id]);
         }
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
コード例 #3
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]);
     }
 }
コード例 #4
0
ファイル: BooksController.php プロジェクト: Araused/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()) && $model->save()) {
         return $this->redirect(['view', 'id' => $model->id]);
     }
     return $this->render('create', ['model' => $model]);
 }
コード例 #5
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->save()) {
         return $this->redirect(['index']);
     } else {
         return $this->renderAjax('create', ['model' => $model]);
     }
 }
コード例 #6
0
ファイル: BooksController.php プロジェクト: alaz1987/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()) && $model->save()) {
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         return $this->render('create', ['model' => $model, 'authors' => $this->getAuthorsArray(Authors::find()->all())]);
     }
 }
コード例 #7
0
ファイル: BooksController.php プロジェクト: akomyagin/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())) {
         $model->uploadImage();
         return $model->save() ? $this->redirect(['index']) : var_dump($model);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
コード例 #8
0
ファイル: BooksController.php プロジェクト: Pontorez/kgr
 /**
  * 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();
     $model->date_create = $model->date_update = date('Y-m-d H:i:s');
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         return $this->redirect(isset($_POST['referer']) ? $_POST['referer'] : '/');
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
コード例 #9
0
ファイル: BooksController.php プロジェクト: sergioIt/basicdev
 /**
  * Creates a new Books model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $book = new Books();
     $author = new Authors();
     if ($book->load(Yii::$app->request->post()) && $book->save()) {
         return $this->redirect(['view', 'id' => $book->id]);
     } else {
         return $this->render('create', ['model' => $book, 'author' => $author]);
     }
 }
コード例 #10
0
 /**
  * Creates a new Books model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate($closeOnLoad = false)
 {
     $model = new Books();
     $data = $this->filterData(Yii::$app->request->post());
     if ($model->load($data) && $model->save()) {
         return $this->redirect(['view', 'id' => $model->id, 'closeOnLoad' => $closeOnLoad]);
     } else {
         return $this->render('create', ['model' => $model, 'authors' => $this->getAuthorsForDropDownList()]);
     }
 }
コード例 #11
0
ファイル: BooksController.php プロジェクト: IuriiP/test160107
 /**
  * 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();
     $model->scenario = 'create';
     if (Yii::$app->request->isPost && $model->load(Yii::$app->request->post())) {
         if ($this->loadPreview($model) && $model->save()) {
             return $this->redirect(['view', 'id' => $model->id]);
         }
     }
     return $this->render('create', ['model' => $model]);
 }
コード例 #12
0
ファイル: BooksController.php プロジェクト: verve000/verve000
 public function actionCreate()
 {
     $model = new Books();
     if ($model->load(Yii::$app->request->post())) {
         $model->uploadImage();
         $model->date_create = date('Y-m-d');
         $model->date_update = date('Y-m-d');
         $model->save();
         return $this->redirect(['index']);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
コード例 #13
0
ファイル: BooksController.php プロジェクト: logs12/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()) && $model->upload()) {
         //var_dump(Yii::$app->request->post());die();
         if ($model->save()) {
             return $this->redirect(['index']);
         } else {
             return $this->render('create', ['authors' => Authors::find()->all(), 'model' => $model]);
         }
     } else {
         return $this->render('create', ['authors' => Authors::find()->all(), 'model' => $model]);
     }
 }
コード例 #14
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();
     $model_b_a_rel = new BookAuthorRel();
     $authorsArr = Authors::authorArr();
     if ($model->load(Yii::$app->request->post()) && $model_b_a_rel->load(Yii::$app->request->post())) {
         $model->save();
         $model_b_a_rel->b_id = $model->id;
         $model_b_a_rel->saveMultiple();
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         return $this->render('create', ['model' => $model, 'model_b_a_rel' => $model_b_a_rel, 'authorsArr' => $authorsArr]);
     }
 }
コード例 #15
0
ファイル: BooksController.php プロジェクト: snedi/imot
 /**
  * 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();
     $modelAuthorsBooks = new AuthorsBooks();
     $data = Yii::$app->request->post();
     if ($model->load($data) && $model->save()) {
         $modelAuthorsBooks->a_id = $data['AuthorsBooks']['a_id'][0];
         $modelAuthorsBooks->b_id = $model->id;
         if ($modelAuthorsBooks->save()) {
             return $this->redirect(['view', 'id' => $model->id]);
         }
     } else {
         return $this->render('create', ['model' => $model, 'modelAuthorsBooks' => $modelAuthorsBooks]);
     }
 }
コード例 #16
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())) {
         $image = $model->uploadImage();
         if ($image !== false) {
             $path = $model->getImageFile();
             $image->saveAs($path);
         }
         //                if ($path = $model->upload()) {
         //                    $model->preview_image = $path;
         //                }
         $model->save();
         return $this->redirect(['index']);
     } else {
         return $this->render('create', ['model' => $model, 'authors' => Authors::getAuthorsList()]);
     }
 }
コード例 #17
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]);
 }
コード例 #18
0
ファイル: BooksController.php プロジェクト: evgenylev/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 (Yii::$app->request->isPost && $model->load(Yii::$app->request->post())) {
         // ???does it need $model->validate() somewhere here???
         $file = UploadedFile::getInstance($model, 'preview');
         if ($file instanceof UploadedFile && $file->error == UPLOAD_ERR_OK) {
             $model->preview = $file->baseName . '.' . $file->extension;
             $file->saveAs('images/books/' . $model->preview);
         } else {
             unset($model->preview);
         }
         $model->save();
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
コード例 #19
0
ファイル: BooksController.php プロジェクト: ustkirill/test2
 /**
  * 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();
     $dir = Yii::getAlias('@uploads');
     if ($model->load(Yii::$app->request->post())) {
         $model->date_create = date('Y-m-d');
         $model->date_update = date('Y-m-d');
         $model->preview = UploadedFile::getInstance($model, 'preview');
         if ($model->preview && $model->validate()) {
             $path = $dir . '/' . $model->generateUniqueFilename() . '.' . $model->preview->extension;
             $model->preview->saveAs($path);
             $model->preview = $path;
         }
         if ($model->save()) {
             return $this->redirect(['view', 'id' => $model->id]);
         }
     }
     return $this->render('create', ['model' => $model]);
 }
コード例 #20
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())) {
         if ($model->imageFile = UploadedFile::getInstance($model, 'imageFile')) {
             $model->upload();
         }
         $model->date_create = date('Y-m-d');
         $model->date_update = date('Y-m-d H:i:s');
         if ($model->save(false)) {
             if (Url::previous()) {
                 return $this->redirect(Url::previous());
             }
             return $this->redirect(Url::toRoute('index'));
         }
     } else {
         Url::remember($_SERVER['HTTP_REFERER']);
         return $this->render('create', ['model' => $model]);
     }
 }
コード例 #21
0
ファイル: BooksController.php プロジェクト: pmesher/taskmy
 /**
  * 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();
     $uploadImages = new UploadImages();
     if ($model->load(Yii::$app->request->post())) {
         $uploadImages->imageFile = UploadedFile::getInstance($model, 'imageFile');
         if ($filename = $uploadImages->upload()) {
             $model->preview = $filename;
         } else {
             $model->addErrors($uploadImages->getErrors());
         }
         if ($model->validate(null, false) && $model->save(false)) {
             return $this->redirect(['index']);
         } else {
             return $this->render('create', ['model' => $model]);
         }
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
コード例 #22
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]);
 }
コード例 #23
0
ファイル: BooksController.php プロジェクト: Kakiho/testtask
 /**
  * Creates a new Books model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $authors_model = Authors::find()->all();
     $authors = [];
     foreach ($authors_model as $author) {
         $authors[$author->author_id] = $author->firstname . " " . $author->lastname;
     }
     $model = new Books();
     $post = Yii::$app->request->post();
     if (isset(Yii::$app->request->post()["Books"])) {
         $post["Books"]["date_create"] = date("Y-m-d");
         $post["Books"]["date_update"] = date("Y-m-d");
     }
     if ($model->load($post) && $model->save()) {
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         return $this->render('create', ['model' => $model, 'authors' => $authors]);
     }
 }