Commit acbe6fc5 authored by Tino Goratsch's avatar Tino Goratsch

- removed the DateRangeColumnRenderer and incorporated its logic into the DateColumnRenderer

- added 1 new unit test for the DateColumnRenderer
parent 3430a0a4
......@@ -54,9 +54,19 @@ class DateColumnRenderer extends AbstractColumnRenderer
/**
* @inheritdoc
*/
protected function getDbValueIfExists(array $dbResultRow, $field)
protected function getValue(array $column, array $dbResultRow)
{
return isset($dbResultRow[$field]) ? $this->dateRangeHelper->formatTimeRange($dbResultRow[$field]) : null;
$field = $this->getFirstDbField($column);
$value = $this->getDbValueIfExists($dbResultRow, $field);
if ($value === null) {
$value = $this->getDefaultValue($column);
} else {
$fields = $this->getDbFields($column);
$value = $this->dateRangeHelper->formatTimeRange($value, $this->getDbValueIfExists($dbResultRow, next($fields)));
}
return $value;
}
/**
......
<?php
namespace ACP3\Core\Helpers\DataGrid\ColumnRenderer;
use ACP3\Core\Date;
use ACP3\Core\Helpers\Formatter\DateRange;
/**
* Class DateRangeColumnRenderer
* @package ACP3\Core\Helpers\DataGrid\ColumnRenderer
*/
class DateRangeColumnRenderer extends AbstractColumnRenderer
{
/**
* @var \ACP3\Core\Date
*/
protected $date;
/**
* @var \ACP3\Core\Helpers\Formatter\DateRange
*/
protected $dateRangeHelper;
/**
* DateRangeColumnRenderer constructor.
*
* @param \ACP3\Core\Date $date
* @param \ACP3\Core\Helpers\Formatter\DateRange $dateRangeHelper
*/
public function __construct(
Date $date,
DateRange $dateRangeHelper
)
{
$this->date = $date;
$this->dateRangeHelper = $dateRangeHelper;
}
/**
* @inheritdoc
*/
public function fetchDataAndRenderColumn(array $column, array $dbResultRow)
{
$dateStart = $this->getValue($column, $dbResultRow);
$dateEnd = $dbResultRow[$column['fields'][1]];
$column['attribute'] = [
'data-order' => $this->date->format($dateStart, 'U')
];
return $this->render(
$column,
$this->dateRangeHelper->formatTimeRange($dateStart, $dateEnd)
);
}
/**
* @return string
*/
public function getType()
{
return 'date_range';
}
}
\ No newline at end of file
......@@ -44,8 +44,8 @@ class DateRange
*/
public function formatTimeRange($start, $end = '', $format = 'long')
{
if ($end === '' || $start >= $end) {
if ($end === '') {
if (empty($end) || $start >= $end) {
if (empty($end)) {
$title = $this->date->format($start, $format);
} else {
$title = sprintf($this->lang->t('system', 'date_published_since'), $this->date->format($start, $format));
......
......@@ -10,12 +10,6 @@ services:
tags:
- { name: core.helpers.data_grid.column_renderer }
core.helpers.data_grid.column_renderer.date_range_column_renderer:
class: ACP3\Core\Helpers\DataGrid\ColumnRenderer\DateRangeColumnRenderer
arguments: [@core.date, @core.helpers.formatter.dateRange]
tags:
- { name: core.helpers.data_grid.column_renderer }
core.helpers.data_grid.column_renderer.header_column_renderer:
class: ACP3\Core\Helpers\DataGrid\ColumnRenderer\HeaderColumnRenderer
arguments: [@core.helpers.formatter.mark_entries]
......
......@@ -325,7 +325,7 @@ class Index extends Core\Modules\AdminController
$dataGrid
->addColumn([
'label' => $this->lang->t('system', 'publication_period'),
'type' => 'date_range',
'type' => 'date',
'fields' => ['start', 'end']
], 30)
->addColumn([
......
......@@ -219,7 +219,7 @@ class Index extends Core\Modules\AdminController
$dataGrid
->addColumn([
'label' => $this->lang->t('system', 'publication_period'),
'type' => 'date_range',
'type' => 'date',
'fields' => ['start', 'end'],
'default_sort' => true
], 50)
......
......@@ -234,7 +234,7 @@ class Index extends Core\Modules\AdminController
$dataGrid
->addColumn([
'label' => $this->lang->t('system', 'publication_period'),
'type' => 'date_range',
'type' => 'date',
'fields' => ['start', 'end'],
'default_sort' => true
], 30)
......
......@@ -189,7 +189,7 @@ class Index extends Core\Modules\AdminController
$dataGrid
->addColumn([
'label' => $this->lang->t('system', 'publication_period'),
'type' => 'date_range',
'type' => 'date',
'fields' => ['start', 'end'],
'default_sort' => true
], 30)
......
......@@ -179,7 +179,7 @@ class Index extends Core\Modules\AdminController
$dataGrid
->addColumn([
'label' => $this->lang->t('system', 'publication_period'),
'type' => 'date_range',
'type' => 'date',
'fields' => ['start', 'end'],
'default_sort' => true
], 30)
......
......@@ -74,4 +74,18 @@ class DateColumnRendererTest extends AbstractColumnRendererTest
$expected = '<td data-order="1445372421"><time datetime="2015-10-20T22:20:21+02:00" title="2015-10-20 22:20">2015-10-20 22:20</time></td>';
$this->compareResults($expected);
}
public function testValidFieldWithDateRange()
{
$this->columnData = array_merge($this->columnData, [
'fields' => ['start', 'end']
]);
$this->dbData = [
'start' => '2015-10-20 20:20:21',
'end' => '2015-10-25 20:20:21'
];
$expected = '<td data-order="1445372421"><time datetime="2015-10-20T22:20:21+02:00">2015-10-20 22:20</time>&ndash;<time datetime="2015-10-25T21:20:21+01:00">2015-10-25 21:20</time></td>';
$this->compareResults($expected);
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment