Skip to content

Commit

Permalink
htmlizer numberFormat
Browse files Browse the repository at this point in the history
  • Loading branch information
yurikuzn committed Sep 13, 2017
1 parent 4e1df42 commit f8523cf
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
30 changes: 29 additions & 1 deletion application/Espo/Core/Htmlizer/Htmlizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ protected function getDataFromEntity(Entity $entity, $skipLinks = false)
$v = [];
}
foreach ($v as $k => $w) {
$keyRaw = $k . '_RAW';
$v[$keyRaw] = $v[$k];
$v[$k] = $this->format($v[$k]);
}
$newList[] = $v;
Expand All @@ -134,6 +136,8 @@ protected function getDataFromEntity(Entity $entity, $skipLinks = false)
$data[$field] = get_object_vars($value);
}
foreach ($data[$field] as $k => $w) {
$keyRaw = $k . '_RAW';
$data[$field][$keyRaw] = $data[$field][$k];
$data[$field][$k] = $this->format($data[$field][$k]);
}
}
Expand All @@ -142,7 +146,9 @@ protected function getDataFromEntity(Entity $entity, $skipLinks = false)
}

if (array_key_exists($field, $data)) {
$data[$field] = $this->format($data[$field]);
$keyRaw = $field . '_RAW';
$data[$keyRaw] = $data[$field];
$data[$field] = $this->format($data[$field]);
}
}

Expand Down Expand Up @@ -171,12 +177,34 @@ protected function getDataFromEntity(Entity $entity, $skipLinks = false)
public function render(Entity $entity, $template, $id = null, $additionalData = array(), $skipLinks = false)
{
$code = \LightnCandy::compile($template, [
'flags' => \LightnCandy::FLAG_HANDLEBARSJS,
'helpers' => [
'file' => function ($context, $options) {
if (count($context) && $context[0]) {
$id = $context[0];
return "?entryPoint=attachment&id=" . $id;
}
},
'numberFormat' => function ($context, $options) {
if ($context && isset($context[0])) {
$number = $context[0];

$decimals = 0;
$decimalPoint = '.';
$thousandsSeparator = ',';

if (isset($options['decimals'])) {
$decimals = $options['decimals'];
}
if (isset($options['decimalPoint'])) {
$decimalPoint = $options['decimalPoint'];
}
if (isset($options['thousandsSeparator'])) {
$thousandsSeparator = $options['thousandsSeparator'];
}
return number_format($number, $decimals, $decimalPoint, $thousandsSeparator);
}
return '';
}
]
]);
Expand Down
29 changes: 29 additions & 0 deletions tests/unit/Espo/Core/Htmlizer/HtmlizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,35 @@ public function testRender()
$html = $this->htmlizer->render($entity, $template);
$this->assertEquals('3,000', $html);

$template = "{{float_RAW}}";
$entity->set('float', 10000.50);
$html = $this->htmlizer->render($entity, $template);
$this->assertEquals('10000.5', $html);

$template = "{{numberFormat float_RAW}}";
$entity->set('float', 10000.60);
$html = $this->htmlizer->render($entity, $template);
$this->assertEquals('10,001', $html);

$template = "{{numberFormat float_RAW decimals=2}}";
$entity->set('float', 10000.601);
$html = $this->htmlizer->render($entity, $template);
$this->assertEquals('10,000.60', $html);

$template = "{{numberFormat float_RAW decimals=0}}";
$entity->set('float', 10000.1);
$html = $this->htmlizer->render($entity, $template);
$this->assertEquals('10,000', $html);

$template = "{{numberFormat float_RAW decimals=2 decimalPoint='.' thousandsSeparator=' '}}";
$entity->set('float', 10000.60);
$html = $this->htmlizer->render($entity, $template);
$this->assertEquals('10 000.60', $html);

$template = "{{file name}}";
$entity->set('name', '1');
$html = $this->htmlizer->render($entity, $template);
$this->assertEquals('?entryPoint=attachment&id=1', $html);
}
}

0 comments on commit f8523cf

Please sign in to comment.