");
},
'ifEqual' => function () {
$args = func_get_args();
$context = $args[count($args) - 1];
if ($args[0] === $args[1]) {
return $context['fn']();
} else {
return $context['inverse'] ? $context['inverse']() : '';
}
},
'ifNotEqual' => function () {
$args = func_get_args();
$context = $args[count($args) - 1];
if ($args[0] !== $args[1]) {
return $context['fn']();
} else {
return $context['inverse'] ? $context['inverse']() : '';
}
},
'ifInArray' => function () {
$args = func_get_args();
$context = $args[count($args) - 1];
$array = $args[1] ?? [];
if (in_array($args[0], $array)) {
return $context['fn']();
} else {
return $context['inverse'] ? $context['inverse']() : '';
}
},
'ifMultipleOf' => function () {
$args = func_get_args();
$context = $args[count($args) - 1];
if ($args[0] % $args[1] === 0) {
return $context['fn']();
} else {
return $context['inverse'] ? $context['inverse']() : '';
}
},
'tableTag' => function () {
$args = func_get_args();
$context = $args[count($args) - 1];
$border = $context['hash']['border'] ?? '0.5pt';
$cellpadding = $context['hash']['cellpadding'] ?? '2';
$width = $context['hash']['width'] ?? null;
$attributesPart = "";
if ($width) {
$attributesPart .= " width=\"{$width}\"";
}
return
new LightnCandy\SafeString("") .
$context['fn']() .
new LightnCandy\SafeString("
");
},
'tdTag' => function () {
$args = func_get_args();
$context = $args[count($args) - 1];
$align = strtolower($context['hash']['align'] ?? 'left');
if (!in_array($align, ['left', 'right', 'center'])) {
$align = 'left';
}
$width = $context['hash']['width'] ?? null;
$attributesPart = "align=\"{$align}\"";
if ($width) {
$attributesPart .= " width=\"{$width}\"";
}
return
new LightnCandy\SafeString("") .
$context['fn']() .
new LightnCandy\SafeString(" | ");
},
'trTag' => function () {
$args = func_get_args();
$context = $args[count($args) - 1];
return
new LightnCandy\SafeString("") .
$context['fn']() .
new LightnCandy\SafeString("
");
},
'checkboxTag' => function () {
$args = func_get_args();
$context = $args[count($args) - 1];
if (count($args) < 2) {
return null;
}
$color = $context['hash']['color'] ?? '#000';
$option = $context['hash']['option'] ?? null;
if (is_null($option)) {
return null;
}
$option = strval($option);
$list = $args[0] ?? [];
if (!is_array($list)) {
return null;
}
$css = "font-family: zapfdingbats; color: {$color}";
if (in_array($option, $list)) {
$html = '';
} else {
$html = '';
}
return new LightnCandy\SafeString($html);
},
];
if ($this->metadata) {
$additionalHelpers = $this->metadata->get(['app', 'templateHelpers']) ?? [];
$helpers = array_merge($helpers, $additionalHelpers);
}
return $helpers;
}
/**
* Generate an HTML for entity by a given template.
*
* @param $cacheId @deprecated Skip or specify NULL.
* @param $additionalData Data will be passed to the template.
* @param $skipLinks Do not process related records.
*/
public function render(
Entity $entity, string $template, ?string $cacheId = null, ?array $additionalData = null, bool $skipLinks = false
) : string {
$template = str_replace('getHelpers();
$code = LightnCandy::compile($template, [
'flags' => LightnCandy::FLAG_HANDLEBARSJS | LightnCandy::FLAG_ERROR_EXCEPTION,
'helpers' => $this->getHelpers(),
]);
$renderer = LightnCandy::prepare($code);
$data = $this->getDataFromEntity($entity, $skipLinks, 0, $template);
if (!array_key_exists('today', $data)) {
$data['today'] = $this->dateTime->getTodayString();
$data['today_RAW'] = date('Y-m-d');
}
if (!array_key_exists('now', $data)) {
$data['now'] = $this->dateTime->getNowString();
$data['now_RAW'] = date('Y-m-d H:i:s');
}
$additionalData = $additionalData ?? [];
foreach ($additionalData as $k => $value) {
$data[$k] = $value;
}
$data['__injectableFactory'] = $this->injectableFactory;
$data['__config'] = $this->config;
$data['__dateTime'] = $this->dateTime;
$data['__metadata'] = $this->metadata;
$data['__entityManager'] = $this->entityManager;
$data['__language'] = $this->language;
$data['__serviceFactory'] = $this->serviceFactory;
$data['__entityType'] = $entity->getEntityType();
$html = $renderer($data);
$html = str_replace('?entryPoint=attachment&', '?entryPoint=attachment&', $html);
if ($this->entityManager) {
$html = preg_replace_callback('/\?entryPoint=attachment\&id=([A-Za-z0-9]*)/', function ($matches) {
$id = $matches[1];
$attachment = $this->entityManager->getEntity('Attachment', $id);
if ($attachment) {
$filePath = $this->entityManager->getRepository('Attachment')->getFilePath($attachment);
return $filePath;
}
}, $html);
}
return $html;
}
protected function getFieldType(string $entityType, string $field)
{
if (!$this->metadata) {
return null;
}
return $this->metadata->get(['entityDefs', $entityType, 'fields', $field, 'type']);
}
}