Skip to content

Commit

Permalink
Merge pull request #2 from italolelis/master
Browse files Browse the repository at this point in the history
Novo suporte para templates e tags
  • Loading branch information
italolelis committed Jan 23, 2014
2 parents 9f22043 + ea05494 commit 17665e9
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 51 deletions.
8 changes: 8 additions & 0 deletions src/Umbrella/SimpleReport/Api/ITemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,12 @@ public function getParams();
public function addParam($param, $value);

public function getParam($param);

public function getTags();

public function setTags(array $tags);

public function getPath();

public function setPath($path);
}
4 changes: 2 additions & 2 deletions src/Umbrella/SimpleReport/BaseConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ public function getFieldTypeInstance($fieldTypeName, $rendererPrefix)

protected function createFieldTypeInstance($fieldTypeName, $rendererPrefix)
{
$classnameBase = "Umbrella\SimpleReport\Fields\\" . ucfirst(strtolower($fieldTypeName)) . 'Type';
$classnameConcrete = ucfirst(strtolower($rendererPrefix)) . $classnameBase;
$classnameBase = ucfirst(strtolower($fieldTypeName)) . 'Type';
$classnameConcrete = "Umbrella\SimpleReport\Fields\\" . ucfirst(strtolower($rendererPrefix)) . $classnameBase;
if (class_exists($classnameConcrete)) {
return new $classnameConcrete($this->option);
} elseif (class_exists($classnameBase)) {
Expand Down
25 changes: 25 additions & 0 deletions src/Umbrella/SimpleReport/BaseTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,15 @@
* Description of SimpleTemplate
*
* @author kelsocm
* @author Ítalo Lelis <[email protected]>
*/
class BaseTemplate implements ITemplate
{

private $fields;
private $params;
private $tags;
private $path;

public function __construct(FieldSet $fieldSet = null, array $params = array())
{
Expand Down Expand Up @@ -73,4 +76,26 @@ public function getParam($param)
return $this->params[$param];
}

public function getTags()
{
return $this->tags;
}

public function setTags(array $tags)
{
$this->tags = $tags;
return $this;
}

public function getPath()
{
return $this->path;
}

public function setPath($path)
{
$this->path = $path;
return $this;
}

}
2 changes: 1 addition & 1 deletion src/Umbrella/SimpleReport/ConfigurationLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function getRootDir()

protected function load()
{
$configFile = "{$this->getRootDir()}/config.ini";
$configFile = __DIR__ . "/../../../config.ini";
$ini_array = parse_ini_file($configFile);
$configurationClassName = $ini_array['simpletablereport.configurationClassName'];
$this->configuration = new $configurationClassName($ini_array);
Expand Down
70 changes: 36 additions & 34 deletions src/Umbrella/SimpleReport/Parser/TemplateParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,64 +20,66 @@ class TemplateParser
{

protected $tags = array();
protected $tlp = null;
protected $template = null;

public function getTemplate($key = null)
public function __construct(\Umbrella\SimpleReport\Api\ITemplate $template)
{
if ($key) {
return $this->tlp[$key];
} else {
return $this->tlp;
}
$this->template = $template;
}

public function setTemplate($arquivo)
public function setTemplate(\Umbrella\SimpleReport\Api\ITemplate $template)
{
$this->tlp = $arquivo;
$this->template = $template;
return $this;
}

public function findTemplate()
public function getTemplate()
{
if ($this->tlp) {
if (!file_exists($this->tlp)) {
throw new Exception('Arquivo de template não encontrado');
}

$arquivo = fopen($this->tlp, "r");
$html = "";

while (!feof($arquivo)) {
$html .= fgets($arquivo);
}
return $this->template;
}

fclose($arquivo);
public function getTags()
{
return $this->tags;
}

return $html;
} else {
throw new Exception('Arquivo de template não foi setado');
public function setTags($values)
{
foreach ($values as $key => $value) {
$this->addTag($key, $value);
}
return $this;
}

public function getTags($key = null)
public function addTag($key, $value)
{
if ($key) {
return $this->tags[$key];
} else {
return $this->tags;
$this->tags[$key] = $value;
return $this;
}

public function removeTag($key)
{
if (!isset($key)) {
return null;
}
unset($this->tags[$key]);
}

public function setTags($key, $value)
protected function findTemplate()
{
$this->tags[$key] = $value;
$file = $this->template->getPath();
if (!file_exists($file)) {
throw new Exception('Arquivo de template não encontrado');
}
return file_get_contents($file);
}

public function parse()
{
$html = $this->findTemplate();

foreach ($this->getTags() as $_tag => $value) {
$html = str_replace("{{" . $_tag . "}}", $value, $html);
foreach ($this->tags as $tag => $value) {
$html = str_replace("{{" . $tag . "}}", $value, $html);
}

return $html;
Expand Down
2 changes: 1 addition & 1 deletion src/Umbrella/SimpleReport/Renderer/HtmlRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ protected function renderTableBodyFields()
{
foreach ($this->template->getFields() as $fieldDescription) {
$this->doWriteTableBodyDataStart();
echo $this->getValue($this->datasource, $fieldDescription, 'HTML');
echo $this->getValue($this->datasource, $fieldDescription, '');
$this->doWriteTableBodyDataEnd();
}
}
Expand Down
72 changes: 59 additions & 13 deletions src/Umbrella/SimpleReport/Renderer/WkPdfRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,57 +32,103 @@ class WkPdfRenderer extends BaseRenderer
{

private $output;
private $imageQuality;
private $htmlRenderer;
private $parser;

public function __construct(IDatasource $datasource, ITemplate $template)
{
parent::__construct($datasource, $template);
$this->htmlRenderer = new HtmlRenderer($datasource, $template);
$this->parser = new TemplateParser();
$this->parser = new TemplateParser($template);
}

protected function initParams()
public function getOutput()
{
$this->output = $this->template->getParam('out');
$this->imageQuality = $this->template->getParam('imageQuality');
return $this->output;
}

public function setOutput($output)
{
$this->output = $output;
return $this;
}

public function render()
{
$this->initParams();
$htmlFile = $this->getHtmlPageContent();
$this->renderPdf($htmlFile);
return $this;
}

protected function getHtmlPageContent()
{
ob_start();
$this->htmlRenderer->render();
$page = ob_get_contents();
$filename = '/tmp/pdf/' . md5($page) . '.html';
$filename = '/tmp/' . microtime() . '.html';

$this->parser->setTemplate($this->template->getParam('template'));
$this->parser->setTags("content", $page);
$this->parser->setTags(array_merge(array(
"content" => $page,
"date" => $this->createDate(),
), $this->template->getTags())
);
$content = $this->parser->parse();

file_put_contents($filename, $content);
ob_end_clean();

return 'file://' . $filename;
return $filename;
}

protected function renderPdf($htmlContent)
protected function createDate()
{
$date = new \DateTime();
return $date->format('d/m/Y');
}

protected function renderPdf($htmlFile)
{
\wkhtmltox_convert('pdf', array(
'out' => $this->output,
'imageQuality' => $this->imageQuality
'imageQuality' => '75'
), array(
array(
'page' => $htmlContent
'page' => 'file://' . $htmlFile
))
);

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

/**
* Seta as permissões nos arquivos de html e pdf
* @param string $htmlFile
* @param string $pdfFile
*/
protected function setPermissions($htmlFile, $pdfFile)
{
chmod($htmlFile, 0777);
chmod($pdfFile, 0777);
}

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

/**
* Envia os headersde pdf para o browser
* @param boolean $attachment Se o pdf será exibido no browser ou baixado pelo usuário
*/
public function send($attachment = true)
{
$type = $attachment ? 'attachment' : 'inline';
header('Content-type: application/pdf');
header('Content-Disposition: ' . $type . '; filename="' . md5($this->output) . '.pdf"');
readfile($this->output);

$this->unlinkFile($this->output);
}

}

0 comments on commit 17665e9

Please sign in to comment.