dolibarr/htdocs/core/modules/barcode/doc/tcpdfbarcode.modules.php

274 lines
7.3 KiB
PHP
Raw Normal View History

<?php
2015-05-03 09:51:54 +00:00
/* Copyright (C) 2005-2009 Laurent Destailleur <eldy@users.sourceforge.net>
2018-10-27 12:43:12 +00:00
* Copyright (C) 2005 Regis Houssin <regis.houssin@inodbox.com>
2015-05-03 09:51:54 +00:00
* Copyright (C) 2015 Francis Appels <francis.appels@yahoo.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
2015-05-03 09:51:54 +00:00
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2019-09-23 19:55:30 +00:00
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/**
2015-05-03 09:51:54 +00:00
* \file htdocs/core/modules/barcode/doc/tcpdfbarcode.modules.php
* \ingroup barcode
* \brief File of class to manage barcode numbering with tcpdf library
*/
require_once DOL_DOCUMENT_ROOT.'/core/modules/barcode/modules_barcode.class.php';
require_once DOL_DOCUMENT_ROOT.'/core/lib/barcode.lib.php'; // This is to include def like $genbarcode_loc and $font_loc
/**
* Class to generate barcode images using tcpdf barcode generator
*/
class modTcpdfbarcode extends ModeleBarCode
{
2018-09-20 10:30:38 +00:00
/**
* Dolibarr version of the loaded document
* @var string
*/
public $version = 'dolibarr'; // 'development', 'experimental', 'dolibarr'
2018-09-20 10:30:38 +00:00
2018-08-17 16:35:51 +00:00
/**
* @var string Error code (or message)
*/
public $error = '';
2018-09-20 10:30:38 +00:00
public $is2d = false;
2018-07-26 17:05:34 +00:00
2015-05-03 09:51:54 +00:00
/**
* Return description of numbering model
*
* @return string Text with description
*/
2019-02-25 21:27:04 +00:00
public function info()
2015-05-03 09:51:54 +00:00
{
global $langs;
2015-05-03 09:51:54 +00:00
return 'TCPDF-barcode';
}
2015-05-03 09:51:54 +00:00
/**
* Return if a module can be used or not
*
2015-05-03 09:51:54 +00:00
* @return boolean true if module can be used
*/
2019-02-25 21:27:04 +00:00
public function isEnabled()
{
return true;
2018-07-26 17:05:34 +00:00
}
/**
2020-04-03 16:01:54 +00:00
* Checks if the numbers already in the database do not
* cause conflicts that would prevent this numbering working.
*
2019-09-08 02:57:09 +00:00
* @return boolean false if conflict, true if ok
*/
2019-02-25 21:27:04 +00:00
public function canBeActivated()
{
2015-05-03 09:51:54 +00:00
global $langs;
2018-07-26 17:05:34 +00:00
2015-05-03 09:51:54 +00:00
return true;
}
2018-07-26 17:05:34 +00:00
/**
* Return true if encoding is supported
*
* @param string $encoding Encoding norm
* @return int >0 if supported, 0 if not
*/
2019-02-25 21:27:04 +00:00
public function encodingIsSupported($encoding)
{
2015-05-03 09:51:54 +00:00
$tcpdfEncoding = $this->getTcpdfEncodingType($encoding);
if (empty($tcpdfEncoding)) {
return 0;
} else {
return 1;
2018-07-26 17:05:34 +00:00
}
}
2015-05-03 09:51:54 +00:00
/**
* Return an image file on the fly (no need to write on disk)
*
* @param string $code Value to encode
* @param string $encoding Mode of encoding
2021-08-17 11:08:03 +00:00
* @param string $readable Code can be read (What is this ? is this used ?)
* @param integer $scale Scale (not used with this engine)
* @param integer $nooutputiferror No output if error (not used with this engine)
* @return int <0 if KO, >0 if OK
2015-05-03 09:51:54 +00:00
*/
2019-02-25 21:27:04 +00:00
public function buildBarCode($code, $encoding, $readable = 'Y', $scale = 1, $nooutputiferror = 0)
2015-05-03 09:51:54 +00:00
{
global $_GET;
2018-07-26 17:05:34 +00:00
2015-05-03 09:51:54 +00:00
$tcpdfEncoding = $this->getTcpdfEncodingType($encoding);
2021-02-23 21:03:23 +00:00
if (empty($tcpdfEncoding)) {
return -1;
}
2018-07-26 17:05:34 +00:00
$color = array(0, 0, 0);
$_GET["code"] = $code;
$_GET["type"] = $encoding;
$_GET["readable"] = $readable;
2018-07-26 17:05:34 +00:00
if ($code) {
2015-05-03 09:51:54 +00:00
// Load the tcpdf barcode class
if ($this->is2d) {
$height = 3;
$width = 3;
require_once TCPDF_PATH.'tcpdf_barcodes_2d.php';
$barcodeobj = new TCPDF2DBarcode($code, $tcpdfEncoding);
} else {
$height = 50;
$width = 1;
require_once TCPDF_PATH.'tcpdf_barcodes_1d.php';
$barcodeobj = new TCPDFBarcode($code, $tcpdfEncoding);
2018-07-26 17:05:34 +00:00
}
2015-05-03 09:51:54 +00:00
dol_syslog("buildBarCode::TCPDF.getBarcodePNG");
$barcodeobj->getBarcodePNG($width, $height, $color);
2018-07-26 17:05:34 +00:00
2015-05-03 09:51:54 +00:00
return 1;
} else {
2015-05-03 09:51:54 +00:00
return -2;
2018-07-26 17:05:34 +00:00
}
2015-05-03 09:51:54 +00:00
}
/**
2015-05-03 09:51:54 +00:00
* Save an image file on disk (with no output)
*
* @param string $code Value to encode
* @param string $encoding Mode of encoding
* @param string $readable Code can be read
* @param integer $scale Scale (not used with this engine)
* @param integer $nooutputiferror No output if error (not used with this engine)
* @return int <0 if KO, >0 if OK
2015-05-03 09:51:54 +00:00
*/
2019-02-25 21:27:04 +00:00
public function writeBarCode($code, $encoding, $readable = 'Y', $scale = 1, $nooutputiferror = 0)
2015-05-03 09:51:54 +00:00
{
global $conf, $_GET;
dol_mkdir($conf->barcode->dir_temp);
if (!is_writable($conf->barcode->dir_temp)) {
$this->error = "Failed to write in temp directory ".$conf->barcode->dir_temp;
dol_syslog('Error in write_file: '.$this->error, LOG_ERR);
return -1;
}
$file = $conf->barcode->dir_temp.'/barcode_'.$code.'_'.$encoding.'.png';
$tcpdfEncoding = $this->getTcpdfEncodingType($encoding);
2021-02-23 21:03:23 +00:00
if (empty($tcpdfEncoding)) {
return -1;
}
$color = array(0, 0, 0);
$_GET["code"] = $code;
$_GET["type"] = $encoding;
$_GET["readable"] = $readable;
2015-05-03 09:51:54 +00:00
if ($code) {
// Load the tcpdf barcode class
if ($this->is2d) {
$height = 1;
$width = 1;
require_once TCPDF_PATH.'tcpdf_barcodes_2d.php';
$barcodeobj = new TCPDF2DBarcode($code, $tcpdfEncoding);
} else {
$height = 50;
$width = 1;
require_once TCPDF_PATH.'tcpdf_barcodes_1d.php';
$barcodeobj = new TCPDFBarcode($code, $tcpdfEncoding);
2018-07-26 17:05:34 +00:00
}
2015-05-03 09:51:54 +00:00
dol_syslog("writeBarCode::TCPDF.getBarcodePngData");
if ($imageData = $barcodeobj->getBarcodePngData($width, $height, $color)) {
if (function_exists('imagecreate')) {
$imageData = imagecreatefromstring($imageData);
}
if (imagepng($imageData, $file)) {
return 1;
} else {
return -3;
}
} else {
return -4;
2018-07-26 17:05:34 +00:00
}
} else {
2015-05-03 09:51:54 +00:00
return -2;
}
2015-05-03 09:51:54 +00:00
}
2018-07-26 17:05:34 +00:00
2015-05-03 09:51:54 +00:00
/**
* get available output_modes for tcpdf class wth its translated description
*
2018-07-26 17:05:34 +00:00
* @param string $dolEncodingType dolibarr barcode encoding type
2015-05-03 09:51:54 +00:00
* @return string tcpdf encoding type
*/
public function getTcpdfEncodingType($dolEncodingType)
{
$tcpdf1dEncodingTypes = array(
'C39' => 'C39',
'C39+' => 'C39+',
'C39E' => 'C39E',
'C39E+' => 'C39E+',
'S25' => 'S25',
'S25+' => 'S25+',
'I25' => 'I25',
'I25+' => 'I25+',
'C128' => 'C128',
'C128A' => 'C128A',
'C128B' => 'C128B',
'C128C' => 'C128C',
'EAN2' => 'EAN2',
'EAN5' => 'EAN5',
'EAN8' => 'EAN8',
'EAN13' => 'EAN13',
'ISBN' => 'EAN13',
'UPC' => 'UPCA',
'UPCE' => 'UPCE',
'MSI' => 'MSI',
'MSI+' => 'MSI+',
'POSTNET' => 'POSTNET',
'PLANET' => 'PLANET',
'RMS4CC' => 'RMS4CC',
'KIX' => 'KIX',
'IMB' => 'IMB',
'CODABAR' => 'CODABAR',
'CODE11' => 'CODE11',
'PHARMA' => 'PHARMA',
'PHARMA2T' => 'PHARMA2T'
);
2018-07-26 17:05:34 +00:00
2015-05-03 09:51:54 +00:00
$tcpdf2dEncodingTypes = array(
'DATAMATRIX' => 'DATAMATRIX',
'PDF417' => 'PDF417',
'QRCODE' => 'QRCODE,L',
'QRCODE,L' => 'QRCODE,L',
'QRCODE,M' => 'QRCODE,M',
'QRCODE,Q' => 'QRCODE,Q',
2018-07-26 17:05:34 +00:00
'QRCODE,H' => 'QRCODE,H'
2015-05-03 09:51:54 +00:00
);
2018-07-26 17:05:34 +00:00
2015-05-03 09:51:54 +00:00
if (array_key_exists($dolEncodingType, $tcpdf1dEncodingTypes)) {
$this->is2d = false;
return $tcpdf1dEncodingTypes[$dolEncodingType];
2019-01-27 09:49:34 +00:00
} elseif (array_key_exists($dolEncodingType, $tcpdf2dEncodingTypes)) {
2015-05-03 09:51:54 +00:00
$this->is2d = true;
return $tcpdf2dEncodingTypes[$dolEncodingType];
} else {
return '';
2018-07-26 17:05:34 +00:00
}
2015-05-03 09:51:54 +00:00
}
2015-05-03 08:26:12 +00:00
}