Passed
Branch develop (a7390e)
by
unknown
25:38
created

pdf_tcpdflabel::write_file()   F

Complexity

Conditions 17
Paths 16401

Size

Total Lines 112
Code Lines 61

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 17
eloc 61
c 0
b 0
f 0
nc 16401
nop 5
dl 0
loc 112
rs 1.0499

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/* Copyright (C) 2003 Steve Dillon
3
 * Copyright (C) 2003 Laurent Passebecq
4
 * Copyright (C) 2001-2003 Rodolphe Quiedeville <[email protected]>
5
 * Copyright (C) 2002-2003 Jean-Louis Bergamo	<[email protected]>
6
 * Copyright (C) 2006-2013 Laurent Destailleur	<[email protected]>
7
 * Copyright (C) 2015 Francis Appels  <[email protected]>
8
 *
9
 * This program is free software; you can redistribute it and/or modify
10
 * it under the terms of the GNU General Public License as published by
11
 * the Free Software Foundation; either version 3 of the License, or
12
 * (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21
 */
22
23
/**
24
 *	\file		htdocs/core/modules/printsheet/doc/pdf_standardlabel.class.php
25
 *	\ingroup	core
26
 *	\brief		Fichier de la classe permettant d'editer au format PDF des etiquettes au format Avery ou personnalise
27
 */
28
29
require_once DOL_DOCUMENT_ROOT.'/core/class/commonstickergenerator.class.php';
30
31
/**
32
 *	Class to generate stick sheet with format Avery or other personalised
33
 */
34
class pdf_tcpdflabel extends CommonStickerGenerator
35
{
36
	// define 1d barcode style
37
	private $_style1d = array(
38
					'position' => '',
39
					'align' => 'C',
40
					'stretch' => false,
41
					'fitwidth' => true,
42
					'cellfitalign' => '',
43
					'border' => false,
44
					'hpadding' => 'auto',
45
					'vpadding' => 'auto',
46
					'fgcolor' => array(0,0,0),
47
					'bgcolor' => false,
48
					'text' => true,
49
					'font' => 'helvetica',
50
					'fontsize' => 8,
51
					'stretchtext' => 4
52
	);
53
54
	// set style for 2d barcode
55
	private $_style2d = array(
56
					'border' => false,
57
					'vpadding' => 'auto',
58
					'hpadding' => 'auto',
59
					'fgcolor' => array(0,0,0),
60
					'bgcolor' => false,
61
					'module_width' => 1, // width of a single module in points
62
					'module_height' => 1 // height of a single module in points
63
	);
64
65
	private $_align2d = 'N';
66
67
	private $_xres = 0.4;
68
69
	/**
70
	 * write barcode to pdf
71
	 *
72
	 * @param PDF	  $pdf		  PDF reference
73
	 * @param string  $code		   code to print
74
	 * @param string  $encoding	   type of barcode
75
	 * @param boolean $is2d		   true if 2d barcode
76
	 * @param int	  $x		   x position in user units
77
	 * @param int	  $y		   y position in user units
78
	 * @param int	  $w		   width in user units
79
	 * @param int	  $h		   height in user units
80
	 * @return void
81
	 */
82
	private function writeBarcode(&$pdf, $code, $encoding, $is2d, $x, $y, $w, $h)
83
	{
84
		if ($is2d) {
85
			$pdf->write2DBarcode($code, $encoding, $x, $y, $w, $h, $this->_style2d, $this->_align2d);
86
		} else {
87
			$pdf->write1DBarcode($code, $encoding, $x, $y, $w, $h, $this->_xres, $this->_style1d);
88
		}
89
	}
90
91
	/**
92
	 * Output a sticker on page at position _COUNTX, _COUNTY (_COUNTX and _COUNTY start from 0)
93
	 *
94
	 * @param	PDF			$pdf			PDF reference
95
	 * @param	Translate	$outputlangs	Output langs
96
	 * @param	array		$param			Associative array containing label content and optional parameters
97
	 * @return	void
98
	 */
99
	public function addSticker(&$pdf, $outputlangs, $param)
100
	{
101
		global $mysoc,$conf;
102
103
		$textleft = $param['textleft'];
104
		$header = $param['textheader'];
105
		$footer = $param['textfooter'];
106
		$textright = $param['textright'];
107
		$code = $param['code'];
108
		$encoding = $param['encoding'];
109
		$is2d = $param['is2d'];
110
111
112
113
		// We are in a new page, then we must add a page
114
		if (($this->_COUNTX ==0) && ($this->_COUNTY==0) and (!$this->_First==1)) {
115
			$pdf->AddPage();
116
		}
117
		$this->_First=0;
118
		$_PosX = $this->_Margin_Left+($this->_COUNTX*($this->_Width+$this->_X_Space));
119
		$_PosY = $this->_Margin_Top+($this->_COUNTY*($this->_Height+$this->_Y_Space));
120
121
		// Define logo
122
		$logo=$conf->mycompany->dir_output.'/logos/'.$mysoc->logo;
123
		if (! is_readable($logo))
124
		{
125
			$logo='';
126
			if (! empty($mysoc->logo_small) && is_readable($conf->mycompany->dir_output.'/logos/thumbs/'.$mysoc->logo_small))
127
			{
128
				$logo=$conf->mycompany->dir_output.'/logos/thumbs/'.$mysoc->logo_small;
129
			}
130
			elseif (! empty($mysoc->logo) && is_readable($conf->mycompany->dir_output.'/logos/'.$mysoc->logo))
131
			{
132
				$logo=$conf->mycompany->dir_output.'/logos/'.$mysoc->logo;
133
			}
134
		}
135
136
		$xleft = 2;
137
		$ytop = 2;
138
139
		// Top
140
		if ($header!='')
141
		{
142
			$pdf->SetXY($_PosX+$xleft, $_PosY+1); // Only 1 mm and not ytop for top text
143
			$pdf->Cell($this->_Width-2*$xleft, $this->_Line_Height, $outputlangs->convToOutputCharset($header), 0, 1, 'C');
144
		}
145
146
		$ytop += (empty($header)?0:(1+$this->_Line_Height));
147
148
		// Define widthtouse and heighttouse
149
		$pageMargins = $pdf->getMargins();
150
		$maxwidthtouse = round($this->_Width - 2*$xleft);
151
		$maxheighttouse = round($this->_Height - 2*$ytop);
152
		$maxheighttouse -= (empty($footer)?0:(1+$this->_Line_Height));
153
		$defaultratio = ($maxwidthtouse/$maxheighttouse);
154
		$widthtouse = $maxwidthtouse;
155
		$heighttouse = $maxheighttouse;
156
		$logoHeight = $heighttouse;
157
		$logoWidth = $widthtouse;
158
159
		//var_dump($this->_Width.'x'.$this->_Height.' with border and scale '.$imgscale.' => max '.$maxwidthtouse.'x'.$maxheighttouse.' => We use '.$widthtouse.'x'.$heighttouse);exit;
160
161
		// Center
162
		if ($textright=='')	// Only a left part
163
		{
164
			// Output left area
165
			if ($textleft == '%LOGO%' && $logo) $pdf->Image($logo, $_PosX+$xleft, $_PosY+$ytop, 0, $logoHeight);
166
			elseif ($code && !empty($encoding))
167
			{
168
				$this->writeBarcode($pdf, $code, $encoding, $is2d, $_PosX+$xleft, $_PosY+$ytop, $widthtouse, $heighttouse);
169
			}
170
			else
171
			{
172
				$pdf->SetXY($_PosX+$xleft, $_PosY+$ytop);
173
				$pdf->MultiCell($this->_Width, $this->_Line_Height, $outputlangs->convToOutputCharset($textleft), 0, 'L');
174
			}
175
		}
176
		elseif ($textleft!='' && $textright!='')	// left and right part
177
		{
178
			$logoHeight = $heighttouse/2;
179
			$logoWidth = $widthtouse/2;
180
			if (($textleft == '%LOGO%' || $textleft == '%PHOTO%' || $textleft == '%BARCODE%') && !strstr($textright, '%') )	 // left part logo/barcode right part text
181
			{
182
				if ($textleft == '%LOGO%' && $logo) $pdf->Image($logo, $_PosX+$xleft, $_PosY+$ytop, $logoWidth, 0);
183
				elseif ($code && !empty($encoding))
184
				{
185
					$this->writeBarcode($pdf, $code, $encoding, $is2d, $_PosX+$xleft, $_PosY+$ytop, $widthtouse/2, $heighttouse);
186
				}
187
				$pdf->SetXY($_PosX+($widthtouse/2), $_PosY+$ytop);
188
				$pdf->MultiCell($widthtouse/2, $this->_Line_Height, $outputlangs->convToOutputCharset($textright), 0, 'R');
189
			}
190
			elseif (($textright == '%LOGO%' || $textright == '%PHOTO%' || $textright == '%BARCODE%') && !strstr($textleft, '%')) // right part logo/barcode left part text
191
			{
192
				if ($textright == '%LOGO%' && $logo) $pdf->Image($logo, $_PosX+($widthtouse/2), $_PosY+$ytop, $logoWidth, 0);
193
				elseif ($code && !empty($encoding))
194
				{
195
					$this->writeBarcode($pdf, $code, $encoding, $is2d, $_PosX+($widthtouse/2), $_PosY+$ytop, $widthtouse/2, $heighttouse);
196
				}
197
				$pdf->SetXY($_PosX+$xleft, $_PosY+$ytop);
198
				$pdf->MultiCell($widthtouse/2, $this->_Line_Height, $outputlangs->convToOutputCharset($textleft), 0, 'L');
199
			}
200
			elseif ($textleft == '%LOGO%')	 // left part logo right part text/barcode
201
			{
202
				if ($logo) $pdf->Image($logo, $_PosX+$xleft, $_PosY+$ytop, 0, $logoHeight);
203
				if ($code && !empty($encoding))
204
				{
205
					$this->writeBarcode($pdf, $code, $encoding, $is2d, $_PosX+$xleft+$logoWidth+1, $_PosY+$ytop, $widthtouse-$logoWidth-1, $heighttouse);
206
				} else {
207
					$pdf->SetXY($_PosX+$xleft+$logoWidth+1, $_PosY+$ytop);
208
					$pdf->MultiCell($widthtouse-$logoWidth-1, $this->_Line_Height, $outputlangs->convToOutputCharset($textright), 0, 'R');
209
				}
210
			}
211
			elseif ($textright == '%LOGO%')  // right part logo left part text/barcode
212
			{
213
				if ($logo) $pdf->Image($logo, $_PosX+$xleft+$widthtouse-$logoWidth+1, $_PosY+$ytop, 0, $logoHeight);
214
				if ($code && !empty($encoding))
215
				{
216
					$this->writeBarcode($pdf, $code, $encoding, $is2d, $_PosX+$xleft, $_PosY+$ytop, $widthtouse-$logoWidth-1, $heighttouse);
217
				} else {
218
					$pdf->SetXY($_PosX+$xleft, $_PosY+$ytop);
219
					$pdf->MultiCell($widthtouse-$logoWidth-1, $this->_Line_Height, $outputlangs->convToOutputCharset($textleft), 0, 'L');
220
				}
221
			}
222
			else	// text on halft left and text on half right
223
			{
224
				$pdf->SetXY($_PosX+$xleft, $_PosY+$ytop);
225
				$pdf->MultiCell(round($this->_Width/2), $this->_Line_Height, $outputlangs->convToOutputCharset($textleft), 0, 'L');
226
				$pdf->SetXY($_PosX+round($this->_Width/2), $_PosY+$ytop);
227
				$pdf->MultiCell(round($this->_Width/2)-2, $this->_Line_Height, $outputlangs->convToOutputCharset($textright), 0, 'R');
228
			}
229
		}
230
		else	// Only a right part
231
		{
232
			// Output right area
233
			if ($textright == '%LOGO%' && $logo) $pdf->Image($logo, $_PosX+$this->_Width-$widthtouse-$xleft, $_PosY+$ytop, 0, $logoHeight);
234
			elseif ($code && !empty($encoding))
235
			{
236
				$this->writeBarcode($pdf, $code, $encoding, $is2d, $_PosX+$this->_Width-$widthtouse-$xleft, $_PosY+$ytop, $widthtouse, $heighttouse);
237
			}
238
			else
239
			{
240
				$pdf->SetXY($_PosX+$xleft, $_PosY+$ytop);
241
				$pdf->MultiCell($this->_Width-$xleft, $this->_Line_Height, $outputlangs->convToOutputCharset($textright), 0, 'R');
242
			}
243
		}
244
245
		// Bottom
246
		if ($footer!='')
247
		{
248
			$pdf->SetXY($_PosX, $_PosY+$this->_Height-$this->_Line_Height-1);
249
			$pdf->Cell($this->_Width, $this->_Line_Height, $outputlangs->convToOutputCharset($footer), 0, 1, 'C');
250
		}
251
		//print "$_PosY+$this->_Height-$this->_Line_Height-1<br>\n";
252
253
		$this->_COUNTY++;
254
255
		if ($this->_COUNTY == $this->_Y_Number) {
256
			// Si on est en bas de page, on remonte le 'curseur' de position
257
			$this->_COUNTX++;
258
			$this->_COUNTY=0;
259
		}
260
261
		if ($this->_COUNTX == $this->_X_Number) {
262
			// Si on est en bout de page, alors on repart sur une nouvelle page
263
			$this->_COUNTX=0;
264
			$this->_COUNTY=0;
265
		}
266
	}
267
268
269
270
271
    // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
272
	/**
273
	 *	Function to build PDF on disk, then output on HTTP strem.
274
	 *
275
	 *	@param	array		$arrayofrecords		Array of record informations (array('textleft'=>,'textheader'=>, ..., 'id'=>,'photo'=>)
276
	 *	@param	Translate	$outputlangs		Lang object for output language
277
	 *	@param	string		$srctemplatepath	Full path of source filename for generator using a template file
278
	 *	@param	string		$outputdir			Output directory for pdf file
279
	 *  @param  string      $filename           Short file name of PDF output file
280
	 *	@return int								1=OK, 0=KO
281
	 */
282
	public function write_file($arrayofrecords, $outputlangs, $srctemplatepath, $outputdir = '', $filename = 'tmp_address_sheet.pdf')
283
	{
284
        // phpcs:enable
285
		global $user,$conf,$langs,$mysoc,$_Avery_Labels;
286
287
		$this->code=$srctemplatepath;
288
		$this->Tformat = $_Avery_Labels[$this->code];
289
		if (empty($this->Tformat)) { dol_print_error('', 'ErrorBadTypeForCard'.$this->code); exit; }
1 ignored issue
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
290
		$this->type = 'pdf';
291
        // standard format or custom
292
        if ($this->Tformat['paper-size']!='custom') {
293
            $this->format = $this->Tformat['paper-size'];
294
        } else {
295
            //custom
296
            $resolution= array($this->Tformat['custom_x'], $this->Tformat['custom_y']);
297
            $this->format = $resolution;
298
        }
299
300
		if (! is_object($outputlangs)) $outputlangs=$langs;
301
		// For backward compatibility with FPDF, force output charset to ISO, because FPDF expect text to be encoded in ISO
302
		if (! empty($conf->global->MAIN_USE_FPDF)) $outputlangs->charset_output='ISO-8859-1';
303
304
		// Load traductions files requiredby by page
305
		$outputlangs->loadLangs(array("main", "dict", "companies", "admin"));
306
307
		$title=$outputlangs->transnoentities('Labels');
308
		$keywords=$title." ".$outputlangs->convToOutputCharset($mysoc->name);
309
310
		$dir = (empty($outputdir)?$conf->adherent->dir_temp:$outputdir);
311
		$file = $dir."/".$filename;
312
313
		if (! file_exists($dir))
314
		{
315
			if (dol_mkdir($dir) < 0)
316
			{
317
				$this->error=$langs->trans("ErrorCanNotCreateDir", $dir);
318
				return 0;
319
			}
320
		}
321
322
		$pdf=pdf_getInstance($this->format, $this->Tformat['metric'], $this->Tformat['orientation']);
323
324
		if (class_exists('TCPDF'))
325
		{
326
			$pdf->setPrintHeader(false);
327
			$pdf->setPrintFooter(false);
328
		}
329
		$pdf->SetFont(pdf_getPDFFont($outputlangs));
330
331
		$pdf->SetTitle($title);
332
		$pdf->SetSubject($title);
333
		$pdf->SetCreator("Dolibarr ".DOL_VERSION);
334
		$pdf->SetAuthor($outputlangs->convToOutputCharset($user->getFullName($outputlangs)));
335
		$pdf->SetKeyWords($keywords);
336
		if (! empty($conf->global->MAIN_DISABLE_PDF_COMPRESSION)) $pdf->SetCompression(false);
337
338
		$pdf->SetMargins(0, 0);
339
		$pdf->SetAutoPageBreak(false);
340
341
		$this->_Metric_Doc = $this->Tformat['metric'];
342
		// Permet de commencer l'impression de l'etiquette desiree dans le cas ou la page a deja servie
343
		$posX=1;
344
		$posY=1;
345
		if ($posX > 0) $posX--; else $posX=0;
346
		if ($posY > 0) $posY--; else $posY=0;
347
		$this->_COUNTX = $posX;
348
		$this->_COUNTY = $posY;
349
		$this->_Set_Format($pdf, $this->Tformat);
350
351
352
		$pdf->Open();
353
		$pdf->AddPage();
354
355
356
		// Add each record
357
		foreach($arrayofrecords as $val)
358
		{
359
			// imprime le texte specifique sur la carte
360
			$this->addSticker($pdf, $outputlangs, $val);
361
		}
362
363
		//$pdf->SetXY(10, 295);
364
		//$pdf->Cell($this->_Width, $this->_Line_Height, 'XXX',0,1,'C');
365
366
367
		// Output to file
368
		$pdf->Output($file, 'F');
369
370
		if (! empty($conf->global->MAIN_UMASK))
371
			@chmod($file, octdec($conf->global->MAIN_UMASK));
372
373
		// Output to http stream
374
		clearstatcache();
375
376
		$attachment=true;
377
		if (! empty($conf->global->MAIN_DISABLE_FORCE_SAVEAS)) $attachment=false;
378
		$type=dol_mimetype($filename);
379
380
		//if ($encoding)   header('Content-Encoding: '.$encoding);
381
		if ($type)		 header('Content-Type: '.$type);
382
		if ($attachment) header('Content-Disposition: attachment; filename="'.$filename.'"');
383
		else header('Content-Disposition: inline; filename="'.$filename.'"');
384
385
		// Ajout directives pour resoudre bug IE
386
		header('Cache-Control: Public, must-revalidate');
387
		header('Pragma: public');
388
389
		readfile($file);
390
391
		$this->result = array('fullpath'=>$file);
392
393
		return 1;
394
	}
395
}
396