diff --git a/src/behaviors/FileBehavior.php b/src/behaviors/FileBehavior.php index 4be4b3b..0f62ccf 100644 --- a/src/behaviors/FileBehavior.php +++ b/src/behaviors/FileBehavior.php @@ -102,15 +102,7 @@ public function getInitialPreview() } foreach ($this->getFiles() as $file) { - if (substr($file->mime, 0, 5) === 'image') { - $initialPreview[] = Html::img($file->getUrl(), ['class' => 'file-preview-image']); - } else { - $initialPreview[] = Html::beginTag('div', ['class' => 'file-preview-other']) . - Html::beginTag('h2') . - Html::tag('i', '', ['class' => 'glyphicon glyphicon-file']) . - Html::endTag('h2') . - Html::endTag('div'); - } + $initialPreview[] =$file->getUrl(true); } return $initialPreview; @@ -132,14 +124,47 @@ public function getInitialPreviewConfig() } foreach ($this->getFiles() as $index => $file) { - $initialPreviewConfig[] = [ + if(str_contains($file->mime,"image")) + { + $initialPreviewConfig[] = [ + 'caption' => "$file->name.$file->type", + 'url' => Url::toRoute(['/attachments/file/delete', + 'id' => $file->id]) + ]; + } + elseif(str_contains($file->mime,"text")){ + $initialPreviewConfig[] = [ + 'type'=> "text", 'caption' => "$file->name.$file->type", 'url' => Url::toRoute(['/attachments/file/delete', - 'id' => $file->id - ]), + 'id' => $file->id]) ]; + } + elseif(str_contains($file->mime,"video")){ + $initialPreviewConfig[] = [ + 'type'=> "video", + 'caption' => "$file->name.$file->type", + 'url' => Url::toRoute(['/attachments/file/delete', + 'id' => $file->id]) + ]; + } + elseif(str_contains($file->mime,"doc")||str_contains($file->mime,".ppt")||str_contains($file->mime,".xls")){ + $initialPreviewConfig[] = [ + 'type'=> "office", + 'caption' => "$file->name.$file->type", + 'url' => Url::toRoute(['/attachments/file/delete', + 'id' => $file->id]) + ]; + } + else{ + $initialPreviewConfig[] = [ + 'type'=> $file->type, + 'caption' => "$file->name.$file->type", + 'url' => Url::toRoute(['/attachments/file/delete', + 'id' => $file->id]) + ]; + } } - return $initialPreviewConfig; } -} \ No newline at end of file +} diff --git a/src/components/AttachmentsInput.php b/src/components/AttachmentsInput.php index bd662fa..3493cb1 100644 --- a/src/components/AttachmentsInput.php +++ b/src/components/AttachmentsInput.php @@ -40,6 +40,8 @@ public function init() FileHelper::removeDirectory($this->getModule()->getUserDirPath()); // Delete all uploaded files in past $this->pluginOptions = array_replace($this->pluginOptions, [ + 'initialPreviewFileType'=> 'image', + 'initialPreviewAsData'=> 'true', //let kartik deal with the previews! 'uploadUrl' => Url::toRoute('/attachments/file/upload'), 'uploadAsync' => false ]); diff --git a/src/controllers/FileController.php b/src/controllers/FileController.php index 9d1ec50..fb9b2af 100644 --- a/src/controllers/FileController.php +++ b/src/controllers/FileController.php @@ -47,12 +47,12 @@ public function actionUpload() } } - public function actionDownload($id) + public function actionDownload($id,$inline=false) { $file = File::findOne(['id' => $id]); $filePath = $this->getModule()->getFilesDirPath($file->hash) . DIRECTORY_SEPARATOR . $file->hash . '.' . $file->type; - return Yii::$app->response->sendFile($filePath, "$file->name.$file->type"); + return Yii::$app->response->sendFile($filePath, "$file->name.$file->type",['inline'=>$inline]); } public function actionDelete($id) diff --git a/src/models/File.php b/src/models/File.php index 316d293..1481842 100644 --- a/src/models/File.php +++ b/src/models/File.php @@ -70,9 +70,9 @@ public function attributeLabels() ]; } - public function getUrl() + public function getUrl($inline = false) { - return Url::to(['/attachments/file/download', 'id' => $this->id]); + return Url::to(['/attachments/file/download', 'id' => $this->id,'inline'=>$inline]); } public function getPath()