barcode field

This commit is contained in:
Yuri Kuznetsov
2019-11-14 12:52:19 +02:00
parent b79bd72c8a
commit d770dd26e0
11 changed files with 319 additions and 66 deletions

View File

@@ -40,7 +40,7 @@ use Espo\Core\Utils\Language;
use Espo\Core\Utils\Metadata;
use Espo\ORM\EntityManager;
require('vendor/zordius/lightncandy/src/lightncandy.php');
use LightnCandy\LightnCandy as LightnCandy;
class Htmlizer
{
@@ -272,46 +272,38 @@ class Htmlizer
public function render(Entity $entity, $template, $id = null, $additionalData = [], $skipLinks = false)
{
$code = \LightnCandy::compile($template, [
'flags' => \LightnCandy::FLAG_HANDLEBARSJS,
$template = str_replace('<tcpdf ', '', $template);
$code = LightnCandy::compile($template, [
'flags' => LightnCandy::FLAG_HANDLEBARSJS | LightnCandy::FLAG_ERROR_EXCEPTION,
'helpers' => [
'file' => function ($context, $options) {
if (count($context) && $context[0]) {
$id = $context[0];
return "?entryPoint=attachment&id=" . $id;
}
'file' => function () {
$args = func_get_args();
$id = $args[0] ?? null;
if (!$id) return '';
return "?entryPoint=attachment&id=" . $id;
},
'numberFormat' => function ($context, $options) {
if ($context && isset($context[0])) {
$number = $context[0];
'var' => function () {
$args = func_get_args();
$c = $args[1] ?? [];
$key = $args[0] ?? null;
if (is_null($key)) return null;
return $c[$key] ?? null;
},
'numberFormat' => function () {
$args = func_get_args();
if (count($args) !== 2) return null;
$context = $args[count($args) - 1];
$number = $args[0] ?? null;
$decimals = 0;
$decimalPoint = '.';
$thousandsSeparator = ',';
if (is_null($number)) return '';
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 '';
$decimals = $context['hash']['decimals'] ?? 0;
$decimalPoint = $context['hash']['decimalPoint'] ?? '.';
$thousandsSeparator = $context['hash']['thousandsSeparator'] ?? ',';
return number_format($number, $decimals, $decimalPoint, $thousandsSeparator);
},
'var' => function ($context, $options) {
if ($context && isset($context[0]) && isset($context[1])) {
if (isset($context[1][$context[0]])) {
return $context[1][$context[0]];
}
}
return;
},
],
'hbhelpers' => [
'dateFormat' => function () {
$args = func_get_args();
if (count($args) !== 2) return null;
@@ -326,6 +318,54 @@ class Htmlizer
return $dateTime->convertSystemDate($dateValue, $format, $language);
},
'barcodeImage' => function () {
$args = func_get_args();
if (count($args) !== 2) return null;
$context = $args[count($args) - 1];
$value = $args[0];
$codeType = $context['hash']['type'] ?? 'CODE128';
$typeMap = [
"CODE128" => 'C128',
"CODE128A" => 'C128A',
"CODE128B" => 'C128B',
"CODE128C" => 'C128C',
"EAN13" => 'EAN13',
"EAN8" => 'EAN8',
"EAN5" => 'EAN5',
"EAN2" => 'EAN2',
"UPC" => 'UPCA',
"UPCE" => 'UPCE',
"ITF14" => 'I25',
"pharmacode" => 'PHARMA',
];
$params = [
$value,
$typeMap[$codeType] ?? null,
'', '',
$context['hash']['width'] ?? 60,
$context['hash']['height'] ?? 30,
0.4,
[
'position' => 'S',
'border' => false,
'padding' => $context['hash']['padding'] ?? 0,
'fgcolor' => $context['hash']['color'] ?? [0,0,0],
'bgcolor' => $context['hash']['bgcolor'] ?? [255,255,255],
'text' => $context['hash']['text'] ?? true,
'font' => 'helvetica',
'fontsize' => $context['hash']['fontsize'] ?? 14,
'stretchtext' => 4,
],
'N',
];
$paramsString = urlencode(json_encode($params));
return new LightnCandy\SafeString("<tcpdf method=\"write1DBarcode\" params=\"{$paramsString}\" />");
},
'ifEqual' => function () {
$args = func_get_args();
$context = $args[count($args) - 1];
@@ -356,23 +396,10 @@ class Htmlizer
return $context['inverse'] ? $context['inverse']() : '';
}
},
]
],
]);
$toRemove = false;
if ($id === null) {
$id = uniqid('', true);
$toRemove = true;
}
$fileName = 'data/cache/templates/' . $id . '.php';
$this->fileManager->putContents($fileName, $code);
$renderer = $this->fileManager->getPhpContents($fileName);
if ($toRemove) {
$this->fileManager->removeFile($fileName);
}
$renderer = LightnCandy::prepare($code);
$data = $this->getDataFromEntity($entity, $skipLinks, 0, $template);
@@ -391,6 +418,7 @@ class Htmlizer
}
$data['__dateTime'] = $this->dateTime;
$data['__metadata'] = $this->metadata;
$html = $renderer($data);

View File

@@ -29,6 +29,36 @@
namespace Espo\Core\Pdf;
define('K_TCPDF_EXTERNAL_CONFIG', true);
define('K_TCPDF_CALLS_IN_HTML', true);
define('K_BLANK_IMAGE', '_blank.png');
define('PDF_PAGE_FORMAT', 'A4');
define('PDF_PAGE_ORIENTATION', 'P');
define('PDF_CREATOR', 'TCPDF');
define('PDF_AUTHOR', 'TCPDF');
define('PDF_UNIT', 'mm');
define('PDF_MARGIN_HEADER', 5);
define('PDF_MARGIN_FOOTER', 10);
define('PDF_MARGIN_TOP', 27);
define('PDF_MARGIN_BOTTOM', 25);
define('PDF_MARGIN_LEFT', 15);
define('PDF_MARGIN_RIGHT', 15);
define('PDF_FONT_NAME_MAIN', 'helvetica');
define('PDF_FONT_SIZE_MAIN', 10);
define('PDF_FONT_NAME_DATA', 'helvetica');
define('PDF_FONT_SIZE_DATA', 8);
define('PDF_FONT_MONOSPACED', 'courier');
define('PDF_IMAGE_SCALE_RATIO', 1.25);
define('HEAD_MAGNIFICATION', 1.1);
define('K_CELL_HEIGHT_RATIO', 1.25);
define('K_TITLE_MAGNIFICATION', 1.3);
define('K_SMALL_RATIO', 2/3);
define('K_THAI_TOPCHARS', true);
define('K_TCPDF_THROW_EXCEPTION_ERROR', false);
define('K_TIMEZONE', 'UTC');
require "vendor/tecnickcom/tcpdf/tcpdf.php";
use \TCPDF_STATIC;
@@ -42,6 +72,16 @@ class Tcpdf extends \TCPDF
protected $useGroupNumbers = false;
public function serializeTCPDFtagParameters($data)
{
return urlencode(json_encode($data));
}
protected function unserializeTCPDFtagParameters($data)
{
return json_decode(urldecode($data), true);
}
public function setUseGroupNumbers($value)
{
$this->useGroupNumbers = $value;
@@ -57,7 +97,8 @@ class Tcpdf extends \TCPDF
$this->footerPosition = $position;
}
public function Footer() {
public function Footer()
{
$breakMargin = $this->getBreakMargin();
$autoPageBreak = $this->AutoPageBreak;
@@ -80,7 +121,8 @@ class Tcpdf extends \TCPDF
$this->SetAutoPageBreak($autoPageBreak, $breakMargin);
}
protected function _putpages() {
protected function _putpages()
{
$filter = ($this->compress) ? '/Filter /FlateDecode ' : '';
// get internal aliases for page numbers
$pnalias = $this->getAllInternalPageNumberAliases();

View File

@@ -135,6 +135,7 @@
"number": "Number (auto-increment)",
"colorpicker": "Color Picker",
"checklist": "Checklist",
"barcode": "Barcode",
"jsonArray": "Json Array",
"jsonObject": "Json Object"
},
@@ -194,6 +195,8 @@
"maxCount": "Max Item Count",
"accept": "Accept",
"viewMap": "View Map Button",
"codeType": "Code Type",
"lastChar": "Last Character",
"displayRawText": "Display raw text (no markdown)"
},
"messages": {

View File

@@ -60,6 +60,15 @@
"javascript: return this.dateTime.getDateShiftedFromToday(10, 'months');": "+10 months",
"javascript: return this.dateTime.getDateShiftedFromToday(11, 'months');": "+11 months",
"javascript: return this.dateTime.getDateShiftedFromToday(1, 'year');": "+1 year"
},
"barcodeType": {
"EAN13": "EAN-13",
"EAN8": "EAN-8",
"EAN5": "EAN-5",
"EAN2": "EAN-2",
"UPC": "UPC (A)",
"UPCE": "UPC (E)",
"pharmacode": "Pharmacode"
}
},
"tooltips": {
@@ -75,6 +84,7 @@
"after": "The date value should be after the date value of the specified field.",
"readOnly": "Field value can't be specified by user. But can be calculated by formula.",
"fileAccept": "Which file types to accept. It's possible to add custom items.",
"barcodeLastChar": "For EAN-13 type.",
"maxFileSize": "If empty or 0 then no limit."
},
"fieldParts": {

View File

@@ -43,5 +43,10 @@
"path": "client/lib/exif-js.js",
"exportsTo": "window",
"exportsAs": "EXIF"
},
"JsBarcode": {
"path": "client/lib/JsBarcode.all.min.js",
"exportsTo": "window",
"exportsAs": "JsBarcode"
}
}

View File

@@ -0,0 +1,50 @@
{
"params": [
{
"name": "required",
"type": "bool",
"default": false
},
{
"name": "codeType",
"type": "enum",
"options": [
"CODE128",
"CODE128A",
"CODE128B",
"CODE128C",
"EAN13",
"EAN8",
"EAN5",
"EAN2",
"UPC",
"UPCE",
"ITF14",
"pharmacode"
],
"translation": "FieldManager.options.barcodeType"
},
{
"name": "lastChar",
"type": "varchar",
"maxLength": 1,
"default": "",
"tooltip": "barcodeLastChar"
},
{
"name":"readOnly",
"type":"bool"
}
],
"validationList": [
"required"
],
"filter": true,
"textFilter": true,
"textFilterForeign": true,
"fieldDefs":{
"type":"varchar",
"len": 255
},
"validatorClassName": "Espo\\Core\\FieldValidators\\VarcharType"
}