Clean code for generating sheet of barcode. Can choose output filename.

This commit is contained in:
Laurent Destailleur 2016-02-08 16:42:34 +01:00
parent 404663472a
commit 7445a0f71c
9 changed files with 87 additions and 66 deletions

View File

@ -190,7 +190,7 @@ if ((! empty($foruserid) || ! empty($foruserlogin) || ! empty($mode)) && ! $mesg
{
$mesg=$langs->trans("ErrorFieldRequired",$langs->transnoentitiesnoconv("DescADHERENT_ETIQUETTE_TYPE"));
}
if (! $mesg) $result=members_label_pdf_create($db, $arrayofmembers, $modellabel, $outputlangs);
if (! $mesg) $result=doc_label_pdf_create($db, $arrayofmembers, $modellabel, $outputlangs);
}
if ($result <= 0)

View File

@ -145,7 +145,9 @@ if ($action == 'builddoc')
// Load barcode class for generating barcode image
$classname = "mod".ucfirst($generator);
$module = new $classname($db);
if ($generator != 'tcpdfbarcode') {
if ($generator != 'tcpdfbarcode')
{
// May be phpbarcode
$template = 'standardlabel';
$is2d = false;
if ($module->encodingIsSupported($encoding))
@ -153,12 +155,12 @@ if ($action == 'builddoc')
$barcodeimage=$conf->barcode->dir_temp.'/barcode_'.$code.'_'.$encoding.'.png';
dol_delete_file($barcodeimage);
// File is created with full name $barcodeimage = $conf->barcode->dir_temp.'/barcode_'.$code.'_'.$encoding.'.png';
$result=$module->writeBarCode($code,$encoding,'Y',4);
$result=$module->writeBarCode($code,$encoding,'Y',4,1);
if ($result <= 0 || ! dol_is_file($barcodeimage))
{
$error++;
setEventMessages('Failed to generate image file of barcode for code='.$code.' encoding='.$encoding.' file='.basename($barcodeimage), null, 'errors');
setEventMessages($module->error, null, 'errors');
}
}
else
@ -207,7 +209,7 @@ if ($action == 'builddoc')
for ($i=0; $i < $numberofsticker; $i++)
{
$arrayofmembers[]=array(
$arrayofrecords[]=array(
'textleft'=>$textleft,
'textheader'=>$textheader,
'textfooter'=>$textfooter,
@ -226,7 +228,7 @@ if ($action == 'builddoc')
// Build and output PDF
if ($mode == 'label')
{
if (! count($arrayofmembers))
if (! count($arrayofrecords))
{
$mesg=$langs->trans("ErrorRecordNotFound");
}
@ -234,7 +236,7 @@ if ($action == 'builddoc')
{
$mesg=$langs->trans("ErrorFieldRequired",$langs->transnoentitiesnoconv("DescADHERENT_ETIQUETTE_TYPE"));
}
if (! $mesg) $result=members_label_pdf_create($db, $arrayofmembers, $modellabel, $outputlangs, $diroutput, $template);
if (! $mesg) $result=doc_label_pdf_create($db, $arrayofrecords, $modellabel, $outputlangs, $diroutput, $template, 'tmp_barcode_sheet.pdf');
}
if ($result <= 0)
@ -283,10 +285,13 @@ print ' <div class="tagtr">';
print ' <div class="tagtd" style="overflow: hidden; white-space: nowrap; max-width: 300px;">';
print $langs->trans("DescADHERENT_ETIQUETTE_TYPE").' &nbsp; ';
print '</div><div class="tagtd maxwidthonsmartphone" style="overflow: hidden; white-space: nowrap;">';
// List of possible labels (defined into $_Avery_Labels variable set into format_cards.lib.php)
// List of possible labels (defined into $_Avery_Labels variable set into core/lib/format_cards.lib.php)
$arrayoflabels=array();
foreach(array_keys($_Avery_Labels) as $codecards)
{
$labeltoshow=$_Avery_Labels[$codecards]['name'];
//$labeltoshow.=' ('.$_Avery_Labels[$row['code']]['paper-size'].')';
$arrayoflabels[$codecards]=$labeltoshow;
$arrayoflabels[$codecards]=$_Avery_Labels[$codecards]['name'];
}
print $form->selectarray('modellabel',$arrayoflabels,(GETPOST('modellabel')?GETPOST('modellabel'):$conf->global->ADHERENT_ETIQUETTE_TYPE),1,0,0);

View File

@ -1,5 +1,5 @@
<?php
/* Copyright (C) 2004-2012 Laurent Destailleur <eldy@users.sourceforge.net>
/* Copyright (C) 2004-2016 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2004-2010 Folke Ashberg: Some lines of code were inspired from work
* of Folke Ashberg into PHP-Barcode 0.3pl2, available as GPL
* source code at http://www.ashberg.de/bar.
@ -59,21 +59,20 @@ else $genbarcode_loc = $conf->global->GENBARCODE_LOCATION;
/**
* Print barcode
*
* @param string $code Code
* @param string $encoding Encoding
* @param integer $scale Scale
* @param string $mode 'png' or 'jpg' ...
* @return array $bars array('encoding': the encoding which has been used, 'bars': the bars, 'text': text-positioning info)
* @param string $code Code
* @param string $encoding Encoding
* @param integer $scale Scale
* @param string $mode 'png' or 'jpg' ...
* @return array|string $bars array('encoding': the encoding which has been used, 'bars': the bars, 'text': text-positioning info) or string with error message
*/
function barcode_print($code, $encoding="ANY", $scale = 2 ,$mode = "png")
{
// DOLCHANGE LDR Add log
dol_syslog("barcode.lib.php::barcode_print $code $encoding $scale $mode");
$bars=barcode_encode($code,$encoding);
if (! $bars || ! empty($bars['error']))
{
// DOLCHANGE LDR Return error message instead of array
// Return error message instead of array
if (empty($bars['error'])) $error='Bad Value '.$code.' for encoding '.$encoding;
else $error=$bars['error'];
dol_syslog('barcode.lib.php::barcode_print '.$error, LOG_ERR);

View File

@ -34,26 +34,34 @@ global $_Avery_Labels;
$sql = "SELECT rowid, code, name, paper_size, orientation, metric, leftmargin, topmargin, nx, ny, spacex, spacey, width, height, font_size, custom_x, custom_y, active FROM ".MAIN_DB_PREFIX."c_format_cards WHERE active=1 ORDER BY code ASC";
$resql = $db->query($sql);
while ($row = $db->fetch_array($resql)) {
$_Avery_Labels[$row['code']]['name']=$row['name'];
$_Avery_Labels[$row['code']]['paper-size']=$row['paper_size'];
$_Avery_Labels[$row['code']]['orientation']=$row['orientation'];
$_Avery_Labels[$row['code']]['metric']=$row['metric'];
$_Avery_Labels[$row['code']]['marginLeft']=$row['leftmargin'];
$_Avery_Labels[$row['code']]['marginTop']=$row['topmargin'];
$_Avery_Labels[$row['code']]['marginTop']=$row['topmargin'];
$_Avery_Labels[$row['code']]['NX']=$row['nx'];
$_Avery_Labels[$row['code']]['NY']=$row['ny'];
$_Avery_Labels[$row['code']]['SpaceX']=$row['spacex'];
$_Avery_Labels[$row['code']]['SpaceY']=$row['spacey'];
$_Avery_Labels[$row['code']]['width']=$row['width'];
$_Avery_Labels[$row['code']]['height']=$row['height'];
$_Avery_Labels[$row['code']]['font-size']=$row['font_size'];
$_Avery_Labels[$row['code']]['custom_x']=$row['custom_x'];
$_Avery_Labels[$row['code']]['custom_y']=$row['custom_y'];
if ($resql)
{
while ($row = $db->fetch_array($resql))
{
$_Avery_Labels[$row['code']]['name']=$row['name'];
$_Avery_Labels[$row['code']]['paper-size']=$row['paper_size'];
$_Avery_Labels[$row['code']]['orientation']=$row['orientation'];
$_Avery_Labels[$row['code']]['metric']=$row['metric'];
$_Avery_Labels[$row['code']]['marginLeft']=$row['leftmargin'];
$_Avery_Labels[$row['code']]['marginTop']=$row['topmargin'];
$_Avery_Labels[$row['code']]['marginTop']=$row['topmargin'];
$_Avery_Labels[$row['code']]['NX']=$row['nx'];
$_Avery_Labels[$row['code']]['NY']=$row['ny'];
$_Avery_Labels[$row['code']]['SpaceX']=$row['spacex'];
$_Avery_Labels[$row['code']]['SpaceY']=$row['spacey'];
$_Avery_Labels[$row['code']]['width']=$row['width'];
$_Avery_Labels[$row['code']]['height']=$row['height'];
$_Avery_Labels[$row['code']]['font-size']=$row['font_size'];
$_Avery_Labels[$row['code']]['custom_x']=$row['custom_x'];
$_Avery_Labels[$row['code']]['custom_y']=$row['custom_y'];
}
}
else
{
dol_print_error($db);
}
// we add characteristics to the name
// We add characteristics to the name
foreach($_Avery_Labels as $key => $val)
{
$_Avery_Labels[$key]['name'].=' ('.$_Avery_Labels[$key]['paper-size'].' - '.$_Avery_Labels[$key]['NX'].'x'.$_Avery_Labels[$key]['NY'].')';

View File

@ -105,13 +105,14 @@ class modPhpbarcode extends ModeleBarCode
/**
* 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
* @param string $readable Code can be read
* @param integer $scale Scale
* @return int <0 if KO, >0 if OK
* @param string $code Value to encode
* @param string $encoding Mode of encoding
* @param string $readable Code can be read
* @param integer $scale Scale
* @param integer $nooutputiferror No output if error
* @return int <0 if KO, >0 if OK
*/
function buildBarCode($code,$encoding,$readable='Y',$scale=1)
function buildBarCode($code,$encoding,$readable='Y',$scale=1,$nooutputiferror=0)
{
global $_GET,$_SERVER;
global $conf;
@ -135,7 +136,7 @@ class modPhpbarcode extends ModeleBarCode
if (! is_array($result))
{
$this->error=$result;
print $this->error;
if (empty($nooutputiferror)) print $this->error;
return -1;
}
@ -145,13 +146,14 @@ class modPhpbarcode extends ModeleBarCode
/**
* 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
* @return int <0 if KO, >0 if OK
* @param string $code Value to encode
* @param string $encoding Mode of encoding
* @param string $readable Code can be read
* @param integer $scale Scale
* @param integer $nooutputiferror No output if error
* @return int <0 if KO, >0 if OK
*/
function writeBarCode($code,$encoding,$readable='Y',$scale=1)
function writeBarCode($code,$encoding,$readable='Y',$scale=1,$nooutputiferror=0)
{
global $conf,$filebarcode;
@ -161,7 +163,7 @@ class modPhpbarcode extends ModeleBarCode
$filebarcode=$file; // global var to be used in barcode_outimage called by barcode_print in buildBarCode
$result=$this->buildBarCode($code,$encoding,$readable,$scale);
$result=$this->buildBarCode($code,$encoding,$readable,$scale,$nooutputiferror);
return $result;
}

View File

@ -89,12 +89,14 @@ class modTcpdfbarcode extends ModeleBarCode
/**
* 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
* @param String $readable Code can be read
* @return int <0 if KO, >0 if OK
* @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
*/
function buildBarCode($code,$encoding,$readable='Y')
function buildBarCode($code,$encoding,$readable='Y',$scale=1,$nooutputiferror=0)
{
global $_GET;
@ -134,12 +136,14 @@ class modTcpdfbarcode extends ModeleBarCode
/**
* 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
* @return int <0 if KO, >0 if OK
* @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
*/
function writeBarCode($code,$encoding,$readable='Y')
function writeBarCode($code,$encoding,$readable='Y',$scale=1,$nooutputiferror=0)
{
global $conf,$_GET;

View File

@ -231,9 +231,10 @@ class pdf_standardlabel extends CommonStickerGenerator
* @param Translate $outputlangs Lang object for output language
* @param string $srctemplatepath Full path of source filename for generator using a template file
* @param string $outputdir Output directory for pdf file
* @param string $filename Short file name of PDF output file
* @return int 1=OK, 0=KO
*/
function write_file($arrayofrecords,$outputlangs,$srctemplatepath,$outputdir='')
function write_file($arrayofrecords,$outputlangs,$srctemplatepath,$outputdir='',$filename='tmp_address_sheet.pdf')
{
global $user,$conf,$langs,$mysoc,$_Avery_Labels;
@ -263,7 +264,6 @@ class pdf_standardlabel extends CommonStickerGenerator
$keywords=$title." ".$outputlangs->convToOutputCharset($mysoc->name);
$dir = (empty($outputdir)?$conf->adherent->dir_temp:$outputdir);
$filename='tmp_address_sheet.pdf';
$file = $dir."/".$filename;
if (! file_exists($dir))

View File

@ -272,9 +272,10 @@ class pdf_tcpdflabel extends CommonStickerGenerator
* @param Translate $outputlangs Lang object for output language
* @param string $srctemplatepath Full path of source filename for generator using a template file
* @param string $outputdir Output directory for pdf file
* @param string $filename Short file name of PDF output file
* @return int 1=OK, 0=KO
*/
function write_file($arrayofrecords,$outputlangs,$srctemplatepath,$outputdir='')
function write_file($arrayofrecords,$outputlangs,$srctemplatepath,$outputdir='',$filename='tmp_address_sheet.pdf')
{
global $user,$conf,$langs,$mysoc,$_Avery_Labels;
@ -304,7 +305,6 @@ class pdf_tcpdflabel extends CommonStickerGenerator
$keywords=$title." ".$outputlangs->convToOutputCharset($mysoc->name);
$dir = (empty($outputdir)?$conf->adherent->dir_temp:$outputdir);
$filename='tmp_address_sheet.pdf';
$file = $dir."/".$filename;
if (! file_exists($dir))

View File

@ -60,7 +60,7 @@ class ModelePDFLabels
/**
* Create a document onto disk accordign to template module
* Create a document onto disk according to template module.
*
* @param DoliDB $db Database handler
* @param array $arrayofrecords Array of records
@ -68,9 +68,10 @@ class ModelePDFLabels
* @param Translate $outputlangs Objet lang a utiliser pour traduction
* @param string $outputdir Output directory
* @param string $template pdf generenate document class to use default 'standardlabel'
* @param string $filename Short file name of PDF output file
* @return int <0 if KO, >0 if OK
*/
function members_label_pdf_create($db, $arrayofrecords, $modele, $outputlangs, $outputdir='', $template='standardlabel')
function doc_label_pdf_create($db, $arrayofrecords, $modele, $outputlangs, $outputdir='', $template='standardlabel', $filename='tmp_address_sheet.pdf')
{
global $conf,$langs;
$langs->load("members");
@ -109,6 +110,8 @@ function members_label_pdf_create($db, $arrayofrecords, $modele, $outputlangs, $
}
else $srctemplatepath=$code;
dol_syslog("modele=".$modele." outputdir=".$outputdir." template=".$template." code=".$code." srctemplatepath=".$srctemplatepath." filename=".$filename, LOG_DEBUG);
// Search template files
$file=''; $classname=''; $filefound=0;
$dirmodels=array('/');
@ -141,7 +144,7 @@ function members_label_pdf_create($db, $arrayofrecords, $modele, $outputlangs, $
// We save charset_output to restore it because write_file can change it if needed for
// output format that does not support UTF8.
$sav_charset_output=$outputlangs->charset_output;
if ($obj->write_file($arrayofrecords, $outputlangs, $srctemplatepath, $outputdir) > 0)
if ($obj->write_file($arrayofrecords, $outputlangs, $srctemplatepath, $outputdir, $filename) > 0)
{
$outputlangs->charset_output=$sav_charset_output;
return 1;
@ -149,7 +152,7 @@ function members_label_pdf_create($db, $arrayofrecords, $modele, $outputlangs, $
else
{
$outputlangs->charset_output=$sav_charset_output;
dol_print_error($db,"members_label_pdf_create Error: ".$obj->error);
dol_print_error($db,"doc_label_pdf_create Error: ".$obj->error);
return -1;
}
}