/** * @param \app\models\books\Model $model * @return bool */ public function fill(\app\models\books\Model $model) { $oldFileNameHash = $model->preview_path_hash; if (!$this->cover) { $model->setAttributes(['preview_path' => null, 'preview_path_hash' => null], false); return true; } $fileName = $this->cover->baseName . '.' . $this->cover->extension; $fileNameHash = sha1($this->cover->baseName . time()) . '.' . $this->cover->extension; $this->cleanOldFile($oldFileNameHash, $fileNameHash, $model); if ($this->validate()) { $model->setAttributes(['preview_path' => $fileName, 'preview_path_hash' => $fileNameHash], false); $this->cover->saveAs(\Yii::getAlias('@covers') . DIRECTORY_SEPARATOR . $fileNameHash); return true; } else { return false; } }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Model::find(); $query->joinWith(['authorModel']); $dataProvider = new ActiveDataProvider(['query' => $query]); $dataProvider->getSort()->attributes['author'] = ['asc' => [ITables::AUTHORS . '.first_name' => SORT_ASC, ITables::AUTHORS . '.last_name' => SORT_ASC], 'desc' => [ITables::AUTHORS . '.first_name' => SORT_DESC, ITables::AUTHORS . '.last_name' => SORT_DESC]]; $this->load($params); if (!$this->validate()) { return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'author_id' => $this->author_id, 'release_date' => $this->release_date, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at]); $query->andFilterWhere(['ILIKE', 'name', $this->name])->andFilterWhere(['ILIKE', 'preview_path', $this->preview_path])->andWhere(ITables::AUTHORS . '.first_name ILIKE \'%' . $this->author . '%\' OR ' . ITables::AUTHORS . '.last_name ILIKE \'%' . $this->author . '%\''); if ($this->release_date_start && $this->release_date_end) { $this->release_date_start = strtotime($this->release_date_start); $this->release_date_end = strtotime($this->release_date_end); if ($this->release_date_start === $this->release_date_end) { $this->release_date_end += 24 * 60 * 60; } $query->andWhere('release_date BETWEEN ' . (int) $this->release_date_start . ' AND ' . (int) $this->release_date_end); } return $dataProvider; }
/** * Finds the Model model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Model the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Model::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }