From 28cd5d7cc19e00f73a50ddd7a1fd7f5e1a0d612c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonathan=20Beli=C3=ABn?= Date: Wed, 28 Aug 2019 17:12:55 +0200 Subject: [PATCH] [WIP] Display links to files Related to #17 --- config/autoload/templates.global.php | 2 + src/App/Twig/FileExtension.php | 88 ++++++++++++++++++++++++++++ src/App/Twig/ValueExtension.php | 30 +++++++--- templates/app/table/body.html.twig | 22 ++++--- templates/app/table/head.html.twig | 22 ++++++- 5 files changed, 145 insertions(+), 19 deletions(-) create mode 100644 src/App/Twig/FileExtension.php diff --git a/config/autoload/templates.global.php b/config/autoload/templates.global.php index 2bca220e..b9ea123c 100644 --- a/config/autoload/templates.global.php +++ b/config/autoload/templates.global.php @@ -5,11 +5,13 @@ return [ 'dependencies' => [ 'factories' => [ + App\Twig\FileExtension::class => Zend\ServiceManager\AbstractFactory\ReflectionBasedAbstractFactory::class, App\Twig\ValueExtension::class => Zend\ServiceManager\AbstractFactory\ReflectionBasedAbstractFactory::class, ], ], 'twig' => [ 'extensions' => [ + App\Twig\FileExtension::class, App\Twig\ValueExtension::class, ], ], diff --git a/src/App/Twig/FileExtension.php b/src/App/Twig/FileExtension.php new file mode 100644 index 00000000..35fb9b33 --- /dev/null +++ b/src/App/Twig/FileExtension.php @@ -0,0 +1,88 @@ + ['html']]), + ]; + } + + public function file($path, bool $preview, bool $download): string + { + if (!is_null($path)) { + $path = trim($path); + } + + if (is_null($path) || strlen($path) === 0 || !file_exists($path)) { + $output = '' + .'' + .' '.basename($path) + .''; + } + + private function preview($path, bool $displayFilename) : string + { + return '' + .' ' + .($displayFilename ? ' '.basename($path) : '') + .''; + } + + private function download($path, bool $displayFilename) : string + { + return '' + .' ' + .($displayFilename ? ' '.basename($path) : '') + .''; + } +} diff --git a/src/App/Twig/ValueExtension.php b/src/App/Twig/ValueExtension.php index c0b488e5..9e7e45e7 100644 --- a/src/App/Twig/ValueExtension.php +++ b/src/App/Twig/ValueExtension.php @@ -18,22 +18,34 @@ public function getFunctions(): array public function value($value, ?string $datatype = null): string { - if (is_null($value)) { - return self::null(); + $textAlign = 'left'; + if ($datatype === 'integer') { + $textAlign = 'right'; + } elseif ($datatype === 'boolean') { + $textAlign = 'center'; } - if ($datatype === 'boolean') { - return self::boolean($value); - } + $output = 'NULL'; } diff --git a/templates/app/table/body.html.twig b/templates/app/table/body.html.twig index 50847710..0c1baa55 100644 --- a/templates/app/table/body.html.twig +++ b/templates/app/table/body.html.twig @@ -16,10 +16,11 @@ {% endif %} - - {{ value(value, column.datatype) }} - + {% if column.config.preview == true or column.config.download == true %} + {{ file(value, column.config.preview, column.config.download) }} + {% else %} + {{ value(value, column.datatype) }} + {% endif %} {% endfor %} @@ -29,10 +30,15 @@ {% set fcolumns = foreign.columns | filter(c => c.name in fselectColumns) %} {% for fcolumn in fcolumns %} - - {{ value(attribute(record.properties, foreign.name ~ '.' ~ fcolumn.name), fcolumn.datatype) }} - + + {% set value = attribute(record.properties, foreign.name ~ '.' ~ fcolumn.name) %} + + {% if fcolumn.config.preview == true or fcolumn.config.download == true %} + {{ file(value, fcolumn.config.preview, fcolumn.config.download) }} + {% else %} + {{ value(value, fcolumn.datatype) }} + {% endif %} + {% endfor %} {% endfor %} diff --git a/templates/app/table/head.html.twig b/templates/app/table/head.html.twig index 40385231..54f4d00d 100644 --- a/templates/app/table/head.html.twig +++ b/templates/app/table/head.html.twig @@ -11,13 +11,23 @@ {% for column in columns %} + {% set columnName = table.name ~ '.' ~ column.name %} - + + {% set colspan = 1 %} + {% if thematic.column == column.name %} + {% set colspan = 2 %} + {% elseif column.config.preview == true or column.config.download == true %} + {% set colspan = 2 %} + {% endif %} + + {{ thematic.column == column.name ? '' }} {{ column.isForeignKey == true ? '' }} + {{ column.config.preview == true or column.config.download == true ? '' }} {{ column.name }} {% if sort == columnName and order == 'asc' %} @@ -37,11 +47,18 @@ {% set fcolumns = foreign.columns | filter(c => c.name in fselectColumns) %} {% for fcolumn in fcolumns %} - + + {% set colspan = 1 %} + {% if fcolumn.config.preview == true or fcolumn.config.download == true %} + {% set colspan = 2 %} + {% endif %} + + {% set columnName = foreign.name ~ '.' ~ fcolumn.name %} + {{ fcolumn.config.preview == true or fcolumn.config.download == true ? '' }} {{ fcolumn.name }} {% if sort == columnName and order == 'asc' %} @@ -53,6 +70,7 @@ {% endif %} + {% endfor %} {% endfor %}