| Conditions | 44 |
| Paths | > 20000 |
| Total Lines | 316 |
| Code Lines | 187 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 167 | public function write_file($object, $outputlangs, $srctemplatepath = '', $hidedetails = 0, $hidedesc = 0, $hideref = 0) |
||
| 168 | { |
||
| 169 | // phpcs:enable |
||
| 170 | global $user,$langs,$conf,$mysoc,$db,$hookmanager; |
||
| 171 | |||
| 172 | if (! is_object($outputlangs)) $outputlangs=$langs; |
||
| 173 | // For backward compatibility with FPDF, force output charset to ISO, because FPDF expect text to be encoded in ISO |
||
| 174 | if (! empty($conf->global->MAIN_USE_FPDF)) $outputlangs->charset_output='ISO-8859-1'; |
||
| 175 | |||
| 176 | // Load traductions files requiredby by page |
||
| 177 | $outputlangs->loadLangs(array("main", "interventions", "dict", "companies")); |
||
| 178 | |||
| 179 | if ($conf->ficheinter->dir_output) |
||
| 180 | { |
||
| 181 | $object->fetch_thirdparty(); |
||
| 182 | |||
| 183 | // Definition of $dir and $file |
||
| 184 | if ($object->specimen) |
||
| 185 | { |
||
| 186 | $dir = $conf->ficheinter->dir_output; |
||
| 187 | $file = $dir . "/SPECIMEN.pdf"; |
||
| 188 | } |
||
| 189 | else |
||
| 190 | { |
||
| 191 | $objectref = dol_sanitizeFileName($object->ref); |
||
| 192 | $dir = $conf->ficheinter->dir_output . "/" . $objectref; |
||
| 193 | $file = $dir . "/" . $objectref . ".pdf"; |
||
| 194 | } |
||
| 195 | |||
| 196 | if (! file_exists($dir)) |
||
| 197 | { |
||
| 198 | if (dol_mkdir($dir) < 0) |
||
| 199 | { |
||
| 200 | $this->error=$langs->transnoentities("ErrorCanNotCreateDir", $dir); |
||
| 201 | return 0; |
||
| 202 | } |
||
| 203 | } |
||
| 204 | |||
| 205 | if (file_exists($dir)) |
||
| 206 | { |
||
| 207 | // Add pdfgeneration hook |
||
| 208 | if (! is_object($hookmanager)) |
||
| 209 | { |
||
| 210 | include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php'; |
||
| 211 | $hookmanager=new HookManager($this->db); |
||
| 212 | } |
||
| 213 | |||
| 214 | $hookmanager->initHooks(array('pdfgeneration')); |
||
| 215 | $parameters=array('file'=>$file,'object'=>$object,'outputlangs'=>$outputlangs); |
||
| 216 | global $action; |
||
| 217 | $reshook=$hookmanager->executeHooks('beforePDFCreation', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks |
||
| 218 | |||
| 219 | $nblines = count($object->lines); |
||
| 220 | |||
| 221 | // Create pdf instance |
||
| 222 | $pdf=pdf_getInstance($this->format); |
||
| 223 | $default_font_size = pdf_getPDFFontSize($outputlangs); // Must be after pdf_getInstance |
||
| 224 | $heightforinfotot = 50; // Height reserved to output the info and total part |
||
| 225 | $heightforfreetext= (isset($conf->global->MAIN_PDF_FREETEXT_HEIGHT)?$conf->global->MAIN_PDF_FREETEXT_HEIGHT:5); // Height reserved to output the free text on last page |
||
| 226 | $heightforfooter = $this->marge_basse + 8; // Height reserved to output the footer (value include bottom margin) |
||
| 227 | if ($conf->global->MAIN_GENERATE_DOCUMENTS_SHOW_FOOT_DETAILS >0) $heightforfooter+= 6; |
||
| 228 | $pdf->SetAutoPageBreak(1, 0); |
||
| 229 | |||
| 230 | if (class_exists('TCPDF')) |
||
| 231 | { |
||
| 232 | $pdf->setPrintHeader(false); |
||
| 233 | $pdf->setPrintFooter(false); |
||
| 234 | } |
||
| 235 | $pdf->SetFont(pdf_getPDFFont($outputlangs)); |
||
| 236 | // Set path to the background PDF File |
||
| 237 | if (! empty($conf->global->MAIN_ADD_PDF_BACKGROUND)) |
||
| 238 | { |
||
| 239 | $pagecount = $pdf->setSourceFile($conf->mycompany->dir_output.'/'.$conf->global->MAIN_ADD_PDF_BACKGROUND); |
||
| 240 | $tplidx = $pdf->importPage(1); |
||
| 241 | } |
||
| 242 | |||
| 243 | $pdf->Open(); |
||
| 244 | $pagenb=0; |
||
| 245 | $pdf->SetDrawColor(128, 128, 128); |
||
| 246 | |||
| 247 | $pdf->SetTitle($outputlangs->convToOutputCharset($object->ref)); |
||
| 248 | $pdf->SetSubject($outputlangs->transnoentities("InterventionCard")); |
||
| 249 | $pdf->SetCreator("Dolibarr ".DOL_VERSION); |
||
| 250 | $pdf->SetAuthor($outputlangs->convToOutputCharset($user->getFullName($outputlangs))); |
||
| 251 | $pdf->SetKeyWords($outputlangs->convToOutputCharset($object->ref)." ".$outputlangs->transnoentities("InterventionCard")); |
||
| 252 | if (! empty($conf->global->MAIN_DISABLE_PDF_COMPRESSION)) $pdf->SetCompression(false); |
||
| 253 | |||
| 254 | $pdf->SetMargins($this->marge_gauche, $this->marge_haute, $this->marge_droite); // Left, Top, Right |
||
| 255 | |||
| 256 | // New page |
||
| 257 | $pdf->AddPage(); |
||
| 258 | if (! empty($tplidx)) $pdf->useTemplate($tplidx); |
||
| 259 | $pagenb++; |
||
| 260 | $this->_pagehead($pdf, $object, 1, $outputlangs); |
||
| 261 | $pdf->SetFont('', '', $default_font_size - 1); |
||
| 262 | $pdf->SetTextColor(0, 0, 0); |
||
| 263 | |||
| 264 | $tab_top = 90; |
||
| 265 | $tab_top_newpage = (empty($conf->global->MAIN_PDF_DONOTREPEAT_HEAD)?42:10); |
||
| 266 | $tab_height = 130; |
||
| 267 | $tab_height_newpage = 150; |
||
| 268 | |||
| 269 | // Affiche notes |
||
| 270 | $notetoshow=empty($object->note_public)?'':$object->note_public; |
||
| 271 | if ($notetoshow) |
||
| 272 | { |
||
| 273 | $substitutionarray=pdf_getSubstitutionArray($outputlangs, null, $object); |
||
| 274 | complete_substitutions_array($substitutionarray, $outputlangs, $object); |
||
| 275 | $notetoshow = make_substitutions($notetoshow, $substitutionarray, $outputlangs); |
||
| 276 | $notetoshow = convertBackOfficeMediasLinksToPublicLinks($notetoshow); |
||
| 277 | |||
| 278 | $tab_top = 88; |
||
| 279 | |||
| 280 | $pdf->SetFont('', '', $default_font_size - 1); |
||
| 281 | $pdf->writeHTMLCell(190, 3, $this->posxdesc-1, $tab_top, dol_htmlentitiesbr($notetoshow), 0, 1); |
||
| 282 | $nexY = $pdf->GetY(); |
||
| 283 | $height_note=$nexY-$tab_top; |
||
| 284 | |||
| 285 | // Rect prend une longueur en 3eme param |
||
| 286 | $pdf->SetDrawColor(192, 192, 192); |
||
| 287 | $pdf->Rect($this->marge_gauche, $tab_top-1, $this->page_largeur-$this->marge_gauche-$this->marge_droite, $height_note+1); |
||
| 288 | |||
| 289 | $tab_height = $tab_height - $height_note; |
||
| 290 | $tab_top = $nexY+6; |
||
| 291 | } |
||
| 292 | else |
||
| 293 | { |
||
| 294 | $height_note=0; |
||
| 295 | } |
||
| 296 | |||
| 297 | $iniY = $tab_top + 7; |
||
| 298 | $curY = $tab_top + 7; |
||
| 299 | $nexY = $tab_top + 7; |
||
| 300 | |||
| 301 | $pdf->SetXY($this->marge_gauche, $tab_top); |
||
| 302 | $pdf->MultiCell(190, 5, $outputlangs->transnoentities("Description"), 0, 'L', 0); |
||
| 303 | $pdf->line($this->marge_gauche, $tab_top + 5, $this->page_largeur-$this->marge_droite, $tab_top + 5); |
||
| 304 | |||
| 305 | $pdf->SetFont('', '', $default_font_size - 1); |
||
| 306 | |||
| 307 | $pdf->SetXY($this->marge_gauche, $tab_top + 5); |
||
| 308 | $text=$object->description; |
||
| 309 | if ($object->duration > 0) |
||
| 310 | { |
||
| 311 | $totaltime=convertSecondToTime($object->duration, 'all', $conf->global->MAIN_DURATION_OF_WORKDAY); |
||
| 312 | $text.=($text?' - ':'').$langs->trans("Total").": ".$totaltime; |
||
| 313 | } |
||
| 314 | $desc=dol_htmlentitiesbr($text, 1); |
||
| 315 | //print $outputlangs->convToOutputCharset($desc); exit; |
||
| 316 | |||
| 317 | $pdf->writeHTMLCell(180, 3, 10, $tab_top + 5, $outputlangs->convToOutputCharset($desc), 0, 1); |
||
| 318 | $nexY = $pdf->GetY(); |
||
| 319 | |||
| 320 | $pdf->line($this->marge_gauche, $nexY, $this->page_largeur-$this->marge_droite, $nexY); |
||
| 321 | |||
| 322 | $nblines = count($object->lines); |
||
| 323 | |||
| 324 | // Loop on each lines |
||
| 325 | for ($i = 0; $i < $nblines; $i++) |
||
| 326 | { |
||
| 327 | $objectligne = $object->lines[$i]; |
||
| 328 | |||
| 329 | $valide = empty($objectligne->id) ? 0 : $objectligne->fetch($objectligne->id); |
||
| 330 | if ($valide > 0 || $object->specimen) |
||
| 331 | { |
||
| 332 | $curY = $nexY; |
||
| 333 | $pdf->SetFont('', '', $default_font_size - 1); // Into loop to work with multipage |
||
| 334 | $pdf->SetTextColor(0, 0, 0); |
||
| 335 | |||
| 336 | $pdf->setTopMargin($tab_top_newpage); |
||
| 337 | $pdf->setPageOrientation('', 1, $heightforfooter+$heightforfreetext+$heightforinfotot); // The only function to edit the bottom margin of current page to set it. |
||
| 338 | $pageposbefore=$pdf->getPage(); |
||
| 339 | |||
| 340 | // Description of product line |
||
| 341 | $curX = $this->posxdesc-1; |
||
| 342 | |||
| 343 | // Description of product line |
||
| 344 | if (empty($conf->global->FICHINTER_DATE_WITHOUT_HOUR)) { |
||
| 345 | $txt=$outputlangs->transnoentities("Date")." : ".dol_print_date($objectligne->datei, 'dayhour', false, $outputlangs, true); |
||
| 346 | } else { |
||
| 347 | $txt=$outputlangs->transnoentities("Date")." : ".dol_print_date($objectligne->datei, 'day', false, $outputlangs, true); |
||
| 348 | } |
||
| 349 | |||
| 350 | if ($objectligne->duration > 0) |
||
| 351 | { |
||
| 352 | $txt.=" - ".$outputlangs->transnoentities("Duration")." : ".convertSecondToTime($objectligne->duration); |
||
| 353 | } |
||
| 354 | $txt='<strong>'.dol_htmlentitiesbr($txt, 1, $outputlangs->charset_output).'</strong>'; |
||
| 355 | $desc=dol_htmlentitiesbr($objectligne->desc, 1); |
||
| 356 | |||
| 357 | $pdf->startTransaction(); |
||
| 358 | $pdf->writeHTMLCell(0, 0, $curX, $curY + 1, dol_concatdesc($txt, $desc), 0, 1, 0); |
||
| 359 | $pageposafter=$pdf->getPage(); |
||
| 360 | if ($pageposafter > $pageposbefore) // There is a pagebreak |
||
| 361 | { |
||
| 362 | $pdf->rollbackTransaction(true); |
||
| 363 | $pageposafter=$pageposbefore; |
||
| 364 | //print $pageposafter.'-'.$pageposbefore;exit; |
||
| 365 | $pdf->setPageOrientation('', 1, $heightforfooter); // The only function to edit the bottom margin of current page to set it. |
||
| 366 | $pdf->writeHTMLCell(0, 0, $curX, $curY, dol_concatdesc($txt, $desc), 0, 1, 0); |
||
| 367 | $pageposafter=$pdf->getPage(); |
||
| 368 | $posyafter=$pdf->GetY(); |
||
| 369 | //var_dump($posyafter); var_dump(($this->page_hauteur - ($heightforfooter+$heightforfreetext+$heightforinfotot))); exit; |
||
| 370 | if ($posyafter > ($this->page_hauteur - ($heightforfooter+$heightforfreetext+$heightforinfotot))) // There is no space left for total+free text |
||
| 371 | { |
||
| 372 | if ($i == ($nblines-1)) // No more lines, and no space left to show total, so we create a new page |
||
| 373 | { |
||
| 374 | $pdf->AddPage('', '', true); |
||
| 375 | if (! empty($tplidx)) $pdf->useTemplate($tplidx); |
||
| 376 | if (empty($conf->global->MAIN_PDF_DONOTREPEAT_HEAD)) $this->_pagehead($pdf, $object, 0, $outputlangs); |
||
| 377 | $pdf->setPage($pageposafter+1); |
||
| 378 | } |
||
| 379 | } |
||
| 380 | } |
||
| 381 | else // No pagebreak |
||
| 382 | { |
||
| 383 | $pdf->commitTransaction(); |
||
| 384 | } |
||
| 385 | |||
| 386 | $nexY = $pdf->GetY(); |
||
| 387 | $pageposafter=$pdf->getPage(); |
||
| 388 | $pdf->setPage($pageposbefore); |
||
| 389 | $pdf->setTopMargin($this->marge_haute); |
||
| 390 | $pdf->setPageOrientation('', 1, 0); // The only function to edit the bottom margin of current page to set it. |
||
| 391 | |||
| 392 | // We suppose that a too long description is moved completely on next page |
||
| 393 | if ($pageposafter > $pageposbefore) { |
||
| 394 | $pdf->setPage($pageposafter); $curY = $tab_top_newpage; |
||
| 395 | } |
||
| 396 | |||
| 397 | $pdf->SetFont('', '', $default_font_size - 1); // On repositionne la police par defaut |
||
| 398 | |||
| 399 | // Detect if some page were added automatically and output _tableau for past pages |
||
| 400 | while ($pagenb < $pageposafter) |
||
| 401 | { |
||
| 402 | $pdf->setPage($pagenb); |
||
| 403 | if ($pagenb == 1) |
||
| 404 | { |
||
| 405 | $this->_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforfooter, 0, $outputlangs, 0, 1); |
||
| 406 | } |
||
| 407 | else |
||
| 408 | { |
||
| 409 | $this->_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforfooter, 0, $outputlangs, 1, 1); |
||
| 410 | } |
||
| 411 | $this->_pagefoot($pdf, $object, $outputlangs, 1); |
||
| 412 | $pagenb++; |
||
| 413 | $pdf->setPage($pagenb); |
||
| 414 | $pdf->setPageOrientation('', 1, 0); // The only function to edit the bottom margin of current page to set it. |
||
| 415 | if (empty($conf->global->MAIN_PDF_DONOTREPEAT_HEAD)) $this->_pagehead($pdf, $object, 0, $outputlangs); |
||
| 416 | } |
||
| 417 | if (isset($object->lines[$i+1]->pagebreak) && $object->lines[$i+1]->pagebreak) |
||
| 418 | { |
||
| 419 | if ($pagenb == 1) |
||
| 420 | { |
||
| 421 | $this->_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforfooter, 0, $outputlangs, 0, 1); |
||
| 422 | } |
||
| 423 | else |
||
| 424 | { |
||
| 425 | $this->_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforfooter, 0, $outputlangs, 1, 1); |
||
| 426 | } |
||
| 427 | $this->_pagefoot($pdf, $object, $outputlangs, 1); |
||
| 428 | // New page |
||
| 429 | $pdf->AddPage(); |
||
| 430 | if (! empty($tplidx)) $pdf->useTemplate($tplidx); |
||
| 431 | $pagenb++; |
||
| 432 | if (empty($conf->global->MAIN_PDF_DONOTREPEAT_HEAD)) $this->_pagehead($pdf, $object, 0, $outputlangs); |
||
| 433 | } |
||
| 434 | } |
||
| 435 | } |
||
| 436 | |||
| 437 | // Show square |
||
| 438 | if ($pagenb == 1) |
||
| 439 | { |
||
| 440 | $this->_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforinfotot - $heightforfreetext - $heightforfooter, 0, $outputlangs, 0, 0); |
||
| 441 | $bottomlasttab=$this->page_hauteur - $heightforinfotot - $heightforfreetext - $heightforfooter + 1; |
||
| 442 | } |
||
| 443 | else |
||
| 444 | { |
||
| 445 | $this->_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforinfotot - $heightforfreetext - $heightforfooter, 0, $outputlangs, 1, 0); |
||
| 446 | $bottomlasttab=$this->page_hauteur - $heightforinfotot - $heightforfreetext - $heightforfooter + 1; |
||
| 447 | } |
||
| 448 | |||
| 449 | $this->_pagefoot($pdf, $object, $outputlangs); |
||
| 450 | if (method_exists($pdf, 'AliasNbPages')) $pdf->AliasNbPages(); |
||
| 451 | |||
| 452 | $pdf->Close(); |
||
| 453 | $pdf->Output($file, 'F'); |
||
| 454 | |||
| 455 | // Add pdfgeneration hook |
||
| 456 | $hookmanager->initHooks(array('pdfgeneration')); |
||
| 457 | $parameters=array('file'=>$file,'object'=>$object,'outputlangs'=>$outputlangs); |
||
| 458 | global $action; |
||
| 459 | $reshook=$hookmanager->executeHooks('afterPDFCreation', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks |
||
| 460 | if ($reshook < 0) |
||
| 461 | { |
||
| 462 | $this->error = $hookmanager->error; |
||
| 463 | $this->errors = $hookmanager->errors; |
||
| 464 | } |
||
| 465 | |||
| 466 | if (! empty($conf->global->MAIN_UMASK)) |
||
| 467 | @chmod($file, octdec($conf->global->MAIN_UMASK)); |
||
| 468 | |||
| 469 | $this->result = array('fullpath'=>$file); |
||
| 470 | |||
| 471 | return 1; |
||
| 472 | } |
||
| 473 | else |
||
| 474 | { |
||
| 475 | $this->error=$langs->trans("ErrorCanNotCreateDir", $dir); |
||
| 476 | return 0; |
||
| 477 | } |
||
| 478 | } |
||
| 479 | else |
||
| 480 | { |
||
| 481 | $this->error=$langs->trans("ErrorConstantNotDefined", "FICHEINTER_OUTPUTDIR"); |
||
| 482 | return 0; |
||
| 483 | } |
||
| 741 |