| Total Complexity | 69 |
| Total Lines | 360 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like pdf_tcpdflabel often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use pdf_tcpdflabel, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 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') |
||
| 394 | } |
||
| 395 | } |
||
| 396 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.