232 public function write_file(
$object, $outputlangs, $srctemplatepath =
'', $hidedetails = 0, $hidedesc = 0, $hideref = 0)
235 global $user, $langs, $conf, $mysoc, $hookmanager;
238 if (empty($srctemplatepath)) {
239 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
244 $hookmanager->initHooks(array(
'odtgeneration'));
246 if (!is_object($outputlangs)) {
247 $outputlangs = $langs;
249 $sav_charset_output = $outputlangs->charset_output;
250 $outputlangs->charset_output =
'UTF-8';
252 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"bills"));
254 if ($conf->mymodule->dir_output) {
268 $dir = $conf->mymodule->multidir_output[isset(
$object->entity) ?
$object->entity : 1];
270 if (!preg_match(
'/specimen/i', $objectref)) {
271 $dir .=
"/".$objectref;
273 $file = $dir.
"/".$objectref.
".odt";
275 if (!file_exists($dir)) {
277 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
282 if (file_exists($dir)) {
284 $newfile = basename($srctemplatepath);
285 $newfiletmp = preg_replace(
'/\.od[ts]/i',
'', $newfile);
286 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
287 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
289 $newfiletmp = $objectref .
'_' . $newfiletmp;
293 $newfileformat = substr($newfile, strrpos($newfile,
'.') + 1);
296 if ($format ==
'1') {
297 $format =
'%Y%m%d%H%M%S';
301 $filename = $newfiletmp .
'.' . $newfileformat;
303 $file = $dir .
'/' . $filename;
310 if (!is_writable($conf->mymodule->dir_temp)) {
311 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory", $conf->mymodule->dir_temp);
312 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
318 $arrayidcontact =
$object->getIdContact(
'external',
'CUSTOMER');
319 if (count($arrayidcontact) > 0) {
321 $result =
$object->fetch_contact($arrayidcontact[0]);
325 $contactobject =
null;
326 if (!empty($usecontact)) {
328 if (
$object->contact->socid !=
$object->thirdparty->id && (!isset($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT) ||
getDolGlobalInt(
'MAIN_USE_COMPANY_NAME_OF_CONTACT'))) {
329 $object->contact->fetch_thirdparty();
330 $socobject =
$object->contact->thirdparty;
331 $contactobject =
$object->contact;
333 $socobject =
$object->thirdparty;
335 $contactobject =
$object->contact;
338 $socobject =
$object->thirdparty;
342 $substitutionarray = array(
343 '__FROM_NAME__' => $this->emetteur->name,
344 '__FROM_EMAIL__' => $this->emetteur->email,
345 '__TOTAL_TTC__' =>
$object->total_ttc,
346 '__TOTAL_HT__' =>
$object->total_ht,
347 '__TOTAL_VAT__' =>
$object->total_tva
351 $parameters = array(
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$substitutionarray);
352 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
356 $paramfreetext =
'MYMODULE_MYOBJECT_FREE_TEXT';
362 require_once ODTPHP_PATH.
'odf.php';
364 $odfHandler =
new Odf(
367 'PATH_TO_TMP' => $conf->mymodule->dir_temp,
369 'DELIMITER_LEFT' =>
'{',
370 'DELIMITER_RIGHT' =>
'}'
374 $this->error = $e->getMessage();
387 $odfHandler->setVars(
'free_text', $newfreetext,
true,
'UTF-8');
388 }
catch (OdfException $e) {
395 $array_objet = $this->get_substitutionarray_object(
$object, $outputlangs);
401 $array_thirdparty_contact = array();
402 if ($usecontact && is_object($contactobject)) {
406 $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other, $array_thirdparty_contact);
410 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
411 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
413 foreach ($tmparray as $key => $value) {
415 if (preg_match(
'/logo$/', $key)) {
417 if (file_exists($value)) {
418 $odfHandler->setImage($key, $value);
420 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
424 $odfHandler->setVars($key, $value,
true,
'UTF-8');
426 }
catch (OdfException $e) {
431 $foundtagforlines = 1;
433 $listlines = $odfHandler->setSegment(
'lines');
434 }
catch (OdfExceptionSegmentNotFound $e) {
436 $foundtagforlines = 0;
440 if ($foundtagforlines) {
442 foreach (
$object->lines as $line) {
447 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray,
'line' => $line);
448 $reshook = $hookmanager->executeHooks(
'ODTSubstitutionLine', $parameters, $this, $action);
449 foreach ($tmparray as $key => $val) {
451 $listlines->setVars($key, $val,
true,
'UTF-8');
452 }
catch (SegmentException $e) {
459 $odfHandler->mergeSegment($listlines);
460 }
catch (OdfException $e) {
461 $this->error = $e->getMessage();
468 $tmparray = $outputlangs->get_translations_for_substitutions();
469 foreach ($tmparray as $key => $value) {
471 $odfHandler->setVars($key, $value,
true,
'UTF-8');
472 }
catch (OdfException $e) {
479 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
480 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
485 $odfHandler->exportAsAttachedPDF($file);
487 $this->error = $e->getMessage();
493 $odfHandler->saveToDisk($file);
495 $this->error = $e->getMessage();
501 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
502 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
508 $this->result = array(
'fullpath' => $file);
512 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);