224 public function write_file($object, $outputlangs, $srctemplatepath, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
227 global $user, $langs, $conf, $mysoc, $hookmanager;
230 if (empty($srctemplatepath)) {
231 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
236 if (!is_object($hookmanager)) {
237 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
240 $hookmanager->initHooks(array(
'odtgeneration'));
243 if (!is_object($outputlangs)) {
244 $outputlangs = $langs;
246 $sav_charset_output = $outputlangs->charset_output;
247 $outputlangs->charset_output =
'UTF-8';
249 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"bills"));
251 if ($conf->mymodule->dir_output) {
253 if (!is_object($object)) {
256 $result = $object->fetch($id);
263 $object->fetch_thirdparty();
265 $dir = $conf->mymodule->multidir_output[isset($object->entity) ? $object->entity : 1];
267 if (!preg_match(
'/specimen/i', $objectref)) {
268 $dir .=
"/".$objectref;
270 $file = $dir.
"/".$objectref.
".odt";
272 if (!file_exists($dir)) {
274 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
279 if (file_exists($dir)) {
281 $newfile = basename($srctemplatepath);
282 $newfiletmp = preg_replace(
'/\.od[ts]/i',
'', $newfile);
283 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
284 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
286 $newfiletmp = $objectref .
'_' . $newfiletmp;
290 $newfileformat = substr($newfile, strrpos($newfile,
'.') + 1);
293 if ($format ==
'1') {
294 $format =
'%Y%m%d%H%M%S';
298 $filename = $newfiletmp .
'.' . $newfileformat;
300 $file = $dir .
'/' . $filename;
307 if (!is_writable($conf->mymodule->dir_temp)) {
308 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory", $conf->mymodule->dir_temp);
309 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
315 $arrayidcontact = $object->getIdContact(
'external',
'CUSTOMER');
316 if (count($arrayidcontact) > 0) {
318 $result = $object->fetch_contact($arrayidcontact[0]);
322 $contactobject =
null;
323 if (!empty($usecontact)) {
325 if ($object->contact->socid != $object->thirdparty->id && (!isset($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT) ||
getDolGlobalInt(
'MAIN_USE_COMPANY_NAME_OF_CONTACT'))) {
326 $object->contact->fetch_thirdparty();
327 $socobject = $object->contact->thirdparty;
328 $contactobject = $object->contact;
330 $socobject = $object->thirdparty;
332 $contactobject = $object->contact;
335 $socobject = $object->thirdparty;
339 $substitutionarray = array(
340 '__FROM_NAME__' => $this->emetteur->name,
341 '__FROM_EMAIL__' => $this->emetteur->email,
342 '__TOTAL_TTC__' => $object->total_ttc,
343 '__TOTAL_HT__' => $object->total_ht,
344 '__TOTAL_VAT__' => $object->total_tva
348 $parameters = array(
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$substitutionarray);
349 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
353 $paramfreetext =
'MYMODULE_MYOBJECT_FREE_TEXT';
359 require_once ODTPHP_PATH.
'odf.php';
361 $odfHandler =
new Odf(
364 'PATH_TO_TMP' => $conf->mymodule->dir_temp,
365 'ZIP_PROXY' =>
'PclZipProxy',
366 'DELIMITER_LEFT' =>
'{',
367 'DELIMITER_RIGHT' =>
'}'
371 $this->error = $e->getMessage();
384 $odfHandler->setVars(
'free_text', $newfreetext,
true,
'UTF-8');
385 }
catch (OdfException $e) {
398 $array_thirdparty_contact = array();
399 if ($usecontact && is_object($contactobject)) {
403 $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other, $array_thirdparty_contact);
407 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
408 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
410 foreach ($tmparray as $key => $value) {
412 if (preg_match(
'/logo$/', $key)) {
414 if (file_exists($value)) {
415 $odfHandler->setImage($key, $value);
417 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
421 $odfHandler->setVars($key, $value,
true,
'UTF-8');
423 }
catch (OdfException $e) {
429 $foundtagforlines = 1;
431 $listlines = $odfHandler->setSegment(
'lines');
432 }
catch (OdfException $e) {
434 $foundtagforlines = 0;
437 if ($foundtagforlines) {
439 foreach ($object->lines as $line) {
444 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray,
'line'=>$line);
445 $reshook = $hookmanager->executeHooks(
'ODTSubstitutionLine', $parameters, $this, $action);
446 foreach ($tmparray as $key => $val) {
448 $listlines->setVars($key, $val,
true,
'UTF-8');
449 }
catch (OdfException $e) {
451 }
catch (SegmentException $e) {
457 $odfHandler->mergeSegment($listlines);
459 }
catch (OdfException $e) {
460 $this->error = $e->getMessage();
466 $tmparray = $outputlangs->get_translations_for_substitutions();
467 foreach ($tmparray as $key => $value) {
469 $odfHandler->setVars($key, $value,
true,
'UTF-8');
470 }
catch (OdfException $e) {
477 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
478 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
483 $odfHandler->exportAsAttachedPDF($file);
485 $this->error = $e->getMessage();
491 $odfHandler->saveToDisk($file);
493 $this->error = $e->getMessage();
499 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
500 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
506 $this->result = array(
'fullpath'=>$file);
510 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);