Skip to content

Commit

Permalink
Merge pull request #35 from d3yii2/testing
Browse files Browse the repository at this point in the history
Allowed file types improved
  • Loading branch information
anothersoftware-lv authored Jan 13, 2020
2 parents 1628c79 + c905634 commit 7b70193
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 38 deletions.
29 changes: 24 additions & 5 deletions components/D3Files.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ public static function getFirstFileHavingExt(array $files, string $extension): ?

/**
* @param array $files
* @param array $viewExtensions
* @param string $viewExtensions
* @return bool
*/
public static function hasViewExtension(array $files, array $viewExtensions): bool
public static function hasViewExtension(array $files, string $viewExtensions): bool
{
foreach ($files as $f) {
$ext = self::getFileExtension($f);
if (in_array($ext, $viewExtensions, true)) {
if (preg_match($viewExtensions, $ext)) {
return true;
break;
}
Expand Down Expand Up @@ -76,12 +76,12 @@ public static function getFilesListByExt(array $files, string $ext): array

/**
* @param array $fileList
* @param array $viewExtensions
* @param string $viewExtensions
* @param array $urlParams
* @param string $contentTarget
* @return array
*/
public static function getPreviewFilesList(array $fileList, array $viewExtensions, array $urlParams, string $contentTarget): array
public static function getPreviewFilesList(array $fileList, string $viewExtensions, array $urlParams, string $contentTarget): array
{
$fl = [];
foreach ($fileList as $i => $file) {
Expand Down Expand Up @@ -127,4 +127,23 @@ public static function getFileFromListById(array $list, string $id): ?array
}
return null;
}

/**
* @param null $modelName
* @return string
* @throws \ReflectionException
*/
public static function getAllowedFileTypes($modelName = null): string
{
if ($modelName) {
// Check for model defined attachment types first
$model = new \ReflectionClass($modelName);
$modelFileTypes = $model->getConstant('D3FILES_ALLOWED_EXT_REGEXP');
if ($modelFileTypes) {
return $modelFileTypes;
}
}

return Yii::$app->getModule('d3files')->fileTypes ?? FileHandler::FILE_TYPES;
}
}
6 changes: 3 additions & 3 deletions components/UploadAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ public function run(int $id): string

$modelFileList = D3FilesComponent::getModelFilesList($postModelName, $modelM->model_id);

$viewExtensions = ['pdf', 'png', 'jpg', 'jpeg'];
$previewExtensions = '/(gif|pdf|jpe?g|png)$/i';

if (D3FilesComponent::hasViewExtension([$renderParam], $viewExtensions)) {
if (D3FilesComponent::hasViewExtension([$renderParam], $previewExtensions)) {
$fModel = new D3filesModel();
$fModel->id = $id;
$urlParams = [
Expand All @@ -122,7 +122,7 @@ public function run(int $id): string
];
$previewFileList = D3FilesComponent::getPreviewFilesList(
$modelFileList,
$viewExtensions,
$previewExtensions,
$urlParams,
D3FilesPreviewWidget::EMBED_CONTENT_CLASS
);
Expand Down
5 changes: 5 additions & 0 deletions views/d3files/_list_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,14 @@

use yii\helpers\Html;
use yii\helpers\Url;
use \d3yii2\d3files\components\D3Files;

?><div class="table-responsive"><table class="table d3files-table"><?php
foreach ($fileList as $file) {
if (!D3Files::hasViewExtension([$file], $viewByExtensions)) {
continue;
}

?><tr><td class="col-xs-10"><?= Html::a(
'<i class="fa fa-cloud-download text-primary"></i> ' . $file['file_name'],
Url::to([
Expand Down
41 changes: 19 additions & 22 deletions widgets/D3FilesPreviewWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,28 @@
* @package d3yii2\d3files\widgets
* Usage by model \d3yii2\d3files\widgets\D3FilesPreviewWidget::widget(['model' => $model])
* Usage by fileList (attachments are joined already) \d3yii2\d3files\widgets\D3FilesPreviewWidget::widget(['fileList' => $fileList])
* @var string $viewType
* @var string $dialogWidgetClass
* @var array $pdfObjectOptions
* @var string VIEW_MODAL_BUTTON
* @var string VIEW_INLINE_BUTTON
* @var string VIEW_IFRAME
* @var string VIEW_TYPE_MODAL
* @var string VIEW_TYPE_INLINE
* @var string EMBED_CONTENT_CLASS
*
* @property null|string $modalTitle
* @property string $filesDropdown
* @property string $modalToolbarContent
* @property string $assetsUrl
*/
class D3FilesPreviewWidget extends D3FilesWidget
{
/**
* @var string $viewType
* @var string $dialogWidgetClass
* @var array $pdfObjectOptions
* @var string VIEW_MODAL_BUTTON
* @var string VIEW_INLINE_BUTTON
* @var string VIEW_IFRAME
* @var string VIEW_TYPE_MODAL
* @var string VIEW_TYPE_INLINE
* @var string EMBED_CONTENT_CLASS
*
* @property null|string $modalTitle
* @property string $filesDropdown
* @property string $modalToolbarContent
* @property string $assetsUrl
*/

public $icon = self::DEFAULT_ICON;
public $viewByExtensions = ['pdf', 'png', 'jpg', 'jpeg'];
public $viewByFancyBoxExtensions = ['pdf', 'png', 'jpg', 'jpeg'];
public $previewExtensions = '/(gif|pdf|jpe?g|png)$/i';
public $viewExtension = 'pdf';
public $currentFile;
public $showPrevNext = false;
Expand Down Expand Up @@ -83,11 +85,6 @@ public function init(): void
$this->viewType = self::VIEW_TYPE_MODAL;
}

// Backward compatibility
if ($this->viewByFancyBoxExtensions) {
$this->viewByExtensions = $this->viewByFancyBoxExtensions;
}

$hasPdf = false;
$hasAjax = false;

Expand Down Expand Up @@ -192,7 +189,7 @@ public function initFilesList(): array
];

// Rebuild the list adding some preview params to files
$this->fileList = D3Files::getPreviewFilesList($this->fileList, $this->viewByExtensions, $urlParams, $this->contentTargetSelector);
$this->fileList = D3Files::getPreviewFilesList($this->fileList, $this->previewExtensions, $urlParams, $this->contentTargetSelector);

return $this->fileList;
}
Expand Down
14 changes: 6 additions & 8 deletions widgets/D3FilesWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ class D3FilesWidget extends Widget
* Has been renamed since 0.9.18
* Use $viewByExtensions instead
*/
public $viewByFancyBoxExtensions = ['pdf'];
public $viewByFancyBoxExtensions;
//File extensions to show
public $viewByExtensions = ['pdf'];
public $viewByExtensions;
public $fileList;
// Implented only in ea\eablankonthema\d3files_views\d3files\files_readonly.php
public $actionColumn;
Expand All @@ -79,6 +79,10 @@ class D3FilesWidget extends Widget
*/
public function init(): void
{
if (!$this->viewByExtensions) {
$this->viewByExtensions = D3Files::getAllowedFileTypes();
}

D3FilesModule::registerTranslations();

if (property_exists($this->model, 'd3filesControllerRoute')) {
Expand Down Expand Up @@ -129,7 +133,6 @@ public function run()

try {
$viewParams = $this->getViewParams();

return $this->render($this->view, $viewParams);
} catch (Exception $exception) {
Yii::error('D3FilesWidget:run Exception message: ' . PHP_EOL . $exception->getMessage());
Expand All @@ -145,11 +148,6 @@ public function run()
*/
public function getViewParams(): ?array
{
// There is no files allowed to view
if ($this->fileList && !D3Files::hasViewExtension($this->fileList, $this->viewByExtensions)) {
return null;
}

return [
'model_name' => $this->model_name,
'model_id' => $this->model_id,
Expand Down

0 comments on commit 7b70193

Please sign in to comment.