Skip to content

Commit

Permalink
Merge pull request #24 from d3yii2/SaveContent
Browse files Browse the repository at this point in the history
Added method saveContent()
  • Loading branch information
uldisn authored Nov 4, 2019
2 parents b9352ab + 82d22d5 commit 96861e9
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 12 deletions.
25 changes: 17 additions & 8 deletions components/FileHandler.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
<?php
namespace d3yii2\d3files\components;

use ReflectionClass;
use Yii;
use yii\base\Exception;
use yii\base\InvalidArgumentException;
use yii\helpers\FileHelper;
use yii\web\NotFoundHttpException;
use yii\web\ForbiddenHttpException;
use yii\web\UploadedFile;
use function dirname;

class FileHandler
{
Expand Down Expand Up @@ -55,7 +58,7 @@ public function __construct($options) {
protected static function getAllowedFileTypes(array $options = []): string
{
// Check for model defined attachment types first
$model = new \ReflectionClass($options['model_name']);
$model = new ReflectionClass($options['model_name']);
$modelFileTypes = $model->getConstant('D3FILES_ALLOWED_EXT_REGEXP');
if ($modelFileTypes) {
return $modelFileTypes;
Expand Down Expand Up @@ -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()
{
Expand All @@ -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.'));
Expand All @@ -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.'));
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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'];
Expand Down
59 changes: 55 additions & 4 deletions models/D3files.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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()) {

Expand All @@ -157,7 +208,7 @@ public static function saveYii2UploadFile(UploadedFile $uploadFile, $modelName,
}

/**
* @return \yii\db\ActiveQuery
* @return ActiveQuery
*/
public function getD3filesModels()
{
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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();
}
Expand Down

0 comments on commit 96861e9

Please sign in to comment.