Skip to content

Commit

Permalink
Merge pull request #44 from leantony/2.0
Browse files Browse the repository at this point in the history
Refine data exporting
  • Loading branch information
leantony authored May 22, 2018
2 parents 21596ce + 495c980 commit efde9c1
Show file tree
Hide file tree
Showing 24 changed files with 544 additions and 176 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,7 @@ vendor
node_modules
*.map

.env
.env

*.log
*.png
13 changes: 10 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
sudo: required
language: php
dist: trusty

php:
- 7.1
- 7.2

addons:
chrome: stable

before_script:
- set -e
- cp .env.travis .env
- composer self-update
- composer install --no-interaction
- travis_retry composer install --no-interaction
- google-chrome-stable --headless --disable-gpu --remote-debugging-port=9222 http://localhost &

script:
- vendor/bin/phpunit tests/Unit/PackageTestCase.php
- vendor/bin/phpunit tests/Unit/PackageTestCases.php
# - vendor/bin/phpunit tests/Browser/BrowserTestCases.php
1 change: 1 addition & 0 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ This package allows rendering of data via a tabular format (grid). The grid uses
+ Jquery pjax (quickly view data without having to reload the page). Note that you'll need to [create the middleware on your own](https://gist.github.com/JeffreyWay/8526696b6f29201c4e33)
+ [Date picker](https://github.com/dangrossman/bootstrap-daterangepicker.git) (optional - for the single date & date range filters)

> **Note that from version 2.0.2 onwards, you'll need the package [barryvdh/laravel-dompdf](https://github.com/barryvdh/laravel-dompdf) to export data from the grid as PDF**
# Getting started

Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@
},
"suggest": {
"maatwebsite/excel": "For exporting grid data as excel (~3.0)",
"spatie/laravel-pjax": "For pjax support on the server side"
"barryvdh/laravel-dompdf": "For exporting grid data as PDF (^0.8.2)"
},
"require-dev": {
"phpunit/phpunit": "~7.0",
"orchestra/testbench": "~3.0",
"orchestra/testbench-dusk": "^3.5"
"orchestra/testbench-dusk": "^3.5",
"phpunit/php-code-coverage": "^6.0"
},
"autoload": {
"psr-4": {
Expand Down
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Feature">
<directory suffix="Test.php">./tests/Feature</directory>
<testsuite name="browser">
<directory suffix=".php">./tests/Browser</directory>
</testsuite>

<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
<testsuite name="unit">
<directory suffix=".php">./tests/Unit</directory>
</testsuite>
</testsuites>
<filter>
Expand Down
69 changes: 25 additions & 44 deletions src/Export/ExcelExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,87 +6,68 @@

namespace Leantony\Grid\Export;

use Excel;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromQuery;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\WithTitle;

class ExcelExport implements FromQuery, WithTitle, WithHeadings, WithMapping
class ExcelExport implements FromCollection, WithTitle, WithHeadings
{
use Exportable;

/**
* @var Builder
* The data
*
* @var Collection
*/
private $query;
protected $data;

/**
* @var array
* The title of the report
*
* @var string
*/
private $pinch;
private $title;

/**
* The columns to export
*
* @var array
*/
private $columns;

/**
* @var string
*/
private $title;
/**
* @var callable
*/
private $mapperFunction;

/**
* The headings to export
*
* @var array
*/
private $headings;

/**
* ExcelExport constructor.
* @param Builder $builder
* @param array $pinch
* @param array $columns
* @param array $headings
* @param string $title
* @param callable $mapperFunction
* DefaultExport constructor.
* @param array $params
*/
public function __construct($builder, array $pinch, array $columns, array $headings, string $title, callable $mapperFunction)
{
$this->query = $builder;
$this->pinch = $pinch;
$this->columns = $columns;
$this->headings = $headings;
$this->title = $title;
$this->mapperFunction = $mapperFunction;
}

public function query()
public function __construct(array $params)
{
return $this->query->select($this->pinch);
$this->title = $params['title'];
$this->columns = $params['columns'];
$this->data = $params['data'];
$this->headings = $params['headings'];
}

public function map($data): array
public function collection()
{
return call_user_func($this->mapperFunction, $data, $this->columns, false);
return $this->data;
}

/**
* @return string
*/
public function title(): string
{
return $this->title;
}

/**
* @return array
*/
public function headings(): array
{
return $this->headings;
Expand Down
34 changes: 34 additions & 0 deletions src/Export/PdfExport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
/**
* Copyright (c) 2018.
* @author Antony [leantony] Chacha
*/

namespace Leantony\Grid\Export;

use Illuminate\Support\Collection;

class PdfExport implements GridExportInterface
{
/**
* Export data from the grid
*
* @param Collection $data
* @param array $args
* @return mixed
* @throws \Throwable
*/
public function export($data, array $args)
{
$exportableColumns = $args['exportableColumns'];
$fileName = $args['fileName'];
$exportView = $args['exportView'];

if(class_exists(\Barryvdh\DomPDF\ServiceProvider::class)) {
$pdf = app('dompdf.wrapper');
$pdf->loadHTML(view($exportView, ['columns' => $exportableColumns, 'data' => $data])->render());
return $pdf->download($fileName . '.pdf');
}
throw new \InvalidArgumentException("PDF library not found. Please install 'barryvdh/laravel-dompdf' to handle PDF exports");
}
}
10 changes: 5 additions & 5 deletions src/HasGridConfigurations.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ trait HasGridConfigurations
*
* @var int
*/
private $maxExportRows;
private $gridExportQueryChunkSize;

/**
* @var string
Expand Down Expand Up @@ -321,12 +321,12 @@ public function getGridExportView(): string
return $this->exportView;
}

public function getGridMaxExportRows(): int
public function getGridExportQueryChunkSize(): int
{
if ($this->maxExportRows === null) {
$this->maxExportRows = config('grid.export.max_rows', 5000);
if ($this->gridExportQueryChunkSize === null) {
$this->gridExportQueryChunkSize = config('grid.export.chunk_size', 300);
}
return $this->maxExportRows;
return $this->gridExportQueryChunkSize;
}

public function getGridColumnsToSkipOnGeneration(): array
Expand Down
48 changes: 31 additions & 17 deletions src/Listeners/DataExportHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@
use Illuminate\Http\Response;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Leantony\Grid\Export\DefaultExporter;
use Leantony\Grid\Export\ExcelExport;
use Leantony\Grid\Export\ExcelExporter;
use Leantony\Grid\Export\HtmlExport;
use Leantony\Grid\Export\JsonExport;
use Leantony\Grid\Export\PdfExport;
use Leantony\Grid\GridInterface;
use Leantony\Grid\GridResources;

Expand Down Expand Up @@ -97,19 +100,28 @@ public function exportAs($type = 'xlsx')
{
switch ($type) {
case 'pdf':
{
return (new PdfExport())->export($this->getExportData(), [
'exportableColumns' => $this->getExportableColumns()[1],
'fileName' => $this->getFileNameForExport(),
'exportView' => $this->getGridExportView(),
]);
}
case 'csv':
case 'xlsx':
{
list($pinch, $columns) = $this->getExportableColumns();
$columns = $this->getExportableColumns()[1];
// headings
$headings = $columns->map(function ($col) {
return $col->name;
})->toArray();

$exporter = new ExcelExport($this->getQuery(), $pinch, $columns->toArray(), $headings, 'report', function ($data, $columns) {
return call_user_func([$this, 'dataFormatter'], $data, $columns, false);
});
return $exporter->download($this->getFileNameForExport() . '.' . $type);
return (new ExcelExport([
'title' => $this->getGrid()->getName(),
'columns' => $columns,
'data' => $this->getExportData(),
'headings' => $headings,
]))->download($this->getFileNameForExport() . '.' . $type);
}
case 'html':
{
Expand Down Expand Up @@ -188,44 +200,46 @@ public function getFileNameForExport(): string
*/
public function getExportData(array $params = []): Collection
{
$useUnformattedKeys = $params['doNotFormatKeys'] ?? false;
$doNotFormatKeys = $params['doNotFormatKeys'] ?? false;

list($pinch, $columns) = $this->getExportableColumns();

// works on the underlying query instance
$values = $this->getQuery()->take($this->getGrid()->getGridMaxExportRows())->get();
$records = new Collection();

// customize the results
$columns = $columns->toArray();
$this->getQuery()->select($pinch)->chunk($this->getGridExportQueryChunkSize(), function ($items) use ($columns, $params, $doNotFormatKeys, $records) {
// customize the results
$columns = $columns->toArray();

$data = $values->map(function ($value) use ($columns, $params, $useUnformattedKeys) {
return call_user_func([$this, 'dataFormatter'], $value, $columns, $useUnformattedKeys);
$data = $items->map(function ($value) use ($columns, $params, $doNotFormatKeys) {
return call_user_func([$this, 'dataFormatter'], $value, $columns, $doNotFormatKeys);
});
$records->push($data);
});

return $data;
return $records[0];
}

/**
* Format data for export
*
* @param mixed $item
* @param array $columns
* @param boolean $useUnformattedKeys
* @param boolean $doNotFormatKeys
* @return array
*/
protected function dataFormatter($item, array $columns, bool $useUnformattedKeys): array
protected function dataFormatter($item, array $columns, bool $doNotFormatKeys): array
{
$data = [];
foreach ($columns as $column) {
// render as per requested on each column
// `processColumns()` would have already taken care of processing the callbacks
// so here, we only pass the required arguments
if (is_callable($column->data)) {
$key = $useUnformattedKeys ? $column->key : $column->name;
$key = $doNotFormatKeys ? $column->key : $column->name;
$value = call_user_func($column->data, $item, $column->key);
array_push($data, [$key => $value]);
} else {
$key = $useUnformattedKeys ? $column->key : $column->name;
$key = $doNotFormatKeys ? $column->key : $column->name;
$value = $item->{$column->key};
array_push($data, [$key => $value]);
}
Expand Down
4 changes: 2 additions & 2 deletions src/resources/config/grid.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@
],

/**
* Max allowed export rows
* Export chunk size
*/
'max_rows' => 5000
'chunk_size' => 200
],

/**
Expand Down
Loading

0 comments on commit efde9c1

Please sign in to comment.