public function actionCreate() { //echo Yii::$app->basePath;exit; $path = realpath(Yii::$app->basePath) . "/images/"; //$path = "localhost/library_software/frontend/images/"; $model = new Book(); $model->attributes = Yii::$app->request->post()['book']; //print_r($model);exit; //echo Yii::$app->request->post()['book']['cat_id'];exit; $model->cat_id = implode(",", $model['cat_id']); //print_r($model);exit; $image = $_FILES['file']['name']; $ext = end(explode(".", $image)); if ($ext == 'jpg' || $ext == 'png' || $ext == 'gif' || $ext == 'jpeg') { $filename = Yii::$app->security->generateRandomString() . ".{$ext}"; $model->cover_photo = $filename; if ($model->save()) { move_uploaded_file($_FILES['file']['tmp_name'], $path . '/' . $filename); return ['error' => false, 'data' => $model]; } else { return ['error' => true, 'data' => $model->errors]; } } else { return ['error' => true, 'data' => ['photo' => 'File type not allowed.']]; } /* print_r($_FILES); print_r(Yii::$app->request->post()['book']); exit; */ }
/** * @param Book|null $book * @return boolean whether the book successfully edited */ public function update($book) { if ($book) { if ($this->validate()) { $book->name = $this->name; $book->author_id = $this->author_id; $book->date = $this->date; if ($this->imageFile || $this->deleteImage) { if (!empty($book->preview)) { $imageToDelete = Yii::getAlias('@webroot') . Book::IMAGE_FOLDER . $book->preview; if (file_exists($imageToDelete)) { unlink($imageToDelete); } $book->preview = ''; } } if ($this->imageFile) { $imageName = Yii::$app->security->generateRandomString() . '.' . $this->imageFile->extension; if (!$this->imageFile->saveAs(Yii::getAlias('@webroot') . Book::IMAGE_FOLDER . $imageName)) { return false; } $book->preview = $imageName; } return $book->save(); } } return false; }
public function prosesAdd() { if ($this->petugas == null) { header("Location: " . base . "/Auth"); exit; } $valid = new Validator($_POST); $valid->rule('required', ['title', 'author', 'publisher', 'category']); if ($valid->validate()) { $book = new Book(); $book->BookTitle = $_POST['title']; $book->BookAuthor = $_POST['author']; $book->PublisherID = $_POST['publisher']; $book->CategoryID = $_POST['category']; $book->BookPageCount = $_POST['pageCount']; $book->BookPublished = $_POST['year']; $book->BookDescription = $_POST['des']; $book->BookPhoto = "acas"; $book->BookDateAdd = Carbon::now(); $book->BookPrice = $_POST['price']; if ($book->save()) { if ($_FILES['photo']['name'] != "") { $uploaddir = '/var/www/limsmvc/img/'; $uploadfile = $uploaddir . $book->BookID; move_uploaded_file($_FILES['photo']['tmp_name'], $uploadfile); } } header("Location: " . base . "/Book"); } else { // Errors print_r($valid->errors()); } }
/** * 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]); } }
/** * 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]); } }
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()]); }
/** * 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]); } }
function runBookInsertion($i) { $book = new Book(); $book->title = 'Hello' . $i; $book->isbn = '1234'; $book->price = $i; //$book->author_id = $this->authors[array_rand($this->authors)]->id; $book->link('author', $this->authors[array_rand($this->authors)]); $book->save(false); $this->books[] = $book; }
/** * 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]); } }
/** * 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]); } }
/** * @Post("/") * * @return Response */ public function addAction() { $bookData = $this->request->getJsonRawBody(); $book = new Book(); $book->setName($bookData->name); $book->setDescription($bookData->description); $response = new Response(); if ($book->save() === true) { $response->setStatusCode(201, "Created"); $response->setJsonContent(['status' => 'OK', 'data' => $book->getId()]); } else { $this->createErrorResponse($response, $book); } return $response; }
/** * create book. * @return Book|null the saved model or null if saving fails */ public function insert() { $this->preview = UploadedFile::getInstance($this, 'preview'); if ($this->validate()) { $book = new Book(); $book->name = $this->name; $book->date = strtotime($this->date); $book->author_id = $this->author_id; if ($book->save()) { // если сохранило сообщение, то дописываем файл ------------------------------ $dir = Yii::getAlias(Yii::$app->params['previewPath']); // если надо - создаем каталог if (!is_dir($dir)) { BaseFileHelper::createDirectory($dir, 0777); } $uploaded = false; $filename = ''; if ($this->preview) { $filename = 'b' . $book->book_id . 'preview.' . $this->preview->extension; $uploaded = $this->preview->saveAs($dir . '/' . $filename); // урезаем размер файла, что б не грущили 100500мегабайт и пересохраняем $img = Image::getImagine()->open($dir . '/' . $filename); $size = $img->getSize(); $ratio = $size->getWidth() / $size->getHeight(); $width = 700; $height = round($width / $ratio); Image::thumbnail($dir . '/' . $filename, $width, $height)->save($dir . '/' . $filename, ['quality' => 80]); // делаем превьюшку $ratio = $size->getWidth() / $size->getHeight(); $width = 200; $height = round($width / $ratio); Image::thumbnail($dir . '/' . $filename, $width, $height)->save($dir . '/th_' . $filename, ['quality' => 80]); } // если файл залился - пишем в базу данные по файлу if ($uploaded) { $bookData = Book::findOne($book->book_id); $bookData->preview = $filename; $bookData->save(); } return $book; } } return null; }
/** * @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()]); } }
/** * 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 (!empty(Yii::$app->request->post())) { $model->setAttributes(Yii::$app->request->post()); $model->authors = Yii::$app->request->post('authors'); $model->tags = Yii::$app->request->post('tags'); $model->formats = Yii::$app->request->post('formats'); if ($model->save()) { $bookFiles = \Yii::$app->basePath . '/files/tmp/book-form/1/'; $imgFile = \Yii::$app->basePath . '/web/image/book/tmp/1/'; $this->copyFiles($bookFiles, \Yii::$app->basePath . '/files/books/' . $model->id . '/'); $this->copyFiles($imgFile, \Yii::$app->basePath . '/web/image/book/' . $model->id . '/'); $this->deleteFiles(\Yii::$app->basePath . '/files/tmp/book-form/1/'); $this->deleteFiles(\Yii::$app->basePath . '/web/image/book/tmp/1/'); return $this->redirect(['view', 'id' => $model->id]); } } return $this->render('create', ['model' => $model]); }
public function insert() { $this->preview = UploadedFile::getInstance($this, 'preview'); if ($this->validate()) { $book = new Book(); $book->name = $this->name; $book->date = strtotime($this->date); $book->author_id = $this->author_id; if ($book->save()) { $dir = Yii::getAlias(Yii::$app->params['previewPath']); if (!is_dir($dir)) { BaseFileHelper::createDirectory($dir, 0777); } $uploaded = false; $filename = ''; if ($this->preview) { $filename = 'b' . $book->book_id . 'preview.' . $this->preview->extension; $uploaded = $this->preview->saveAs($dir . '/' . $filename); $img = Image::getImagine()->open($dir . '/' . $filename); $size = $img->getSize(); $ratio = $size->getWidth() / $size->getHeight(); $width = 700; $height = round($width / $ratio); Image::thumbnail($dir . '/' . $filename, $width, $height)->save($dir . '/' . $filename, ['quality' => 80]); $ratio = $size->getWidth() / $size->getHeight(); $width = 200; $height = round($width / $ratio); Image::thumbnail($dir . '/' . $filename, $width, $height)->save($dir . '/th_' . $filename, ['quality' => 80]); } if ($uploaded) { $bookData = Book::findOne($book->book_id); $bookData->preview = $filename; $bookData->save(); } return $book; } } return null; }
/** * 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]); } }
/** * Сохраняет модель и связанные данные * * @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]); } }
public function actionCreateBook() { $model = new Book(); $model->slotID = $_POST['slotID']; $model->date = $_POST['Date']; $model->content = $_POST['content']; $model->studentID = $_POST['booker']; $status = array(); $exist = Book::find()->select('studentID')->where(['slotID' => $model->slotID, 'date' => $model->date, 'studentID' => $model->studentID])->all(); if (!$exist) { $model->save(); $status["status"] = "ok"; } else { $status["status"] = "false"; } echo json_encode($status); }
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]); } }
/** * Удалить * * @param Book $model * @return null */ public function delete(Book $model) { $model->status = Book::STATUS_DELETE; return $model->save(); }
$author->update($request->all()); return redirect('authors/' . $author->id); }); // --- BOOKS --- $app->get('/books', function () use($app) { $books = Book::all(); return view('books.index', ['books' => $books]); }); $app->get('/books/new', function (Request $request) use($app) { $authors = Author::all(); return view('books.new', ['authors' => $authors, 'request' => $request]); }); $app->post('/books', function (Request $request) use($app) { $this->validate($request, Book::VALIDATION_RULES); $new_book = new Book($request->all()); $new_book->save(); return redirect('books/' . $new_book->id); }); $app->get('/books/{id}', function ($id, Request $request) use($app) { $book = Book::find($id); $authors = Author::all(); return view('books.edit', ['authors' => $authors, 'request' => $request, 'book' => $book, 'id' => $id]); }); $app->post('/books/{id}/update', function ($id, Request $request) use($app) { $this->validate($request, Book::VALIDATION_RULES); $book = Book::find($id); $book->update($request->all()); return redirect('books/' . $book->id); }); $app->get('authors/{id}/books', function ($id, Request $request) use($app) { $author = Author::find($id);