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 if (!is_object($hookmanager)) {
233 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
236 $hookmanager->initHooks(array(
'odtgeneration'));
239 if (!is_object($outputlangs)) {
240 $outputlangs = $langs;
242 $sav_charset_output = $outputlangs->charset_output;
243 $outputlangs->charset_output =
'UTF-8';
245 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"bills"));
247 if ($conf->mymodule->dir_output) {
249 if (!is_object($object)) {
252 $result = $object->fetch($id);
259 $object->fetch_thirdparty();
261 $dir = $conf->mymodule->multidir_output[isset($object->entity) ? $object->entity : 1];
263 if (!preg_match(
'/specimen/i', $objectref)) {
264 $dir .=
"/".$objectref;
266 $file = $dir.
"/".$objectref.
".odt";
268 if (!file_exists($dir)) {
270 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
275 if (file_exists($dir)) {
277 $newfile = basename($srctemplatepath);
278 $newfiletmp = preg_replace(
'/\.od[ts]/i',
'', $newfile);
279 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
280 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
282 $newfiletmp = $objectref .
'_' . $newfiletmp;
286 $newfileformat = substr($newfile, strrpos($newfile,
'.') + 1);
289 if ($format ==
'1') {
290 $format =
'%Y%m%d%H%M%S';
294 $filename = $newfiletmp .
'.' . $newfileformat;
296 $file = $dir .
'/' . $filename;
303 if (!is_writable($conf->mymodule->dir_temp)) {
304 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory", $conf->mymodule->dir_temp);
305 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
311 $arrayidcontact = $object->getIdContact(
'external',
'CUSTOMER');
312 if (count($arrayidcontact) > 0) {
314 $result = $object->fetch_contact($arrayidcontact[0]);
318 $contactobject =
null;
319 if (!empty($usecontact)) {
321 if ($object->contact->socid != $object->thirdparty->id && (!isset($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT) ||
getDolGlobalInt(
'MAIN_USE_COMPANY_NAME_OF_CONTACT'))) {
322 $object->contact->fetch_thirdparty();
323 $socobject = $object->contact->thirdparty;
324 $contactobject = $object->contact;
326 $socobject = $object->thirdparty;
328 $contactobject = $object->contact;
331 $socobject = $object->thirdparty;
335 $substitutionarray = array(
336 '__FROM_NAME__' => $this->emetteur->name,
337 '__FROM_EMAIL__' => $this->emetteur->email,
338 '__TOTAL_TTC__' => $object->total_ttc,
339 '__TOTAL_HT__' => $object->total_ht,
340 '__TOTAL_VAT__' => $object->total_tva
344 $parameters = array(
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$substitutionarray);
345 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
349 $paramfreetext =
'MYMODULE_MYOBJECT_FREE_TEXT';
355 require_once ODTPHP_PATH.
'odf.php';
357 $odfHandler =
new Odf(
360 'PATH_TO_TMP' => $conf->mymodule->dir_temp,
362 'DELIMITER_LEFT' =>
'{',
363 'DELIMITER_RIGHT' =>
'}'
367 $this->error = $e->getMessage();
380 $odfHandler->setVars(
'free_text', $newfreetext,
true,
'UTF-8');
381 }
catch (OdfException $e) {
394 $array_thirdparty_contact = array();
395 if ($usecontact && is_object($contactobject)) {
399 $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other, $array_thirdparty_contact);
403 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
404 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
406 foreach ($tmparray as $key => $value) {
408 if (preg_match(
'/logo$/', $key)) {
410 if (file_exists($value)) {
411 $odfHandler->setImage($key, $value);
413 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
417 $odfHandler->setVars($key, $value,
true,
'UTF-8');
419 }
catch (OdfException $e) {
425 $foundtagforlines = 1;
427 $listlines = $odfHandler->setSegment(
'lines');
428 }
catch (OdfExceptionSegmentNotFound $e) {
430 $foundtagforlines = 0;
432 }
catch (OdfException $e) {
433 $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);