220 public function write_file($object, $outputlangs, $srctemplatepath, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
223 global $user, $langs, $conf, $mysoc, $hookmanager;
226 if (empty($srctemplatepath)) {
227 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
232 $hookmanager->initHooks(array(
'odtgeneration'));
234 if (!is_object($outputlangs)) {
235 $outputlangs = $langs;
237 $sav_charset_output = $outputlangs->charset_output;
238 $outputlangs->charset_output =
'UTF-8';
240 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"bills"));
242 if ($conf->mymodule->dir_output) {
244 if (!is_object($object)) {
247 $result = $object->fetch($id);
254 $object->fetch_thirdparty();
256 $dir = $conf->mymodule->multidir_output[isset($object->entity) ? $object->entity : 1];
258 if (!preg_match(
'/specimen/i', $objectref)) {
259 $dir .=
"/".$objectref;
261 $file = $dir.
"/".$objectref.
".odt";
263 if (!file_exists($dir)) {
265 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
270 if (file_exists($dir)) {
272 $newfile = basename($srctemplatepath);
273 $newfiletmp = preg_replace(
'/\.od[ts]/i',
'', $newfile);
274 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
275 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
277 $newfiletmp = $objectref .
'_' . $newfiletmp;
281 $newfileformat = substr($newfile, strrpos($newfile,
'.') + 1);
284 if ($format ==
'1') {
285 $format =
'%Y%m%d%H%M%S';
289 $filename = $newfiletmp .
'.' . $newfileformat;
291 $file = $dir .
'/' . $filename;
298 if (!is_writable($conf->mymodule->dir_temp)) {
299 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory", $conf->mymodule->dir_temp);
300 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
306 $arrayidcontact = $object->getIdContact(
'external',
'CUSTOMER');
307 if (count($arrayidcontact) > 0) {
309 $result = $object->fetch_contact($arrayidcontact[0]);
313 $contactobject =
null;
314 if (!empty($usecontact)) {
316 if ($object->contact->socid != $object->thirdparty->id && (!isset($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT) ||
getDolGlobalInt(
'MAIN_USE_COMPANY_NAME_OF_CONTACT'))) {
317 $object->contact->fetch_thirdparty();
318 $socobject = $object->contact->thirdparty;
319 $contactobject = $object->contact;
321 $socobject = $object->thirdparty;
323 $contactobject = $object->contact;
326 $socobject = $object->thirdparty;
330 $substitutionarray = array(
331 '__FROM_NAME__' => $this->emetteur->name,
332 '__FROM_EMAIL__' => $this->emetteur->email,
333 '__TOTAL_TTC__' => $object->total_ttc,
334 '__TOTAL_HT__' => $object->total_ht,
335 '__TOTAL_VAT__' => $object->total_tva
339 $parameters = array(
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$substitutionarray);
340 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
344 $paramfreetext =
'MYMODULE_MYOBJECT_FREE_TEXT';
350 require_once ODTPHP_PATH.
'odf.php';
352 $odfHandler =
new Odf(
355 'PATH_TO_TMP' => $conf->mymodule->dir_temp,
357 'DELIMITER_LEFT' =>
'{',
358 'DELIMITER_RIGHT' =>
'}'
362 $this->error = $e->getMessage();
375 $odfHandler->setVars(
'free_text', $newfreetext,
true,
'UTF-8');
376 }
catch (OdfException $e) {
389 $array_thirdparty_contact = array();
390 if ($usecontact && is_object($contactobject)) {
394 $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other, $array_thirdparty_contact);
398 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
399 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
401 foreach ($tmparray as $key => $value) {
403 if (preg_match(
'/logo$/', $key)) {
405 if (file_exists($value)) {
406 $odfHandler->setImage($key, $value);
408 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
412 $odfHandler->setVars($key, $value,
true,
'UTF-8');
414 }
catch (OdfException $e) {
420 $foundtagforlines = 1;
422 $listlines = $odfHandler->setSegment(
'lines');
423 }
catch (OdfExceptionSegmentNotFound $e) {
425 $foundtagforlines = 0;
427 }
catch (OdfException $e) {
428 $foundtagforlines = 0;
432 if ($foundtagforlines) {
434 foreach ($object->lines as $line) {
439 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray,
'line'=>$line);
440 $reshook = $hookmanager->executeHooks(
'ODTSubstitutionLine', $parameters, $this, $action);
441 foreach ($tmparray as $key => $val) {
443 $listlines->setVars($key, $val,
true,
'UTF-8');
444 }
catch (OdfException $e) {
446 }
catch (SegmentException $e) {
452 $odfHandler->mergeSegment($listlines);
454 }
catch (OdfException $e) {
455 $this->error = $e->getMessage();
461 $tmparray = $outputlangs->get_translations_for_substitutions();
462 foreach ($tmparray as $key => $value) {
464 $odfHandler->setVars($key, $value,
true,
'UTF-8');
465 }
catch (OdfException $e) {
472 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
473 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
478 $odfHandler->exportAsAttachedPDF($file);
480 $this->error = $e->getMessage();
486 $odfHandler->saveToDisk($file);
488 $this->error = $e->getMessage();
494 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
495 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
501 $this->result = array(
'fullpath'=>$file);
505 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);