From 360c21f0f69d395389c69065af994fb25dcd3f57 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Wed, 18 Apr 2018 22:01:05 +0200 Subject: [PATCH] Fix #142 --- Controller.php | 3 +- Theme/Backend/media-single.tpl.php | 14 ++--- Theme/Backend/template-functions.php | 55 ------------------ Views/MediaView.php | 85 ++++++++++++++++++++++++++++ 4 files changed, 94 insertions(+), 63 deletions(-) create mode 100644 Views/MediaView.php diff --git a/Controller.php b/Controller.php index a409bbc..a7cb42a 100644 --- a/Controller.php +++ b/Controller.php @@ -18,6 +18,7 @@ use Modules\Media\Models\Media; use Modules\Media\Models\MediaMapper; use Modules\Media\Models\CollectionMapper; use Modules\Media\Models\Collection; +use Modules\Media\Views\MediaView; use Modules\Media\Models\UploadFile; use Modules\Media\Models\UploadStatus; @@ -143,7 +144,7 @@ class Controller extends ModuleAbstract implements WebInterface */ public function viewMediaSingle(RequestAbstract $request, ResponseAbstract $response, $data = null) : \Serializable { - $view = new View($this->app, $request, $response); + $view = new MediaView($this->app, $request, $response); $view->setTemplate('/Modules/Media/Theme/Backend/media-single'); $view->addData('nav', $this->app->moduleManager->get('Navigation')->createNavigationMid(1000401001, $request, $response)); diff --git a/Theme/Backend/media-single.tpl.php b/Theme/Backend/media-single.tpl.php index b20832b..5c3658d 100644 --- a/Theme/Backend/media-single.tpl.php +++ b/Theme/Backend/media-single.tpl.php @@ -47,7 +47,7 @@ echo $this->getData('nav')->render();
- request->getData('sub') ?? '')) : ?> + isCollectionFunction($media, $this->request->getData('sub') ?? '')) : ?>
@@ -63,7 +63,7 @@ echo $this->getData('nav')->render(); getPath())) : foreach ($media as $key => $value) : $url = UriFactory::build('/{/lang}/backend/media/single?{?}&id=' . $value->getId()); - $icon = $fileIconFunction(FileUtils::getExtensionType($value->getExtension())); + $icon = $this->fileIconFunction(FileUtils::getExtensionType($value->getExtension())); ?>
@@ -72,11 +72,11 @@ echo $this->getData('nav')->render(); printHtml($value->getSize()); ?> printHtml($value->getCreatedBy()->getName1()); ?> printHtml($value->getCreatedAt()->format('Y-m-d H:i:s')); ?> - request->getData('sub') ?? ''); ?> + dirPathFunction($media, $this->request->getData('sub') ?? ''); ?> $value) : $url = UriFactory::build('/{/lang}/backend/media/single?{?}&id=' . $media->getId() . '&sub=' . substr($value, strlen($media->getPath()))); - $icon = $fileIconFunction(FileUtils::getExtensionType(!is_dir($value) ? File::extension($value) : 'collection')); + $icon = $this->fileIconFunction(FileUtils::getExtensionType(!is_dir($value) ? File::extension($value) : 'collection')); ?>
@@ -94,9 +94,9 @@ echo $this->getData('nav')->render();
request->getData('sub') ?? ''); + $path = $this->filePathFunction($media, $this->request->getData('sub') ?? ''); - if ($isImageFunction($media, $path)) : ?> + if ($this->isImageFunction($media, $path)) : ?>
@@ -108,7 +108,7 @@ echo $this->getData('nav')->render();
                     isAbsolute() ? $path : __DIR__ . '/../../../../' . $path);
+                    $output = $this->lineContentFunction($media->isAbsolute() ? $path : __DIR__ . '/../../../../' . $path);
                     foreach ($output as $line) : ?>printHtml($line); ?>
                     
diff --git a/Theme/Backend/template-functions.php b/Theme/Backend/template-functions.php index 27fa5bf..e040c17 100644 --- a/Theme/Backend/template-functions.php +++ b/Theme/Backend/template-functions.php @@ -28,58 +28,3 @@ $fileIconFunction = function (int $extensionType) : string return 'file-o'; }; - -// todo: move all functions below here to view since they are view related and not template specific -use \phpOMS\System\File\FileUtils; -use \phpOMS\System\File\Local\File; - -$filePathFunction = function ($media, string $sub) : string -{ - if (is_file($media->getPath() . $sub) - && phpOMS\Utils\StringUtils::startsWith( - str_replace('\\', '/', realpath($media->getPath() . $sub)), - $media->getPath() - ) - ) { - return $media->getPath() . $sub; - } - - return $media->getPath(); -}; - -$dirPathFunction = function ($media, string $sub) : string -{ - if (is_dir($media->getPath() . $sub) - && phpOMS\Utils\StringUtils::startsWith( - str_replace('\\', '/', realpath($media->getPath() . $sub)), - $media->getPath() - ) - ) { - return $media->getPath() . $sub; - } - - return $media->getPath(); -}; - -$isCollectionFunction = function ($media, string $sub) : bool -{ - return ($media->getExtension() === 'collection' - && !is_file($media->getPath() . $sub)) - || (is_dir($media->getPath()) - && ($sub === null || is_dir($media->getPath() . $sub)) - ); -}; - -$lineContentFunction = function (string $path) : array -{ - $output = file_get_contents($path); - $output = str_replace(["\r\n", "\r"], "\n", $output); - - return explode("\n", $output); -}; - -$isImageFunction = function ($media, string $path) : bool -{ - return FileUtils::getExtensionType($media->getExtension()) === ExtensionType::IMAGE - || FileUtils::getExtensionType(File::extension($path)) === ExtensionType::IMAGE; -}; diff --git a/Views/MediaView.php b/Views/MediaView.php new file mode 100644 index 0000000..fa74759 --- /dev/null +++ b/Views/MediaView.php @@ -0,0 +1,85 @@ +getPath() . $sub) + && StringUtils::startsWith( + str_replace('\\', '/', realpath($media->getPath() . $sub)), + $media->getPath() + ) + ) { + return $media->getPath() . $sub; + } + + return $media->getPath(); + } + + protected function dirPathFunction(Media $media, string $sub) : string + { + if (is_dir($media->getPath() . $sub) + && StringUtils::startsWith( + str_replace('\\', '/', realpath($media->getPath() . $sub)), + $media->getPath() + ) + ) { + return $media->getPath() . $sub; + } + + return $media->getPath(); + } + + protected function isCollectionFunction(Media $media, string $sub) : bool + { + return ($media->getExtension() === 'collection' + && !is_file($media->getPath() . $sub)) + || (is_dir($media->getPath()) + && ($sub === null || is_dir($media->getPath() . $sub)) + ); + } + + protected function lineContentFunction(string $path) : array + { + $output = file_get_contents($path); + $output = str_replace(["\r\n", "\r"], "\n", $output); + + return explode("\n", $output); + } + + protected function isImageFunction(Media $media, string $path) : bool + { + return FileUtils::getExtensionType($media->getExtension()) === ExtensionType::IMAGE + || FileUtils::getExtensionType(File::extension($path)) === ExtensionType::IMAGE; + } +}