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 $hookmanager->initHooks(array(
'odtgeneration'));
238 if (!is_object($outputlangs)) {
239 $outputlangs = $langs;
241 $sav_charset_output = $outputlangs->charset_output;
242 $outputlangs->charset_output =
'UTF-8';
244 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"bills"));
246 if ($conf->mymodule->dir_output) {
248 if (!is_object($object)) {
251 $result = $object->fetch($id);
258 $object->fetch_thirdparty();
260 $dir = $conf->mymodule->multidir_output[isset($object->entity) ? $object->entity : 1];
262 if (!preg_match(
'/specimen/i', $objectref)) {
263 $dir .=
"/".$objectref;
265 $file = $dir.
"/".$objectref.
".odt";
267 if (!file_exists($dir)) {
269 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
274 if (file_exists($dir)) {
276 $newfile = basename($srctemplatepath);
277 $newfiletmp = preg_replace(
'/\.od[ts]/i',
'', $newfile);
278 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
279 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
281 $newfiletmp = $objectref .
'_' . $newfiletmp;
285 $newfileformat = substr($newfile, strrpos($newfile,
'.') + 1);
288 if ($format ==
'1') {
289 $format =
'%Y%m%d%H%M%S';
293 $filename = $newfiletmp .
'.' . $newfileformat;
295 $file = $dir .
'/' . $filename;
302 if (!is_writable($conf->mymodule->dir_temp)) {
303 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory", $conf->mymodule->dir_temp);
304 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
310 $arrayidcontact = $object->getIdContact(
'external',
'CUSTOMER');
311 if (count($arrayidcontact) > 0) {
313 $result = $object->fetch_contact($arrayidcontact[0]);
317 $contactobject =
null;
318 if (!empty($usecontact)) {
320 if ($object->contact->socid != $object->thirdparty->id && (!isset($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT) ||
getDolGlobalInt(
'MAIN_USE_COMPANY_NAME_OF_CONTACT'))) {
321 $object->contact->fetch_thirdparty();
322 $socobject = $object->contact->thirdparty;
323 $contactobject = $object->contact;
325 $socobject = $object->thirdparty;
327 $contactobject = $object->contact;
330 $socobject = $object->thirdparty;
334 $substitutionarray = array(
335 '__FROM_NAME__' => $this->emetteur->name,
336 '__FROM_EMAIL__' => $this->emetteur->email,
337 '__TOTAL_TTC__' => $object->total_ttc,
338 '__TOTAL_HT__' => $object->total_ht,
339 '__TOTAL_VAT__' => $object->total_tva
343 $parameters = array(
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$substitutionarray);
344 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
348 $paramfreetext =
'MYMODULE_MYOBJECT_FREE_TEXT';
354 require_once ODTPHP_PATH.
'odf.php';
356 $odfHandler =
new Odf(
359 'PATH_TO_TMP' => $conf->mymodule->dir_temp,
360 'ZIP_PROXY' =>
'PclZipProxy',
361 'DELIMITER_LEFT' =>
'{',
362 'DELIMITER_RIGHT' =>
'}'
366 $this->error = $e->getMessage();
379 $odfHandler->setVars(
'free_text', $newfreetext,
true,
'UTF-8');
380 }
catch (OdfException $e) {
393 $array_thirdparty_contact = array();
394 if ($usecontact && is_object($contactobject)) {
398 $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other, $array_thirdparty_contact);
402 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
403 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
405 foreach ($tmparray as $key => $value) {
407 if (preg_match(
'/logo$/', $key)) {
409 if (file_exists($value)) {
410 $odfHandler->setImage($key, $value);
412 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
416 $odfHandler->setVars($key, $value,
true,
'UTF-8');
418 }
catch (OdfException $e) {
424 $foundtagforlines = 1;
426 $listlines = $odfHandler->setSegment(
'lines');
427 }
catch (OdfException $e) {
429 $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);