/** * Finds the Setting model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Setting the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = File::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
public function run() { if (isset($this->modelId) && isset($this->model) && !$this->model->isNewRecord) { /** @var $fileModel File*/ $fileModel = \Yii::createObject(File::className()); $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); $files = $fileModel->find()->where(['model' => get_class($this->model), 'model_id' => $this->modelId])->all(); $initialPreview = $initialPreviewConfig = []; foreach ($files as $file) { $initialPreview[] = "<img src='" . \Yii::$app->params['imageDomain'] . '/' . $file->url . "' class='file-preview-image'>"; $initialPreviewConfig[] = ['caption' => $file->name, 'url' => Url::to(['/system/widgets/file-upload/delete', 'id' => $file->file_id])]; } echo $form->field($fileModel, 'files[]')->widget(FileInput::classname(), ['options' => ['multiple' => true], 'pluginOptions' => ['initialPreview' => $initialPreview, 'initialPreviewConfig' => $initialPreviewConfig, 'overwriteInitial' => false, 'uploadUrl' => Url::to(['/system/widgets/file-upload/upload']), 'uploadExtraData' => ['model' => get_class($this->model), 'modelId' => $this->modelId], 'maxFileCount' => 10, 'allowedFileExtensions' => ['jpg', 'gif', 'png']]]); $form::end(); } }