public function actionForm($book_id) { $product = Book::findOne($book_id); $productImage = new BookImage(); if (!empty($_FILES['BookImage'])) { $img = $_FILES['BookImage']['name']['url']; $ext = end(explode(".", $img)); $name = microtime(); $name = str_replace(' ', '', $name); $name = str_replace('.', '', $name); $name = $name . '.' . $ext; $tmp = $_FILES['BookImage']['tmp_name']['url']; $productImage->url = $name; move_uploaded_file($tmp, '../uploads/' . $name); } if (!empty($_POST)) { $productImage->name = $_POST['BookImage']['name']; $productImage->product_id = $book_id; if ($productImage->save()) { $session = new Session(); $session->open(); $session->setFlash('message', 'Data Saved.'); return $this->redirect(['index', 'book_id' => $book_id]); } } return $this->render('//BookImage/Form', ['product' => $product, 'productImage' => $productImage]); }
/** * @param $id * @return null|Book * @throws NotFoundHttpException */ private function loadModel($id) { $model = Book::findOne($id); if (null == $model) { throw new NotFoundHttpException('Книга не найдена'); } return $model; }
public function actionDelete($id) { $book = Book::findOne(['id' => $id]); if ($book) { $book->delete(); } $this->redirect(Url::to(['books/index'])); }
protected function findModel($id) { if (($model = Book::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
public function actionDelete() { $id = (int) Yii::$app->request->get('id'); /** @var Book $book */ $book = Book::findOne(['id' => $id]); $result = $book ? $book->delete() : false; Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; return ['result' => (bool) $result]; }
public function update() { $this->preview = UploadedFile::getInstance($this, 'preview'); if ($this->validate()) { $book = Book::findOne($this->book_id); $book->name = $this->name; $book->date = strtotime($this->date); $book->author_id = $this->author_id; $dir = Yii::getAlias(Yii::$app->params['previewPath']); $uploaded = false; if ($this->preview) { $filename = 'b' . $this->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]); } else { $filename = ''; } if ($uploaded) { if (is_file(Yii::getAlias(Yii::$app->params['previewPath']) . '/' . $book->preview)) { unlink(Yii::getAlias(Yii::$app->params['previewPath']) . '/' . $book->preview); } $book->preview = $filename; } $book->save(); return $book; } return null; }
/** * View book. * @return mixed */ public function actionViewAjax($id) { $this->layout = '/empty'; $model = Book::findOne($id); return $this->render('view-ajax', ['model' => $model]); }
/** * update book. * @return Book|null the saved model or null if saving fails */ public function update() { // подтягиваем файлы перед валидацией, что бы все вместе проверять $this->preview = UploadedFile::getInstance($this, 'preview'); if ($this->validate()) { $book = Book::findOne($this->book_id); $book->name = $this->name; $book->date = strtotime($this->date); $book->author_id = $this->author_id; // Дописываем файл $dir = Yii::getAlias(Yii::$app->params['previewPath']); $uploaded = false; if ($this->preview) { // если файл залился - пишем в базу данные по файлу $filename = 'b' . $this->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]); } else { $filename = ''; } if ($uploaded) { // удалим старый файл, если он был if (is_file(Yii::getAlias(Yii::$app->params['previewPath']) . '/' . $book->preview)) { unlink(Yii::getAlias(Yii::$app->params['previewPath']) . '/' . $book->preview); } $book->preview = $filename; // обновляем имя файла аватара } $book->save(); return $book; } return null; }
public function actionDelete($id) { $product = Book::findOne($id); if (!empty($product)) { if (!empty($product->img)) { unlink('../uploads/' . $product->img); } $product->delete(); $session = new Session(); $session->open(); $session->setFlash('message', 'Data Deleted.'); return $this->redirect(['index']); } }
/** * Удаление книги. * * @param int $id * @return \yii\web\Response */ public function actionDelete($id) { /** @var Book $modelBook */ $modelBook = Book::findOne($id); if ($modelBook->delete()) { return $this->redirect(['back-book/index']); } return $this->refresh(); }
public function actionUpdate($id = NULL) { if ($id === NULL) { throw new HttpException(404, 'Not Found'); } $model = Book::findOne($id); if ($model === NULL) { throw new HttpException(404, 'Document Does Not Exist'); } if (isset($_POST['Book'])) { $model->name = $_POST['Book']['name']; $model->date_create = $_POST['Book']['date_create']; $model->date_update = $_POST['Book']['date_update']; $model->preview = $_POST['Book']['preview']; $model->date = $_POST['Book']['date']; $model->author_id = $_POST['Book']['author_id']; if ($model->save()) { return $this->redirect(['site/index']); } } echo $this->render('create', array('model' => $model)); }
/** * Страница просмотра книги по ее уникальному псевдниму. * * @param string $alias * @return string */ public function actionView($alias) { $modelBook = Book::findOne(['alias' => $alias]); return $this->render('view', ['modelBook' => $modelBook]); }
protected function getReservationFieldsProvider($id) { $book = \app\models\Book::findOne($id); $tourFields = \app\models\TourField::find()->where(['tour_id' => $book->tour_id])->orderBy('position')->all(); $bookValues = \app\models\BookValue::find()->where(['book_id' => $book->id])->indexBy('tour_field_id')->all(); //print '<pre>'; print_r($tourFields); die(); foreach ($tourFields as $field) { $data[] = ['name' => $field->name, 'value' => array_key_exists($field->id, $bookValues) ? $bookValues[$field->id]->value : '']; } //print '<pre>'; print_r($data); die(); $provider = new \yii\data\ArrayDataProvider(['allModels' => $data]); return $provider; }
public function actionDelete($id) { Book::findOne($id)->delete(); return $this->goBack(); }