/**
  * Creates a new Book model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Book();
     $returning = function () use($model) {
         $authors = Author::find()->all();
         $dropDownSearchItems = ArrayHelper::map($authors, 'id', 'lastname');
         return $this->render('create', ['model' => $model, 'dropDownAuthorItems' => $dropDownSearchItems]);
     };
     if ($model->load(Yii::$app->request->post())) {
         $model->image = UploadedFile::getInstance($model, 'image');
         if (is_object($model->image)) {
             $preview = 'preview_img/' . uniqid() . '_' . $model->image->name;
             $model->image->saveAs($preview);
             $model->image = null;
             $model->preview = '/' . $preview;
         }
         if ($model->save()) {
             return $this->redirect(['view', 'id' => $model->id]);
         } else {
             return $returning();
         }
     } else {
         return $returning();
     }
 }
Ejemplo n.º 2
0
 /**
  * Creates a new Book model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Book();
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
Ejemplo n.º 3
0
 /**
  * Creates a new Book model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Book();
     $authors = \app\models\Author::find()->all();
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         return $this->redirect(['index']);
     } else {
         return $this->render('create', ['model' => $model, 'authors' => $authors]);
     }
 }
Ejemplo n.º 4
0
 public function actionCreate()
 {
     $book = new Book();
     if (Yii::$app->request->isPost) {
         $book->load(Yii::$app->request->post());
         $book->save();
         $this->redirect(Url::to(['books/index']));
     }
     return $this->render('create', ['book' => $book, 'authors' => Author::find()->all()]);
 }
Ejemplo n.º 5
0
 /**
  * Creates a new Book model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Book();
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         \yii\caching\TagDependency::invalidate(Yii::$app->cache, 'books');
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
Ejemplo n.º 6
0
 /**
  * Creates a new Book model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Book();
     if ($model->load(Yii::$app->request->post())) {
         $model->previewFile = UploadedFile::getInstance($model, 'previewFile');
         if ($model->upload() && $model->save()) {
             return $this->redirect(['index']);
         }
     }
     return $this->render('create', ['model' => $model]);
 }
Ejemplo n.º 7
0
 /**
  * Creates a new Book model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Book();
     $searchModel = new BookSearch();
     $authors = $searchModel->getAuthors()->all();
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         return $this->render('create', ['model' => $model, 'authors' => $authors]);
     }
 }
Ejemplo n.º 8
0
 /**
  * Creates a new Book model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Book();
     if ($model->load(Yii::$app->request->post())) {
         $file = UploadedFile::getInstance($model, 'preview');
         if ($model->upload($file) && $model->save()) {
             return $this->redirect(Url::previous());
         }
     }
     return $this->render('create', ['model' => $model, 'authors' => Author::find()->all()]);
 }
Ejemplo n.º 9
0
 /**
  * Creates a new Book model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Book();
     if (Yii::$app->request->isPost) {
         $model->load(Yii::$app->request->post());
         $model->imageFile = UploadedFile::getInstance($model, 'preview');
         $model->uploadFile();
         $model->save(false);
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         return $this->render('create', ['model' => $model, 'authors' => (new AuthorCollection())->createEntity(Author::find()->all(), true)]);
     }
 }
Ejemplo n.º 10
0
 /**
  * Creates a new Book model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Book();
     $authors = Author::find()->asArray()->all();
     for ($i = 1; $i <= Author::find()->count(); $i++) {
         $authors_array[$i] = $authors[$i - 1]['firstname'] . ' ' . $authors[$i - 1]['lastname'];
     }
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         return $this->render('create', ['model' => $model, 'authors_array' => $authors_array]);
     }
 }
Ejemplo n.º 11
0
 /**
  * Creates a new Book model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Book();
     if ($model->load(Yii::$app->request->post())) {
         $model->bookImage = UploadedFile::getInstance($model, 'bookImage');
         if ($model->validate() && $model->save()) {
             if (Url::previous()) {
                 return $this->redirect(Url::previous());
             }
             return $this->redirect(Url::toRoute('index'));
         }
     }
     return $this->render('create', ['model' => $model]);
 }
Ejemplo n.º 12
0
 /**
  * Creates a new Book model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Book();
     if ($model->load(Yii::$app->request->post())) {
         $image = UploadedFile::getInstance($model, 'preview');
         $temp_var = explode(".", $image->name);
         $ext = end($temp_var);
         $model->date = Book::getIntDate($model->date);
         $model->preview = Yii::$app->security->generateRandomString() . ".{$ext}";
         $path = Yii::$app->params['uploadPath'] . $model->preview;
         $image->saveAs($path);
         $model->save();
         return $this->redirect(['index']);
     } else {
         return $this->render('create', ['model' => $model, 'author' => Author::find()->all()]);
     }
 }
Ejemplo n.º 13
0
 /**
  * Creates a new Book model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Book();
     if ($model->load(Yii::$app->request->post()) && $model->validate()) {
         $image = UploadedFile::getInstance($model, 'imageFile');
         if (!empty($image)) {
             $model->image = Yii::$app->security->generateRandomString() . '.' . $image->extension;
             $path = Yii::$app->basePath . '/' . Book::IMAGES_DIRECTORY . $model->image;
             $model->save();
             $image->saveAs($path);
         } else {
             $model->save();
         }
         return $this->redirect(['index']);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
Ejemplo n.º 14
0
 /**
  * @param Book $model
  * @param $template
  * @return array|Response
  */
 protected function save($model, $template)
 {
     if ($model->load(Yii::$app->request->post())) {
         if ($model->validate()) {
             if ($model->save(false)) {
                 $params = ArrayHelper::merge(['index'], $this->getSession()->get(self::SESSION_PARAMS));
                 return $this->redirect($params);
             } else {
                 Yii::$app->response->format = Response::FORMAT_JSON;
                 return ActiveForm::validate($model);
             }
         } else {
             Yii::$app->response->format = Response::FORMAT_JSON;
             return ActiveForm::validate($model);
         }
     } else {
         return $this->{$this->getCurrentRender()}($template, ['model' => $model, 'authors' => $this->getAuthors()]);
     }
 }
Ejemplo n.º 15
0
 /**
  * Creates a new Book model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Book();
     $time = time();
     $model->date_create = date('Y-m-d H:i:s', $time);
     $authorData = Author::find()->all();
     $authorsList = [];
     foreach ($authorData as $author) {
         $authorsList[$author->id] = $author->firstname . ' ' . $author->lastname;
     }
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         if ($model->preview = UploadedFile::getInstance($model, 'preview')) {
             $path = Yii::$app->basePath . Yii::$app->params['uploadPath'] . $model->preview;
             $model->preview->saveAs($path);
             $model->preview = Yii::$app->params['uploadPath'] . $model->preview;
         }
         $model->save();
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         return $this->render('create', ['model' => $model, 'authors' => $authorsList]);
     }
 }
Ejemplo n.º 16
0
 /**
  * Сохраняет модель и связанные данные
  *
  * @param Book $modelBook
  * @return \yii\web\Response
  */
 private function _saveBook(Book $modelBook)
 {
     $authorIds = Yii::$app->request->post('authors');
     $tagIds = Yii::$app->request->post('tags');
     if ($modelBook->load(Yii::$app->request->post()) && $modelBook->save()) {
         array_map(function ($model) use($modelBook) {
             $modelBook->unlink('authors', $model, true);
         }, $modelBook->authors);
         foreach ($authorIds ?: [] as $authorId) {
             /** @var Author $modelAuthor */
             $modelAuthor = Author::findOne($authorId);
             $modelBook->link('authors', $modelAuthor);
         }
         array_map(function ($model) use($modelBook) {
             $modelBook->unlink('tags', $model, true);
         }, $modelBook->tags);
         foreach ($tagIds ?: [] as $tagId) {
             /** @var Tag $modelTag */
             $modelTag = Tag::findOne($tagId);
             $modelBook->link('tags', $modelTag);
         }
         return $this->redirect(['back-book/view', 'id' => $modelBook->id]);
     }
 }
Ejemplo n.º 17
0
 public function actionBook_detail($id)
 {
     $model = new Book();
     if (Yii::$app->user->isGuest) {
         return $this->redirect(Yii::$app->homeUrl . '?r=site/login');
     }
     if ($model->load(Yii::$app->request->post())) {
         $model->room_id = $id;
         $model->date_create = date('y-m-d', time());
         $room = Room::findOne($id);
         $totalPrice = (strtotime($model->date_to) - strtotime($model->date_from)) / (24 * 3600);
         $model->price = $totalPrice * $room->price;
         if ($model->save()) {
             $room->status = 1;
             $room->name = $model->name;
             $room->id_card = $model->id_card;
             $room->save();
             return $this->redirect(['view', 'id' => $model->id]);
         }
     } else {
         return $this->render('book_detail', ['model' => $model]);
     }
 }