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")); $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) if ($result <= 0)

View File

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

View File

@ -1,5 +1,5 @@
<?php <?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 * 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 * of Folke Ashberg into PHP-Barcode 0.3pl2, available as GPL
* source code at http://www.ashberg.de/bar. * source code at http://www.ashberg.de/bar.
@ -63,17 +63,16 @@ else $genbarcode_loc = $conf->global->GENBARCODE_LOCATION;
* @param string $encoding Encoding * @param string $encoding Encoding
* @param integer $scale Scale * @param integer $scale Scale
* @param string $mode 'png' or 'jpg' ... * @param string $mode 'png' or 'jpg' ...
* @return array $bars array('encoding': the encoding which has been used, 'bars': the bars, 'text': text-positioning info) * @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") 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"); dol_syslog("barcode.lib.php::barcode_print $code $encoding $scale $mode");
$bars=barcode_encode($code,$encoding); $bars=barcode_encode($code,$encoding);
if (! $bars || ! empty($bars['error'])) 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; if (empty($bars['error'])) $error='Bad Value '.$code.' for encoding '.$encoding;
else $error=$bars['error']; else $error=$bars['error'];
dol_syslog('barcode.lib.php::barcode_print '.$error, LOG_ERR); dol_syslog('barcode.lib.php::barcode_print '.$error, LOG_ERR);

View File

@ -34,7 +34,10 @@ 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"; $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); $resql = $db->query($sql);
while ($row = $db->fetch_array($resql)) { if ($resql)
{
while ($row = $db->fetch_array($resql))
{
$_Avery_Labels[$row['code']]['name']=$row['name']; $_Avery_Labels[$row['code']]['name']=$row['name'];
$_Avery_Labels[$row['code']]['paper-size']=$row['paper_size']; $_Avery_Labels[$row['code']]['paper-size']=$row['paper_size'];
$_Avery_Labels[$row['code']]['orientation']=$row['orientation']; $_Avery_Labels[$row['code']]['orientation']=$row['orientation'];
@ -52,8 +55,13 @@ while ($row = $db->fetch_array($resql)) {
$_Avery_Labels[$row['code']]['custom_x']=$row['custom_x']; $_Avery_Labels[$row['code']]['custom_x']=$row['custom_x'];
$_Avery_Labels[$row['code']]['custom_y']=$row['custom_y']; $_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) foreach($_Avery_Labels as $key => $val)
{ {
$_Avery_Labels[$key]['name'].=' ('.$_Avery_Labels[$key]['paper-size'].' - '.$_Avery_Labels[$key]['NX'].'x'.$_Avery_Labels[$key]['NY'].')'; $_Avery_Labels[$key]['name'].=' ('.$_Avery_Labels[$key]['paper-size'].' - '.$_Avery_Labels[$key]['NX'].'x'.$_Avery_Labels[$key]['NY'].')';

View File

@ -109,9 +109,10 @@ class modPhpbarcode extends ModeleBarCode
* @param string $encoding Mode of encoding * @param string $encoding Mode of encoding
* @param string $readable Code can be read * @param string $readable Code can be read
* @param integer $scale Scale * @param integer $scale Scale
* @param integer $nooutputiferror No output if error
* @return int <0 if KO, >0 if OK * @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 $_GET,$_SERVER;
global $conf; global $conf;
@ -135,7 +136,7 @@ class modPhpbarcode extends ModeleBarCode
if (! is_array($result)) if (! is_array($result))
{ {
$this->error=$result; $this->error=$result;
print $this->error; if (empty($nooutputiferror)) print $this->error;
return -1; return -1;
} }
@ -149,9 +150,10 @@ class modPhpbarcode extends ModeleBarCode
* @param string $encoding Mode of encoding * @param string $encoding Mode of encoding
* @param string $readable Code can be read * @param string $readable Code can be read
* @param integer $scale Scale * @param integer $scale Scale
* @param integer $nooutputiferror No output if error
* @return int <0 if KO, >0 if OK * @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; 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 $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; 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) * Return an image file on the fly (no need to write on disk)
* *
* @param String $code Value to encode * @param string $code Value to encode
* @param String $encoding Mode of encoding * @param string $encoding Mode of encoding
* @param String $readable Code can be read * @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 * @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; global $_GET;
@ -134,12 +136,14 @@ class modTcpdfbarcode extends ModeleBarCode
/** /**
* Save an image file on disk (with no output) * Save an image file on disk (with no output)
* *
* @param String $code Value to encode * @param string $code Value to encode
* @param String $encoding Mode of encoding * @param string $encoding Mode of encoding
* @param String $readable Code can be read * @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 * @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; global $conf,$_GET;

View File

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

View File

@ -272,9 +272,10 @@ class pdf_tcpdflabel extends CommonStickerGenerator
* @param Translate $outputlangs Lang object for output language * @param Translate $outputlangs Lang object for output language
* @param string $srctemplatepath Full path of source filename for generator using a template file * @param string $srctemplatepath Full path of source filename for generator using a template file
* @param string $outputdir Output directory for pdf 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 * @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; global $user,$conf,$langs,$mysoc,$_Avery_Labels;
@ -304,7 +305,6 @@ class pdf_tcpdflabel extends CommonStickerGenerator
$keywords=$title." ".$outputlangs->convToOutputCharset($mysoc->name); $keywords=$title." ".$outputlangs->convToOutputCharset($mysoc->name);
$dir = (empty($outputdir)?$conf->adherent->dir_temp:$outputdir); $dir = (empty($outputdir)?$conf->adherent->dir_temp:$outputdir);
$filename='tmp_address_sheet.pdf';
$file = $dir."/".$filename; $file = $dir."/".$filename;
if (! file_exists($dir)) 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 DoliDB $db Database handler
* @param array $arrayofrecords Array of records * @param array $arrayofrecords Array of records
@ -68,9 +68,10 @@ class ModelePDFLabels
* @param Translate $outputlangs Objet lang a utiliser pour traduction * @param Translate $outputlangs Objet lang a utiliser pour traduction
* @param string $outputdir Output directory * @param string $outputdir Output directory
* @param string $template pdf generenate document class to use default 'standardlabel' * @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 * @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; global $conf,$langs;
$langs->load("members"); $langs->load("members");
@ -109,6 +110,8 @@ function members_label_pdf_create($db, $arrayofrecords, $modele, $outputlangs, $
} }
else $srctemplatepath=$code; else $srctemplatepath=$code;
dol_syslog("modele=".$modele." outputdir=".$outputdir." template=".$template." code=".$code." srctemplatepath=".$srctemplatepath." filename=".$filename, LOG_DEBUG);
// Search template files // Search template files
$file=''; $classname=''; $filefound=0; $file=''; $classname=''; $filefound=0;
$dirmodels=array('/'); $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 // We save charset_output to restore it because write_file can change it if needed for
// output format that does not support UTF8. // output format that does not support UTF8.
$sav_charset_output=$outputlangs->charset_output; $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; $outputlangs->charset_output=$sav_charset_output;
return 1; return 1;
@ -149,7 +152,7 @@ function members_label_pdf_create($db, $arrayofrecords, $modele, $outputlangs, $
else else
{ {
$outputlangs->charset_output=$sav_charset_output; $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; return -1;
} }
} }