Skip to content

Commit

Permalink
closes #1, #2, #3
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaneGlee committed May 12, 2019
1 parent f52bfba commit e0491f9
Show file tree
Hide file tree
Showing 11 changed files with 136 additions and 75 deletions.
7 changes: 7 additions & 0 deletions src/DataProviders/DataProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace FocalStrategy\ReportGenerator\DataProviders;

interface DataProvider
{
}
12 changes: 12 additions & 0 deletions src/DataProviders/PagedDataProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace FocalStrategy\ReportGenerator\DataProviders;

use FocalStrategy\ReportGenerator\DataProviders\DataProvider;

interface PagedDataProvider extends DataProvider
{
public function getPaged(int $start, int $length, array $ordering);

public function getTotal();
}
10 changes: 10 additions & 0 deletions src/DataProviders/SimpleDataProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace FocalStrategy\ReportGenerator\DataProviders;

use FocalStrategy\ReportGenerator\DataProviders\DataProvider;

interface SimpleDataProvider extends DataProvider
{
public function get();
}
93 changes: 21 additions & 72 deletions src/ReportGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,23 @@
namespace FocalStrategy\ReportGenerator;

use App;
use Factory;
use FocalStrategy\Actions\Core\ActionRenderType;
use FocalStrategy\Core\ValueInterface;
use FocalStrategy\DateRange\DateRange;
use FocalStrategy\Filter\FilterManager;
use FocalStrategy\ViewObjects\Transformer;
use FocalStrategy\ViewObjects\ViewObject;
use FocalStrategy\ReportGenerator\Aggregates\AggregationRow;
use FocalStrategy\ReportGenerator\Aggregates\SumAggregation;
use FocalStrategy\ReportGenerator\DataProviders\DataProvider;
use FocalStrategy\ReportGenerator\Formatters\Formatter;
use FocalStrategy\ReportGenerator\ReportType;
use FocalStrategy\ReportGenerator\Values\Value;
use FocalStrategy\Core\ValueInterface;
use Factory;
use FocalStrategy\ViewObjects\ViewObject;
use Illuminate\Support\Collection;

class ReportGenerator
{
protected $view_object_class_name;
protected $view_factory_method;

protected $provided_factory;
protected $provider;

protected $filter;
protected $range;
Expand All @@ -32,34 +29,17 @@ class ReportGenerator

protected $fields = [];
protected $aggregates = [];
protected $additional_data = [];

protected $settings = [
'aggregate'=> true,
'id_name'=> 'NO',
];

protected $actions = [];

public function __construct(string $view_object_class_name = null, string $view_factory_method = 'get')
{
$this->view_object_class_name = $view_object_class_name;
$this->view_factory_method = $view_factory_method;
$this->transformer = App::make(Transformer::class);
}

public function setProvidedFactory($factory)
{
$this->provided_factory = $factory;
}

public function setFilterManager(FilterManager $filter)
{
$this->filter = $filter;
}

public function setDateRange(DateRange $range)
public function __construct(DataProvider $provider)
{
$this->range = $range;
$this->provider = $provider;
}

public function setAsync(bool $async, string $async_route)
Expand All @@ -78,11 +58,6 @@ public function settings(array $settings)
$this->settings = array_merge($this->settings, $settings);
}

public function with($field, $value)
{
$this->additional_data[$field] = $value;
}

public function col(string $display_name, string $field_name)
{
$this->fields[$field_name] = new ReportColumn($field_name, $display_name);
Expand Down Expand Up @@ -125,8 +100,8 @@ public function generate()
];
}

$factory = $this->getFactory();
$results = $factory->{$this->view_factory_method}($this->filter, $this->range);
$provider = $this->getDataProvider();
$results = $provider->get();

if ($results === null) {
return [];
Expand All @@ -147,15 +122,15 @@ public function generate()
}
}

$totals = $this->getTotals($results, ReportType::HTML(), $factory);
$totals = $this->getTotals($results, ReportType::HTML(), $provider);
$results = $this->format($results, ReportType::HTML());

return [
'structure' => $this->fields,
'data' => $results,
'aggregate' => $totals,
// debug
'view_object_class_name' => $this->view_object_class_name
'provider' => get_class($this->provider)
];
}

Expand All @@ -181,22 +156,11 @@ public function async($data)
}
}

if (isset($data['search']) && isset($data['search']['value'])) {
if (!$this->filter->has('search_term')) {
$this->filter->addValue('search_term', $data['search']['value']);
}
}

$factory = $this->getFactory();

$method = 'paged';

$results = $factory->{$method}(
$provider = $this->getDataProvider();
$results = $provider->getPaged(
$data['start'],
$data['length'],
$ordering,
$this->filter,
$this->range
$ordering
);

foreach ($results as $row) {
Expand Down Expand Up @@ -265,7 +229,7 @@ public function async($data)
$process[] = $r;
}

$total = $factory->count($this->filter, $this->range);
$total = $provider->getTotal();

$response['data'] = $process;
$response['recordsTotal'] = $total;
Expand Down Expand Up @@ -322,7 +286,7 @@ private function format($results, ReportType $type)
return $results;
}

private function getTotals(Collection $results, ReportType $type, $factory) : array
private function getTotals(Collection $results, ReportType $type, $provider) : array
{
$totals = [];

Expand All @@ -334,7 +298,7 @@ private function getTotals(Collection $results, ReportType $type, $factory) : ar

foreach ($aggregates as $ag) {
if ($ag instanceof FactoryRequired) {
$ag->setFactory($factory);
$ag->setFactory($provider);
}
$totals[] = $ag->generate($this->fields, $results, $type);
}
Expand All @@ -344,9 +308,7 @@ private function getTotals(Collection $results, ReportType $type, $factory) : ar

public function toArray()
{
$results = $this->getFactory();
$results = $results->{$this->view_factory_method}($this->filter, $this->range);

$results = $this->getDataProvider()->get();
$results = $this->format($results, ReportType::PLAIN());

$flat = [];
Expand All @@ -373,21 +335,8 @@ private function getSettingValue($key)
return $this->settings[$key];
}

public function getFactory()
public function getDataProvider()
{
$factory = null;
if ($this->provided_factory) {
$factory = $this->provided_factory;
} else {
$class = $this->view_object_class_name;
$factory = $class::getFactory();
$factory = App::make($factory);
}

if (method_exists($factory, 'setAdditionalData')) {
$factory->setAdditionalData($this->additional_data);
}

return $factory;
return $this->provider;
}
}
4 changes: 4 additions & 0 deletions src/ReportGeneratorServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,9 @@ public function boot()
$this->publishes([
__DIR__.'/resources/views' => resource_path('views/vendor/report_generator'),
]);

$this->publishes([
__DIR__.'/public' => public_path('vendor/focalstrategy/report_generator'),
], 'public');
}
}
5 changes: 5 additions & 0 deletions src/mix-manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"/public/css/actions.css": "/public/css/actions.css",
"/packages/Actions/src/public/js/actions.js": "/packages/Actions/src/public/js/actions.js",
"/public/js/report_generator.js": "/public/js/report_generator.js"
}
1 change: 1 addition & 0 deletions src/public/css/actions.css

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

1 change: 1 addition & 0 deletions src/public/js/report_generator.js

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

66 changes: 66 additions & 0 deletions src/resources/js/fs-async-tables.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
$(function() {
$('.async_table').each(function() {
var $self = $(this);

var columns = [];
var init_sort = 0;
$self.find('th').each(function(i) {
var $th = $(this);

var col = {name:'',title:'',orderable:true,searchable:true};
col.title = $th.text();
col.name = $th.data('name');
col.orderable = $th.data('orderable');
col.searchable = $th.data('searchable');

if($th.data('init-sort')) {
init_sort = i;
}

columns.push(col);
});

var dom = $('.table_search').length > 0 ? 'rtip' : 'lfrtip';

$self.DataTable({
processing: true,
serverSide: true,
ajax: {
'url':$self.data('route'),
'data':function ( d ) {
var data = $self.data('filters');
$.each(data,function(i,itm) {
d[i] = itm;
});
}
},
pageLength: 25,
lengthChange: false,
orderClasses: false,
scrollX: true,
stateSave: true,
stateDuration: -1,
order: [[init_sort, 'desc']],
"searching": true,
columns: columns,
dom:dom
});
});

$(document).on('filter_manager:change',function(e, filters, url) {
$('.async_table').each(function() {
$(this).data('filters',filters);

if($.fn.DataTable.isDataTable( $(this) ) ) {
var dt = $(this).DataTable();

if(filters.search_term
&& $('.dataTables_filter input',this).val() != filters.search_term) {
dt.search(filters.search_term);
}

dt.ajax.reload();
}
});
});
});
7 changes: 5 additions & 2 deletions src/resources/views/report.blade.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
@if (isset($view_object_class_name) && config('app.debug'))
<!-- Report Generator: VO = {{ $view_object_class_name }} -->
@if (isset($provider) && config('app.debug'))
<!-- Report Generator: VO = {{ $provider }} -->
@endif

<div class="card">
<h5 class="card-header">
{{ $title ?? 'No Title' }}
</h5>
@if(isset($prefix))
{!! $prefix !!}
@endif
<div class="table-responsive">
<table data-pageLength="{{ isset($page_length) ? $page_length : 25 }}" class="table table-bordered table-hover datatable {{ isset($checkbox_select) ? 'checkbox_select' : '' }}">
<thead>
Expand Down
5 changes: 4 additions & 1 deletion src/resources/views/report_async.blade.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
<div class="card">
<h5 class="card-header">
{{ $title }}
{{ $title ?? 'No Title' }}
</h5>
@if(isset($prefix))
{!! $prefix !!}
@endif
<table class="table async_table" data-route="{{ $async_route }}">
<thead>
<tr>
Expand Down

0 comments on commit e0491f9

Please sign in to comment.