Skip to content

Commit

Permalink
Nova funcionalidade de paginação no footer dos relatórios.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ítalo Lelis de Vietro committed Mar 26, 2014
1 parent 3a80b79 commit f2fc4c0
Show file tree
Hide file tree
Showing 6 changed files with 263 additions and 20 deletions.
3 changes: 2 additions & 1 deletion src/Umbrella/SimpleReport/BaseRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ protected function getValue(IDatasource $datasource, FieldDefinition $fieldDescr
return $fieldTypeInstance->render($unformattedFieldValue);
}

protected function getColumnCountTotal ($field)
protected function getColumnCountTotal($field)
{
$countAll = 0;
foreach ($this->datasource as $key => $fieldData) {
Expand All @@ -77,4 +77,5 @@ protected function getColumnCountTotal ($field)
}
return $countAll;
}

}
76 changes: 76 additions & 0 deletions src/Umbrella/SimpleReport/Renderer/GroupHtmlRenderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

/*
* Copyright 2014 kelsoncm <[email protected]>.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Umbrella\SimpleReport\Renderer;

/**
* @author Ítalo Lelis de Vietro <[email protected]>
*/
class GroupHtmlRenderer extends HtmlRenderer
{

protected function renderTableBodyRows()
{
for ($this->datasource->rewind(); $this->datasource->valid(); $this->datasource->next()) {
$this->doWriteTableBodyRowStart();
$this->renderTableBodyFields();
$this->doWriteTableBodyRowEnd();
}
}

protected function renderTableBodyFields()
{
foreach ($this->template->getFields() as $fieldDescription) {
$this->doWriteTableBodyDataStart();

echo $this->getValue($this->datasource, $fieldDescription, '');

$this->doWriteTableBodyDataEnd();
}
}

protected function renderTableBodyGroupedFields()
{
foreach ($this->template->getFields() as $fieldDescription) {
$this->doWriteTableBodyDataStart();
echo $this->getValue($this->datasource, $fieldDescription, '');
$this->doWriteTableBodyDataEnd();
}
}

protected function doWriteTableBodyRowStart()
{
echo $this->getOption('table.body.row.start');
}

protected function doWriteTableBodyDataStart()
{
echo $this->getOption('table.body.data.start');
}

protected function doWriteTableBodyDataEnd()
{
echo $this->getOption('table.body.data.end');
}

protected function doWriteTableBodyRowEnd()
{
echo $this->getOption('table.body.row.end');
}

}
166 changes: 152 additions & 14 deletions src/Umbrella/SimpleReport/Renderer/WkPdfRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@

namespace Umbrella\SimpleReport\Renderer;

use Umbrella\SimpleReport\Api\IRenderer;

use DateTime;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Umbrella\SimpleReport\Api\IDatasource;
use Umbrella\SimpleReport\Api\IRenderer;
use Umbrella\SimpleReport\Api\ITemplate;
use Umbrella\SimpleReport\BaseRenderer;
use Umbrella\SimpleReport\Parser\TemplateParser;

/**
Expand All @@ -44,7 +43,7 @@ class WkPdfRenderer implements IRenderer
* @var ITemplate
*/
private $template;

/**
* @var HtmlRenderer
*/
Expand All @@ -65,6 +64,31 @@ class WkPdfRenderer implements IRenderer
*/
private $length = 0;

/**
* @var boolean
*/
private $useFooter = false;

/**
* @var string
*/
private $footerPathTemplate;

/**
* @var string
*/
private $footerPath;

/**
* @var string
*/
private $footerHtmlUrl;

/**
* @var array
*/
private $footerText = array();

/**
* Inicializa uma nova instancia da classe WkPdfRenderer
* @param IDatasource $datasource Uma instância de IDatasource
Expand All @@ -77,6 +101,61 @@ public function __construct(IDatasource $datasource, ITemplate $template, IHtmlR
$this->parser = new TemplateParser($template);
}

public function getUseFooter()
{
return $this->useFooter;
}

public function getFooterPathTemplate()
{
return $this->footerPathTemplate;
}

public function getFooterPath()
{
return $this->footerPath;
}

public function getFooterHtmlUrl()
{
return $this->footerHtmlUrl;
}

public function getFooterText()
{
return $this->footerText;
}

public function setUseFooter($useFooter)
{
$this->useFooter = $useFooter;
return $this;
}

public function setFooterPathTemplate($footerPathTemplate)
{
$this->footerPathTemplate = $footerPathTemplate;
return $this;
}

public function setFooterPath($footerPath)
{
$this->footerPath = $footerPath;
return $this;
}

public function setFooterHtmlUrl($footerHtmlUrl)
{
$this->footerHtmlUrl = $footerHtmlUrl;
return $this;
}

public function setFooterText($footerText)
{
$this->footerText = $footerText;
return $this;
}

/**
* Retorna o
* @return string
Expand Down Expand Up @@ -118,12 +197,12 @@ public function render()
flush();
});
} else {
$response = new \Symfony\Component\HttpFoundation\Response();
$response = new Response();
$this->renderPdf($htmlFile);
}

$d = $response->headers->makeDisposition(ResponseHeaderBag::DISPOSITION_INLINE, md5($this->output));
$response->headers->set('Content-Disposition', $d);
$disposition = $response->headers->makeDisposition(ResponseHeaderBag::DISPOSITION_INLINE, md5($this->output));
$response->headers->set('Content-Disposition', $disposition);
$response->headers->set('Content-type', 'application/pdf');
$response->headers->set('Cache-Control', 'max-age=0, must-revalidate');
$response->headers->set('Pragma', 'public');
Expand All @@ -140,7 +219,7 @@ protected function getHtmlPageContent()
$this->htmlRenderer->render();
$page = ob_get_contents();
$filename = '/tmp/' . microtime() . '.html';

$this->parser->setTags(array_merge(array(
"content" => $page,
"date" => $this->createDate(),
Expand All @@ -165,13 +244,15 @@ public function renderPdf($htmlFile)
'out' => $this->output,
'imageQuality' => '75'
), array(
array(
'page' => 'file://' . $htmlFile
))
);
array_merge(array(
'page' => "file://{$htmlFile}",
), $this->getFooter())
));

$this->setPermissions($htmlFile, $this->output);
$this->unlinkFile($htmlFile);
$this->unlinkFile($this->footerPath);

$this->length = readfile($this->output);
}

Expand All @@ -182,14 +263,71 @@ public function renderPdf($htmlFile)
*/
protected function setPermissions($htmlFile, $pdfFile)
{
var_dump($pdfFile);
chmod($htmlFile, 0777);
chmod($pdfFile, 0777);
}

public function unlinkFile($file)
{
unlink($file);
if (file_exists($file)) {
unlink($file);
}
}

/**
* adiciona a propriedade do footer
*/
private function getFooter()
{
if ($this->useFooter) {
$text = implode('<br />', $this->footerText);
fopen($this->footerPath, 'a');
chmod($this->footerPath, 0755);
$text = preg_replace(
array('#\$\{TEXTO\}#i', '#\$\{([^\}]+)\}#ie'), array($text, "'<span class=\"'.strtolower('$1').'\">'.strtolower('$1').'</span>'"), $this->appendScript(file_get_contents($this->footerPathTemplate))
);
file_put_contents($this->footerPath, $text);
return array('footer.htmlUrl' => $this->footerHtmlUrl);
}
return array();
}

/**
* Adiciona o script para gerar as variáveis dinamicas
*
* @param string $text
* @return mixed
*/
private function appendScript($text)
{
$script = '<script>function subst() {var vars={};var x=document.location.search.substring(1).split(\'&\');';
$script .= 'for(var i in x) {var z=x[i].split(\'=\',2);vars[z[0]] = unescape(z[1]);}var x=[\'frompage\',\'topage\',\'page\',';
$script .= '\'webpage\',\'section\',\'subsection\',\'subsubsection\']; for(var i in x) { var y = document.getElementsByClassName(x[i]);';
$script .= 'for(var j=0; j<y.length; ++j) y[j].textContent = vars[x[i]];}}</script></head><body onload="subst()">';
return preg_replace('#\<\/head\>([^\<]+|)\<body([^\>]+|)\>#im', $script, $text);
}

/**
* Atribui um template do footer diferente
*
* @param realpath $footerPathTemplate
*/
public function setFooterTemplate($footerPathTemplate)
{
$this->footerPathTemplate = $footerPathTemplate;
return $this;
}

/**
* Força a exibição do Footer
*
* @param boolean $useFooter
* @return Application_Pdf_WkPdf
*/
public function showFooter($useFooter = true)
{
$this->useFooter = $useFooter;
return $this;
}

}
1 change: 1 addition & 0 deletions test/footer.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!DOCTYPE html><html class="no-js" lang="pt-br"> <head> <meta charset="utf-8"> </head> <body> ${PAGE} - ${TOPAGE} </body></html>
Expand Down
35 changes: 31 additions & 4 deletions test/simpletablereport/SimpleWkPdfTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@
*/
require_once 'SimpleTest.php';

class SimpleWkPdfTest extends SimpleTest {
class SimpleWkPdfTest extends SimpleTest
{

/**
*
* @return array
*/
public function getData() {
public function getData()
{
return array
(
array('id' => 1, 'nome' => 'Kelson'),
Expand All @@ -47,7 +49,8 @@ public function getData() {
);
}

public function getFieldset() {
public function getFieldset()
{
$fieldSet = new FieldSet();
return $fieldSet
->addField('id', '#', FieldType::INTEGER)
Expand All @@ -62,7 +65,8 @@ public function getFieldset() {
/**
*
*/
public function testArrayDatasourceInitial() {
public function testArrayDatasourceInitial()
{
$fieldSet = $this->getFieldset();
$datasource = new ArrayDatasource($this->getData());

Expand All @@ -76,4 +80,27 @@ public function testArrayDatasourceInitial() {
$renderer->render();
}

/**
*
*/
public function testFooter()
{
$fieldSet = $this->getFieldset();
$datasource = new ArrayDatasource($this->getData());

$template = new BaseTemplate($fieldSet);
$template->addParam('imageQuality', 100);
$template->setPath('/var/www/simpletablereport/test/test.html');

$html = new \Umbrella\SimpleReport\Renderer\HtmlRenderer($datasource, $template);
$renderer = new WkPdfRenderer($datasource, $template, $html);
$renderer->setOutput('/tmp/teste-footer.pdf');

$renderer->setUseFooter(true)
->setFooterPathTemplate('/var/www/simpletablereport/test/footer.html')
->setFooterPath('/tmp/teste-footer')
->setFooterHtmlUrl('file:///var/www/simpletablereport/test/footer.html');
$renderer->render();
}

}
Loading

0 comments on commit f2fc4c0

Please sign in to comment.