Skip to content

Commit

Permalink
Added GridView class to separate rendering and configuration logic in…
Browse files Browse the repository at this point in the history
… grid. Changed twig extension.
  • Loading branch information
pfilsx committed Oct 22, 2019
1 parent a6b6ed2 commit 09fb2f3
Show file tree
Hide file tree
Showing 11 changed files with 332 additions and 169 deletions.
2 changes: 1 addition & 1 deletion src/Grid/Columns/AbstractColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

abstract class AbstractColumn
{
protected $attributes;
protected $attributes = [];

protected $value = null;

Expand Down
22 changes: 19 additions & 3 deletions src/Grid/DataGrid.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@
* Class DataGrid
* @package Pfilsx\DataGrid\Grid
* @internal
* TODO translation_domain in columns and builder
*/
class DataGrid
class DataGrid implements DataGridInterface
{
/**
* @var Template
Expand Down Expand Up @@ -142,28 +141,45 @@ public function getTemplate()
* @throws \Twig\Error\RuntimeError
* @throws \Twig\Error\SyntaxError
*/
public function setTemplate(string $path)
protected function setTemplate(string $path)
{
$this->template = $this->container->getTwig()->loadTemplate($path);
foreach ($this->builder->getColumns() as $column){
$column->setTemplate($this->template);
}
}

/**
* @internal
* @return string
*/
public function getNoDataMessage()
{
return $this->container->getTranslator() !== null
? $this->container->getTranslator()->trans($this->configuration->getNoDataMessage(), [], $this->configuration->getTranslationDomain())
: ucfirst($this->configuration->getNoDataMessage());
}

/**
* @internal
* @return bool
*/
public function hasPagination()
{
return $this->builder->hasPagination();
}

/**
* @internal
* @return array
*/
public function getPaginationOptions()
{
return $this->builder->getPager()->getPaginationOptions();
}

public function createView(): DataGridView
{
return new DataGridView($this, $this->container);
}
}
3 changes: 1 addition & 2 deletions src/Grid/DataGridFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@


use Pfilsx\DataGrid\Config\ConfigurationContainerInterface;
use Pfilsx\DataGrid\Config\ConfigurationInterface;
use InvalidArgumentException;
use Pfilsx\DataGrid\DataGridServiceContainer;
use Pfilsx\DataGrid\Grid\Providers\DataProvider;
Expand Down Expand Up @@ -52,7 +51,7 @@ public function __construct(DataGridServiceContainer $container, ConfigurationCo
}


public function createGrid(string $gridType, $dataProvider): DataGrid
public function createGrid(string $gridType, $dataProvider): DataGridInterface
{
if (!is_subclass_of($gridType, AbstractGridType::class)) {
throw new InvalidArgumentException('Expected subclass of ' . AbstractGridType::class);
Expand Down
2 changes: 1 addition & 1 deletion src/Grid/DataGridFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ interface DataGridFactoryInterface
{
public function __construct(DataGridServiceContainer $container, ConfigurationContainerInterface $configs);

public function createGrid(string $gridType, $dataProvider): DataGrid;
public function createGrid(string $gridType, $dataProvider): DataGridInterface;
}
17 changes: 17 additions & 0 deletions src/Grid/DataGridInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php


namespace Pfilsx\DataGrid\Grid;


use Pfilsx\DataGrid\Config\ConfigurationContainerInterface;
use Pfilsx\DataGrid\DataGridServiceContainer;

interface DataGridInterface
{
public function __construct(DataGridBuilderInterface $builder,
ConfigurationContainerInterface $defaultConfiguration,
DataGridServiceContainer $container);

public function createView(): DataGridView;
}
124 changes: 124 additions & 0 deletions src/Grid/DataGridView.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php


namespace Pfilsx\DataGrid\Grid;


use Pfilsx\DataGrid\DataGridServiceContainer;

class DataGridView
{
/**
* @var DataGrid
*/
private $grid;
/**
* @var DataGridServiceContainer
*/
private $container;

private $renderStarted = false;
private $headRendered = false;
private $filtersRendered = false;
private $bodyRendered = false;
private $renderEnded = false;


public function __construct(DataGrid $grid, DataGridServiceContainer $container)
{
$this->grid = $grid;
$this->container = $container;
}

public function renderGridStart($attributes = []): ?string
{
if (!$this->renderStarted) {
$this->renderStarted = true;
return $this->grid->getTemplate()->renderBlock('grid_start', [
'attr' => $attributes,
'data_grid' => $this->grid,
'request' => $this->container->getRequest()->getCurrentRequest()
]);
}
return null;
}

public function renderGridHead(): ?string
{
if (!$this->headRendered) {
$this->headRendered = true;
return $this->grid->getTemplate()->renderBlock('grid_head', [
'data_grid' => $this->grid,
'request' => $this->container->getRequest()->getCurrentRequest()
]);
}
return null;
}

public function renderGridFilters(): ?string
{
if (!$this->filtersRendered) {
$this->filtersRendered = true;
return $this->grid->getTemplate()->renderBlock('grid_filters', [
'data_grid' => $this->grid,
'request' => $this->container->getRequest()->getCurrentRequest()
]);
}
return null;
}

public function renderGridBody(): ?string
{
if (!$this->bodyRendered) {
$this->bodyRendered = true;
return $this->grid->getTemplate()->renderBlock('grid_body', [
'data_grid' => $this->grid,
'request' => $this->container->getRequest()->getCurrentRequest()
]);
}
return null;
}

public function renderGridEnd(): ?string
{
if ($this->renderStarted && !$this->renderEnded) {
$this->renderEnded = true;
return $this->grid->getTemplate()->renderBlock('grid_end', [
'data_grid' => $this->grid,
'request' => $this->container->getRequest()->getCurrentRequest()
]);
}
return null;
}

public function renderGridPagination(): ?string
{
return $this->grid->getTemplate()->renderBlock('grid_pagination', [
'data_grid' => $this->grid,
'request' => $this->container->getRequest()->getCurrentRequest()
]);
}

public function renderGridWidget(): ?string
{
return implode("\n", [
'<thead>',
$this->renderGridHead(),
$this->renderGridFilters(),
'</thead>',
'<tbody>',
$this->renderGridBody(),
'</tbody>'
]);
}

public function renderGridView($attributes = []): ?string
{
return implode("\n", [
$this->renderGridStart($attributes),
$this->renderGridWidget(),
$this->renderGridEnd(),
$this->renderGridPagination()
]);
}
}
72 changes: 34 additions & 38 deletions src/Resources/views/grid.blocks.html.twig
Original file line number Diff line number Diff line change
@@ -1,28 +1,13 @@
{% block grid_table %}
{% block grid_start %}
{% set attr = attr|merge({'class': attr.class|default('')~' data_grid_table'}) %}
<table {{ block('attributes') }}>
<thead>
{{ block('grid_head') }}
</thead>
<tbody>
{{ block('grid_body') }}
</tbody>
</table>
{% if data_grid.hasPagination and data_grid.paginationOptions.pages|length > 1 %}
{{ block('pagination') }}
{% endif %}
{% endblock %}

{% block grid_head %}
<tr>
{% for column in data_grid.columns %}
{% if column.isVisible %}
{% if column.hasAttributes %}
{% set attr = column.attributes %}
{% else %}
{% set attr = [] %}
{% endif %}

{% set attr = column.attributes|default({}) %}
<th {{ block('attributes') }}>
{% if column.hasSort %}
{% if column.getSort is same as('ASC') %}
Expand All @@ -43,11 +28,15 @@
{% endif %}
{% endfor %}
</tr>
{% endblock %}

{% block grid_filters %}
{% if data_grid.hasFilters %}
<tr>
{% for column in data_grid.columns %}
{% if column.isVisible %}
<th>{{ column.filterContent|raw }}</th>
{% set attr = column.attributes|default({}) %}
<th {{ block('attributes') }}>{{ column.filterContent|raw }}</th>
{% endif %}
{% endfor %}
</tr>
Expand All @@ -59,11 +48,7 @@
<tr>
{% for column in data_grid.columns %}
{% if column.isVisible %}
{% if column.hasAttributes %}
{% set attr = column.attributes %}
{% else %}
{% set attr = [] %}
{% endif %}
{% set attr = column.attributes|default({}) %}
<td {{ block('attributes') }}>{{ column.getCellContent(entity)|raw }}</td>
{% endif %}
{% endfor %}
Expand All @@ -75,6 +60,32 @@
{% endfor %}
{% endblock %}

{% block grid_end %}
</table>
{% endblock %}

{% block grid_pagination %}
{% if data_grid.hasPagination and data_grid.paginationOptions.pages|length > 1 %}
{% set pages = data_grid.paginationOptions.pages %}
{% set current_page = data_grid.paginationOptions.currentPage %}
{{ block('paginator') }}
{% endif %}
{% endblock %}

{% block paginator %}
{% for i in pages %}
{% if i == current_page %}
{{ i }}
{% elseif i is same as(null) %}
...
{% else %}
<a href="{{ path(request.get('_route'),
request.query|merge({'data_grid': request.query.get('data_grid')|default({})|merge({'page': i})})) }}"
class="data_grid_page" data-page="{{ i }}">{{ i }}</a>
{% endif %}
{% endfor %}
{% endblock %}

{% block attributes -%}

{%- for attrname, attrvalue in attr -%}
Expand Down Expand Up @@ -107,21 +118,6 @@
<img src="{{ asset(src) }}" height="{{ height }}" width="{{ width }}" alt="{{ alt }}"/>
{% endblock %}

{% block pagination %}
{% set pages = data_grid.paginationOptions.pages %}
{% set current_page = data_grid.paginationOptions.currentPage %}
{% for i in pages %}
{% if i == current_page %}
{{ i }}
{% elseif i is same as(null) %}
...
{% else %}
<a href="{{ path(request.get('_route'),
request.query|merge({'data_grid': request.query.get('data_grid')|default({})|merge({'page': i})})) }}"
class="data_grid_page" data-page="{{ i }}">{{ i }}</a>
{% endif %}
{% endfor %}
{% endblock %}

{% block grid_filter %}
{% set attr = options|merge({class: (options.class|default('') ~ ' data_grid_filter')|trim}) %}
Expand Down
Loading

0 comments on commit 09fb2f3

Please sign in to comment.