From 82d22d5f1b945c69d6fe078703ead18a1d94cc11 Mon Sep 17 00:00:00 2001 From: uldisn Date: Mon, 4 Nov 2019 13:41:06 +0200 Subject: [PATCH] Added method saveContent() Signed-off-by: uldisn --- components/FileHandler.php | 25 ++++++++++------ models/D3files.php | 59 +++++++++++++++++++++++++++++++++++--- 2 files changed, 72 insertions(+), 12 deletions(-) diff --git a/components/FileHandler.php b/components/FileHandler.php index 9dddc94..985bfd7 100644 --- a/components/FileHandler.php +++ b/components/FileHandler.php @@ -1,12 +1,15 @@ getConstant('D3FILES_ALLOWED_EXT_REGEXP'); if ($modelFileTypes) { return $modelFileTypes; @@ -83,7 +86,7 @@ protected static function getUploadDirPath($model_name) * copy posted file to upload directory * @return bool * @throws NotFoundHttpException - * @throws \yii\base\Exception + * @throws Exception */ public function upload() { @@ -93,7 +96,7 @@ public function upload() } $filePath = $this->getFilePath(); - $dir = \dirname($filePath); + $dir = dirname($filePath); FileHelper::createDirectory($dir); if (!move_uploaded_file($_FILES['upload_file']['tmp_name'], $filePath)) { throw new NotFoundHttpException(Yii::t('d3files', 'The uploaded file does not exist.')); @@ -109,12 +112,12 @@ public function upload() * @param UploadedFile $uploadedFile * @return bool * @throws NotFoundHttpException - * @throws \yii\base\Exception + * @throws Exception */ public function uploadYii2UloadFile(UploadedFile $uploadedFile) { $filePath = $this->getFilePath(); - FileHelper::createDirectory(\dirname($filePath)); + FileHelper::createDirectory(dirname($filePath)); if (!$uploadedFile->saveAs($filePath)) { throw new NotFoundHttpException(Yii::t('d3files', 'The uploaded file does not exist.')); } @@ -144,12 +147,12 @@ public function getFilePath() * save file. Alternative for method upload() * @param $fileContent * @return bool - * @throws \yii\base\Exception + * @throws Exception */ public function save(&$fileContent) { $filePath = $this->getFilePath(); - FileHelper::createDirectory(\dirname($filePath)); + FileHelper::createDirectory(dirname($filePath)); file_put_contents($filePath, $fileContent); return true; @@ -218,7 +221,13 @@ public function open() exit; } - + + public function setModelId($id) + { + $this->options['model_id'] = $id; + } + + protected static function createSaveFileName($d3files_id, $file_name) { return $d3files_id . '.' . pathinfo($file_name)['extension']; diff --git a/models/D3files.php b/models/D3files.php index 078ec69..2dff1da 100644 --- a/models/D3files.php +++ b/models/D3files.php @@ -3,6 +3,8 @@ namespace d3yii2\d3files\models; use Yii; +use yii\base\Exception; +use yii\db\ActiveQuery; use yii\db\ActiveRecord; use yii\db\Expression; use yii\web\UploadedFile; @@ -109,6 +111,55 @@ public static function saveFile($fileName, $modelName, $modelId, $filePath, $fil } } + /** + * alternative for uploading file + * + * @param string $fileName + * @param string $modelName + * @param int $modelId + * @param string $fileContent + * @param string $fileTypes + * @param int $userId + * @throws ForbiddenHttpException + * @throws Exception + */ + public static function saveContent($fileName, $modelName, $modelId, $fileContent, $fileTypes, $userId = 0 ) + { + $fileHandler = new FileHandler( + [ + 'model_name' => $modelName, + 'model_id' => uniqid('d3files',false), + 'file_name' => $fileName, + 'file_types' => $fileTypes, + ] + ); + + $model = new self(); + + $model->file_name = $fileName; + $model->add_datetime = new Expression('NOW()'); + $model->user_id = $userId; + + if ($model->save()) { + + // Get or create model name id + $modelMN = new D3filesModelName(); + $model_name_id = $modelMN->getByName($modelName, true); + + $modelM = new D3filesModel(); + $modelM->d3files_id = $model->id; + $modelM->is_file = 1; + $modelM->model_name_id = $model_name_id; + $modelM->model_id = $modelId; + $modelM->save(); + $fileHandler->setModelId($model->id); + $fileHandler->save($fileContent); + } else { + $fileHandler->remove(); + throw new \Exception(500, Yii::t('d3files', 'Insert DB record failed')); + } + } + /** * Upload yii\web\UploadedFile * @param UploadedFile $uploadFile @@ -134,7 +185,7 @@ public static function saveYii2UploadFile(UploadedFile $uploadFile, $modelName, $model->file_name = $uploadFile->name; $model->add_datetime = new Expression('NOW()'); - $model->user_id = \Yii::$app->person->user_id; + $model->user_id = Yii::$app->person->user_id; if ($model->save()) { @@ -157,7 +208,7 @@ public static function saveYii2UploadFile(UploadedFile $uploadFile, $modelName, } /** - * @return \yii\db\ActiveQuery + * @return ActiveQuery */ public function getD3filesModels() { @@ -195,7 +246,7 @@ public static function fileListForWidget($modelName, $modelId) { ':model_id' => $modelId, ]; - $connection = \Yii::$app->getDb(); + $connection = Yii::$app->getDb(); $command = $connection->createCommand($sSql, $parameters); return $command->queryAll(); } @@ -273,7 +324,7 @@ function ($id) { return (int) $id; }, ':model_name' => $modelClass, ]; - $connection = \Yii::$app->getDb(); + $connection = Yii::$app->getDb(); $command = $connection->createCommand($sSql, $parameters); return $command->queryAll(); }