| Conditions | 62 |
| Paths | > 20000 |
| Total Lines | 397 |
| Code Lines | 257 |
| 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 |
||
| 430 | public function write_file($object, $outputlangs, $srctemplatepath) |
||
| 431 | { |
||
| 432 | // phpcs:enable |
||
| 433 | global $user, $langs, $conf, $mysoc, $hookmanager; |
||
| 434 | |||
| 435 | if (empty($srctemplatepath)) { |
||
| 436 | dol_syslog("doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING); |
||
| 437 | return -1; |
||
| 438 | } |
||
| 439 | |||
| 440 | if (!is_object($outputlangs)) { |
||
| 441 | $outputlangs = $langs; |
||
| 442 | } |
||
| 443 | $sav_charset_output = $outputlangs->charset_output; |
||
| 444 | $outputlangs->charset_output = 'UTF-8'; |
||
| 445 | |||
| 446 | // Load translation files required by the page |
||
| 447 | $outputlangs->loadLangs(array("main", "dict", "companies", "projects")); |
||
| 448 | |||
| 449 | if ($conf->project->dir_output) { |
||
| 450 | // If $object is id instead of object |
||
| 451 | if (!is_object($object)) { |
||
| 452 | $id = $object; |
||
| 453 | $object = new Task($this->db); |
||
| 454 | $result = $object->fetch($id); |
||
| 455 | if ($result < 0) { |
||
| 456 | dol_print_error($this->db, $object->error); |
||
| 457 | return -1; |
||
| 458 | } |
||
| 459 | } |
||
| 460 | $project = new Project($this->db); |
||
| 461 | $project->fetch($object->fk_project); |
||
| 462 | $project->fetch_thirdparty(); |
||
| 463 | |||
| 464 | $dir = $conf->project->dir_output . "/" . $project->ref . "/"; |
||
| 465 | $objectref = dol_sanitizeFileName($object->ref); |
||
| 466 | if (!preg_match('/specimen/i', $objectref)) { |
||
| 467 | $dir .= "/" . $objectref; |
||
| 468 | } |
||
| 469 | $file = $dir . "/" . $objectref . ".odt"; |
||
| 470 | |||
| 471 | if (!file_exists($dir)) { |
||
| 472 | if (dol_mkdir($dir) < 0) { |
||
| 473 | $this->error = $langs->transnoentities("ErrorCanNotCreateDir", $dir); |
||
| 474 | return -1; |
||
| 475 | } |
||
| 476 | } |
||
| 477 | |||
| 478 | |||
| 479 | if (file_exists($dir)) { |
||
| 480 | //print "srctemplatepath=".$srctemplatepath; // Src filename |
||
| 481 | $newfile = basename($srctemplatepath); |
||
| 482 | $newfiletmp = preg_replace('/\.(ods|odt)/i', '', $newfile); |
||
| 483 | $newfiletmp = preg_replace('/template_/i', '', $newfiletmp); |
||
| 484 | $newfiletmp = preg_replace('/modele_/i', '', $newfiletmp); |
||
| 485 | $newfiletmp = $objectref . '_' . $newfiletmp; |
||
| 486 | //$file=$dir.'/'.$newfiletmp.'.'.dol_print_date(dol_now(),'%Y%m%d%H%M%S').'.odt'; |
||
| 487 | $file = $dir . '/' . $newfiletmp . '.odt'; |
||
| 488 | //print "newdir=".$dir; |
||
| 489 | //print "newfile=".$newfile; |
||
| 490 | //print "file=".$file; |
||
| 491 | //print "conf->societe->dir_temp=".$conf->societe->dir_temp; |
||
| 492 | |||
| 493 | dol_mkdir($conf->project->dir_temp); |
||
| 494 | if (!is_writable($conf->project->dir_temp)) { |
||
| 495 | $this->error = $langs->transnoentities("ErrorFailedToWriteInTempDirectory", $conf->project->dir_temp); |
||
| 496 | dol_syslog('Error in write_file: ' . $this->error, LOG_ERR); |
||
| 497 | return -1; |
||
| 498 | } |
||
| 499 | |||
| 500 | $socobject = $project->thirdparty; |
||
| 501 | |||
| 502 | // Make substitution |
||
| 503 | $substitutionarray = array( |
||
| 504 | '__FROM_NAME__' => $this->emetteur->name, |
||
| 505 | '__FROM_EMAIL__' => $this->emetteur->email, |
||
| 506 | ); |
||
| 507 | complete_substitutions_array($substitutionarray, $langs, $object); |
||
| 508 | // Call the ODTSubstitution hook |
||
| 509 | $tmparray = array(); |
||
| 510 | $action = ''; |
||
| 511 | $parameters = array('file' => $file, 'object' => $object, 'outputlangs' => $outputlangs, 'substitutionarray' => &$tmparray); |
||
| 512 | $reshook = $hookmanager->executeHooks('ODTSubstitution', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks |
||
| 513 | |||
| 514 | // Open and load template |
||
| 515 | require_once ODTPHP_PATH . 'odf.php'; |
||
| 516 | try { |
||
| 517 | $odfHandler = new Odf( |
||
| 518 | $srctemplatepath, |
||
| 519 | array( |
||
| 520 | 'PATH_TO_TMP' => $conf->project->dir_temp, |
||
| 521 | 'ZIP_PROXY' => 'PclZipProxy', // PhpZipProxy or PclZipProxy. Got "bad compression method" error when using PhpZipProxy. |
||
| 522 | 'DELIMITER_LEFT' => '{', |
||
| 523 | 'DELIMITER_RIGHT' => '}' |
||
| 524 | ) |
||
| 525 | ); |
||
| 526 | } catch (Exception $e) { |
||
| 527 | $this->error = $e->getMessage(); |
||
| 528 | return -1; |
||
| 529 | } |
||
| 530 | // After construction $odfHandler->contentXml contains content and |
||
| 531 | // [!-- BEGIN row.lines --]*[!-- END row.lines --] has been replaced by |
||
| 532 | // [!-- BEGIN lines --]*[!-- END lines --] |
||
| 533 | //print html_entity_decode($odfHandler->__toString()); |
||
| 534 | //print exit; |
||
| 535 | |||
| 536 | |||
| 537 | // Define substitution array |
||
| 538 | $substitutionarray = getCommonSubstitutionArray($outputlangs, 0, null, $object); |
||
| 539 | $array_object_from_properties = $this->get_substitutionarray_each_var_object($object, $outputlangs); |
||
| 540 | $array_objet = $this->get_substitutionarray_object($project, $outputlangs); |
||
| 541 | $array_user = $this->get_substitutionarray_user($user, $outputlangs); |
||
| 542 | $array_soc = $this->get_substitutionarray_mysoc($mysoc, $outputlangs); |
||
| 543 | $array_thirdparty = $this->get_substitutionarray_thirdparty($socobject, $outputlangs); |
||
| 544 | $array_other = $this->get_substitutionarray_other($outputlangs); |
||
| 545 | |||
| 546 | $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other); |
||
| 547 | complete_substitutions_array($tmparray, $outputlangs, $object); |
||
| 548 | |||
| 549 | foreach ($tmparray as $key => $value) { |
||
| 550 | try { |
||
| 551 | if (preg_match('/logo$/', $key)) { // Image |
||
| 552 | if (file_exists($value)) { |
||
| 553 | $odfHandler->setImage($key, $value); |
||
| 554 | } else { |
||
| 555 | $odfHandler->setVars($key, 'ErrorFileNotFound', true, 'UTF-8'); |
||
| 556 | } |
||
| 557 | } else { // Text |
||
| 558 | $odfHandler->setVars($key, $value, true, 'UTF-8'); |
||
| 559 | } |
||
| 560 | } catch (OdfException $e) { |
||
| 561 | dol_syslog($e->getMessage(), LOG_INFO); |
||
| 562 | } |
||
| 563 | } |
||
| 564 | |||
| 565 | // Replace tags of lines for tasks |
||
| 566 | try { |
||
| 567 | // Security check |
||
| 568 | $socid = 0; |
||
| 569 | if (!empty($project->fk_soc)) { |
||
| 570 | $socid = $project->fk_soc; |
||
| 571 | } |
||
| 572 | |||
| 573 | $tmparray = $this->get_substitutionarray_tasks($object, $outputlangs); |
||
| 574 | complete_substitutions_array($tmparray, $outputlangs, $object); |
||
| 575 | foreach ($tmparray as $key => $val) { |
||
| 576 | try { |
||
| 577 | $odfHandler->setVars($key, $val, true, 'UTF-8'); |
||
| 578 | } catch (OdfException $e) { |
||
| 579 | dol_syslog($e->getMessage(), LOG_INFO); |
||
| 580 | } catch (SegmentException $e) { |
||
| 581 | dol_syslog($e->getMessage(), LOG_INFO); |
||
| 582 | } |
||
| 583 | } |
||
| 584 | |||
| 585 | // Replace tags of lines for contacts task |
||
| 586 | $sourcearray = array('internal', 'external'); |
||
| 587 | $contact_arrray = array(); |
||
| 588 | foreach ($sourcearray as $source) { |
||
| 589 | $contact_temp = $object->liste_contact(-1, $source); |
||
| 590 | if ((is_array($contact_temp) && count($contact_temp) > 0)) { |
||
| 591 | $contact_arrray = array_merge($contact_arrray, $contact_temp); |
||
| 592 | } |
||
| 593 | } |
||
| 594 | if ((is_array($contact_arrray) && count($contact_arrray) > 0)) { |
||
| 595 | $listlinestaskres = $odfHandler->setSegment('tasksressources'); |
||
| 596 | |||
| 597 | foreach ($contact_arrray as $contact) { |
||
| 598 | if ($contact['source'] == 'internal') { |
||
| 599 | $objectdetail = new User($this->db); |
||
| 600 | $objectdetail->fetch($contact['id']); |
||
| 601 | $contact['socname'] = $mysoc->name; |
||
| 602 | } elseif ($contact['source'] == 'external') { |
||
| 603 | $objectdetail = new Contact($this->db); |
||
| 604 | $objectdetail->fetch($contact['id']); |
||
| 605 | |||
| 606 | $soc = new Societe($this->db); |
||
| 607 | $soc->fetch($contact['socid']); |
||
| 608 | $contact['socname'] = $soc->name; |
||
| 609 | } |
||
| 610 | $contact['fullname'] = $objectdetail->getFullName($outputlangs, 1); |
||
| 611 | |||
| 612 | $tmparray = $this->get_substitutionarray_tasksressource($contact, $outputlangs); |
||
| 613 | |||
| 614 | foreach ($tmparray as $key => $val) { |
||
| 615 | try { |
||
| 616 | $listlinestaskres->setVars($key, $val, true, 'UTF-8'); |
||
| 617 | } catch (OdfException $e) { |
||
| 618 | dol_syslog($e->getMessage(), LOG_INFO); |
||
| 619 | } catch (SegmentException $e) { |
||
| 620 | dol_syslog($e->getMessage(), LOG_INFO); |
||
| 621 | } |
||
| 622 | } |
||
| 623 | $listlinestaskres->merge(); |
||
| 624 | } |
||
| 625 | $odfHandler->mergeSegment($listlinestaskres); |
||
| 626 | } |
||
| 627 | |||
| 628 | // Time resources |
||
| 629 | $sql = "SELECT t.rowid, t.element_date as task_date, t.element_duration as task_duration, t.fk_user, t.note"; |
||
| 630 | $sql .= ", u.lastname, u.firstname"; |
||
| 631 | $sql .= " FROM " . MAIN_DB_PREFIX . "element_time as t"; |
||
| 632 | $sql .= " , " . MAIN_DB_PREFIX . "user as u"; |
||
| 633 | $sql .= " WHERE t.fk_element =" . ((int) $object->id); |
||
| 634 | $sql .= " AND t.elementtype = 'task'"; |
||
| 635 | $sql .= " AND t.fk_user = u.rowid"; |
||
| 636 | $sql .= " ORDER BY t.element_date DESC"; |
||
| 637 | |||
| 638 | $resql = $this->db->query($sql); |
||
| 639 | if ($resql) { |
||
| 640 | $num = $this->db->num_rows($resql); |
||
| 641 | $i = 0; |
||
| 642 | $tasks = array(); |
||
| 643 | $listlinestasktime = $odfHandler->setSegment('taskstimes'); |
||
| 644 | while ($i < $num) { |
||
| 645 | $row = $this->db->fetch_array($resql); |
||
| 646 | if (!empty($row['fk_user'])) { |
||
| 647 | $objectdetail = new User($this->db); |
||
| 648 | $objectdetail->fetch($row['fk_user']); |
||
| 649 | // TODO Use a cache to aoid fetch for same user |
||
| 650 | $row['fullcivname'] = $objectdetail->getFullName($outputlangs, 1); |
||
| 651 | } else { |
||
| 652 | $row['fullcivname'] = ''; |
||
| 653 | } |
||
| 654 | |||
| 655 | $tmparray = $this->get_substitutionarray_taskstime($row, $outputlangs); |
||
| 656 | |||
| 657 | foreach ($tmparray as $key => $val) { |
||
| 658 | try { |
||
| 659 | $listlinestasktime->setVars($key, $val, true, 'UTF-8'); |
||
| 660 | } catch (OdfException $e) { |
||
| 661 | dol_syslog($e->getMessage(), LOG_INFO); |
||
| 662 | } catch (SegmentException $e) { |
||
| 663 | dol_syslog($e->getMessage(), LOG_INFO); |
||
| 664 | } |
||
| 665 | } |
||
| 666 | $listlinestasktime->merge(); |
||
| 667 | $i++; |
||
| 668 | } |
||
| 669 | $this->db->free($resql); |
||
| 670 | |||
| 671 | $odfHandler->mergeSegment($listlinestasktime); |
||
| 672 | } |
||
| 673 | |||
| 674 | |||
| 675 | // Replace tags of project files |
||
| 676 | $listtasksfiles = $odfHandler->setSegment('tasksfiles'); |
||
| 677 | |||
| 678 | $upload_dir = $conf->project->dir_output . '/' . dol_sanitizeFileName($project->ref) . '/' . dol_sanitizeFileName($object->ref); |
||
| 679 | $filearray = dol_dir_list($upload_dir, "files", 0, '', '(\.meta|_preview.*\.png)$', 'name', SORT_ASC, 1); |
||
| 680 | |||
| 681 | |||
| 682 | foreach ($filearray as $filedetail) { |
||
| 683 | $tmparray = $this->get_substitutionarray_task_file($filedetail, $outputlangs); |
||
| 684 | //dol_syslog(get_class($this).'::main $tmparray'.var_export($tmparray,true)); |
||
| 685 | foreach ($tmparray as $key => $val) { |
||
| 686 | try { |
||
| 687 | $listtasksfiles->setVars($key, $val, true, 'UTF-8'); |
||
| 688 | } catch (OdfException $e) { |
||
| 689 | dol_syslog($e->getMessage(), LOG_INFO); |
||
| 690 | } catch (SegmentException $e) { |
||
| 691 | dol_syslog($e->getMessage(), LOG_INFO); |
||
| 692 | } |
||
| 693 | } |
||
| 694 | $listtasksfiles->merge(); |
||
| 695 | } |
||
| 696 | //$listlines->merge(); |
||
| 697 | |||
| 698 | $odfHandler->mergeSegment($listtasksfiles); |
||
| 699 | } catch (OdfException $e) { |
||
| 700 | $this->error = $e->getMessage(); |
||
| 701 | dol_syslog($this->error, LOG_WARNING); |
||
| 702 | return -1; |
||
| 703 | } |
||
| 704 | |||
| 705 | |||
| 706 | |||
| 707 | // Replace tags of project files |
||
| 708 | try { |
||
| 709 | $listlines = $odfHandler->setSegment('projectfiles'); |
||
| 710 | |||
| 711 | $upload_dir = $conf->project->dir_output . '/' . dol_sanitizeFileName($object->ref); |
||
| 712 | $filearray = dol_dir_list($upload_dir, "files", 0, '', '(\.meta|_preview.*\.png)$', 'name', SORT_ASC, 1); |
||
| 713 | |||
| 714 | |||
| 715 | foreach ($filearray as $filedetail) { |
||
| 716 | //dol_syslog(get_class($this).'::main $filedetail'.var_export($filedetail,true)); |
||
| 717 | $tmparray = $this->get_substitutionarray_project_file($filedetail, $outputlangs); |
||
| 718 | |||
| 719 | foreach ($tmparray as $key => $val) { |
||
| 720 | try { |
||
| 721 | $listlines->setVars($key, $val, true, 'UTF-8'); |
||
| 722 | } catch (OdfException $e) { |
||
| 723 | dol_syslog($e->getMessage(), LOG_INFO); |
||
| 724 | } catch (SegmentException $e) { |
||
| 725 | dol_syslog($e->getMessage(), LOG_INFO); |
||
| 726 | } |
||
| 727 | } |
||
| 728 | $listlines->merge(); |
||
| 729 | } |
||
| 730 | $odfHandler->mergeSegment($listlines); |
||
| 731 | } catch (OdfException $e) { |
||
| 732 | $this->error = $e->getMessage(); |
||
| 733 | dol_syslog($this->error, LOG_WARNING); |
||
| 734 | return -1; |
||
| 735 | } |
||
| 736 | |||
| 737 | // Replace tags of lines for contacts |
||
| 738 | $sourcearray = array('internal', 'external'); |
||
| 739 | $contact_arrray = array(); |
||
| 740 | foreach ($sourcearray as $source) { |
||
| 741 | $contact_temp = $project->liste_contact(-1, $source); |
||
| 742 | if ((is_array($contact_temp) && count($contact_temp) > 0)) { |
||
| 743 | $contact_arrray = array_merge($contact_arrray, $contact_temp); |
||
| 744 | } |
||
| 745 | } |
||
| 746 | if ((is_array($contact_arrray) && count($contact_arrray) > 0)) { |
||
| 747 | try { |
||
| 748 | $listlines = $odfHandler->setSegment('projectcontacts'); |
||
| 749 | |||
| 750 | foreach ($contact_arrray as $contact) { |
||
| 751 | if ($contact['source'] == 'internal') { |
||
| 752 | $objectdetail = new User($this->db); |
||
| 753 | $objectdetail->fetch($contact['id']); |
||
| 754 | $contact['socname'] = $mysoc->name; |
||
| 755 | } elseif ($contact['source'] == 'external') { |
||
| 756 | $objectdetail = new Contact($this->db); |
||
| 757 | $objectdetail->fetch($contact['id']); |
||
| 758 | |||
| 759 | $soc = new Societe($this->db); |
||
| 760 | $soc->fetch($contact['socid']); |
||
| 761 | $contact['socname'] = $soc->name; |
||
| 762 | } |
||
| 763 | $contact['fullname'] = $objectdetail->getFullName($outputlangs, 1); |
||
| 764 | |||
| 765 | $tmparray = $this->get_substitutionarray_project_contacts($contact, $outputlangs); |
||
| 766 | |||
| 767 | foreach ($tmparray as $key => $val) { |
||
| 768 | try { |
||
| 769 | $listlines->setVars($key, $val, true, 'UTF-8'); |
||
| 770 | } catch (OdfException $e) { |
||
| 771 | dol_syslog($e->getMessage(), LOG_INFO); |
||
| 772 | } catch (SegmentException $e) { |
||
| 773 | dol_syslog($e->getMessage(), LOG_INFO); |
||
| 774 | } |
||
| 775 | } |
||
| 776 | $listlines->merge(); |
||
| 777 | } |
||
| 778 | $odfHandler->mergeSegment($listlines); |
||
| 779 | } catch (OdfException $e) { |
||
| 780 | $this->error = $e->getMessage(); |
||
| 781 | dol_syslog($this->error, LOG_WARNING); |
||
| 782 | return -1; |
||
| 783 | } |
||
| 784 | } |
||
| 785 | |||
| 786 | |||
| 787 | // Call the beforeODTSave hook |
||
| 788 | $parameters = array('odfHandler' => &$odfHandler, 'file' => $file, 'object' => $object, 'outputlangs' => $outputlangs, 'substitutionarray' => &$tmparray); |
||
| 789 | $reshook = $hookmanager->executeHooks('beforeODTSave', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks |
||
| 790 | |||
| 791 | |||
| 792 | // Write new file |
||
| 793 | if (getDolGlobalString('MAIN_ODT_AS_PDF')) { |
||
| 794 | try { |
||
| 795 | $odfHandler->exportAsAttachedPDF($file); |
||
| 796 | } catch (Exception $e) { |
||
| 797 | $this->error = $e->getMessage(); |
||
| 798 | dol_syslog($e->getMessage(), LOG_INFO); |
||
| 799 | return -1; |
||
| 800 | } |
||
| 801 | } else { |
||
| 802 | try { |
||
| 803 | $odfHandler->saveToDisk($file); |
||
| 804 | } catch (Exception $e) { |
||
| 805 | $this->error = $e->getMessage(); |
||
| 806 | dol_syslog($e->getMessage(), LOG_INFO); |
||
| 807 | return -1; |
||
| 808 | } |
||
| 809 | } |
||
| 810 | $parameters = array('odfHandler' => &$odfHandler, 'file' => $file, 'object' => $object, 'outputlangs' => $outputlangs, 'substitutionarray' => &$tmparray); |
||
| 811 | $reshook = $hookmanager->executeHooks('afterODTCreation', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks |
||
| 812 | |||
| 813 | dolChmod($file); |
||
| 814 | |||
| 815 | $odfHandler = null; // Destroy object |
||
| 816 | |||
| 817 | $this->result = array('fullpath' => $file); |
||
| 818 | |||
| 819 | return 1; // Success |
||
| 820 | } else { |
||
| 821 | $this->error = $langs->transnoentities("ErrorCanNotCreateDir", $dir); |
||
| 822 | return -1; |
||
| 823 | } |
||
| 824 | } |
||
| 825 | |||
| 826 | return -1; |
||
| 827 | } |
||
| 829 |