Conditions | 46 |
Paths | > 20000 |
Total Lines | 356 |
Code Lines | 222 |
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 |
||
148 | public function write_file($object, $outputlangs) |
||
149 | { |
||
150 | // phpcs:enable |
||
151 | global $conf, $hookmanager, $langs, $user; |
||
152 | |||
153 | if (!is_object($outputlangs)) { |
||
154 | $outputlangs = $langs; |
||
155 | } |
||
156 | // For backward compatibility with FPDF, force output charset to ISO, because FPDF expect text to be encoded in ISO |
||
157 | if (getDolGlobalString('MAIN_USE_FPDF')) { |
||
158 | $outputlangs->charset_output = 'ISO-8859-1'; |
||
159 | } |
||
160 | |||
161 | // Load traductions files required by page |
||
162 | $outputlangs->loadLangs(array("main", "dict", "companies", "projects")); |
||
163 | |||
164 | if ($conf->project->multidir_output[$object->entity]) { |
||
165 | //$nblines = count($object->lines); // This is set later with array of tasks |
||
166 | |||
167 | $objectref = dol_sanitizeFileName($object->ref); |
||
168 | $dir = $conf->project->multidir_output[$object->entity]; |
||
169 | if (!preg_match('/specimen/i', $objectref)) { |
||
170 | $dir .= "/" . $objectref; |
||
171 | } |
||
172 | $file = $dir . "/" . $objectref . ".pdf"; |
||
173 | |||
174 | if (!file_exists($dir)) { |
||
175 | if (dol_mkdir($dir) < 0) { |
||
176 | $this->error = $langs->transnoentities("ErrorCanNotCreateDir", $dir); |
||
177 | return 0; |
||
178 | } |
||
179 | } |
||
180 | |||
181 | if (file_exists($dir)) { |
||
182 | // Add pdfgeneration hook |
||
183 | if (!is_object($hookmanager)) { |
||
184 | $hookmanager = new HookManager($this->db); |
||
185 | } |
||
186 | $hookmanager->initHooks(array('pdfgeneration')); |
||
187 | $parameters = array('file' => $file, 'object' => $object, 'outputlangs' => $outputlangs); |
||
188 | global $action; |
||
189 | $reshook = $hookmanager->executeHooks('beforePDFCreation', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks |
||
190 | |||
191 | // Create pdf instance |
||
192 | $pdf = pdf_getInstance($this->format); |
||
193 | $default_font_size = pdf_getPDFFontSize($outputlangs); // Must be after pdf_getInstance |
||
194 | $pdf->SetAutoPageBreak(1, 0); |
||
195 | |||
196 | $heightforinfotot = 40; // Height reserved to output the info and total part |
||
197 | $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 |
||
198 | $heightforfooter = $this->marge_basse + 8; // Height reserved to output the footer (value include bottom margin) |
||
199 | if (getDolGlobalString('MAIN_GENERATE_DOCUMENTS_SHOW_FOOT_DETAILS')) { |
||
200 | $heightforfooter += 6; |
||
201 | } |
||
202 | |||
203 | if (class_exists('TCPDF')) { |
||
204 | $pdf->setPrintHeader(false); |
||
205 | $pdf->setPrintFooter(false); |
||
206 | } |
||
207 | $pdf->SetFont(pdf_getPDFFont($outputlangs)); |
||
208 | // Set path to the background PDF File |
||
209 | if (getDolGlobalString('MAIN_ADD_PDF_BACKGROUND')) { |
||
210 | $pagecount = $pdf->setSourceFile($conf->mycompany->dir_output . '/' . getDolGlobalString('MAIN_ADD_PDF_BACKGROUND')); |
||
211 | $tplidx = $pdf->importPage(1); |
||
212 | } |
||
213 | |||
214 | // Complete object by loading several other information |
||
215 | $task = new Task($this->db); |
||
216 | $tasksarray = $task->getTasksArray(0, 0, $object->id); |
||
217 | |||
218 | if (!$object->id > 0) { // Special case when used with object = specimen, we may return all lines |
||
219 | $tasksarray = array_slice($tasksarray, 0, min(5, count($tasksarray))); |
||
220 | } |
||
221 | |||
222 | $object->lines = $tasksarray; |
||
223 | $nblines = count($object->lines); |
||
224 | |||
225 | $pdf->Open(); |
||
226 | $pagenb = 0; |
||
227 | $pdf->SetDrawColor(128, 128, 128); |
||
228 | |||
229 | $pdf->SetTitle($outputlangs->convToOutputCharset($object->ref)); |
||
230 | $pdf->SetSubject($outputlangs->transnoentities("Project")); |
||
231 | $pdf->SetCreator("Dolibarr " . DOL_VERSION); |
||
232 | $pdf->SetAuthor($outputlangs->convToOutputCharset($user->getFullName($outputlangs))); |
||
233 | $pdf->SetKeyWords($outputlangs->convToOutputCharset($object->ref) . " " . $outputlangs->transnoentities("Project")); |
||
234 | if (getDolGlobalString('MAIN_DISABLE_PDF_COMPRESSION')) { |
||
235 | $pdf->SetCompression(false); |
||
236 | } |
||
237 | |||
238 | // @phan-suppress-next-line PhanPluginSuspiciousParamOrder |
||
239 | $pdf->SetMargins($this->marge_gauche, $this->marge_haute, $this->marge_droite); // Left, Top, Right |
||
240 | |||
241 | // New page |
||
242 | $pdf->AddPage(); |
||
243 | if (!empty($tplidx)) { |
||
244 | $pdf->useTemplate($tplidx); |
||
245 | } |
||
246 | $pagenb++; |
||
247 | $this->_pagehead($pdf, $object, 1, $outputlangs); |
||
248 | $pdf->SetFont('', '', $default_font_size - 1); |
||
249 | $pdf->MultiCell(0, 3, ''); // Set interline to 3 |
||
250 | $pdf->SetTextColor(0, 0, 0); |
||
251 | |||
252 | $tab_top = 50; |
||
253 | $tab_top_newpage = (!getDolGlobalInt('MAIN_PDF_DONOTREPEAT_HEAD') ? 42 : 10); |
||
254 | |||
255 | $tab_height = $this->page_hauteur - $tab_top - $heightforfooter - $heightforfreetext; |
||
256 | |||
257 | // Show public note |
||
258 | $notetoshow = empty($object->note_public) ? '' : $object->note_public; |
||
259 | if ($notetoshow) { |
||
260 | $substitutionarray = pdf_getSubstitutionArray($outputlangs, null, $object); |
||
261 | complete_substitutions_array($substitutionarray, $outputlangs, $object); |
||
262 | $notetoshow = make_substitutions($notetoshow, $substitutionarray, $outputlangs); |
||
263 | $notetoshow = convertBackOfficeMediasLinksToPublicLinks($notetoshow); |
||
264 | |||
265 | $tab_top -= 2; |
||
266 | |||
267 | $pdf->SetFont('', '', $default_font_size - 1); |
||
268 | $pdf->writeHTMLCell(190, 3, $this->posxref - 1, $tab_top - 2, dol_htmlentitiesbr($notetoshow), 0, 1); |
||
269 | $nexY = $pdf->GetY(); |
||
270 | $height_note = $nexY - $tab_top; |
||
271 | |||
272 | // Rect takes a length in 3rd parameter |
||
273 | $pdf->SetDrawColor(192, 192, 192); |
||
274 | $pdf->Rect($this->marge_gauche, $tab_top - 2, $this->page_largeur - $this->marge_gauche - $this->marge_droite, $height_note + 2); |
||
275 | |||
276 | $tab_height = $tab_height - $height_note; |
||
277 | $tab_top = $nexY + 6; |
||
278 | } else { |
||
279 | $height_note = 0; |
||
280 | } |
||
281 | |||
282 | $heightoftitleline = 10; |
||
283 | $iniY = $tab_top + $heightoftitleline + 1; |
||
284 | $curY = $tab_top + $heightoftitleline + 1; |
||
285 | $nexY = $tab_top + $heightoftitleline + 1; |
||
286 | |||
287 | $tmpuser = new User($this->db); |
||
288 | |||
289 | // TODO We should loop on record of times spent grouped by user instead of lines of tasks |
||
290 | |||
291 | // Loop on each lines |
||
292 | for ($i = 0; $i < $nblines; $i++) { |
||
293 | $curY = $nexY; |
||
294 | $pdf->SetFont('', '', $default_font_size - 1); // Into loop to work with multipage |
||
295 | $pdf->SetTextColor(0, 0, 0); |
||
296 | |||
297 | $pdf->setTopMargin($tab_top_newpage); |
||
298 | $pdf->setPageOrientation('', 1, $heightforfooter + $heightforfreetext + $heightforinfotot); // The only function to edit the bottom margin of current page to set it. |
||
299 | $pageposbefore = $pdf->getPage(); |
||
300 | |||
301 | // Description of line |
||
302 | $ref = $object->lines[$i]->ref; |
||
303 | $libelleline = $object->lines[$i]->label; |
||
304 | //$progress=($object->lines[$i]->progress?$object->lines[$i]->progress.'%':''); |
||
305 | $datestart = dol_print_date($object->lines[$i]->date_start, 'day'); |
||
306 | $dateend = dol_print_date($object->lines[$i]->date_end, 'day'); |
||
307 | $duration = convertSecondToTime((int)$object->lines[$i]->duration, 'allhourmin'); |
||
308 | |||
309 | $showpricebeforepagebreak = 1; |
||
310 | |||
311 | $pdf->startTransaction(); |
||
312 | // Label |
||
313 | $pdf->SetXY($this->posxlabel, $curY); |
||
314 | $pdf->MultiCell($this->posxtimespent - $this->posxlabel, 3, $outputlangs->convToOutputCharset($libelleline), 0, 'L'); |
||
315 | $pageposafter = $pdf->getPage(); |
||
316 | if ($pageposafter > $pageposbefore) { // There is a pagebreak |
||
317 | $pdf->rollbackTransaction(true); |
||
318 | $pageposafter = $pageposbefore; |
||
319 | //print $pageposafter.'-'.$pageposbefore;exit; |
||
320 | $pdf->setPageOrientation('', 1, $heightforfooter); // The only function to edit the bottom margin of current page to set it. |
||
321 | // Label |
||
322 | $pdf->SetXY($this->posxlabel, $curY); |
||
323 | $posybefore = $pdf->GetY(); |
||
324 | $pdf->MultiCell($this->posxtimespent - $this->posxlabel, 3, $outputlangs->convToOutputCharset($libelleline), 0, 'L'); |
||
325 | $pageposafter = $pdf->getPage(); |
||
326 | $posyafter = $pdf->GetY(); |
||
327 | if ($posyafter > ($this->page_hauteur - ($heightforfooter + $heightforfreetext + $heightforinfotot))) { // There is no space left for total+free text |
||
328 | if ($i == ($nblines - 1)) { // No more lines, and no space left to show total, so we create a new page |
||
329 | $pdf->AddPage('', '', true); |
||
330 | if (!empty($tplidx)) { |
||
331 | $pdf->useTemplate($tplidx); |
||
332 | } |
||
333 | if (!getDolGlobalInt('MAIN_PDF_DONOTREPEAT_HEAD')) { |
||
334 | $this->_pagehead($pdf, $object, 0, $outputlangs); |
||
335 | } |
||
336 | $pdf->setPage($pageposafter + 1); |
||
337 | } |
||
338 | } else { |
||
339 | // We found a page break |
||
340 | |||
341 | // Allows data in the first page if description is long enough to break in multiples pages |
||
342 | if (getDolGlobalString('MAIN_PDF_DATA_ON_FIRST_PAGE')) { |
||
343 | $showpricebeforepagebreak = 1; |
||
344 | } else { |
||
345 | $showpricebeforepagebreak = 0; |
||
346 | } |
||
347 | |||
348 | $forcedesconsamepage = 1; |
||
349 | if ($forcedesconsamepage) { |
||
350 | $pdf->rollbackTransaction(true); |
||
351 | $pageposafter = $pageposbefore; |
||
352 | $pdf->setPageOrientation('', 1, $heightforfooter); // The only function to edit the bottom margin of current page to set it. |
||
353 | |||
354 | $pdf->AddPage('', '', true); |
||
355 | if (!empty($tplidx)) { |
||
356 | $pdf->useTemplate($tplidx); |
||
357 | } |
||
358 | if (!getDolGlobalInt('MAIN_PDF_DONOTREPEAT_HEAD')) { |
||
359 | $this->_pagehead($pdf, $object, 0, $outputlangs); |
||
360 | } |
||
361 | $pdf->setPage($pageposafter + 1); |
||
362 | $pdf->SetFont('', '', $default_font_size - 1); // On repositionne la police par default |
||
363 | $pdf->MultiCell(0, 3, ''); // Set interline to 3 |
||
364 | $pdf->SetTextColor(0, 0, 0); |
||
365 | |||
366 | $pdf->setPageOrientation('', 1, $heightforfooter); // The only function to edit the bottom margin of current page to set it. |
||
367 | $curY = $tab_top_newpage + $heightoftitleline + 1; |
||
368 | |||
369 | // Label |
||
370 | $pdf->SetXY($this->posxlabel, $curY); |
||
371 | $posybefore = $pdf->GetY(); |
||
372 | $pdf->MultiCell($this->posxtimespent - $this->posxlabel, 3, $outputlangs->convToOutputCharset($libelleline), 0, 'L'); |
||
373 | $pageposafter = $pdf->getPage(); |
||
374 | $posyafter = $pdf->GetY(); |
||
375 | } |
||
376 | } |
||
377 | //var_dump($i.' '.$posybefore.' '.$posyafter.' '.($this->page_hauteur - ($heightforfooter + $heightforfreetext + $heightforinfotot)).' '.$showpricebeforepagebreak); |
||
378 | } else { // No pagebreak |
||
379 | $pdf->commitTransaction(); |
||
380 | } |
||
381 | $posYAfterDescription = $pdf->GetY(); |
||
382 | |||
383 | $nexY = $pdf->GetY(); |
||
384 | $pageposafter = $pdf->getPage(); |
||
385 | $pdf->setPage($pageposbefore); |
||
386 | $pdf->setTopMargin($this->marge_haute); |
||
387 | $pdf->setPageOrientation('', 1, 0); // The only function to edit the bottom margin of current page to set it. |
||
388 | |||
389 | // We suppose that a too long description is moved completely on next page |
||
390 | if ($pageposafter > $pageposbefore && empty($showpricebeforepagebreak)) { |
||
391 | //var_dump($pageposbefore.'-'.$pageposafter.'-'.$showpricebeforepagebreak); |
||
392 | $pdf->setPage($pageposafter); |
||
393 | $curY = $tab_top_newpage + $heightoftitleline + 1; |
||
394 | } |
||
395 | |||
396 | $pdf->SetFont('', '', $default_font_size - 1); // We reposition the default font |
||
397 | |||
398 | // Ref of task |
||
399 | $pdf->SetXY($this->posxref, $curY); |
||
400 | $pdf->MultiCell($this->posxlabel - $this->posxref, 3, $outputlangs->convToOutputCharset($ref), 0, 'L'); |
||
401 | // timespent |
||
402 | $pdf->SetXY($this->posxtimespent, $curY); |
||
403 | $pdf->MultiCell($this->posxuser - $this->posxtimespent, 3, $duration ? $duration : '', 0, 'R'); |
||
404 | // Progress |
||
405 | //$pdf->SetXY($this->posxprogress, $curY); |
||
406 | //$pdf->MultiCell($this->posxuser-$this->posxprogress, 3, $progress, 0, 'R'); |
||
407 | |||
408 | // User spending time |
||
409 | /*var_dump($object->lines[$i]);exit; |
||
410 | $tmpuser->fetch($object->lines[$i]->fk_user); |
||
411 | $pdf->SetXY($this->posxuser, $curY); |
||
412 | $pdf->MultiCell($this->page_largeur - $this->marge_droite - $this->posxuser, 3, $tmpuser->getFullName($outputlangs, 0, -1, 20), 0, 'C'); |
||
413 | */ |
||
414 | |||
415 | // Add line |
||
416 | if (getDolGlobalString('MAIN_PDF_DASH_BETWEEN_LINES') && $i < ($nblines - 1)) { |
||
417 | $pdf->setPage($pageposafter); |
||
418 | $pdf->SetLineStyle(array('dash' => '1,1', 'color' => array(80, 80, 80))); |
||
419 | //$pdf->SetDrawColor(190,190,200); |
||
420 | $pdf->line($this->marge_gauche, $nexY + 1, $this->page_largeur - $this->marge_droite, $nexY + 1); |
||
421 | $pdf->SetLineStyle(array('dash' => 0)); |
||
422 | } |
||
423 | |||
424 | $nexY += 2; // Add space between lines |
||
425 | |||
426 | // Detect if some page were added automatically and output _tableau for past pages |
||
427 | while ($pagenb < $pageposafter) { |
||
428 | $pdf->setPage($pagenb); |
||
429 | if ($pagenb == 1) { |
||
430 | $this->_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforfooter, 0, $outputlangs, 0, 1); |
||
431 | } else { |
||
432 | $this->_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforfooter, 0, $outputlangs, 1, 1); |
||
433 | } |
||
434 | $this->_pagefoot($pdf, $object, $outputlangs, 1); |
||
435 | $pagenb++; |
||
436 | $pdf->setPage($pagenb); |
||
437 | $pdf->setPageOrientation('', 1, 0); // The only function to edit the bottom margin of current page to set it. |
||
438 | if (!getDolGlobalInt('MAIN_PDF_DONOTREPEAT_HEAD')) { |
||
439 | $this->_pagehead($pdf, $object, 0, $outputlangs); |
||
440 | } |
||
441 | if (!empty($tplidx)) { |
||
442 | $pdf->useTemplate($tplidx); |
||
443 | } |
||
444 | } |
||
445 | if (isset($object->lines[$i + 1]->pagebreak) && $object->lines[$i + 1]->pagebreak) { |
||
446 | if ($pagenb == 1) { |
||
447 | $this->_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforfooter, 0, $outputlangs, 0, 1); |
||
448 | } else { |
||
449 | $this->_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforfooter, 0, $outputlangs, 1, 1); |
||
450 | } |
||
451 | $this->_pagefoot($pdf, $object, $outputlangs, 1); |
||
452 | // New page |
||
453 | $pdf->AddPage(); |
||
454 | if (!empty($tplidx)) { |
||
455 | $pdf->useTemplate($tplidx); |
||
456 | } |
||
457 | $pagenb++; |
||
458 | if (!getDolGlobalInt('MAIN_PDF_DONOTREPEAT_HEAD')) { |
||
459 | $this->_pagehead($pdf, $object, 0, $outputlangs); |
||
460 | } |
||
461 | } |
||
462 | } |
||
463 | |||
464 | // Show square |
||
465 | if ($pagenb == 1) { |
||
466 | $this->_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforinfotot - $heightforfreetext - $heightforfooter, 0, $outputlangs, 0, 0); |
||
467 | } else { |
||
468 | $this->_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforinfotot - $heightforfreetext - $heightforfooter, 0, $outputlangs, 1, 0); |
||
469 | } |
||
470 | $bottomlasttab = $this->page_hauteur - $heightforinfotot - $heightforfreetext - $heightforfooter + 1; |
||
471 | |||
472 | // Footer of the page |
||
473 | $this->_pagefoot($pdf, $object, $outputlangs); |
||
474 | if (method_exists($pdf, 'AliasNbPages')) { |
||
475 | $pdf->AliasNbPages(); |
||
476 | } |
||
477 | |||
478 | $pdf->Close(); |
||
479 | |||
480 | $pdf->Output($file, 'F'); |
||
481 | |||
482 | // Add pdfgeneration hook |
||
483 | $hookmanager->initHooks(array('pdfgeneration')); |
||
484 | $parameters = array('file' => $file, 'object' => $object, 'outputlangs' => $outputlangs); |
||
485 | global $action; |
||
486 | $reshook = $hookmanager->executeHooks('afterPDFCreation', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks |
||
487 | if ($reshook < 0) { |
||
488 | $this->error = $hookmanager->error; |
||
489 | $this->errors = $hookmanager->errors; |
||
490 | } |
||
491 | |||
492 | dolChmod($file); |
||
493 | |||
494 | $this->result = array('fullpath' => $file); |
||
495 | |||
496 | return 1; // No error |
||
497 | } else { |
||
498 | $this->error = $langs->transnoentities("ErrorCanNotCreateDir", $dir); |
||
499 | return 0; |
||
500 | } |
||
501 | } else { |
||
502 | $this->error = $langs->transnoentities("ErrorConstantNotDefined", "PROJECT_OUTPUTDIR"); |
||
503 | return 0; |
||
504 | } |
||
666 |