82 public $version =
'dolibarr';
95 $langs->loadLangs(array(
"main",
"companies"));
98 $this->
name =
"ODT templates";
99 $this->
description = $langs->trans(
"DocumentModelOdt");
100 $this->scandir =
'PROJECT_TASK_ADDON_PDF_ODT_PATH';
104 $this->page_largeur = 0;
105 $this->page_hauteur = 0;
106 $this->format = array($this->page_largeur, $this->page_hauteur);
107 $this->marge_gauche = 0;
108 $this->marge_droite = 0;
109 $this->marge_haute = 0;
110 $this->marge_basse = 0;
112 $this->option_logo = 1;
113 $this->option_tva = 0;
114 $this->option_modereg = 0;
115 $this->option_condreg = 0;
116 $this->option_multilang = 0;
117 $this->option_escompte = 0;
118 $this->option_credit_note = 0;
119 $this->option_freetext = 1;
120 $this->option_draft_watermark = 0;
129 if (!$this->emetteur->country_code) {
130 $this->emetteur->country_code = substr($langs->defaultlang, -2);
155 $array_key.
'_id' =>
$object->id,
156 $array_key.
'_ref' =>
$object->ref,
157 $array_key.
'_title' =>
$object->title,
158 $array_key.
'_description' =>
$object->description,
163 $array_key.
'_note_private' =>
$object->note_private,
164 $array_key.
'_note_public' =>
$object->note_public,
165 $array_key.
'_public' =>
$object->public,
166 $array_key.
'_statut' =>
$object->getLibStatut()
170 if (is_array(
$object->array_options) && count(
$object->array_options)) {
194 'task_ref' => $task->ref,
195 'task_fk_project' => $task->fk_project,
196 'task_projectref' => $task->projectref,
197 'task_projectlabel' => $task->projectlabel,
198 'task_label' => $task->label,
199 'task_description' => $task->description,
200 'task_fk_parent' => $task->fk_task_parent,
201 'task_duration' => $task->duration_effective,
203 'task_planned_workload' => $task->planned_workload,
204 'task_planned_workload_formated' =>
convertSecondToTime($task->planned_workload,
'allhourmin'),
205 'task_progress' => $task->progress,
206 'task_public' => $task->public,
209 'task_note_private' => (
string) $task->note_private,
210 'task_note_public' => (
string) $task->note_public
214 if (is_array($task->array_options) && count($task->array_options)) {
215 $task->fetch_optionals();
235 'projcontacts_id' => $contact[
'id'],
236 'projcontacts_rowid' => $contact[
'rowid'],
237 'projcontacts_role' => $contact[
'libelle'],
238 'projcontacts_lastname' => $contact[
'lastname'],
239 'projcontacts_firstname' => $contact[
'firstname'],
240 'projcontacts_fullcivname' => $contact[
'fullname'],
241 'projcontacts_socname' => $contact[
'socname'],
242 'projcontacts_email' => $contact[
'email']
258 'projfile_name' => $file[
'name'],
260 'projfile_size' => $file[
'size']
276 'projref_type' => (
string) $refdetail[
'type'],
277 'projref_ref' => (
string) $refdetail[
'ref'],
279 'projref_socname' => (
string) $refdetail[
'socname'],
280 'projref_amountht' =>
price($refdetail[
'amountht'], 0, $outputlangs),
281 'projref_amountttc' =>
price($refdetail[
'amountttc'], 0, $outputlangs),
282 'projref_status' => (
int) $refdetail[
'status']
300 'taskressource_rowid' => $taskresource[
'rowid'],
301 'taskressource_role' => $taskresource[
'libelle'],
302 'taskressource_lastname' => $taskresource[
'lastname'],
303 'taskressource_firstname' => $taskresource[
'firstname'],
304 'taskressource_fullcivname' => $taskresource[
'fullname'],
305 'taskressource_socname' => $taskresource[
'socname'],
306 'taskressource_email' => $taskresource[
'email']
324 'tasktime_rowid' => $tasktime[
'rowid'],
325 'tasktime_task_date' =>
dol_print_date($tasktime[
'task_date'],
'day'),
327 'tasktime_note' => $tasktime[
'note'],
328 'tasktime_fk_user' => $tasktime[
'fk_user'],
329 'tasktime_user_name' => $tasktime[
'lastname'],
330 'tasktime_user_first' => $tasktime[
'firstname'],
331 'tasktime_fullcivname' => $tasktime[
'fullcivname']
347 'tasksfile_name' => $file[
'name'],
349 'tasksfile_size' => $file[
'size']
362 global
$conf, $langs;
365 $langs->loadLangs(array(
"errors",
"companies"));
367 $form =
new Form($this->db);
370 $texte .=
'<form action="'.$_SERVER[
"PHP_SELF"].
'" method="POST" enctype="multipart/form-data">';
371 $texte .=
'<input type="hidden" name="token" value="'.newToken().
'">';
372 $texte .=
'<input type="hidden" name="page_y" value="">';
373 $texte .=
'<input type="hidden" name="action" value="setModuleOptions">';
374 $texte .=
'<input type="hidden" name="param1" value="PROJECT_TASK_ADDON_PDF_ODT_PATH">';
375 $texte .=
'<table class="nobordernopadding centpercent">';
378 $texte .=
'<tr><td>';
379 $texttitle = $langs->trans(
"ListOfDirectories");
380 $listofdir = explode(
',', preg_replace(
'/[\r\n]+/',
',', trim(
$conf->global->PROJECT_TASK_ADDON_PDF_ODT_PATH)));
381 $listoffiles = array();
382 foreach ($listofdir as $key => $tmpdir) {
383 $tmpdir = trim($tmpdir);
384 $tmpdir = preg_replace(
'/DOL_DATA_ROOT/', DOL_DATA_ROOT, $tmpdir);
386 unset($listofdir[$key]);
389 if (!is_dir($tmpdir)) {
390 $texttitle .=
img_warning($langs->trans(
"ErrorDirNotFound", $tmpdir),
'');
392 $tmpfiles =
dol_dir_list($tmpdir,
'files', 0,
'\.(ods|odt)');
393 if (count($tmpfiles)) {
394 $listoffiles = array_merge($listoffiles, $tmpfiles);
398 $texthelp = $langs->trans(
"ListOfDirectoriesForModelGenODT");
399 $texthelp .=
'<br><br><span class="opacitymedium">'.$langs->trans(
"ExampleOfDirectoriesForModelGen").
'</span>';
401 $texthelp .=
'<br>'.$langs->trans(
"FollowingSubstitutionKeysCanBeUsed").
'<br>';
402 $texthelp .= $langs->transnoentitiesnoconv(
"FullListOnOnlineDocumentation");
404 $texte .= $form->textwithpicto($texttitle, $texthelp, 1,
'help',
'', 1, 3, $this->
name);
405 $texte .=
'<div><div style="display: inline-block; min-width: 100px; vertical-align: middle;">';
406 $texte .=
'<textarea class="flat textareafordir" spellcheck="false" cols="60" name="value1">';
408 $texte .=
'</textarea>';
409 $texte .=
'</div><div style="display: inline-block; vertical-align: middle;">';
410 $texte .=
'<input type="submit" class="button button-edit reposition smallpaddingimp" name="modify" value="'.dol_escape_htmltag($langs->trans(
"Modify")).
'">';
411 $texte .=
'<br></div></div>';
414 $nbofiles = count($listoffiles);
416 $texte .= $langs->trans(
"NumberOfModelFilesFound").
': <b>';
424 $texte .=
'<div id="div_'.get_class($this).
'" class="hiddenx">';
426 foreach ($listoffiles as $file) {
427 $texte .=
'- '.$file[
'name'].
' <a href="'.DOL_URL_ROOT.
'/document.php?modulepart=doctemplates&file=tasks/'.urlencode(basename($file[
'name'])).
'">'.
img_picto(
'',
'listlight').
'</a>';
428 $texte .=
' <a class="reposition" href="'.$_SERVER[
"PHP_SELF"].
'?modulepart=doctemplates&keyforuploaddir=PROJECT_TASK_ADDON_PDF_ODT_PATH&action=deletefile&token='.newToken().
'&file='.urlencode(basename($file[
'name'])).
'">'.
img_picto(
'',
'delete').
'</a>';
434 $texte .=
'<div>'.$langs->trans(
"UploadNewTemplate");
436 $maxmin = $maxfilesizearray[
'maxmin'];
438 $texte .=
'<input type="hidden" name="MAX_FILE_SIZE" value="'.($maxmin * 1024).
'">';
440 $texte .=
' <input type="file" name="uploadfile">';
441 $texte .=
'<input type="hidden" value="PROJECT_TASK_ADDON_PDF_ODT_PATH" name="keyforuploaddir">';
442 $texte .=
'<input type="submit" class="button smallpaddingimp reposition" value="'.dol_escape_htmltag($langs->trans(
"Upload")).
'" name="upload">';
448 $texte .=
'</table>';
466 public function write_file(
$object, $outputlangs, $srctemplatepath =
'', $hidedetails = 0, $hidedesc = 0, $hideref = 0)
471 if (empty($srctemplatepath)) {
472 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
477 if (!is_object($hookmanager)) {
478 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
481 $hookmanager->initHooks(array(
'odtgeneration'));
484 if (!is_object($outputlangs)) {
485 $outputlangs = $langs;
487 $sav_charset_output = $outputlangs->charset_output;
488 $outputlangs->charset_output =
'UTF-8';
491 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"projects"));
493 if (
$conf->project->dir_output) {
504 $project =
new Project($this->db);
505 $project->fetch((
int)
$object->fk_project);
506 $project->fetch_thirdparty();
508 $dir =
$conf->project->dir_output.
"/".$project->ref.
"/";
510 if (!preg_match(
'/specimen/i', $objectref)) {
511 $dir .=
"/".$objectref;
513 $file = $dir.
"/".$objectref.
".odt";
515 if (!file_exists($dir)) {
517 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
523 if (file_exists($dir)) {
525 $newfile = basename($srctemplatepath);
526 $newfiletmp = preg_replace(
'/\.(ods|odt)/i',
'', $newfile);
527 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
528 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
529 $newfiletmp = $objectref .
'_' . $newfiletmp;
531 $file = $dir .
'/' . $newfiletmp .
'.odt';
538 if (!is_writable(
$conf->project->dir_temp)) {
539 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory",
$conf->project->dir_temp);
540 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
544 $socobject = $project->thirdparty;
547 $substitutionarray = array(
548 '__FROM_NAME__' => $this->emetteur->name,
549 '__FROM_EMAIL__' => $this->emetteur->email,
555 $parameters = array(
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
556 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
559 require_once ODTPHP_PATH.
'odf.php';
561 $odfHandler =
new Odf(
564 'PATH_TO_TMP' =>
$conf->project->dir_temp,
566 'DELIMITER_LEFT' =>
'{',
567 'DELIMITER_RIGHT' =>
'}'
571 $this->error = $e->getMessage();
582 $substitutionarray = getCommonSubstitutionArray($outputlangs, 0,
null,
$object);
590 $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other);
600 foreach ($tmparray as $key => $value) {
602 if (preg_match(
'/logo$/', $key)) {
603 if (file_exists($value)) {
604 $odfHandler->setImage($key, $value, $ratio);
606 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
609 $odfHandler->setVars($key, $value,
true,
'UTF-8');
611 }
catch (OdfException $e) {
622 if (!empty($project->fk_soc)) {
623 $socid = $project->fk_soc;
628 foreach ($tmparray as $key => $val) {
630 $odfHandler->setVars($key, $val,
true,
'UTF-8');
631 }
catch (OdfException $e) {
637 $sourcearray = array(
'internal',
'external');
638 $contact_array = array();
639 foreach ($sourcearray as $source) {
640 $contact_temp =
$object->liste_contact(-1, $source);
641 if ((is_array($contact_temp) && count($contact_temp) > 0)) {
642 $contact_array = array_merge($contact_array, $contact_temp);
646 $listlinestaskres =
null;
648 $listlinestaskres = $odfHandler->setSegment(
'tasksressources');
649 }
catch (OdfExceptionSegmentNotFound $e) {
651 $listlinestaskres =
null;
654 if ($listlinestaskres && (is_array($contact_array) && count($contact_array) > 0)) {
655 foreach ($contact_array as $contact) {
656 if ($contact[
'source'] ==
'internal') {
657 $objectdetail =
new User($this->db);
658 $objectdetail->fetch($contact[
'id']);
659 $contact[
'socname'] =
$mysoc->name;
660 } elseif ($contact[
'source'] ==
'external') {
661 $objectdetail =
new Contact($this->db);
662 $objectdetail->fetch($contact[
'id']);
665 $soc->fetch($contact[
'socid']);
666 $contact[
'socname'] = $soc->name;
668 dol_syslog(get_class().
'::'.__METHOD__.
' Unexpected contact source:'.$contact[
'source'], LOG_WARNING);
669 $objectdetail =
null;
671 $contact[
'fullname'] = is_object($objectdetail) ? $objectdetail->getFullName($outputlangs, 1) :
null;
675 foreach ($tmparray as $key => $val) {
677 $listlinestaskres->setVars($key, $val,
true,
'UTF-8');
678 }
catch (SegmentException $e) {
682 $listlinestaskres->merge();
684 $odfHandler->mergeSegment($listlinestaskres);
688 $listlinestasktime =
null;
690 $listlinestasktime = $odfHandler->setSegment(
'taskstimes');
691 }
catch (OdfExceptionSegmentNotFound $e) {
693 $listlinestasktime =
null;
698 $sql =
"SELECT t.rowid, t.element_date as task_date, t.element_duration as task_duration, t.fk_user, t.note";
699 $sql .=
", u.lastname, u.firstname";
700 $sql .=
" FROM ".MAIN_DB_PREFIX.
"element_time as t";
701 $sql .=
" , ".MAIN_DB_PREFIX.
"user as u";
702 $sql .=
" WHERE t.fk_element =".((int)
$object->id);
703 $sql .=
" AND t.elementtype = 'task'";
704 $sql .=
" AND t.fk_user = u.rowid";
705 $sql .=
" ORDER BY t.element_date DESC";
707 $resql = $this->db->query($sql);
708 if ($listlinestasktime !==
null && $resql) {
709 $num = $this->db->num_rows($resql);
714 $row = $this->db->fetch_array($resql);
715 if (!empty($row[
'fk_user'])) {
716 $objectdetail =
new User($this->db);
717 $objectdetail->fetch($row[
'fk_user']);
719 $row[
'fullcivname'] = $objectdetail->getFullName($outputlangs, 1);
721 $row[
'fullcivname'] =
'';
726 foreach ($tmparray as $key => $val) {
728 $listlinestasktime->setVars($key, $val,
true,
'UTF-8');
729 }
catch (SegmentException $e) {
733 $listlinestasktime->merge();
736 $this->db->free($resql);
738 $odfHandler->mergeSegment($listlinestasktime);
744 $listtasksfiles =
null;
746 $listtasksfiles = $odfHandler->setSegment(
'tasksfiles');
747 }
catch (OdfExceptionSegmentNotFound $e) {
749 $listtasksfiles =
null;
752 if ($listtasksfiles !==
null) {
754 $filearray =
dol_dir_list($upload_dir,
"files", 0,
'',
'(\.meta|_preview.*\.png)$',
'name', SORT_ASC, 1);
756 foreach ($filearray as $filedetail) {
759 foreach ($tmparray as $key => $val) {
761 $listtasksfiles->setVars($key, $val,
true,
'UTF-8');
762 }
catch (SegmentException $e) {
766 $listtasksfiles->merge();
770 $odfHandler->mergeSegment($listtasksfiles);
772 }
catch (OdfException $e) {
773 $this->error = $e->getMessage();
783 $listlines = $odfHandler->setSegment(
'projectfiles');
784 }
catch (OdfExceptionSegmentNotFound $e) {
789 if ($listlines !==
null) {
792 $filearray =
dol_dir_list($upload_dir,
"files", 0,
'',
'(\.meta|_preview.*\.png)$',
'name', SORT_ASC, 1);
794 foreach ($filearray as $filedetail) {
798 foreach ($tmparray as $key => $val) {
800 $listlines->setVars($key, $val,
true,
'UTF-8');
801 }
catch (SegmentException $e) {
807 $odfHandler->mergeSegment($listlines);
808 }
catch (OdfException $e) {
809 $this->error = $e->getMessage();
816 $sourcearray = array(
'internal',
'external');
817 $contact_array = array();
818 foreach ($sourcearray as $source) {
819 $contact_temp = $project->liste_contact(-1, $source);
820 if ((is_array($contact_temp) && count($contact_temp) > 0)) {
821 $contact_array = array_merge($contact_array, $contact_temp);
827 $listlines = $odfHandler->setSegment(
'projectcontacts');
828 }
catch (OdfExceptionSegmentNotFound $e) {
833 if ($listlines !==
null && (is_array($contact_array) && count($contact_array) > 0)) {
835 foreach ($contact_array as $contact) {
836 if ($contact[
'source'] ==
'internal') {
837 $objectdetail =
new User($this->db);
838 $objectdetail->fetch($contact[
'id']);
839 $contact[
'socname'] =
$mysoc->name;
840 } elseif ($contact[
'source'] ==
'external') {
841 $objectdetail =
new Contact($this->db);
842 $objectdetail->fetch($contact[
'id']);
845 $soc->fetch($contact[
'socid']);
846 $contact[
'socname'] = $soc->name;
848 dol_syslog(get_class().
'::'.__METHOD__.
' Unexpected contact source:'.$contact[
'source'], LOG_ERR);
851 $contact[
'fullname'] = $objectdetail->getFullName($outputlangs, 1);
855 foreach ($tmparray as $key => $val) {
857 $listlines->setVars($key, $val,
true,
'UTF-8');
858 }
catch (SegmentException $e) {
864 $odfHandler->mergeSegment($listlines);
865 }
catch (OdfException $e) {
866 $this->error = $e->getMessage();
874 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
875 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
881 $odfHandler->exportAsAttachedPDF($file);
883 $this->error = $e->getMessage();
889 $odfHandler->saveToDisk($file);
891 $this->error = $e->getMessage();
896 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
897 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
903 $this->result = array(
'fullpath' => $file);
907 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);