Skip to content

Commit

Permalink
[WIP] Display links to files
Browse files Browse the repository at this point in the history
Related to #17
  • Loading branch information
jbelien committed Aug 28, 2019
1 parent a039de3 commit 28cd5d7
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 19 deletions.
2 changes: 2 additions & 0 deletions config/autoload/templates.global.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
],
],
Expand Down
88 changes: 88 additions & 0 deletions src/App/Twig/FileExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

declare(strict_types=1);

namespace App\Twig;

use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

class FileExtension extends AbstractExtension
{
public function getFunctions(): array
{
return [
new TwigFunction('file', [$this, 'file'], ['is_safe' => ['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 = '<td';
$output .= ' class="text-nowrap"';
$output .= ' colspan="2"';
$output .= '>';

if (is_null($path)) {
$output .= ValueExtension::null();
} elseif (strlen($path) === 0) {
$output .= '';
} elseif (!file_exists($path)) {
$output .= self::notexists($path);
}

$output .= '</td>';
} else {
$output = '';

if ($preview === true) {
$output = '<td ';
$output .= ' class="text-nowrap"';
$output .= $download !== true ? ' colspan="2"' : '';
$output .= '>';
$output .= self::preview($path, true);
$output .= '</td>';
}

if ($download === true) {
$output .= '<td ';
$output .= ' class="text-nowrap"';
$output .= $preview !== true ? ' colspan="2"' : '';
$output .= '>';
$output .= self::download($path, $preview && $download ? false : true);
$output .= '</td>';
}
}

return $output;
}

private function notexists($path) : string
{
return '<span class="text-muted" title="File does not exists." style="cursor: help; font-style: italic;">'
.'<i class="fas fa-fw fa-exclamation-circle"></i>'
.' '.basename($path)
.'</span>';
}

private function preview($path, bool $displayFilename) : string
{
return '<a href="#" style="text-decoration: none;">'
.'<i class="far fa-fw fa-eye"></i> '
.($displayFilename ? ' '.basename($path) : '')
.'</a>';
}

private function download($path, bool $displayFilename) : string
{
return '<a href="#" style="text-decoration: none;">'
.'<i class="fas fa-fw fa-file-download"></i> '
.($displayFilename ? ' '.basename($path) : '')
.'</a>';
}
}
30 changes: 21 additions & 9 deletions src/App/Twig/ValueExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<td';
$output .= sprintf(' class="text-nowrap text-%s"', $textAlign);
$output .= sprintf(' data-datatype="%s"', (string) $datatype);
$output .= '>';

if (is_string($value) && filter_var($value, FILTER_VALIDATE_URL) !== false) {
return self::varcharLink($value);
if (is_null($value)) {
$output .= self::null();
} elseif ($datatype === 'boolean') {
$output .= self::boolean($value);
} elseif (is_string($value) && filter_var($value, FILTER_VALIDATE_URL) !== false) {
$output .= self::varcharLink($value);
} else {
$output .= (string) $value;
}

return (string) $value;
$output .= '</td>';

return $output;
}

private static function null(): string
public static function null(): string
{
return '<span class="text-muted font-italic">NULL</span>';
}
Expand Down
22 changes: 14 additions & 8 deletions templates/app/table/body.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@
</td>
{% endif %}

<td class="text-nowrap{{ column.datatype == 'integer' ? ' text-right' : column.datatype == 'boolean' ? ' text-center' }}"
data-datatype="{{ column.datatype }}">
{{ value(value, column.datatype) }}
</td>
{% 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 %}

Expand All @@ -29,10 +30,15 @@
{% set fcolumns = foreign.columns | filter(c => c.name in fselectColumns) %}

{% for fcolumn in fcolumns %}
<td
class="table-info text-nowrap{{ fcolumn.datatype == 'integer' ? ' text-right' : fcolumn.datatype == 'boolean' ? ' text-center' }}">
{{ value(attribute(record.properties, foreign.name ~ '.' ~ fcolumn.name), fcolumn.datatype) }}
</td>

{% 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 %}
Expand Down
22 changes: 20 additions & 2 deletions templates/app/table/head.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,23 @@
</tr>
<tr>
{% for column in columns %}

{% set columnName = table.name ~ '.' ~ column.name %}
<th scope="col" {{ thematic.column == column.name ? ' colspan="2"'}}>

{% 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 %}

<th scope="col" colspan="{{ colspan }}">
<a href="{{ path('table', {'config': configId, 'offset': 0}) }}?sort={{ columnName }}&amp;order={{ sort == columnName and order == 'asc' ? 'desc' : 'asc' }}"
class="d-flex justify-content-between align-items-center">
<span class="text-nowrap">
{{ thematic.column == column.name ? '<i class="fas fa-palette"></i>' }}
{{ column.isForeignKey == true ? '<i class="fas fa-link"></i>' }}
{{ column.config.preview == true or column.config.download == true ? '<i class="fas fa-file"></i>' }}
{{ column.name }}
</span>
{% if sort == columnName and order == 'asc' %}
Expand All @@ -37,11 +47,18 @@
{% set fcolumns = foreign.columns | filter(c => c.name in fselectColumns) %}

{% for fcolumn in fcolumns %}
<th scope="col">

{% set colspan = 1 %}
{% if fcolumn.config.preview == true or fcolumn.config.download == true %}
{% set colspan = 2 %}
{% endif %}

<th scope="col" colspan="{{ colspan }}">
{% set columnName = foreign.name ~ '.' ~ fcolumn.name %}
<a href="{{ path('table', {'config': configId, 'offset': 0}) }}?sort={{ columnName }}&amp;order={{ sort == columnName and order == 'asc' ? 'desc' : 'asc' }}"
class="d-flex justify-content-between align-items-center">
<span class="text-nowrap">
{{ fcolumn.config.preview == true or fcolumn.config.download == true ? '<i class="fas fa-file"></i>' }}
{{ fcolumn.name }}
</span>
{% if sort == columnName and order == 'asc' %}
Expand All @@ -53,6 +70,7 @@
{% endif %}
</a>
</th>

{% endfor %}

{% endfor %}
Expand Down

0 comments on commit 28cd5d7

Please sign in to comment.