27 require_once DOL_DOCUMENT_ROOT.
'/core/modules/ticket/modules_ticket.php';
28 require_once DOL_DOCUMENT_ROOT.
'/core/lib/company.lib.php';
29 require_once DOL_DOCUMENT_ROOT.
'/core/lib/functions2.lib.php';
30 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
31 require_once DOL_DOCUMENT_ROOT.
'/core/lib/doc.lib.php';
48 public $phpmin = array(5, 6);
54 public $version =
'dolibarr';
64 global $conf, $langs, $mysoc;
67 $langs->loadLangs(array(
"main",
"companies"));
70 $this->
name =
"ODT templates";
71 $this->
description = $langs->trans(
"DocumentModelOdt");
72 $this->scandir =
'TICKET_ADDON_PDF_ODT_PATH';
76 $this->page_largeur = 0;
77 $this->page_hauteur = 0;
78 $this->format = array($this->page_largeur, $this->page_hauteur);
79 $this->marge_gauche = 0;
80 $this->marge_droite = 0;
81 $this->marge_haute = 0;
82 $this->marge_basse = 0;
84 $this->option_logo = 1;
85 $this->option_tva = 0;
86 $this->option_multilang = 1;
87 $this->option_freetext = 0;
88 $this->option_draft_watermark = 0;
91 $this->emetteur = $mysoc;
92 if (!$this->emetteur->country_code) {
93 $this->emetteur->country_code = substr($langs->defaultlang, -2);
106 global $conf, $langs;
109 $langs->loadLangs(array(
'companies',
'errors'));
114 $texte .=
'<form action="'.$_SERVER[
"PHP_SELF"].
'" method="POST" enctype="multipart/form-data">';
115 $texte .=
'<input type="hidden" name="token" value="'.newToken().
'">';
116 $texte .=
'<input type="hidden" name="page_y" value="">';
117 $texte .=
'<input type="hidden" name="action" value="setModuleOptions">';
118 $texte .=
'<input type="hidden" name="param1" value="TICKET_ADDON_PDF_ODT_PATH">';
120 $texte .=
'<table class="nobordernopadding" width="100%">';
123 $texte .=
'<tr><td>';
124 $texttitle = $langs->trans(
"ListOfDirectories");
125 $listofdir = explode(
',', preg_replace(
'/[\r\n]+/',
',', trim($conf->global->TICKET_ADDON_PDF_ODT_PATH)));
126 $listoffiles = array();
127 foreach ($listofdir as $key => $tmpdir) {
128 $tmpdir = trim($tmpdir);
129 $tmpdir = preg_replace(
'/DOL_DATA_ROOT/', DOL_DATA_ROOT, $tmpdir);
131 unset($listofdir[$key]);
134 if (!is_dir($tmpdir)) {
135 $texttitle .=
img_warning($langs->trans(
"ErrorDirNotFound", $tmpdir), 0);
137 $tmpfiles =
dol_dir_list($tmpdir,
'files', 0,
'\.(ods|odt)');
138 if (count($tmpfiles)) {
139 $listoffiles = array_merge($listoffiles, $tmpfiles);
143 $texthelp = $langs->trans(
"ListOfDirectoriesForModelGenODT");
145 $texthelp .=
'<br>'.$langs->trans(
"FollowingSubstitutionKeysCanBeUsed").
'<br>';
146 $texthelp .= $langs->transnoentitiesnoconv(
"FullListOnOnlineDocumentation");
148 $texte .=
$form->textwithpicto($texttitle, $texthelp, 1,
'help',
'', 1);
149 $texte .=
'<div><div style="display: inline-block; min-width: 100px; vertical-align: middle;">';
150 $texte .=
'<textarea class="flat" cols="60" name="value1">';
151 $texte .= $conf->global->TICKET_ADDON_PDF_ODT_PATH;
152 $texte .=
'</textarea>';
153 $texte .=
'</div><div style="display: inline-block; vertical-align: middle;">';
154 $texte .=
'<input type="submit" class="button small reposition" name="modify" value="'.$langs->trans(
"Modify").
'">';
155 $texte .=
'<br></div></div>';
158 if (count($listofdir)) {
159 $texte .= $langs->trans(
"NumberOfModelFilesFound").
': <b>'.count($listoffiles).
'</b>';
161 $texte .=
'<div id="div_'.get_class($this).
'" class="hiddenx">';
163 foreach ($listoffiles as $file) {
164 $texte .=
'- '.$file[
'name'].
' <a href="'.DOL_URL_ROOT.
'/document.php?modulepart=doctemplates&file=tickets/'.urlencode(basename($file[
'name'])).
'">'.
img_picto(
'',
'listlight').
'</a><br>';
169 $texte .=
'<div>'.$langs->trans(
"UploadNewTemplate");
170 $texte .=
' <input type="file" name="uploadfile">';
172 $maxmin = $maxfilesizearray[
'maxmin'];
174 $texte .=
'<input type="hidden" name="MAX_FILE_SIZE" value="'.($maxmin * 1024).
'">';
176 $texte .=
'<input type="hidden" value="TICKET_ADDON_PDF_ODT_PATH" name="keyforuploaddir">';
177 $texte .=
'<input type="submit" class="button small reposition" value="'.dol_escape_htmltag($langs->trans(
"Upload")).
'" name="upload">';
182 $texte .=
'<td rowspan="2" class="tdtop hideonsmartphone">';
183 $texte .=
'<span class="opacitymedium">';
184 $texte .= $langs->trans(
"ExampleOfDirectoriesForModelGen");
189 $texte .=
'</table>';
207 public function write_file($object, $outputlangs, $srctemplatepath, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
210 global $user, $langs, $conf, $mysoc, $hookmanager;
212 if (empty($srctemplatepath)) {
213 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
218 if (!is_object($hookmanager)) {
219 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
222 $hookmanager->initHooks(array(
'odtgeneration'));
225 if (!is_object($outputlangs)) {
226 $outputlangs = $langs;
228 $sav_charset_output = $outputlangs->charset_output;
229 $outputlangs->charset_output =
'UTF-8';
232 $outputlangs->loadLangs(array(
"main",
"companies",
"bills",
"dict"));
234 if ($conf->ticket->dir_output) {
236 if (!is_object($object)) {
238 $object =
new User($this->
db);
239 $result = $object->fetch($id);
246 $object->fetch_thirdparty();
248 $dir = $conf->ticket->dir_output;
250 if (!preg_match(
'/specimen/i', $objectref)) {
251 $dir .=
"/".$objectref;
253 $file = $dir.
"/".$objectref.
".odt";
255 if (!file_exists($dir)) {
257 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
262 if (file_exists($dir)) {
264 $newfile = basename($srctemplatepath);
265 $newfiletmp = preg_replace(
'/\.od(t|s)/i',
'', $newfile);
266 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
267 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
269 $newfiletmp = $objectref.
'_'.$newfiletmp;
272 $newfileformat = substr($newfile, strrpos($newfile,
'.') + 1);
273 if (!empty($conf->global->MAIN_DOC_USE_TIMING)) {
274 $format = $conf->global->MAIN_DOC_USE_TIMING;
275 if ($format ==
'1') {
276 $format =
'%Y%m%d%H%M%S';
280 $filename = $newfiletmp.
'.'.$newfileformat;
282 $file = $dir.
'/'.$filename;
289 if (!is_writable($conf->ticket->dir_temp)) {
290 $this->error =
"Failed to write in temp directory ".$conf->ticket->dir_temp;
291 dol_syslog(
'Error in write_file: '.$this->error, LOG_ERR);
297 $arrayidcontact = $object->getIdContact(
'external',
'CUSTOMER');
298 if (count($arrayidcontact) > 0) {
300 $result = $object->fetch_contact($arrayidcontact[0]);
304 if (!empty($usecontact)) {
306 if ($object->contact->socid != $object->thirdparty->id && (!isset($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT) || !empty($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT))) {
307 $object->contact->fetch_thirdparty();
308 $socobject = $object->contact->thirdparty;
309 $contactobject = $object->contact;
311 $socobject = $object->thirdparty;
313 $contactobject = $object->contact;
316 $socobject = $object->thirdparty;
320 require_once ODTPHP_PATH.
'odf.php';
322 $odfHandler =
new odf(
325 'PATH_TO_TMP' => $conf->ticket->dir_temp,
326 'ZIP_PROXY' =>
'PclZipProxy',
327 'DELIMITER_LEFT' =>
'{',
328 'DELIMITER_RIGHT' =>
'}'
332 $this->error = $e->getMessage();
343 $array_thirdparty_contact = array();
344 if ($usecontact && is_object($contactobject)) {
348 $tmparray = array_merge($array_user, $array_soc, $array_thirdparty, $array_other, $array_thirdparty_contact);
350 $object->fetch_optionals();
352 $parameters = array(
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
353 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
354 foreach ($tmparray as $key => $value) {
356 if (preg_match(
'/logo$/', $key)) {
357 if (file_exists($value)) {
358 $odfHandler->setImage($key, $value);
360 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
364 $odfHandler->setVars($key, $value,
true,
'UTF-8');
366 }
catch (OdfException $e) {
372 $tmparray = $outputlangs->get_translations_for_substitutions();
373 foreach ($tmparray as $key => $value) {
375 $odfHandler->setVars($key, $value,
true,
'UTF-8');
376 }
catch (OdfException $e) {
382 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs);
383 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
386 if (!empty($conf->global->MAIN_ODT_AS_PDF)) {
388 $odfHandler->exportAsAttachedPDF($file);
390 $this->error = $e->getMessage();
396 $odfHandler->saveToDisk($file);
398 $this->error = $e->getMessage();
404 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
406 if (!empty($conf->global->MAIN_UMASK)) {
407 @chmod($file, octdec($conf->global->MAIN_UMASK));
412 $this->result = array(
'fullpath'=>$file);
416 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
436 $array_other = array();
437 foreach ($object as $key => $value) {
438 if (!is_array($value) && !is_object($value)) {
439 $array_other[$array_key.
'_'.$key] = $value;