227 public function write_file(
$object, $outputlangs, $srctemplatepath =
'', $hidedetails = 0, $hidedesc = 0, $hideref = 0)
230 global $user, $langs, $conf, $mysoc, $hookmanager;
233 if (empty($srctemplatepath)) {
234 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
239 $hookmanager->initHooks(array(
'odtgeneration'));
241 if (!is_object($outputlangs)) {
242 $outputlangs = $langs;
244 $sav_charset_output = $outputlangs->charset_output;
245 $outputlangs->charset_output =
'UTF-8';
247 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"bills"));
249 if ($conf->mymodule->dir_output) {
263 $dir = $conf->mymodule->multidir_output[isset(
$object->entity) ?
$object->entity : 1];
265 if (!preg_match(
'/specimen/i', $objectref)) {
266 $dir .=
"/".$objectref;
268 $file = $dir.
"/".$objectref.
".odt";
270 if (!file_exists($dir)) {
272 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
277 if (file_exists($dir)) {
279 $newfile = basename($srctemplatepath);
280 $newfiletmp = preg_replace(
'/\.od[ts]/i',
'', $newfile);
281 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
282 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
284 $newfiletmp = $objectref .
'_' . $newfiletmp;
288 $newfileformat = substr($newfile, strrpos($newfile,
'.') + 1);
291 if ($format ==
'1') {
292 $format =
'%Y%m%d%H%M%S';
296 $filename = $newfiletmp .
'.' . $newfileformat;
298 $file = $dir .
'/' . $filename;
305 if (!is_writable($conf->mymodule->dir_temp)) {
306 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory", $conf->mymodule->dir_temp);
307 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
313 $arrayidcontact =
$object->getIdContact(
'external',
'CUSTOMER');
314 if (count($arrayidcontact) > 0) {
316 $result =
$object->fetch_contact($arrayidcontact[0]);
320 $contactobject =
null;
321 if (!empty($usecontact)) {
323 if (
$object->contact->socid !=
$object->thirdparty->id && (!isset($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT) ||
getDolGlobalInt(
'MAIN_USE_COMPANY_NAME_OF_CONTACT'))) {
324 $object->contact->fetch_thirdparty();
325 $socobject =
$object->contact->thirdparty;
326 $contactobject =
$object->contact;
328 $socobject =
$object->thirdparty;
330 $contactobject =
$object->contact;
333 $socobject =
$object->thirdparty;
337 $substitutionarray = array(
338 '__FROM_NAME__' => $this->emetteur->name,
339 '__FROM_EMAIL__' => $this->emetteur->email,
340 '__TOTAL_TTC__' =>
$object->total_ttc,
341 '__TOTAL_HT__' =>
$object->total_ht,
342 '__TOTAL_VAT__' =>
$object->total_tva
346 $parameters = array(
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$substitutionarray);
347 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
351 $paramfreetext =
'MYMODULE_MYOBJECT_FREE_TEXT';
357 require_once ODTPHP_PATH.
'odf.php';
359 $odfHandler =
new Odf(
362 'PATH_TO_TMP' => $conf->mymodule->dir_temp,
363 'ZIP_PROXY' =>
'PclZipProxy',
364 'DELIMITER_LEFT' =>
'{',
365 'DELIMITER_RIGHT' =>
'}'
369 $this->error = $e->getMessage();
382 $odfHandler->setVars(
'free_text', $newfreetext,
true,
'UTF-8');
383 }
catch (OdfException $e) {
390 $array_objet = $this->get_substitutionarray_object(
$object, $outputlangs);
396 $array_thirdparty_contact = array();
397 if ($usecontact && is_object($contactobject)) {
401 $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other, $array_thirdparty_contact);
405 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
406 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
408 foreach ($tmparray as $key => $value) {
410 if (preg_match(
'/logo$/', $key)) {
412 if (file_exists($value)) {
413 $odfHandler->setImage($key, $value);
415 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
419 $odfHandler->setVars($key, $value,
true,
'UTF-8');
421 }
catch (OdfException $e) {
426 $foundtagforlines = 1;
428 $listlines = $odfHandler->setSegment(
'lines');
429 }
catch (OdfExceptionSegmentNotFound $e) {
431 $foundtagforlines = 0;
435 if ($foundtagforlines) {
437 foreach (
$object->lines as $line) {
442 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray,
'line' => $line);
443 $reshook = $hookmanager->executeHooks(
'ODTSubstitutionLine', $parameters, $this, $action);
444 foreach ($tmparray as $key => $val) {
446 $listlines->setVars($key, $val,
true,
'UTF-8');
447 }
catch (SegmentException $e) {
454 $odfHandler->mergeSegment($listlines);
455 }
catch (OdfException $e) {
456 $this->error = $e->getMessage();
463 $tmparray = $outputlangs->get_translations_for_substitutions();
464 foreach ($tmparray as $key => $value) {
466 $odfHandler->setVars($key, $value,
true,
'UTF-8');
467 }
catch (OdfException $e) {
474 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
475 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
480 $odfHandler->exportAsAttachedPDF($file);
482 $this->error = $e->getMessage();
488 $odfHandler->saveToDisk($file);
490 $this->error = $e->getMessage();
496 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
497 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
503 $this->result = array(
'fullpath' => $file);
507 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);