Skip to content

Commit

Permalink
Blank date fix (#265)
Browse files Browse the repository at this point in the history
* Blank date fix

* fix cs

* increase requirement

* cs-fixer
  • Loading branch information
ThaDafinser authored Feb 18, 2017
1 parent 51957ba commit 1259e0b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 38 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"require-dev": {
"phpunit/phpunit": "^5.6.1",
"friendsofphp/php-cs-fixer": "^1.11",
"doctrine/doctrine-orm-module": "^1.1.0",
"doctrine/doctrine-orm-module": "^1.1.1",

"zendframework/zend-console": "~2.5",
"zendframework/zend-db": "~2.5",
Expand Down
80 changes: 43 additions & 37 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 @@ -36,7 +37,7 @@ public function isHtml()

public function execute()
{
$options = $this->getOptions();
$options = $this->getOptions();
$optionsExport = $options['settings']['export'];

$optionsRenderer = $this->getOptionsRenderer();
Expand All @@ -49,8 +50,8 @@ public function execute()
$sheet->setTitle($this->translate($optionsRenderer['sheetName']));

if (true === $optionsRenderer['displayTitle']) {
$sheet->setCellValue('A'.$optionsRenderer['rowTitle'], $this->getTitle());
$sheet->getStyle('A'.$optionsRenderer['rowTitle'])
$sheet->setCellValue('A' . $optionsRenderer['rowTitle'], $this->getTitle());
$sheet->getStyle('A' . $optionsRenderer['rowTitle'])
->getFont()
->setSize(15);
}
Expand All @@ -69,7 +70,7 @@ public function execute()
* Header
*/
$xColumn = 0;
$yRow = $optionsRenderer['startRowData'];
$yRow = $optionsRenderer['startRowData'];
foreach ($this->getColumnsToExport() as $col) {
/* @var $column Column\AbstractColumn */
$sheet->setCellValueByColumnAndRow($xColumn, $yRow, $this->translate($col->getLabel()));
Expand All @@ -95,7 +96,7 @@ public function execute()

/* @var $column Column\AbstractColumn */
$currentColumn = PHPExcel_Cell::stringFromColumnIndex($xColumn);
$cell = $sheet->getCell($currentColumn.$yRow);
$cell = $sheet->getCell($currentColumn . $yRow);

switch (get_class($col->getType())) {

Expand All @@ -109,29 +110,34 @@ public function execute()

if (! $value instanceof \DateTime && is_scalar($value)) {
$value = \DateTime::createFromFormat($dateType->getSourceDateTimeFormat(), $value);
$value->setTimezone(new \DateTimeZone($dateType->getSourceTimezone()));
if ($value instanceof \DateTime) {
$value->setTimezone(new \DateTimeZone($dateType->getSourceTimezone()));
}
}

$value->setTimezone(new \DateTimeZone($dateType->getOutputTimezone()));
$cell->setValue(\PHPExcel_Shared_Date::PHPToExcel($value));
if ($value instanceof \DateTime) {
// only apply this if we have a date object (else leave it blank)
$value->setTimezone(new \DateTimeZone($dateType->getOutputTimezone()));
$cell->setValue(\PHPExcel_Shared_Date::PHPToExcel($value));

if ($dateType->getOutputPattern()) {
$outputPattern = $dateType->getOutputPattern();
} else {
$outputPattern = \PHPExcel_Style_NumberFormat::FORMAT_DATE_DATETIME;
}
if ($dateType->getOutputPattern()) {
$outputPattern = $dateType->getOutputPattern();
} else {
$outputPattern = \PHPExcel_Style_NumberFormat::FORMAT_DATE_DATETIME;
}

$cell->getStyle()
->getNumberFormat()
->setFormatCode($outputPattern);
$cell->getStyle()
->getNumberFormat()
->setFormatCode($outputPattern);
}
break;

default:
$cell->setValueExplicit($value, PHPExcel_Cell_DataType::TYPE_STRING);
break;
}

$columnStyle = $sheet->getStyle($currentColumn.$yRow);
$columnStyle = $sheet->getStyle($currentColumn . $yRow);
$columnStyle->getAlignment()->setWrapText(true);

/*
Expand Down Expand Up @@ -159,7 +165,7 @@ public function execute()

case Column\Style\BackgroundColor::class:
$columnStyle->getFill()->applyFromArray([
'type' => \PHPExcel_Style_Fill::FILL_SOLID,
'type' => \PHPExcel_Style_Fill::FILL_SOLID,
'color' => [
'rgb' => $style->getRgbHexString(),
],
Expand Down Expand Up @@ -196,7 +202,7 @@ public function execute()
break;

default:
throw new \Exception('Not defined yet: "'.get_class($style).'"');
throw new \Exception('Not defined yet: "' . get_class($style) . '"');
break;
}
}
Expand All @@ -216,9 +222,9 @@ public function execute()
// Letzte Zeile merken

// Autofilter + Freeze
$sheet->setAutoFilter('A'.$optionsRenderer['startRowData'].':'.$highest['column'].$highest['row']);
$sheet->setAutoFilter('A' . $optionsRenderer['startRowData'] . ':' . $highest['column'] . $highest['row']);
$freezeRow = $optionsRenderer['startRowData'] + 1;
$sheet->freezePane('A'.$freezeRow);
$sheet->freezePane('A' . $freezeRow);

// repeat the data header for each page!
$sheet->getPageSetup()->setRowsToRepeatAtTopByStartAndEnd($optionsRenderer['startRowData'], $optionsRenderer['startRowData']);
Expand All @@ -238,17 +244,17 @@ public function execute()
],
],
'fill' => [
'type' => PHPExcel_Style_Fill::FILL_SOLID,
'type' => PHPExcel_Style_Fill::FILL_SOLID,
'startcolor' => [
'argb' => PHPExcel_Style_Color::COLOR_YELLOW,
],
],
];
$range = 'A'.$optionsRenderer['startRowData'].':'.$highest['column'].$optionsRenderer['startRowData'];
$range = 'A' . $optionsRenderer['startRowData'] . ':' . $highest['column'] . $optionsRenderer['startRowData'];
$sheet->getStyle($range)->applyFromArray($style);

// print borders
$range = 'A'.$freezeRow.':'.$highest['column'].$highest['row'];
$range = 'A' . $freezeRow . ':' . $highest['column'] . $highest['row'];
$sheet->getStyle($range)->applyFromArray([
'borders' => [
'allborders' => [
Expand All @@ -260,18 +266,18 @@ public function execute()
/*
* Save the file
*/
$path = $optionsExport['path'];
$saveFilename = date('Y-m-d_H-i-s').$this->getCacheId().'.xlsx';
$path = $optionsExport['path'];
$saveFilename = date('Y-m-d_H-i-s') . $this->getCacheId() . '.xlsx';

$excelWriter = new \PHPExcel_Writer_Excel2007($phpExcel);
$excelWriter->setPreCalculateFormulas(false);
$excelWriter->save($path.'/'.$saveFilename);
$excelWriter->save($path . '/' . $saveFilename);

/*
* Send the response stream
*/
$response = new ResponseStream();
$response->setStream(fopen($path.'/'.$saveFilename, 'r'));
$response->setStream(fopen($path . '/' . $saveFilename, 'r'));

$headers = new Headers();
$headers->addHeaders([
Expand All @@ -280,11 +286,11 @@ public function execute()
'application/octet-stream',
'application/download',
],
'Content-Length' => filesize($path.'/'.$saveFilename),
'Content-Disposition' => 'attachment;filename='.$this->getFilename().'.xlsx',
'Cache-Control' => 'must-revalidate',
'Pragma' => 'no-cache',
'Expires' => 'Thu, 1 Jan 1970 00:00:00 GMT',
'Content-Length' => filesize($path . '/' . $saveFilename),
'Content-Disposition' => 'attachment;filename=' . $this->getFilename() . '.xlsx',
'Cache-Control' => 'must-revalidate',
'Pragma' => 'no-cache',
'Expires' => 'Thu, 1 Jan 1970 00:00:00 GMT',
]);

$response->setHeaders($headers);
Expand Down Expand Up @@ -335,7 +341,7 @@ protected function setPrinting(PHPExcel $phpExcel)
/*
* Printing setup
*/
$papersize = $optionsRenderer['papersize'];
$papersize = $optionsRenderer['papersize'];
$orientation = $optionsRenderer['orientation'];
foreach ($phpExcel->getAllSheets() as $sheet) {
/* @var $sheet \PHPExcel_Worksheet */
Expand Down Expand Up @@ -381,9 +387,9 @@ protected function setPrinting(PHPExcel $phpExcel)
*/
protected function setHeaderFooter(\PHPExcel_Worksheet $sheet)
{
$textRight = $this->translate('Page').' &P / &N';
$textRight = $this->translate('Page') . ' &P / &N';

$sheet->getHeaderFooter()->setOddHeader('&L&16&G '.$this->translate($this->getTitle()));
$sheet->getHeaderFooter()->setOddFooter('&R'.$textRight);
$sheet->getHeaderFooter()->setOddHeader('&L&16&G ' . $this->translate($this->getTitle()));
$sheet->getHeaderFooter()->setOddFooter('&R' . $textRight);
}
}

0 comments on commit 1259e0b

Please sign in to comment.