-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
144 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
|
||
namespace Grido\Components\Exports; | ||
|
||
use Grido\Components\Columns\Column; | ||
use Nette\Http\IResponse; | ||
|
||
class PdfExport extends BaseExport | ||
{ | ||
|
||
/** | ||
* @return void | ||
*/ | ||
protected function printData() | ||
{ | ||
$columns = $this->grid[Column::ID]->getComponents(); | ||
$header = []; | ||
$headerItems = $this->header ? $this->header : $columns; | ||
foreach ($headerItems as $column) { | ||
$header[] = $this->header ? $column : $column->getLabel(); | ||
} | ||
|
||
$datasource = $this->grid->getData(FALSE, FALSE, FALSE); | ||
$iterations = ceil($datasource->getCount() / $this->fetchLimit); | ||
$formattedData = []; | ||
$sums = []; | ||
for ($i = 0; $i < $iterations; $i++) { | ||
$datasource->limit($i * $this->fetchLimit, $this->fetchLimit); | ||
$data = $this->customData ? call_user_func_array($this->customData, [$datasource]) : $datasource->getData(); | ||
|
||
foreach ($data as $items) { | ||
$row = []; | ||
|
||
$columns = $this->customData ? $items : $columns; | ||
|
||
foreach ($columns as $columnName => $column) { | ||
$row[$columnName] = $this->customData ? $column : $column->renderExport($items); | ||
if ($column instanceof \Ciki\Grido\ColumnNumber && $column->getCalculateSum()) { | ||
if (!isset($sums[$columnName])) { | ||
$sums[$columnName] = 0; | ||
} | ||
// dump($row[$columnName], $column->getValueForSumCalculation($items), $column->getValue($items)); | ||
// $sums[$columnName] += $column->getValueForSumCalculation($items); | ||
$sums[$columnName] += $row[$columnName] * 100; // => cents | ||
} | ||
} | ||
$formattedData[] = $row; | ||
} | ||
} | ||
|
||
// Tip: In template to make a new page use <pagebreak> | ||
$template = $this->getPresenter()->getTemplate(); | ||
$template->setFile(__DIR__ . '/pdf_export.latte'); | ||
$template->header = $header; | ||
$template->data = \Nette\Utils\ArrayHash::from($formattedData); | ||
$template->sums = \Nette\Utils\ArrayHash::from($sums); | ||
$template->columns = $columns; | ||
// dump($header, $row, $data, $columns, $template->data, $sums);die; | ||
|
||
$pdf = new \Joseki\Application\Responses\PdfResponse($template); | ||
|
||
// optional | ||
// $pdf->documentTitle = date("Y-m-d H:i") . " PDF export"; // creates filename | ||
$pdf->pageFormat = "A4-L"; // wide format | ||
$pdf->getMPDF()->setFooter("|© www.kartamesta.sk|"); | ||
// $pdf->outputDestination = $pdf::OUTPUT_DOWNLOAD; | ||
// $pdf->save = $pdf::OUTPUT_DOWNLOAD; | ||
echo $pdf->__toString(); | ||
} | ||
|
||
|
||
/** | ||
* @param IResponse $httpResponse | ||
* @param string $label | ||
*/ | ||
protected function setHttpHeaders(IResponse $httpResponse, $label) | ||
{ | ||
$encoding = 'utf-8'; | ||
$httpResponse->setHeader('Content-Description', 'File Transfer'); | ||
$httpResponse->setHeader('Content-Transfer-Encoding', 'binary'); | ||
$httpResponse->setHeader('Content-Encoding', $encoding); | ||
$httpResponse->setHeader('Content-Type', "application/pdf; charset=$encoding"); | ||
$httpResponse->setHeader('Content-Disposition', "attachment; filename=\"$label.pdf\""); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
{** | ||
* @param array $header public header names | ||
* @param Nette\Utils\ArrayHash $data | ||
* @param Nette\Utils\ArrayHash $sums | ||
* @param RecursiveComponentIterator $columns | ||
*} | ||
|
||
{*extends TPL_INC_DIR . '/@styledBareLayout.latte'*} | ||
|
||
{*block #body*} | ||
|
||
<style> | ||
body { | ||
font-size:90%; | ||
} | ||
h2 { | ||
font-size:18px; | ||
} | ||
/* original have line separator specified via unsupported rule, thus we are shrinking padding here */ | ||
table.table tbody td, | ||
table.table thead th { | ||
padding: 6px; | ||
} | ||
</style> | ||
|
||
|
||
<table class="table"> | ||
<thead> | ||
<tr> | ||
<th n:foreach="$header as $h">{$h}</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<tr n:foreach="$data as $v"> | ||
<td n:foreach="$v as $v2">{$v2}</td> | ||
</tr> | ||
<tr n:if="!empty($sums)"> | ||
<td n:foreach="$columns as $column"> | ||
{ifset $sums[$column->name]} | ||
∑ {$column->renderSum($sums[$column->name] / 100)} | ||
{/ifset} | ||
</td> | ||
</tr> | ||
</tbody> | ||
</table> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters