pdf_tcpdflabel::writeBarcode()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 8
dl 0
loc 6
rs 10
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
/* Copyright (C) 2003       Steve Dillon
4
 * Copyright (C) 2003       Laurent Passebecq
5
 * Copyright (C) 2001-2003  Rodolphe Quiedeville        <[email protected]>
6
 * Copyright (C) 2002-2003  Jean-Louis Bergamo	        <[email protected]>
7
 * Copyright (C) 2006-2013  Laurent Destailleur	        <[email protected]>
8
 * Copyright (C) 2015       Francis Appels              <[email protected]>
9
 * Copyright (C) 2024       Rafael San José             <[email protected]>
10
 *
11
 * This program is free software; you can redistribute it and/or modify
12
 * it under the terms of the GNU General Public License as published by
13
 * the Free Software Foundation; either version 3 of the License, or
14
 * (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
23
 */
24
25
use Dolibarr\Code\Core\Classes\Translate;
26
use Dolibarr\Lib\ViewMain;
27
28
/**
29
 *  \file       htdocs/core/modules/printsheet/doc/pdf_standardlabel.class.php
30
 *  \ingroup    core
31
 *  \brief      Fichier de la class permettant d'editer au format PDF des etiquettes au format Avery ou personnalise
32
 */
33
34
35
/**
36
 *  Class to generate stick sheet with format Avery or other personalised
37
 */
38
class pdf_tcpdflabel extends CommonStickerGenerator
39
{
40
    // define 1d barcode style
41
    private $_style1d = array(
42
        'position' => '',
43
        'align' => 'C',
44
        'stretch' => false,
45
        'fitwidth' => true,
46
        'cellfitalign' => '',
47
        'border' => false,
48
        'hpadding' => 'auto',
49
        'vpadding' => 'auto',
50
        'fgcolor' => array(0, 0, 0),
51
        'bgcolor' => false,
52
        'text' => true,
53
        'font' => 'helvetica',
54
        'fontsize' => 8,
55
        'stretchtext' => 4
56
    );
57
58
    // set style for 2d barcode
59
    private $_style2d = array(
60
        'border' => false,
61
        'vpadding' => 'auto',
62
        'hpadding' => 'auto',
63
        'fgcolor' => array(0, 0, 0),
64
        'bgcolor' => false,
65
        'module_width' => 1, // width of a single module in points
66
        'module_height' => 1 // height of a single module in points
67
    );
68
69
    private $_align2d = 'N';
70
71
    private $_xres = 0.4;
72
73
    /**
74
     * write barcode to pdf
75
     *
76
     * @param TCPDF $pdf PDF reference
77
     * @param string $code code to print
78
     * @param string $encoding type of barcode
79
     * @param boolean $is2d true if 2d barcode
80
     * @param int $x x position in user units
81
     * @param int $y y position in user units
82
     * @param int $w width in user units
83
     * @param int $h height in user units
84
     * @return void
85
     */
86
    private function writeBarcode(&$pdf, $code, $encoding, $is2d, $x, $y, $w, $h)
87
    {
88
        if ($is2d) {
89
            $pdf->write2DBarcode($code, $encoding, $x, $y, $w, $h, $this->_style2d, $this->_align2d);
90
        } else {
91
            $pdf->write1DBarcode($code, $encoding, $x, $y, $w, $h, $this->_xres, $this->_style1d);
92
        }
93
    }
94
95
    /**
96
     * Output a sticker on page at position _COUNTX, _COUNTY (_COUNTX and _COUNTY start from 0)
97
     *
98
     * @param TCPDF $pdf PDF reference
99
     * @param Translate $outputlangs Output langs
100
     * @param array $param Associative array containing label content and optional parameters
101
     * @return  void
102
     */
103
    public function addSticker(&$pdf, $outputlangs, $param)
104
    {
105
        global $mysoc, $conf;
106
107
        $textleft = $param['textleft'];
108
        $header = $param['textheader'];
109
        $footer = $param['textfooter'];
110
        $textright = $param['textright'];
111
        $code = $param['code'];
112
        $encoding = $param['encoding'];
113
        $is2d = $param['is2d'];
114
115
116
        // We are in a new page, then we must add a page
117
        if (($this->_COUNTX == 0) && ($this->_COUNTY == 0) and (!$this->_First == 1)) {
118
            $pdf->AddPage();
119
        }
120
        $this->_First = 0;
121
        $_PosX = $this->_Margin_Left + ($this->_COUNTX * ($this->_Width + $this->_X_Space));
122
        $_PosY = $this->_Margin_Top + ($this->_COUNTY * ($this->_Height + $this->_Y_Space));
123
124
        // Define logo
125
        $logo = $conf->mycompany->dir_output . '/logos/' . $mysoc->logo;
126
        if (!is_readable($logo)) {
127
            $logo = '';
128
            if (!empty($mysoc->logo_small) && is_readable($conf->mycompany->dir_output . '/logos/thumbs/' . $mysoc->logo_small)) {
129
                $logo = $conf->mycompany->dir_output . '/logos/thumbs/' . $mysoc->logo_small;
130
            } elseif (!empty($mysoc->logo) && is_readable($conf->mycompany->dir_output . '/logos/' . $mysoc->logo)) {
131
                $logo = $conf->mycompany->dir_output . '/logos/' . $mysoc->logo;
132
            }
133
        }
134
135
        $xleft = 2;
136
        $ytop = 2;
137
138
        // Top
139
        if ($header != '') {
140
            $pdf->SetXY($_PosX + $xleft, $_PosY + 1); // Only 1 mm and not ytop for top text
141
            $pdf->Cell(2 * strlen($header), $this->_Line_Height, $outputlangs->convToOutputCharset($header), 0, 1, 'C');
142
        }
143
144
        $ytop += (empty($header) ? 0 : (1 + $this->_Line_Height));
145
146
        // Define widthtouse and heighttouse
147
        $pageMargins = $pdf->getMargins();
148
        $maxwidthtouse = round($this->_Width - 2 * $xleft);
149
        $maxheighttouse = round($this->_Height - 2 * $ytop);
150
        $maxheighttouse -= (empty($footer) ? 0 : (1 + $this->_Line_Height));
151
        $defaultratio = ($maxwidthtouse / $maxheighttouse);
152
        $widthtouse = $maxwidthtouse;
153
        $heighttouse = $maxheighttouse;
154
        $logoHeight = $heighttouse;
155
        $logoWidth = $widthtouse;
156
157
        //var_dump($this->_Width.'x'.$this->_Height.' with border and scale '.$imgscale.' => max '.$maxwidthtouse.'x'.$maxheighttouse.' => We use '.$widthtouse.'x'.$heighttouse);exit;
158
159
        // Center
160
        if ($textright == '') { // Only a left part
161
            // Output left area
162
            if ($textleft == '%LOGO%' && $logo) {
163
                $pdf->Image($logo, $_PosX + $xleft, $_PosY + $ytop, 0, $logoHeight);
164
            } elseif ($code && !empty($encoding)) {
165
                $this->writeBarcode($pdf, $code, $encoding, $is2d, $_PosX + $xleft, $_PosY + $ytop, $widthtouse, $heighttouse);
166
            } else {
167
                $pdf->SetXY($_PosX + $xleft, $_PosY + $ytop);
168
                $pdf->MultiCell($this->_Width, $this->_Line_Height, $outputlangs->convToOutputCharset($textleft), 0, 'L');
169
            }
170
        } elseif ($textleft != '' && $textright != '') {    // left and right part
171
            $logoHeight = $heighttouse / 2;
172
            $logoWidth = $widthtouse / 2;
173
            if (($textleft == '%LOGO%' || $textleft == '%PHOTO%' || $textleft == '%BARCODE%') && !strstr($textright, '%')) {     // left part logo/barcode right part text
174
                if ($textleft == '%LOGO%' && $logo) {
175
                    $pdf->Image($logo, $_PosX + $xleft, $_PosY + $ytop, $logoWidth, 0);
176
                } elseif ($code && !empty($encoding)) {
177
                    $this->writeBarcode($pdf, $code, $encoding, $is2d, $_PosX + $xleft, $_PosY + $ytop, $widthtouse / 2, $heighttouse);
178
                }
179
                $pdf->SetXY($_PosX + ($widthtouse / 2), $_PosY + $ytop);
180
                $pdf->MultiCell($widthtouse / 2, $this->_Line_Height, $outputlangs->convToOutputCharset($textright), 0, 'R');
181
            } elseif (($textright == '%LOGO%' || $textright == '%PHOTO%' || $textright == '%BARCODE%') && !strstr($textleft, '%')) { // right part logo/barcode left part text
182
                if ($textright == '%LOGO%' && $logo) {
183
                    $pdf->Image($logo, $_PosX + ($widthtouse / 2), $_PosY + $ytop, $logoWidth, 0);
184
                } elseif ($code && !empty($encoding)) {
185
                    $this->writeBarcode($pdf, $code, $encoding, $is2d, $_PosX + ($widthtouse / 2), $_PosY + $ytop, $widthtouse / 2, $heighttouse);
186
                }
187
                $pdf->SetXY($_PosX + $xleft, $_PosY + $ytop);
188
                $pdf->MultiCell($widthtouse / 2, $this->_Line_Height, $outputlangs->convToOutputCharset($textleft), 0, 'L');
189
            } elseif ($textleft == '%LOGO%') {   // left part logo right part text/barcode
190
                if ($logo) {
191
                    $pdf->Image($logo, $_PosX + $xleft, $_PosY + $ytop, 0, $logoHeight);
192
                }
193
                if ($code && !empty($encoding)) {
194
                    $this->writeBarcode($pdf, $code, $encoding, $is2d, $_PosX + $xleft + $logoWidth + 1, $_PosY + $ytop, $widthtouse - $logoWidth - 1, $heighttouse);
195
                } else {
196
                    $pdf->SetXY($_PosX + $xleft + $logoWidth + 1, $_PosY + $ytop);
197
                    $pdf->MultiCell($widthtouse - $logoWidth - 1, $this->_Line_Height, $outputlangs->convToOutputCharset($textright), 0, 'R');
198
                }
199
            } elseif ($textright == '%LOGO%') {  // right part logo left part text/barcode
200
                if ($logo) {
201
                    $pdf->Image($logo, $_PosX + $xleft + $widthtouse - $logoWidth + 1, $_PosY + $ytop, 0, $logoHeight);
202
                }
203
                if ($code && !empty($encoding)) {
204
                    $this->writeBarcode($pdf, $code, $encoding, $is2d, $_PosX + $xleft, $_PosY + $ytop, $widthtouse - $logoWidth - 1, $heighttouse);
205
                } else {
206
                    $pdf->SetXY($_PosX + $xleft, $_PosY + $ytop);
207
                    $pdf->MultiCell($widthtouse - $logoWidth - 1, $this->_Line_Height, $outputlangs->convToOutputCharset($textleft), 0, 'L');
208
                }
209
            } else { // text on halft left and text on half right
210
                $pdf->SetXY($_PosX + $xleft, $_PosY + $ytop);
211
                $pdf->MultiCell(round($this->_Width / 2), $this->_Line_Height, $outputlangs->convToOutputCharset($textleft), 0, 'L');
212
                $pdf->SetXY($_PosX + round($this->_Width / 2), $_PosY + $ytop);
213
                $pdf->MultiCell(round($this->_Width / 2) - 2, $this->_Line_Height, $outputlangs->convToOutputCharset($textright), 0, 'R');
214
            }
215
        } else { // Only a right part
216
            // Output right area
217
            if ($textright == '%LOGO%' && $logo) {
218
                $pdf->Image($logo, $_PosX + $this->_Width - $widthtouse - $xleft, $_PosY + $ytop, 0, $logoHeight);
219
            } elseif ($code && !empty($encoding)) {
220
                $this->writeBarcode($pdf, $code, $encoding, $is2d, $_PosX + $this->_Width - $widthtouse - $xleft, $_PosY + $ytop, $widthtouse, $heighttouse);
221
            } else {
222
                $pdf->SetXY($_PosX + $xleft, $_PosY + $ytop);
223
                $pdf->MultiCell($this->_Width - $xleft, $this->_Line_Height, $outputlangs->convToOutputCharset($textright), 0, 'R');
224
            }
225
        }
226
227
        // Bottom
228
        if ($footer != '') {
229
            $pdf->SetXY($_PosX, $_PosY + $this->_Height - $this->_Line_Height - 1);
230
            $pdf->Cell($this->_Width, $this->_Line_Height, $outputlangs->convToOutputCharset($footer), 0, 1, 'C');
231
        }
232
        //print "$_PosY+$this->_Height-$this->_Line_Height-1<br>\n";
233
234
        $this->_COUNTY++;
235
236
        if ($this->_COUNTY == $this->_Y_Number) {
237
            // Si on est en bas de page, on remonte le 'curseur' de position
238
            $this->_COUNTX++;
239
            $this->_COUNTY = 0;
240
        }
241
242
        if ($this->_COUNTX == $this->_X_Number) {
243
            // Si on est en bout de page, alors on repart sur une nouvelle page
244
            $this->_COUNTX = 0;
245
            $this->_COUNTY = 0;
246
        }
247
    }
248
249
250
251
252
    // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
253
254
    /**
255
     *  Function to build PDF on disk, then output on HTTP stream.
256
     *
257
     * @param array $arrayofrecords Array of record information (array('textleft'=>,'textheader'=>, ..., 'id'=>,'photo'=>)
258
     * @param Translate $outputlangs Lang object for output language
259
     * @param string $srctemplatepath Full path of source filename for generator using a template file
260
     * @param string $outputdir Output directory for pdf file
261
     * @param string $filename Short file name of PDF output file
262
     * @return int                             1=OK, 0=KO
263
     */
264
    public function write_file($arrayofrecords, $outputlangs, $srctemplatepath, $outputdir = '', $filename = 'tmp_address_sheet.pdf')
265
    {
266
        // phpcs:enable
267
        global $user, $conf, $langs, $mysoc, $_Avery_Labels;
268
269
        $this->code = $srctemplatepath;
270
        $this->Tformat = $_Avery_Labels[$this->code];
271
        if (empty($this->Tformat)) {
272
            dol_print_error(null, 'ErrorBadTypeForCard' . $this->code);
273
            exit;
0 ignored issues
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...
274
        }
275
        $this->type = 'pdf';
276
        // standard format or custom
277
        if ($this->Tformat['paper-size'] != 'custom') {
278
            $this->format = $this->Tformat['paper-size'];
279
        } else {
280
            //custom
281
            $resolution = array($this->Tformat['custom_x'], $this->Tformat['custom_y']);
282
            $this->format = $resolution;
283
        }
284
285
        if (!is_object($outputlangs)) {
286
            $outputlangs = $langs;
287
        }
288
        // For backward compatibility with FPDF, force output charset to ISO, because FPDF expect text to be encoded in ISO
289
        if (getDolGlobalString('MAIN_USE_FPDF')) {
290
            $outputlangs->charset_output = 'ISO-8859-1';
291
        }
292
293
        // Load traductions files required by page
294
        $outputlangs->loadLangs(array("main", "dict", "companies", "admin"));
295
296
        $title = $outputlangs->transnoentities('Labels');
297
        $keywords = $title . " " . $outputlangs->convToOutputCharset($mysoc->name);
298
299
        $dir = (empty($outputdir) ? $conf->adherent->dir_temp : $outputdir);
300
        $file = $dir . "/" . $filename;
301
302
        if (!file_exists($dir)) {
303
            if (dol_mkdir($dir) < 0) {
304
                $this->error = $langs->trans("ErrorCanNotCreateDir", $dir);
305
                return 0;
306
            }
307
        }
308
309
        $pdf = pdf_getInstance($this->format, $this->Tformat['metric'], $this->Tformat['orientation']);
310
311
        if (class_exists('TCPDF')) {
312
            $pdf->setPrintHeader(false);
313
            $pdf->setPrintFooter(false);
314
        }
315
        $pdf->SetFont(pdf_getPDFFont($outputlangs));
316
317
        $pdf->SetTitle($title);
318
        $pdf->SetSubject($title);
319
        $pdf->SetCreator("Dolibarr " . DOL_VERSION);
320
        $pdf->SetAuthor($outputlangs->convToOutputCharset($user->getFullName($outputlangs)));
321
        $pdf->SetKeyWords($keywords);
322
        if (getDolGlobalString('MAIN_DISABLE_PDF_COMPRESSION')) {
323
            $pdf->SetCompression(false);
324
        }
325
326
        $pdf->SetMargins(0, 0);
327
        $pdf->SetAutoPageBreak(false);
328
329
        $this->_Metric_Doc = $this->Tformat['metric'];
330
        // Permet de commencer l'impression de l'etiquette desiree dans le cas ou la page a deja service
331
        $posX = 1;
332
        $posY = 1;
333
        if ($posX > 0) {
334
            $posX--;
335
        } else {
336
            $posX = 0;
337
        }
338
        if ($posY > 0) {
339
            $posY--;
340
        } else {
341
            $posY = 0;
342
        }
343
        $this->_COUNTX = $posX;
344
        $this->_COUNTY = $posY;
345
        $this->_Set_Format($pdf, $this->Tformat);
346
347
348
        $pdf->Open();
349
        $pdf->AddPage();
350
351
352
        // Add each record
353
        foreach ($arrayofrecords as $val) {
354
            // imprime le texte specifique sur la carte
355
            $this->addSticker($pdf, $outputlangs, $val);
356
        }
357
358
        //$pdf->SetXY(10, 295);
359
        //$pdf->Cell($this->_Width, $this->_Line_Height, 'XXX',0,1,'C');
360
361
362
        // Output to file
363
        $pdf->Output($file, 'F');
364
365
        dolChmod($file);
366
367
        $this->result = array('fullpath' => $file);
368
369
        return 1;
370
    }
371
}
372