Skip to content

Commit

Permalink
Merge pull request #226 from kokspflanze/bugfix/translator_null
Browse files Browse the repository at this point in the history
translator optional
  • Loading branch information
ThaDafinser committed Feb 7, 2016
2 parents d692df4 + 96e96ec commit 4e2bc66
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 19 deletions.
12 changes: 12 additions & 0 deletions src/ZfcDatagrid/Renderer/AbstractRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,15 @@ public function getTranslator()
return $this->translator;
}

/**
* @param $string
* @return string
*/
public function translate($string)
{
return $this->getTranslator() ? $this->getTranslator()->translate($string) : $string;
}

/**
* Set the title
*
Expand All @@ -455,6 +464,9 @@ public function getTitle()
return $this->title;
}

/**
* @param Cache\Storage\StorageInterface $cache
*/
public function setCache(Cache\Storage\StorageInterface $cache)
{
$this->cache = $cache;
Expand Down
3 changes: 2 additions & 1 deletion src/ZfcDatagrid/Renderer/Csv/Renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Render datagrid as CSV
*
*/

namespace ZfcDatagrid\Renderer\Csv;

use Zend\Http\Headers;
Expand Down Expand Up @@ -61,7 +62,7 @@ public function execute()
if (isset($optionsRenderer['header']) && true === $optionsRenderer['header']) {
$header = [];
foreach ($this->getColumnsToExport() as $col) {
$header[] = $this->getTranslator()->translate($col->getLabel());
$header[] = $this->translate($col->getLabel());
}
fputcsv($fp, $header, $delimiter, $enclosure);
}
Expand Down
2 changes: 1 addition & 1 deletion src/ZfcDatagrid/Renderer/JqGrid/Renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ public function getData()
foreach ($column->getActions() as $action) {
/* @var $action \ZfcDatagrid\Column\Action\AbstractAction */
if ($action->isDisplayed($row) === true) {
$action->setTitle($this->getTranslator()->translate($action->getTitle()));
$action->setTitle($this->translate($action->getTitle()));
$actions[] = $action->toHtml($row);
}
}
Expand Down
16 changes: 8 additions & 8 deletions src/ZfcDatagrid/Renderer/PHPExcel/Renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/**
* Output as an excel file
*/

namespace ZfcDatagrid\Renderer\PHPExcel;

use PHPExcel;
Expand Down Expand Up @@ -45,8 +46,7 @@ public function execute()
// Sheet 1
$phpExcel->setActiveSheetIndex(0);
$sheet = $phpExcel->getActiveSheet();
$sheet->setTitle($this->getTranslator()
->translate($optionsRenderer['sheetName']));
$sheet->setTitle($this->translate($optionsRenderer['sheetName']));

if (true === $optionsRenderer['displayTitle']) {
$sheet->setCellValue('A' . $optionsRenderer['rowTitle'], $this->getTitle());
Expand All @@ -72,8 +72,7 @@ public function execute()
$yRow = $optionsRenderer['startRowData'];
foreach ($this->getColumnsToExport() as $col) {
/* @var $column \ZfcDatagrid\Column\AbstractColumn */
$label = $this->getTranslator()->translate($col->getLabel());
$sheet->setCellValueByColumnAndRow($xColumn, $yRow, $label);
$sheet->setCellValueByColumnAndRow($xColumn, $yRow, $this->translate($col->getLabel()));

$sheet->getColumnDimension(PHPExcel_Cell::stringFromColumnIndex($xColumn))->setWidth($col->getWidth());

Expand Down Expand Up @@ -341,13 +340,14 @@ protected function setPrinting(PHPExcel $phpExcel)
$phpExcel->setActiveSheetIndex(0);
}

/**
* @param \PHPExcel_Worksheet $sheet
*/
protected function setHeaderFooter(\PHPExcel_Worksheet $sheet)
{
$translator = $this->getTranslator();

$textRight = $translator->translate('Page') . ' &P / &N';
$textRight = $this->translate('Page') . ' &P / &N';

$sheet->getHeaderFooter()->setOddHeader('&L&16&G ' . $translator->translate($this->getTitle()));
$sheet->getHeaderFooter()->setOddHeader('&L&16&G ' . $this->translate($this->getTitle()));
$sheet->getHeaderFooter()->setOddFooter('&R' . $textRight);
}
}
3 changes: 2 additions & 1 deletion src/ZfcDatagrid/Renderer/TCPDF/Renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/**
* Output as a PDF file
*/

namespace ZfcDatagrid\Renderer\TCPDF;

use TCPDF;
Expand Down Expand Up @@ -272,7 +273,7 @@ protected function printTableHeader()

$this->columnsPositionX[$col->getUniqueId()] = $x;

$label = $this->getTranslator()->translate($col->getLabel());
$label = $this->translate($col->getLabel());

// Do not wrap header labels, it will look very ugly, that's why max height is set to 7!
$pdf->MultiCell($col->getWidth(), 7, $label, 1, $this->getTextAlignment(), true, 2, $x, $y, true, 0, false, true, 7);
Expand Down
20 changes: 12 additions & 8 deletions src/ZfcDatagrid/Renderer/ZendTable/Renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,6 @@ public function execute()
private function getTable()
{
$paginator = $this->getPaginator();
$translator = $this->getTranslator();

$options = [
'columnWidths' => $this->getColumnWidths(),
Expand All @@ -246,10 +245,8 @@ private function getTable()
*/
$tableRow = new Table\Row();
foreach ($this->getColumnsToDisplay() as $column) {
$label = $column->getLabel();
if ($this->getTranslator() !== null) {
$label = $this->getTranslator()->translate($label);
}
$label = $this->translate($column->getLabel());

if (function_exists('mb_strtoupper')) {
$label = mb_strtoupper($label);
} else {
Expand Down Expand Up @@ -299,12 +296,19 @@ private function getTable()
*/
$tableRow = new Table\Row();

$footer = $translator->translate('Page') . ' ';
$footer .= $paginator->getCurrentPageNumber() . ' ' . $translator->translate('of') . ' ' . $paginator->count();
$footer = $this->translate('Page') . ' ';
$footer .= sprintf('%s %s %s', $paginator->getCurrentPageNumber(), $this->translate('of'), $paginator->count());

$footer .= ' / ';

$footer .= $translator->translate('Showing') . ' ' . $paginator->getCurrentItemCount() . ' ' . $translator->translate('of') . ' ' . $paginator->getTotalItemCount() . ' ' . $translator->translate('items');
$footer .= sprintf(
'%s %s %s %s %s',
$this->translate('Showing'),
$paginator->getCurrentItemCount(),
$this->translate('of'),
$paginator->getTotalItemCount(),
$this->translate('items')
);

$tableColumn = new Table\Column($footer);
$tableColumn->setColSpan(count($options['columnWidths']));
Expand Down
16 changes: 16 additions & 0 deletions tests/ZfcDatagridTest/Renderer/AbstractRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,22 @@ public function testTranslator()
$this->assertSame($translator, $renderer->getTranslator());
}

public function testTranslate()
{
/* @var $renderer \ZfcDatagrid\Renderer\AbstractRenderer */
$renderer = $this->getMockForAbstractClass('ZfcDatagrid\Renderer\AbstractRenderer');
$this->assertEquals('foobar', $renderer->translate('foobar'));

$translator = $this->getMock('Zend\I18n\Translator\Translator', ['translate'], [], '', false);
$translator->expects($this->any())
->method('translate')
->willReturn('barfoo');

$renderer->setTranslator($translator);

$this->assertEquals('barfoo', $renderer->translate('foobar'));
}

public function testTitle()
{
/* @var $renderer \ZfcDatagrid\Renderer\AbstractRenderer */
Expand Down

0 comments on commit 4e2bc66

Please sign in to comment.