From 4c0c3e3ee68edb5e1a45b3078e1464bbb0a73b4a Mon Sep 17 00:00:00 2001 From: Arthur LORENT Date: Sun, 5 Jan 2020 21:52:51 +0100 Subject: [PATCH] Issue #34 (#35) Fixes #34 --- composer.json | 2 +- config/laravel-table.php | 55 +++++++++++++-------- src/Traits/TableColumnsValidationChecks.php | 1 + tests/Unit/EmptyStatusTest.php | 2 + views/bootstrap/tbody.blade.php | 6 +-- 5 files changed, 41 insertions(+), 25 deletions(-) diff --git a/composer.json b/composer.json index c98d4dfa..47f6f6a1 100644 --- a/composer.json +++ b/composer.json @@ -33,7 +33,7 @@ }, "require-dev": { "mockery/mockery": "^1.0", - "nunomaduro/larastan": "^0.4", + "nunomaduro/larastan": "^0.4.3||^0.5", "orchestra/testbench": "~3.8.0||^4.0", "php-coveralls/php-coveralls": "^2.1", "phpmd/phpmd": "^2.7", diff --git a/config/laravel-table.php b/config/laravel-table.php index 478486cc..cffc2771 100644 --- a/config/laravel-table.php +++ b/config/laravel-table.php @@ -2,41 +2,54 @@ return [ + /* + * Default classes for each table parts. + */ 'classes' => [ 'container' => ['table-responsive'], - 'table' => ['table-striped', 'table-hover'], - 'tr' => [], - 'th' => ['align-middle'], - 'td' => ['align-middle'], - 'results' => ['table-dark', 'font-weight-bold'], - 'disabled' => ['table-danger', 'disabled'], + 'table' => ['table-striped', 'table-hover'], + 'tr' => [], + 'th' => ['align-middle'], + 'td' => ['align-middle'], + 'results' => ['table-dark', 'font-weight-bold'], + 'disabled' => ['table-danger', 'disabled'], ], + /* + * Table action icons are defined here. + */ 'icon' => [ 'rowsNumber' => '', - 'sort' => '', - 'sortAsc' => '', - 'sortDesc' => '', - 'search' => '', - 'validate' => '', - 'cancel' => '', - 'create' => '', - 'edit' => '', - 'destroy' => '', - 'show' => '', + 'sort' => '', + 'sortAsc' => '', + 'sortDesc' => '', + 'search' => '', + 'validate' => '', + 'info' => '', + 'cancel' => '', + 'create' => '', + 'edit' => '', + 'destroy' => '', + 'show' => '', ], + /* + * Default table values + */ 'value' => [ - 'rowsNumber' => 20, + 'rowsNumber' => 20, 'rowsNumberSelectionActivation' => true, ], + /* + * Default template paths for each table parts. + */ 'template' => [ - 'table' => 'bootstrap.table', - 'thead' => 'bootstrap.thead', - 'tbody' => 'bootstrap.tbody', + 'table' => 'bootstrap.table', + 'thead' => 'bootstrap.thead', + 'tbody' => 'bootstrap.tbody', 'results' => 'bootstrap.results', - 'tfoot' => 'bootstrap.tfoot', + 'tfoot' => 'bootstrap.tfoot', ], ]; diff --git a/src/Traits/TableColumnsValidationChecks.php b/src/Traits/TableColumnsValidationChecks.php index 30867a63..be977b50 100644 --- a/src/Traits/TableColumnsValidationChecks.php +++ b/src/Traits/TableColumnsValidationChecks.php @@ -135,6 +135,7 @@ protected function tableData(Column $column, Builder $query): array $table = $column->databaseSearchedTable ?: $column->databaseDefaultTable; if ($column->databaseSearchedTable) { $fromSqlStatement = last(explode(' from ', (string) $query->toSql())); + $aliases = []; preg_match_all('/["`]([a-zA-Z0-9_]*)["`] as ["`]([a-zA-Z0-9_]*)["`]/', $fromSqlStatement, $aliases); if (! empty(array_filter($aliases))) { $position = array_keys(Arr::where(array_shift($aliases), function ($alias) use ($table) { diff --git a/tests/Unit/EmptyStatusTest.php b/tests/Unit/EmptyStatusTest.php index e3c13e7b..51aaf6e9 100644 --- a/tests/Unit/EmptyStatusTest.php +++ b/tests/Unit/EmptyStatusTest.php @@ -15,6 +15,7 @@ public function testEmptyListHtml() $table->column('name'); $table->render(); $html = view('laravel-table::' . $table->tbodyComponentPath, compact('table'))->render(); + $this->assertStringContainsString(config('laravel-table.icon.info'), $html); $this->assertStringContainsString(__('laravel-table::laravel-table.emptyTable'), $html); } @@ -27,6 +28,7 @@ public function testFilledListHtml() $table->column('email'); $table->render(); $html = view('laravel-table::' . $table->tbodyComponentPath, compact('table'))->render(); + $this->assertStringNotContainsString(config('laravel-table.icon.info'), $html); $this->assertStringNotContainsString(__('laravel-table::laravel-table.emptyTable'), $html); } } diff --git a/views/bootstrap/tbody.blade.php b/views/bootstrap/tbody.blade.php index c75de60e..f2410380 100644 --- a/views/bootstrap/tbody.blade.php +++ b/views/bootstrap/tbody.blade.php @@ -3,7 +3,7 @@ trClasses) }}> tdClasses, 'text-center', 'p-3') }}{{ htmlAttributes($table->columnsCount() > 1 ? ['colspan' => $table->columnsCount()] : null) }} scope="row"> - + {!! config('laravel-table.icon.info') !!} @lang('laravel-table::laravel-table.emptyTable') @@ -16,7 +16,7 @@ $value = $model->{$column->databaseDefaultColumn}; $customValue = $column->valueClosure ? ($column->valueClosure)($model, $column) : null; $html = $column->htmlClosure ? ($column->htmlClosure)($model, $column) : null; - $link = $column->url instanceof Closure ? ($column->url)($model, $column) : ($column->url !== true + $link = $column->url instanceof Closure ? ($column->url)($model, $column) : ($column->url !== true ? $column->url : ($customValue ? $customValue : $value)); $showIcon = $column->icon && (($customValue || $value) || $column->displayIconWhenNoValue); @@ -52,7 +52,7 @@ {{ Str::limit(strip_tags($value), $column->stringLimit) }} {{-- datetime format --}} @elseif($column->dateTimeFormat) - {{ $value + {{ $value ? \Carbon\Carbon::parse($value)->format($column->dateTimeFormat) : null }} {{-- basic value --}}