public function save() { $model = Books::findOne($this->id); $fileInstance = UploadedFile::getInstance($this, self::FIELD_PREVIEW); if ($fileInstance) { $filePath = 'images/' . $fileInstance->baseName . '.' . $fileInstance->extension; $model->preview = $fileInstance->baseName . '.' . $fileInstance->extension; $fileInstance->saveAs($filePath, false); $imagine = new Gd\Imagine(); try { $filePath = 'images/preview_' . $fileInstance->baseName . '.' . $fileInstance->extension; $img = $imagine->open($fileInstance->tempName); $size = $img->getSize(); if ($size->getHeight() > $this->imageSizeLimit['width'] || $size->getWidth() > $this->imageSizeLimit['height']) { $img->resize(new Box($this->imageSizeLimit['width'], $this->imageSizeLimit['height'])); } $img->save($filePath); // } catch (\Imagine\Exception\RuntimeException $ex) { $this->addError(self::FIELD_PREVIEW, 'Imagine runtime exception: ' . $ex->getMessage()); return FALSE; } } if (!$this->validate()) { return false; } $model->name = $this->name; $model->author_id = $this->author_id; $model->date = DateTime::createFromFormat('d/m/Y', $this->date)->format('Y-m-d'); $model->date_update = new Expression('NOW()'); $model->save(); return true; }
protected function findModel($id) { if (($model = Books::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested book not exist.'); } }
private function loadModel($id) { $model = Books::findOne($id); if ($model == NULL) { throw new HttpException(404, 'Model not found.'); } return $model; }
public function actionUpload($id) { if (($model = Books::findOne($id)) !== null) { $file = UploadedFile::getInstanceByName('file'); if ($file) { $file->saveAs($model->getCoverPath()); } else { throw new NotFoundHttpException('Failed to load the file.'); } } else { throw new NotFoundHttpException('The book does not exist.'); } }
/** * Updates a new Rents model with closing info. * @return mixed */ public function actionClose() { if (isset($_GET['book_id'])) { if ($model = Rents::find()->where(['book_id' => $_GET['book_id'], 'user_id' => Yii::$app->user->id, 'status' => 'rented'])->one()) { $model->prev_date = date('Y-m-d', time()); $model->status = 'closed'; $model->save(); $book = Books::findOne($model->book_id)->updateStatus('available'); } return $this->redirect(['book/index']); } else { return $this->redirect(['book/index']); } }
public function test_Delete_Warning_FileWasDeletedWithSyncON() { $log_filename = $this->initAppFileSystem() . '/runtime/logs/logs.txt'; $this->mockYiiApplication(['bootstrap' => ['log'], 'components' => ['log' => ['traceLevel' => 0, 'targets' => ['generic' => ['class' => \yii\log\FileTarget::class, 'logVars' => [], 'logFile' => $log_filename, 'enabled' => true, 'levels' => ['warning']]]]]]); $book_delete = $this->books['expected'][0]; \Yii::$app->mycfg->library->sync = true; $book = Books::findOne(['book_guid' => $book_delete['book_guid']]); $book->delete(); \Yii::getLogger()->flush(true); $this->assertRegExp('/filename\\-1\' was removed before record deletion with sync enabled$/', file_get_contents($log_filename)); }
/** * Finds the Books model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Books the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Books::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('Запрашиваемая странца не существует.'); } }
/** * @dataProvider pSync * * ACTION MUST: * * 1. not allow changes of book_guid, created and updated date, filename * 2. generate filename based on title * 3. generate updated_date * 4. rename file if sync is ON */ public function test_action_Manage_Edit($sync) { $_SERVER['REQUEST_METHOD'] = 'POST'; $book = $this->books['insert'][0]; $book_expected = $this->books['expected'][0]; $filename_expected = $filename_old = \Yii::$app->mycfg->library->directory . $book_expected['filename']; file_put_contents($filename_expected, 'sample-data'); \Yii::$app->mycfg->library->sync = $sync; $_POST['oper'] = 'edit'; $_POST['id'] = $book['book_guid']; // CHANGING // [ 1 ] $_POST['created_date'] = '2000-01-01'; $_POST['updated_date'] = '2000-01-01'; $_POST['filename'] = '2000-01-01'; // [ 2 ] $book_expected['filename'] = ", ''title book #1'', []."; // [ 3 ] $book_expected['updated_date'] = (new \DateTime())->format('Y-m-d H:i:s'); $this->controllerSite->runAction('manage'); /* @var $book_current \yii\db\BaseActiveRecord */ $book_current = Books::findOne(['book_guid' => $book['book_guid']]); //remove seconds, as it fails on slow machines, definely fails on Travis $book_expected['updated_date'] = (new \DateTime($book_expected['updated_date']))->format('Y-m-d H:i'); $book_current['updated_date'] = (new \DateTime($book_current['updated_date']))->format('Y-m-d H:i'); //var_dump($book_expected,$book_current->getAttributes()); die; $this->assertArraySubset($book_expected, $book_current->getAttributes()); if ($sync) { $filename_expected = \Yii::$app->mycfg->library->directory . $book_expected['filename']; $this->assertFileNotExists($filename_old); } $this->assertFileExists($filename_expected); $this->assertEquals(file_get_contents($filename_expected), 'sample-data'); }
/** * Finds the Books model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Books the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { //$model = new Books(); //var_dump($model); if (($model = Books::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
/** * @dataProvider pSync * * ACTION MUST: * * 1. not allow changes of book_guid, created and updated date, filename * 2. generate filename based on title * 3. generate updated_date * 4. rename file if sync is ON */ public function test_action_Manage_Edit($sync) { // CONFIGURE $book = $this->books['inserted'][0]; $book_expected = $this->books['expected'][0]; $filename_expected = $filename_old = \Yii::$app->mycfg->library->directory . $book['filename']; file_put_contents($filename_expected, 'sample-data'); $_SERVER['REQUEST_METHOD'] = 'POST'; $_POST['oper'] = 'edit'; $_POST['id'] = $book['book_guid']; $_POST['created_date'] = '2000-01-01'; $_POST['updated_date'] = '2000-01-01'; $_POST['filename'] = '2000-01-01'; \Yii::$app->mycfg->library->sync = $sync; // - - - - - - $this->controllerSite->runAction('manage'); $book_expected['filename'] = ", ''title book #1'', []."; // #1 // WORKAROUND FOR TRAVIS $dt = new \DateTime(); $dt->setTimezone(new \DateTimeZone(\Yii::$app->getTimeZone())); $book_expected['updated_date'] = $dt->format('Y-m-d H:i:s'); //CHECKING /* @var $book_current \yii\db\BaseActiveRecord */ $book_current = Books::findOne(['book_guid' => $book['book_guid']]); // #2 // WORKAROUND FOR TRAVIS: remove seconds, as it fails on slow machines, definely fails on Travis $book_expected['updated_date'] = (new \DateTime($book_expected['updated_date']))->format('Y-m-d H:i'); $book_current['updated_date'] = (new \DateTime($book_current['updated_date']))->format('Y-m-d H:i'); // #3 $book_current_arr = $book_current->getAttributes(); $keys = array_keys($book_expected); foreach ($keys as $k) { if ($k == 'filename') { // skip filename checks here. checked at #4 below continue; } $this->assertEquals($book_expected[$k], $book_current_arr[$k], "expected '{$k}' doesn't match"); } // #4 if ($sync) { // file rename if sync ON $filename_expected = \Yii::$app->mycfg->library->directory . $book_expected['filename']; // renamed new $this->assertFileNotExists($filename_old); // old is not existed } $this->assertFileExists($filename_expected); $this->assertEquals(file_get_contents($filename_expected), 'sample-data'); }
private function update($id, $attributes) { /* @var $book Books */ $book = Books::findOne(['book_guid' => $id]); $book->scenario = 'edit'; $book->attributes = $attributes; if (!$book->save()) { throw new \yii\web\BadRequestHttpException(print_r($book->getErrors(), true)); } }
public function actionViewBook() { if (Yii::$app->user->isGuest) { throw new \yii\web\HttpException(404); } if (!($model = Books::findOne([Books::FIELD_ID => Yii::$app->request->get(Books::FIELD_ID)]))) { return false; } return $this->renderPartial('_book_view_modal', ['model' => $model]); }