| Conditions | 109 | 
| Total Lines | 664 | 
| Code Lines | 452 | 
| 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  | 
            ||
| 468 | public function write_file($object, $outputlangs, $srctemplatepath)  | 
            ||
| 469 |     { | 
            ||
| 470 | // phpcs:enable  | 
            ||
| 471 | global $user, $langs, $conf, $mysoc, $hookmanager;  | 
            ||
| 472 | |||
| 473 |         if (empty($srctemplatepath)) { | 
            ||
| 474 |             dol_syslog("doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING); | 
            ||
| 475 | return -1;  | 
            ||
| 476 | }  | 
            ||
| 477 | |||
| 478 | // Add odtgeneration hook  | 
            ||
| 479 |         if (!is_object($hookmanager)) { | 
            ||
| 480 | $hookmanager = new HookManager($this->db);  | 
            ||
| 481 | }  | 
            ||
| 482 |         $hookmanager->initHooks(array('odtgeneration')); | 
            ||
| 483 | global $action;  | 
            ||
| 484 | |||
| 485 |         if (!is_object($outputlangs)) { | 
            ||
| 486 | $outputlangs = $langs;  | 
            ||
| 487 | }  | 
            ||
| 488 | $sav_charset_output = $outputlangs->charset_output;  | 
            ||
| 489 | $outputlangs->charset_output = 'UTF-8';  | 
            ||
| 490 | |||
| 491 | // Load translation files required by the page  | 
            ||
| 492 |         $outputlangs->loadLangs(array("main", "dict", "companies", "projects")); | 
            ||
| 493 | |||
| 494 |         if ($conf->project->dir_output) { | 
            ||
| 495 | // If $object is id instead of object  | 
            ||
| 496 |             if (!is_object($object)) { | 
            ||
| 497 | $id = $object;  | 
            ||
| 498 | $object = new Project($this->db);  | 
            ||
| 499 | $result = $object->fetch($id);  | 
            ||
| 500 |                 if ($result < 0) { | 
            ||
| 501 | dol_print_error($this->db, $object->error);  | 
            ||
| 502 | return -1;  | 
            ||
| 503 | }  | 
            ||
| 504 | }  | 
            ||
| 505 | |||
| 506 | $object->fetch_thirdparty();  | 
            ||
| 507 | |||
| 508 | $dir = $conf->project->dir_output;  | 
            ||
| 509 | $objectref = dol_sanitizeFileName($object->ref);  | 
            ||
| 510 |             if (!preg_match('/specimen/i', $objectref)) { | 
            ||
| 511 | $dir .= "/" . $objectref;  | 
            ||
| 512 | }  | 
            ||
| 513 | $file = $dir . "/" . $objectref . ".odt";  | 
            ||
| 514 | |||
| 515 |             if (!file_exists($dir)) { | 
            ||
| 516 |                 if (dol_mkdir($dir) < 0) { | 
            ||
| 517 |                     $this->error = $langs->transnoentities("ErrorCanNotCreateDir", $dir); | 
            ||
| 518 | return -1;  | 
            ||
| 519 | }  | 
            ||
| 520 | }  | 
            ||
| 521 | |||
| 522 |             if (file_exists($dir)) { | 
            ||
| 523 | //print "srctemplatepath=".$srctemplatepath; // Src filename  | 
            ||
| 524 | $newfile = basename($srctemplatepath);  | 
            ||
| 525 |                 $newfiletmp = preg_replace('/\.od[ts]/i', '', $newfile); | 
            ||
| 526 |                 $newfiletmp = preg_replace('/template_/i', '', $newfiletmp); | 
            ||
| 527 |                 $newfiletmp = preg_replace('/modele_/i', '', $newfiletmp); | 
            ||
| 528 | $newfiletmp = $objectref . '_' . $newfiletmp;  | 
            ||
| 529 | //$file=$dir.'/'.$newfiletmp.'.'.dol_print_date(dol_now(),'%Y%m%d%H%M%S').'.odt';  | 
            ||
| 530 | // Get extension (ods or odt)  | 
            ||
| 531 | $newfileformat = substr($newfile, strrpos($newfile, '.') + 1);  | 
            ||
| 532 |                 if (getDolGlobalString('MAIN_DOC_USE_TIMING')) { | 
            ||
| 533 |                     $format = getDolGlobalString('MAIN_DOC_USE_TIMING'); | 
            ||
| 534 |                     if ($format == '1') { | 
            ||
| 535 | $format = '%Y%m%d%H%M%S';  | 
            ||
| 536 | }  | 
            ||
| 537 | $filename = $newfiletmp . '-' . dol_print_date(dol_now(), $format) . '.' . $newfileformat;  | 
            ||
| 538 |                 } else { | 
            ||
| 539 | $filename = $newfiletmp . '.' . $newfileformat;  | 
            ||
| 540 | }  | 
            ||
| 541 | $file = $dir . '/' . $filename;  | 
            ||
| 542 | //print "newdir=".$dir;  | 
            ||
| 543 | //print "newfile=".$newfile;  | 
            ||
| 544 | //print "file=".$file;  | 
            ||
| 545 | //print "conf->societe->dir_temp=".$conf->societe->dir_temp;  | 
            ||
| 546 | |||
| 547 | dol_mkdir($conf->project->dir_temp);  | 
            ||
| 548 |                 if (!is_writable($conf->project->dir_temp)) { | 
            ||
| 549 |                     $this->error = $langs->transnoentities("ErrorFailedToWriteInTempDirectory", $conf->project->dir_temp); | 
            ||
| 550 |                     dol_syslog('Error in write_file: ' . $this->error, LOG_ERR); | 
            ||
| 551 | return -1;  | 
            ||
| 552 | }  | 
            ||
| 553 | |||
| 554 | // If PROJECTLEADER contact defined on project, we use it  | 
            ||
| 555 | $usecontact = false;  | 
            ||
| 556 |                 $arrayidcontact = $object->getIdContact('external', 'PROJECTLEADER'); | 
            ||
| 557 |                 if (count($arrayidcontact) > 0) { | 
            ||
| 558 | $usecontact = true;  | 
            ||
| 559 | $result = $object->fetch_contact($arrayidcontact[0]);  | 
            ||
| 560 | }  | 
            ||
| 561 | |||
| 562 | // Recipient name  | 
            ||
| 563 | $contactobject = null;  | 
            ||
| 564 |                 if (!empty($usecontact)) { | 
            ||
| 565 | // if we have a PROJECTLEADER contact and we don't use it as recipient we store the contact object for later use  | 
            ||
| 566 | $contactobject = $object->contact;  | 
            ||
| 567 | }  | 
            ||
| 568 | |||
| 569 | $socobject = $object->thirdparty;  | 
            ||
| 570 | |||
| 571 | // Make substitution  | 
            ||
| 572 | $substitutionarray = array(  | 
            ||
| 573 | '__FROM_NAME__' => $this->emetteur->name,  | 
            ||
| 574 | '__FROM_EMAIL__' => $this->emetteur->email,  | 
            ||
| 575 | );  | 
            ||
| 576 | complete_substitutions_array($substitutionarray, $langs, $object);  | 
            ||
| 577 | // Call the ODTSubstitution hook  | 
            ||
| 578 |                 $parameters = array('file' => $file, 'object' => $object, 'outputlangs' => $outputlangs, 'substitutionarray' => &$substitutionarray); | 
            ||
| 579 |                 $reshook = $hookmanager->executeHooks('ODTSubstitution', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks | 
            ||
| 580 | |||
| 581 | // Open and load template  | 
            ||
| 582 | require_once ODTPHP_PATH . 'odf.php';  | 
            ||
| 583 |                 try { | 
            ||
| 584 | $odfHandler = new Odf(  | 
            ||
| 585 | $srctemplatepath,  | 
            ||
| 586 | array(  | 
            ||
| 587 | 'PATH_TO_TMP' => $conf->project->dir_temp,  | 
            ||
| 588 | 'ZIP_PROXY' => 'PclZipProxy', // PhpZipProxy or PclZipProxy. Got "bad compression method" error when using PhpZipProxy.  | 
            ||
| 589 |                             'DELIMITER_LEFT'  => '{', | 
            ||
| 590 | 'DELIMITER_RIGHT' => '}'  | 
            ||
| 591 | )  | 
            ||
| 592 | );  | 
            ||
| 593 |                 } catch (Exception $e) { | 
            ||
| 594 | $this->error = $e->getMessage();  | 
            ||
| 595 | dol_syslog($e->getMessage(), LOG_INFO);  | 
            ||
| 596 | return -1;  | 
            ||
| 597 | }  | 
            ||
| 598 | // After construction $odfHandler->contentXml contains content and  | 
            ||
| 599 | // [!-- BEGIN row.lines --]*[!-- END row.lines --] has been replaced by  | 
            ||
| 600 | // [!-- BEGIN lines --]*[!-- END lines --]  | 
            ||
| 601 | |||
| 602 | // Define substitution array  | 
            ||
| 603 | $substitutionarray = getCommonSubstitutionArray($outputlangs, 0, null, $object);  | 
            ||
| 604 | $array_object_from_properties = $this->get_substitutionarray_each_var_object($object, $outputlangs);  | 
            ||
| 605 | $array_objet = $this->get_substitutionarray_object($object, $outputlangs);  | 
            ||
| 606 | $array_user = $this->get_substitutionarray_user($user, $outputlangs);  | 
            ||
| 607 | $array_soc = $this->get_substitutionarray_mysoc($mysoc, $outputlangs);  | 
            ||
| 608 | $array_thirdparty = $this->get_substitutionarray_thirdparty($socobject, $outputlangs);  | 
            ||
| 609 | $array_other = $this->get_substitutionarray_other($outputlangs);  | 
            ||
| 610 | // retrieve contact information for use in object as contact_xxx tags  | 
            ||
| 611 | $array_project_contact = array();  | 
            ||
| 612 |                 if ($usecontact && is_object($contactobject)) { | 
            ||
| 613 | $array_project_contact = $this->get_substitutionarray_contact($contactobject, $outputlangs, 'contact');  | 
            ||
| 614 | }  | 
            ||
| 615 | |||
| 616 | $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other, $array_project_contact);  | 
            ||
| 617 | complete_substitutions_array($tmparray, $outputlangs, $object);  | 
            ||
| 618 | |||
| 619 | // Call the ODTSubstitution hook  | 
            ||
| 620 |                 $parameters = array('odfHandler' => &$odfHandler, 'file' => $file, 'object' => $object, 'outputlangs' => $outputlangs, 'substitutionarray' => &$tmparray); | 
            ||
| 621 |                 $reshook = $hookmanager->executeHooks('ODTSubstitution', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks | 
            ||
| 622 | |||
| 623 |                 foreach ($tmparray as $key => $value) { | 
            ||
| 624 |                     try { | 
            ||
| 625 |                         if (preg_match('/logo$/', $key)) { // Image | 
            ||
| 626 |                             if (file_exists($value)) { | 
            ||
| 627 | $odfHandler->setImage($key, $value);  | 
            ||
| 628 |                             } else { | 
            ||
| 629 | $odfHandler->setVars($key, 'ErrorFileNotFound', true, 'UTF-8');  | 
            ||
| 630 | }  | 
            ||
| 631 |                         } else { // Text | 
            ||
| 632 | $odfHandler->setVars($key, $value, true, 'UTF-8');  | 
            ||
| 633 | }  | 
            ||
| 634 |                     } catch (OdfException $e) { | 
            ||
| 635 | dol_syslog($e->getMessage(), LOG_INFO);  | 
            ||
| 636 | }  | 
            ||
| 637 | }  | 
            ||
| 638 | |||
| 639 | // Replace tags of lines for tasks  | 
            ||
| 640 |                 try { | 
            ||
| 641 |                     $listlines = $odfHandler->setSegment('tasks'); | 
            ||
| 642 | |||
| 643 | $taskstatic = new Task($this->db);  | 
            ||
| 644 | |||
| 645 | // Security check  | 
            ||
| 646 | $socid = 0;  | 
            ||
| 647 |                     if (!empty($object->fk_soc)) { | 
            ||
| 648 | $socid = $object->fk_soc;  | 
            ||
| 649 | }  | 
            ||
| 650 | |||
| 651 | $tasksarray = $taskstatic->getTasksArray(0, 0, $object->id, $socid, 0);  | 
            ||
| 652 | |||
| 653 | |||
| 654 |                     foreach ($tasksarray as $task) { | 
            ||
| 655 | $tmparray = $this->get_substitutionarray_tasks($task, $outputlangs);  | 
            ||
| 656 | //complete_substitutions_array($tmparray, $outputlangs, $object, $task, "completesubstitutionarray_lines");  | 
            ||
| 657 |                         foreach ($tmparray as $key => $val) { | 
            ||
| 658 |                             try { | 
            ||
| 659 | $listlines->setVars($key, $val, true, 'UTF-8');  | 
            ||
| 660 |                             } catch (SegmentException $e) { | 
            ||
| 661 | dol_syslog($e->getMessage(), LOG_INFO);  | 
            ||
| 662 | }  | 
            ||
| 663 | }  | 
            ||
| 664 | |||
| 665 | $taskobj = new Task($this->db);  | 
            ||
| 666 | $taskobj->fetch($task->id);  | 
            ||
| 667 | |||
| 668 | // Replace tags of lines for contacts task  | 
            ||
| 669 |                         $sourcearray = array('internal', 'external'); | 
            ||
| 670 | $contact_arrray = array();  | 
            ||
| 671 |                         foreach ($sourcearray as $source) { | 
            ||
| 672 | $contact_temp = $taskobj->liste_contact(-1, $source);  | 
            ||
| 673 |                             if ((is_array($contact_temp) && count($contact_temp) > 0)) { | 
            ||
| 674 | $contact_arrray = array_merge($contact_arrray, $contact_temp);  | 
            ||
| 675 | }  | 
            ||
| 676 | }  | 
            ||
| 677 |                         if ((is_array($contact_arrray) && count($contact_arrray) > 0)) { | 
            ||
| 678 |                             $listlinestaskres = $listlines->__get('tasksressources'); | 
            ||
| 679 | |||
| 680 |                             foreach ($contact_arrray as $contact) { | 
            ||
| 681 |                                 if ($contact['source'] == 'internal') { | 
            ||
| 682 | $objectdetail = new User($this->db);  | 
            ||
| 683 | $objectdetail->fetch($contact['id']);  | 
            ||
| 684 | $contact['socname'] = $mysoc->name;  | 
            ||
| 685 |                                 } elseif ($contact['source'] == 'external') { | 
            ||
| 686 | $objectdetail = new Contact($this->db);  | 
            ||
| 687 | $objectdetail->fetch($contact['id']);  | 
            ||
| 688 | |||
| 689 | $soc = new Societe($this->db);  | 
            ||
| 690 | $soc->fetch($contact['socid']);  | 
            ||
| 691 | $contact['socname'] = $soc->name;  | 
            ||
| 692 | }  | 
            ||
| 693 | $contact['fullname'] = $objectdetail->getFullName($outputlangs, 1);  | 
            ||
| 694 | |||
| 695 | $tmparray = $this->get_substitutionarray_tasksressource($contact, $outputlangs);  | 
            ||
| 696 | |||
| 697 |                                 foreach ($tmparray as $key => $val) { | 
            ||
| 698 |                                     try { | 
            ||
| 699 | $listlinestaskres->setVars($key, $val, true, 'UTF-8');  | 
            ||
| 700 |                                     } catch (SegmentException $e) { | 
            ||
| 701 | dol_syslog($e->getMessage(), LOG_INFO);  | 
            ||
| 702 | }  | 
            ||
| 703 | }  | 
            ||
| 704 | $listlinestaskres->merge();  | 
            ||
| 705 | }  | 
            ||
| 706 | }  | 
            ||
| 707 | |||
| 708 | //Time resources  | 
            ||
| 709 | $sql = "SELECT t.rowid, t.element_date as task_date, t.element_duration as task_duration, t.fk_user, t.note";  | 
            ||
| 710 | $sql .= ", u.lastname, u.firstname, t.thm";  | 
            ||
| 711 | $sql .= " FROM " . MAIN_DB_PREFIX . "element_time as t";  | 
            ||
| 712 | $sql .= " , " . MAIN_DB_PREFIX . "user as u";  | 
            ||
| 713 | $sql .= " WHERE t.fk_element =" . ((int) $task->id);  | 
            ||
| 714 | $sql .= " AND t.elementtype = 'task'";  | 
            ||
| 715 | $sql .= " AND t.fk_user = u.rowid";  | 
            ||
| 716 | $sql .= " ORDER BY t.element_date DESC";  | 
            ||
| 717 | |||
| 718 | $resql = $this->db->query($sql);  | 
            ||
| 719 |                         if ($resql) { | 
            ||
| 720 | $num = $this->db->num_rows($resql);  | 
            ||
| 721 | $i = 0;  | 
            ||
| 722 | $tasks = array();  | 
            ||
| 723 | $row = array();  | 
            ||
| 724 |                             $listlinestasktime = $listlines->__get('taskstimes'); | 
            ||
| 725 |                             if (empty($num)) { | 
            ||
| 726 | $row['rowid'] = '';  | 
            ||
| 727 | $row['task_date'] = '';  | 
            ||
| 728 | $row['task_duration'] = '';  | 
            ||
| 729 | $row['$tasktime'] = '';  | 
            ||
| 730 | $row['note'] = '';  | 
            ||
| 731 | $row['fk_user'] = '';  | 
            ||
| 732 | $row['name'] = '';  | 
            ||
| 733 | $row['firstname'] = '';  | 
            ||
| 734 | $row['fullcivname'] = '';  | 
            ||
| 735 | $row['amountht'] = '';  | 
            ||
| 736 | $row['amountttc'] = '';  | 
            ||
| 737 | $row['thm'] = '';  | 
            ||
| 738 | $tmparray = $this->get_substitutionarray_taskstime($row, $outputlangs);  | 
            ||
| 739 |                                 foreach ($tmparray as $key => $val) { | 
            ||
| 740 |                                     try { | 
            ||
| 741 | $listlinestasktime->setVars($key, $val, true, 'UTF-8');  | 
            ||
| 742 |                                     } catch (SegmentException $e) { | 
            ||
| 743 | dol_syslog($e->getMessage(), LOG_INFO);  | 
            ||
| 744 | }  | 
            ||
| 745 | }  | 
            ||
| 746 | $listlinestasktime->merge();  | 
            ||
| 747 | }  | 
            ||
| 748 |                             while ($i < $num) { | 
            ||
| 749 | $row = $this->db->fetch_array($resql);  | 
            ||
| 750 |                                 if (!empty($row['fk_user'])) { | 
            ||
| 751 | $objectdetail = new User($this->db);  | 
            ||
| 752 | $objectdetail->fetch($row['fk_user']);  | 
            ||
| 753 | $row['fullcivname'] = $objectdetail->getFullName($outputlangs, 1);  | 
            ||
| 754 |                                 } else { | 
            ||
| 755 | $row['fullcivname'] = '';  | 
            ||
| 756 | }  | 
            ||
| 757 | |||
| 758 |                                 if (!empty($row['thm'])) { | 
            ||
| 759 | $row['amountht'] = ($row['task_duration'] / 3600) * $row['thm'];  | 
            ||
| 760 | $defaultvat = get_default_tva($mysoc, $mysoc);  | 
            ||
| 761 | $row['amountttc'] = price2num($row['amountht'] * (1 + ($defaultvat / 100)), 'MT');  | 
            ||
| 762 |                                 } else { | 
            ||
| 763 | $row['amountht'] = 0;  | 
            ||
| 764 | $row['amountttc'] = 0;  | 
            ||
| 765 | $row['thm'] = 0;  | 
            ||
| 766 | }  | 
            ||
| 767 | |||
| 768 | $tmparray = $this->get_substitutionarray_taskstime($row, $outputlangs);  | 
            ||
| 769 | |||
| 770 |                                 foreach ($tmparray as $key => $val) { | 
            ||
| 771 |                                     try { | 
            ||
| 772 | $listlinestasktime->setVars($key, $val, true, 'UTF-8');  | 
            ||
| 773 |                                     } catch (SegmentException $e) { | 
            ||
| 774 | dol_syslog($e->getMessage(), LOG_INFO);  | 
            ||
| 775 | }  | 
            ||
| 776 | }  | 
            ||
| 777 | $listlinestasktime->merge();  | 
            ||
| 778 | $i++;  | 
            ||
| 779 | }  | 
            ||
| 780 | $this->db->free($resql);  | 
            ||
| 781 | }  | 
            ||
| 782 | |||
| 783 | |||
| 784 | // Replace tags of project files  | 
            ||
| 785 |                         $listtasksfiles = $listlines->__get('tasksfiles'); | 
            ||
| 786 | |||
| 787 | $upload_dir = $conf->project->dir_output . '/' . dol_sanitizeFileName($object->ref) . '/' . dol_sanitizeFileName($task->ref);  | 
            ||
| 788 | $filearray = dol_dir_list($upload_dir, "files", 0, '', '(\.meta|_preview.*\.png)$', 'name', SORT_ASC, 1);  | 
            ||
| 789 | |||
| 790 | |||
| 791 |                         foreach ($filearray as $filedetail) { | 
            ||
| 792 | $tmparray = $this->get_substitutionarray_task_file($filedetail, $outputlangs);  | 
            ||
| 793 | //dol_syslog(get_class($this).'::main $tmparray'.var_export($tmparray,true));  | 
            ||
| 794 |                             foreach ($tmparray as $key => $val) { | 
            ||
| 795 |                                 try { | 
            ||
| 796 | $listtasksfiles->setVars($key, $val, true, 'UTF-8');  | 
            ||
| 797 |                                 } catch (SegmentException $e) { | 
            ||
| 798 | dol_syslog($e->getMessage(), LOG_INFO);  | 
            ||
| 799 | }  | 
            ||
| 800 | }  | 
            ||
| 801 | $listtasksfiles->merge();  | 
            ||
| 802 | }  | 
            ||
| 803 | $listlines->merge();  | 
            ||
| 804 | }  | 
            ||
| 805 | $odfHandler->mergeSegment($listlines);  | 
            ||
| 806 |                 } catch (OdfException $e) { | 
            ||
| 807 | $ExceptionTrace = $e->getTrace();  | 
            ||
| 808 | // no segment defined on ODT is not an error  | 
            ||
| 809 |                     if ($ExceptionTrace[0]['function'] != 'setSegment') { | 
            ||
| 810 | $this->error = $e->getMessage();  | 
            ||
| 811 | dol_syslog($this->error, LOG_WARNING);  | 
            ||
| 812 | return -1;  | 
            ||
| 813 | }  | 
            ||
| 814 | }  | 
            ||
| 815 | |||
| 816 | // Replace tags of project files  | 
            ||
| 817 |                 try { | 
            ||
| 818 |                     $listlines = $odfHandler->setSegment('projectfiles'); | 
            ||
| 819 | |||
| 820 | $upload_dir = $conf->project->dir_output . '/' . dol_sanitizeFileName($object->ref);  | 
            ||
| 821 | $filearray = dol_dir_list($upload_dir, "files", 0, '', '(\.meta|_preview.*\.png)$', 'name', SORT_ASC, 1);  | 
            ||
| 822 | |||
| 823 |                     foreach ($filearray as $filedetail) { | 
            ||
| 824 | //dol_syslog(get_class($this).'::main $filedetail'.var_export($filedetail,true));  | 
            ||
| 825 | $tmparray = $this->get_substitutionarray_project_file($filedetail, $outputlangs);  | 
            ||
| 826 | |||
| 827 |                         foreach ($tmparray as $key => $val) { | 
            ||
| 828 |                             try { | 
            ||
| 829 | $listlines->setVars($key, $val, true, 'UTF-8');  | 
            ||
| 830 |                             } catch (SegmentException $e) { | 
            ||
| 831 | dol_syslog($e->getMessage(), LOG_INFO);  | 
            ||
| 832 | }  | 
            ||
| 833 | }  | 
            ||
| 834 | $listlines->merge();  | 
            ||
| 835 | }  | 
            ||
| 836 | $odfHandler->mergeSegment($listlines);  | 
            ||
| 837 |                 } catch (OdfException $e) { | 
            ||
| 838 | $this->error = $e->getMessage();  | 
            ||
| 839 | dol_syslog($this->error, LOG_WARNING);  | 
            ||
| 840 | return -1;  | 
            ||
| 841 | }  | 
            ||
| 842 | |||
| 843 | // Replace tags of lines for contacts  | 
            ||
| 844 |                 $sourcearray = array('internal', 'external'); | 
            ||
| 845 | $contact_arrray = array();  | 
            ||
| 846 |                 foreach ($sourcearray as $source) { | 
            ||
| 847 | $contact_temp = $object->liste_contact(-1, $source);  | 
            ||
| 848 |                     if ((is_array($contact_temp) && count($contact_temp) > 0)) { | 
            ||
| 849 | $contact_arrray = array_merge($contact_arrray, $contact_temp);  | 
            ||
| 850 | }  | 
            ||
| 851 | }  | 
            ||
| 852 |                 if ((is_array($contact_arrray) && count($contact_arrray) > 0)) { | 
            ||
| 853 |                     try { | 
            ||
| 854 |                         $listlines = $odfHandler->setSegment('projectcontacts'); | 
            ||
| 855 | |||
| 856 |                         foreach ($contact_arrray as $contact) { | 
            ||
| 857 |                             if ($contact['source'] == 'internal') { | 
            ||
| 858 | $objectdetail = new User($this->db);  | 
            ||
| 859 | $objectdetail->fetch($contact['id']);  | 
            ||
| 860 | $contact['socname'] = $mysoc->name;  | 
            ||
| 861 |                             } elseif ($contact['source'] == 'external') { | 
            ||
| 862 | $objectdetail = new Contact($this->db);  | 
            ||
| 863 | $objectdetail->fetch($contact['id']);  | 
            ||
| 864 | |||
| 865 | $soc = new Societe($this->db);  | 
            ||
| 866 | $soc->fetch($contact['socid']);  | 
            ||
| 867 | $contact['socname'] = $soc->name;  | 
            ||
| 868 | }  | 
            ||
| 869 | $contact['fullname'] = $objectdetail->getFullName($outputlangs, 1);  | 
            ||
| 870 | |||
| 871 | $tmparray = $this->get_substitutionarray_project_contacts($contact, $outputlangs);  | 
            ||
| 872 |                             foreach ($tmparray as $key => $val) { | 
            ||
| 873 |                                 try { | 
            ||
| 874 | $listlines->setVars($key, $val, true, 'UTF-8');  | 
            ||
| 875 |                                 } catch (SegmentException $e) { | 
            ||
| 876 | dol_syslog($e->getMessage(), LOG_INFO);  | 
            ||
| 877 | }  | 
            ||
| 878 | }  | 
            ||
| 879 | $listlines->merge();  | 
            ||
| 880 | }  | 
            ||
| 881 | $odfHandler->mergeSegment($listlines);  | 
            ||
| 882 |                     } catch (OdfException $e) { | 
            ||
| 883 | $this->error = $e->getMessage();  | 
            ||
| 884 | dol_syslog($this->error, LOG_WARNING);  | 
            ||
| 885 | return -1;  | 
            ||
| 886 | }  | 
            ||
| 887 | }  | 
            ||
| 888 | |||
| 889 | //List of referent  | 
            ||
| 890 | |||
| 891 | $listofreferent = array(  | 
            ||
| 892 | 'propal' => array(  | 
            ||
| 893 | 'title' => "ListProposalsAssociatedProject",  | 
            ||
| 894 | 'class' => 'Propal',  | 
            ||
| 895 | 'table' => 'propal',  | 
            ||
| 896 |                         'test' => isModEnabled('propal') && $user->hasRight('propal', 'lire') | 
            ||
| 897 | ),  | 
            ||
| 898 | 'order' => array(  | 
            ||
| 899 | 'title' => "ListOrdersAssociatedProject",  | 
            ||
| 900 | 'class' => 'Commande',  | 
            ||
| 901 | 'table' => 'commande',  | 
            ||
| 902 |                         'test' => isModEnabled('order') && $user->hasRight('commande', 'lire') | 
            ||
| 903 | ),  | 
            ||
| 904 | 'invoice' => array(  | 
            ||
| 905 | 'title' => "ListInvoicesAssociatedProject",  | 
            ||
| 906 | 'class' => 'Facture',  | 
            ||
| 907 | 'table' => 'facture',  | 
            ||
| 908 |                         'test' => isModEnabled('invoice') && $user->hasRight('facture', 'lire') | 
            ||
| 909 | ),  | 
            ||
| 910 | 'invoice_predefined' => array(  | 
            ||
| 911 | 'title' => "ListPredefinedInvoicesAssociatedProject",  | 
            ||
| 912 | 'class' => 'FactureRec',  | 
            ||
| 913 | 'table' => 'facture_rec',  | 
            ||
| 914 |                         'test' => isModEnabled('invoice') && $user->hasRight('facture', 'lire') | 
            ||
| 915 | ),  | 
            ||
| 916 | 'proposal_supplier' => array(  | 
            ||
| 917 | 'title' => "ListSupplierProposalsAssociatedProject",  | 
            ||
| 918 | 'class' => 'SupplierProposal',  | 
            ||
| 919 | 'table' => 'supplier_proposal',  | 
            ||
| 920 |                         'test' => isModEnabled('supplier_proposal') && $user->hasRight('supplier_proposal', 'lire') | 
            ||
| 921 | ),  | 
            ||
| 922 | 'order_supplier' => array(  | 
            ||
| 923 | 'title' => "ListSupplierOrdersAssociatedProject",  | 
            ||
| 924 | 'table' => 'commande_fournisseur',  | 
            ||
| 925 | 'class' => 'CommandeFournisseur',  | 
            ||
| 926 |                         'test' => (isModEnabled("fournisseur") && !getDolGlobalString('MAIN_USE_NEW_SUPPLIERMOD') && $user->hasRight('fournisseur', 'commande', 'lire')) || (isModEnabled("supplier_order") && $user->hasRight('supplier_order', 'lire')) | 
            ||
| 927 | ),  | 
            ||
| 928 | 'invoice_supplier' => array(  | 
            ||
| 929 | 'title' => "ListSupplierInvoicesAssociatedProject",  | 
            ||
| 930 | 'table' => 'facture_fourn',  | 
            ||
| 931 | 'class' => 'FactureFournisseur',  | 
            ||
| 932 |                         'test' => (isModEnabled("fournisseur") && !getDolGlobalString('MAIN_USE_NEW_SUPPLIERMOD') && $user->hasRight('fournisseur', 'facture', 'lire')) || (isModEnabled("supplier_invoice") && $user->hasRight('supplier_invoice', 'lire')) | 
            ||
| 933 | ),  | 
            ||
| 934 | 'contract' => array(  | 
            ||
| 935 | 'title' => "ListContractAssociatedProject",  | 
            ||
| 936 | 'class' => 'Contrat',  | 
            ||
| 937 | 'table' => 'contrat',  | 
            ||
| 938 |                         'test' => isModEnabled('contract') && $user->hasRight('contrat', 'lire') | 
            ||
| 939 | ),  | 
            ||
| 940 | 'intervention' => array(  | 
            ||
| 941 | 'title' => "ListFichinterAssociatedProject",  | 
            ||
| 942 | 'class' => 'Fichinter',  | 
            ||
| 943 | 'table' => 'fichinter',  | 
            ||
| 944 | 'disableamount' => 1,  | 
            ||
| 945 |                         'test' => isModEnabled('intervention') && $user->hasRight('ficheinter', 'lire') | 
            ||
| 946 | ),  | 
            ||
| 947 | 'shipping' => array(  | 
            ||
| 948 | 'title' => "ListShippingAssociatedProject",  | 
            ||
| 949 | 'class' => 'Expedition',  | 
            ||
| 950 | 'table' => 'expedition',  | 
            ||
| 951 | 'disableamount' => 1,  | 
            ||
| 952 |                         'test' => isModEnabled('shipping') && $user->hasRight('expedition', 'lire') | 
            ||
| 953 | ),  | 
            ||
| 954 | 'trip' => array(  | 
            ||
| 955 | 'title' => "ListTripAssociatedProject",  | 
            ||
| 956 | 'class' => 'Deplacement',  | 
            ||
| 957 | 'table' => 'deplacement',  | 
            ||
| 958 | 'disableamount' => 1,  | 
            ||
| 959 |                         'test' => isModEnabled('deplacement') && $user->hasRight('deplacement', 'lire') | 
            ||
| 960 | ),  | 
            ||
| 961 | 'expensereport' => array(  | 
            ||
| 962 | 'title' => "ListExpenseReportsAssociatedProject",  | 
            ||
| 963 | 'class' => 'ExpenseReportLine',  | 
            ||
| 964 | 'table' => 'expensereport_det',  | 
            ||
| 965 |                         'test' => isModEnabled('expensereport') && $user->hasRight('expensereport', 'lire') | 
            ||
| 966 | ),  | 
            ||
| 967 | 'donation' => array(  | 
            ||
| 968 | 'title' => "ListDonationsAssociatedProject",  | 
            ||
| 969 | 'class' => 'Don',  | 
            ||
| 970 | 'table' => 'don',  | 
            ||
| 971 |                         'test' => isModEnabled('don') && $user->hasRight('don', 'lire') | 
            ||
| 972 | ),  | 
            ||
| 973 | 'loan' => array(  | 
            ||
| 974 | 'title' => "ListLoanAssociatedProject",  | 
            ||
| 975 | 'class' => 'Loan',  | 
            ||
| 976 | 'table' => 'loan',  | 
            ||
| 977 |                         'test' => isModEnabled('loan') && $user->hasRight('loan', 'read') | 
            ||
| 978 | ),  | 
            ||
| 979 | 'chargesociales' => array(  | 
            ||
| 980 | 'title' => "ListSocialContributionAssociatedProject",  | 
            ||
| 981 | 'class' => 'ChargeSociales',  | 
            ||
| 982 | 'table' => 'chargesociales',  | 
            ||
| 983 |                         'urlnew' => constant('BASE_URL') . '/compta/sociales/card.php?action=create&projectid=' . $object->id, | 
            ||
| 984 |                         'test' => isModEnabled('tax') && $user->hasRight('tax', 'charges', 'lire') | 
            ||
| 985 | ),  | 
            ||
| 986 | 'stock_mouvement' => array(  | 
            ||
| 987 | 'title' => "ListMouvementStockProject",  | 
            ||
| 988 | 'class' => 'MouvementStock',  | 
            ||
| 989 | 'table' => 'stock_mouvement',  | 
            ||
| 990 |                         'test' => (isModEnabled('stock') && $user->hasRight('stock', 'mouvement', 'lire') && getDolGlobalString('STOCK_MOVEMENT_INTO_PROJECT_OVERVIEW')) | 
            ||
| 991 | ),  | 
            ||
| 992 | 'agenda' => array(  | 
            ||
| 993 | 'title' => "ListActionsAssociatedProject",  | 
            ||
| 994 | 'class' => 'ActionComm',  | 
            ||
| 995 | 'table' => 'actioncomm',  | 
            ||
| 996 | 'disableamount' => 1,  | 
            ||
| 997 |                         'test' => isModEnabled('agenda') && $user->hasRight('agenda', 'allactions', 'lire') | 
            ||
| 998 | ),  | 
            ||
| 999 | );  | 
            ||
| 1000 | |||
| 1001 | // Insert list of objects into the project  | 
            ||
| 1002 |                 try { | 
            ||
| 1003 |                     $listlines = $odfHandler->setSegment('projectrefs'); | 
            ||
| 1004 | |||
| 1005 |                     foreach ($listofreferent as $keyref => $valueref) { | 
            ||
| 1006 | $title = $valueref['title'];  | 
            ||
| 1007 | $tablename = $valueref['table'];  | 
            ||
| 1008 | $classname = $valueref['class'];  | 
            ||
| 1009 | $qualified = $valueref['test'];  | 
            ||
| 1010 |                         if ($qualified) { | 
            ||
| 1011 | $elementarray = $object->get_element_list($keyref, $tablename);  | 
            ||
| 1012 |                             if (count($elementarray) > 0 && is_array($elementarray)) { | 
            ||
| 1013 | $total_ht = 0;  | 
            ||
| 1014 | $total_ttc = 0;  | 
            ||
| 1015 | $num = count($elementarray);  | 
            ||
| 1016 |                                 for ($i = 0; $i < $num; $i++) { | 
            ||
| 1017 | $ref_array = array();  | 
            ||
| 1018 | $ref_array['type'] = $langs->trans($classname);  | 
            ||
| 1019 | |||
| 1020 | $element = new $classname($this->db);  | 
            ||
| 1021 | $element->fetch($elementarray[$i]);  | 
            ||
| 1022 | $element->fetch_thirdparty();  | 
            ||
| 1023 | |||
| 1024 | //Ref object  | 
            ||
| 1025 | $ref_array['ref'] = $element->ref;  | 
            ||
| 1026 | |||
| 1027 | //Date object  | 
            ||
| 1028 | $dateref = $element->date;  | 
            ||
| 1029 |                                     if (empty($dateref)) { | 
            ||
| 1030 | $dateref = $element->datep;  | 
            ||
| 1031 | }  | 
            ||
| 1032 |                                     if (empty($dateref)) { | 
            ||
| 1033 | $dateref = $element->date_contrat;  | 
            ||
| 1034 | }  | 
            ||
| 1035 | $ref_array['date'] = $dateref;  | 
            ||
| 1036 | |||
| 1037 | //Soc object  | 
            ||
| 1038 |                                     if (is_object($element->thirdparty)) { | 
            ||
| 1039 | $ref_array['socname'] = $element->thirdparty->name;  | 
            ||
| 1040 |                                     } else { | 
            ||
| 1041 | $ref_array['socname'] = '';  | 
            ||
| 1042 | }  | 
            ||
| 1043 | |||
| 1044 | //Amount object  | 
            ||
| 1045 |                                     if (empty($valueref['disableamount'])) { | 
            ||
| 1046 |                                         if (!empty($element->total_ht)) { | 
            ||
| 1047 | $ref_array['amountht'] = $element->total_ht;  | 
            ||
| 1048 | $ref_array['amountttc'] = $element->total_ttc;  | 
            ||
| 1049 |                                         } else { | 
            ||
| 1050 | $ref_array['amountht'] = 0;  | 
            ||
| 1051 | $ref_array['amountttc'] = 0;  | 
            ||
| 1052 | }  | 
            ||
| 1053 |                                     } else { | 
            ||
| 1054 | $ref_array['amountht'] = '';  | 
            ||
| 1055 | $ref_array['amountttc'] = '';  | 
            ||
| 1056 | }  | 
            ||
| 1057 | |||
| 1058 | $ref_array['status'] = $element->getLibStatut(0);  | 
            ||
| 1059 | |||
| 1060 | $tmparray = $this->get_substitutionarray_project_reference($ref_array, $outputlangs);  | 
            ||
| 1061 | |||
| 1062 |                                     foreach ($tmparray as $key => $val) { | 
            ||
| 1063 |                                         try { | 
            ||
| 1064 | $listlines->setVars($key, $val, true, 'UTF-8');  | 
            ||
| 1065 |                                         } catch (SegmentException $e) { | 
            ||
| 1066 | dol_syslog($e->getMessage(), LOG_INFO);  | 
            ||
| 1067 | }  | 
            ||
| 1068 | }  | 
            ||
| 1069 | $listlines->merge();  | 
            ||
| 1070 | }  | 
            ||
| 1071 | }  | 
            ||
| 1072 | }  | 
            ||
| 1073 | $odfHandler->mergeSegment($listlines);  | 
            ||
| 1074 | }  | 
            ||
| 1075 |                 } catch (OdfExceptionSegmentNotFound $e) { | 
            ||
| 1076 | // Do nothing  | 
            ||
| 1077 |                 } catch (OdfException $e) { | 
            ||
| 1078 | $this->error = $e->getMessage();  | 
            ||
| 1079 | dol_syslog($this->error, LOG_WARNING);  | 
            ||
| 1080 | return -1;  | 
            ||
| 1081 | }  | 
            ||
| 1082 | |||
| 1083 | // Replace labels translated  | 
            ||
| 1084 | $tmparray = $outputlangs->get_translations_for_substitutions();  | 
            ||
| 1085 |                 foreach ($tmparray as $key => $value) { | 
            ||
| 1086 |                     try { | 
            ||
| 1087 | $odfHandler->setVars($key, $value, true, 'UTF-8');  | 
            ||
| 1088 |                     } catch (OdfException $e) { | 
            ||
| 1089 | dol_syslog($e->getMessage(), LOG_INFO);  | 
            ||
| 1090 | }  | 
            ||
| 1091 | }  | 
            ||
| 1092 | |||
| 1093 | // Call the beforeODTSave hook  | 
            ||
| 1094 |                 $parameters = array('odfHandler' => &$odfHandler, 'file' => $file, 'object' => $object, 'outputlangs' => $outputlangs, 'substitutionarray' => &$tmparray); | 
            ||
| 1095 |                 $reshook = $hookmanager->executeHooks('beforeODTSave', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks | 
            ||
| 1096 | |||
| 1097 | |||
| 1098 | // Write new file  | 
            ||
| 1099 |                 if (getDolGlobalString('MAIN_ODT_AS_PDF')) { | 
            ||
| 1100 |                     try { | 
            ||
| 1101 | $odfHandler->exportAsAttachedPDF($file);  | 
            ||
| 1102 |                     } catch (Exception $e) { | 
            ||
| 1103 | $this->error = $e->getMessage();  | 
            ||
| 1104 | return -1;  | 
            ||
| 1105 | }  | 
            ||
| 1106 |                 } else { | 
            ||
| 1107 |                     try { | 
            ||
| 1108 | $odfHandler->saveToDisk($file);  | 
            ||
| 1109 |                     } catch (Exception $e) { | 
            ||
| 1110 | $this->error = $e->getMessage();  | 
            ||
| 1111 | dol_syslog($e->getMessage(), LOG_INFO);  | 
            ||
| 1112 | return -1;  | 
            ||
| 1113 | }  | 
            ||
| 1114 | }  | 
            ||
| 1115 |                 $parameters = array('odfHandler' => &$odfHandler, 'file' => $file, 'object' => $object, 'outputlangs' => $outputlangs, 'substitutionarray' => &$tmparray); | 
            ||
| 1116 |                 $reshook = $hookmanager->executeHooks('afterODTCreation', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks | 
            ||
| 1117 | |||
| 1118 | dolChmod($file);  | 
            ||
| 1119 | |||
| 1120 | $odfHandler = null; // Destroy object  | 
            ||
| 1121 | |||
| 1122 |                 $this->result = array('fullpath' => $file); | 
            ||
| 1123 | |||
| 1124 | return 1; // Success  | 
            ||
| 1125 |             } else { | 
            ||
| 1126 |                 $this->error = $langs->transnoentities("ErrorCanNotCreateDir", $dir); | 
            ||
| 1127 | return -1;  | 
            ||
| 1128 | }  | 
            ||
| 1129 | }  | 
            ||
| 1130 | |||
| 1131 | return -1;  | 
            ||
| 1132 | }  | 
            ||
| 1134 |